summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-04 10:07:57 +0100
committertroido <troido@protonmail.com>2020-02-04 10:07:57 +0100
commit057f38d6933207fb2e39c32f94269db83e7f1db2 (patch)
tree930531b3ef7e0443d6a84448840a155317c9a7b9
parent33576fe7a5f241dbd884315c3552ad8186562e74 (diff)
added some unit tests; added hashmap! macro
-rw-r--r--src/assemblages.rs35
-rw-r--r--src/util.rs28
2 files changed, 57 insertions, 6 deletions
diff --git a/src/assemblages.rs b/src/assemblages.rs
index 729a65d..f90c5d0 100644
--- a/src/assemblages.rs
+++ b/src/assemblages.rs
@@ -26,17 +26,18 @@ macro_rules! assemblage {
builder
}
- fn init_from_json(&mut self, mut _args: Vec<Value>, _kwargs: HashMap<&str, Value>) {
+ #[allow(unused_variables, unused_mut)]
+ fn init_from_json(&mut self, mut args: Vec<Value>, kwargs: HashMap<&str, Value>) {
$(
- if _args.len() > 0 {
- let val = _args.remove(0);
+ if args.len() > 0 {
+ let val = args.remove(0);
if let Some(actual_val) = unpack_json!($argt, val) {
self.$arg = actual_val;
}
}
)*
$(
- if let Some(val) = _kwargs.get(stringify!($arg)) {
+ if let Some(val) = kwargs.get(stringify!($arg)) {
if let Some(actual_val) = unpack_json!($argt, val) {
self.$arg = actual_val;
}
@@ -44,8 +45,6 @@ macro_rules! assemblage {
)*
}
}
- unsafe impl Send for $name {}
- unsafe impl Sync for $name {}
}
}
@@ -87,3 +86,27 @@ impl Player {
Player { name: name.to_string()}
}
}
+
+
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+ use super::super::hashmap;
+ #[test]
+ fn test_assemblage_from_json() {
+ let mut p = Player::new("Joe");
+ assert_eq!(p.name, "Joe");
+ p.init_from_json(vec![json!("Bob"), json!("Mike")], HashMap::new());
+ assert_eq!(p.name, "Bob");
+ p.init_from_json(vec![], hashmap!("sprite" => json!("stone")));
+ assert_eq!(p.name, "Bob");
+ p.init_from_json(vec![], hashmap!("name" => json!("Teddy")));
+ assert_eq!(p.name, "Teddy");
+ p.init_from_json(vec![json!("Bill")], hashmap!("name" => json!("Stan")));
+ assert_eq!(p.name, "Stan");
+ }
+}
+
+
diff --git a/src/util.rs b/src/util.rs
index fed0400..7f3c7cd 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -10,3 +10,31 @@ pub fn clamp<T: Ord>(val: T, lower: T, upper: T) -> T{
pub trait ToJson {
fn to_json(&self) -> Value;
}
+
+#[macro_export]
+macro_rules! hashmap {
+ ( $($key:expr => $value:expr ),* ) => {{
+ #[allow(unused_mut)]
+ let mut h = std::collections::HashMap::new();
+ $(
+ h.insert($key, $value);
+ )*
+ h
+ }}
+}
+
+#[cfg(test)]
+mod tests {
+ use std::collections::HashMap;
+ #[test]
+ fn test_hashmap_macro() {
+ let mut h = hashmap!("hello" => 1, "world" => 2);
+ assert_eq!(h.remove("hello"), Some(1));
+ assert_eq!(h.remove("world"), Some(2));
+ assert!(h.is_empty());
+ let h2: HashMap<i32, usize> = hashmap!();
+ assert!(h2.is_empty());
+ assert_eq!(h2, HashMap::new());
+
+ }
+}