Skip to content

Commit

Permalink
Add board_size to botlink model (#207)
Browse files Browse the repository at this point in the history
* Update bot model

* Remove Cargo.lock

* Add some serde tests
  • Loading branch information
Terkwood authored Apr 5, 2020
1 parent 1b045a6 commit 0d0bd72
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 182 deletions.
File renamed without changes.
179 changes: 0 additions & 179 deletions micro-model/bot/Cargo.lock

This file was deleted.

6 changes: 5 additions & 1 deletion micro-model/bot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "micro_model_bot"
version = "0.1.0"
version = "0.1.1"
authors = ["terkwood <[email protected]>"]
edition = "2018"

Expand All @@ -9,3 +9,7 @@ serde = "1.0.104"
serde_derive = "1.0.104"
micro_model_moves = { git = "https://github.com/Terkwood/BUGOUT", branch = "unstable" }
bincode = "1.2.1"

[dev-dependencies]
serde_json = "1.0.51"
uuid = { version = "0.8.1", features = ["v4", "serde"] }
49 changes: 47 additions & 2 deletions micro-model/bot/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ use serde_derive::{Deserialize, Serialize};
/// requests that robocall coordinate with
/// tinybrain to generate moves for a given
/// game ID and player.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AttachBot {
pub game_id: GameId,
pub player: Player,
pub board_size: Option<u8>,
}

/// This reply is sent once a bot is listening
/// as a certain player in a certain game.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct BotAttached {
pub game_id: GameId,
pub player: Player,
Expand All @@ -35,3 +38,45 @@ impl BotAttached {
Ok(bincode::serialize(&self)?)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::Player;
use uuid::Uuid;

#[test]
fn test_attach_bot_json() {
let expected = AttachBot {
game_id: GameId(Uuid::nil()),
player: Player::BLACK,
board_size: Some(9),
};
let json = serde_json::to_string(&expected).expect("to_string");
let actual: AttachBot = serde_json::from_str(&json).expect("from_str");
assert_eq!(actual, expected)
}

#[test]
fn test_bot_attached_json() {
let expected = BotAttached {
game_id: GameId(Uuid::new_v4()),
player: Player::WHITE,
};
let json = serde_json::to_string(&expected).expect("to_string");
let actual: BotAttached = serde_json::from_str(&json).expect("from_str");
assert_eq!(actual, expected)
}

#[test]
fn test_camel_case_attach_bot() {
let input = AttachBot {
game_id: GameId(Uuid::nil()),
player: Player::BLACK,
board_size: Some(19),
};
let json = serde_json::to_string(&input).expect("to_string");
assert!(json.contains("gameId"));
assert!(json.contains("boardSize"))
}
}
1 change: 1 addition & 0 deletions micro-model/moves/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cargo.lock

0 comments on commit 0d0bd72

Please sign in to comment.