Last active
March 25, 2017 10:08
-
-
Save cod3beat/3b5ea5f1669d2d8b414aaaea4eeb82f4 to your computer and use it in GitHub Desktop.
unit testing collection exploration on rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[cfg(test)] | |
mod string_manipulation { | |
#[test] | |
fn appending_string_literal() { | |
let mut s = String::from("foo"); | |
s.push_str("bar"); | |
assert_eq!(s, "foobar"); | |
} | |
#[test] | |
fn appending_a_string() { | |
let mut foo = String::from("foo"); | |
let bar = String::from("bar"); | |
// this is in immutable reference | |
foo.push_str(&bar); | |
assert_eq!(foo, "foobar"); | |
} | |
#[test] | |
fn appending_a_string_by_turning_it_into_a_string_literal() { | |
let mut foo = String::from("foo"); | |
let bar = String::from("bar"); | |
foo.push_str(bar.as_str()); | |
assert_eq!(foo, "foobar"); | |
} | |
#[test] | |
fn adding_string_using_plus() { | |
let foo = String::from("foo"); | |
let bar = String::from("bar"); | |
let foobar = foo + &bar; | |
assert_eq!(foobar, "foobar"); | |
} | |
#[test] | |
fn multiple_addition() { | |
let satu = String::from("satu"); | |
let dua = String::from("dua"); | |
let tiga = String::from("tiga"); | |
let semua = format!("{}-{}-{}", satu, dua, tiga); | |
assert_eq!(semua, "satu-dua-tiga"); | |
} | |
} | |
#[cfg(test)] | |
mod hash_map { | |
use std::collections::HashMap; | |
#[test] | |
fn adding_element() { | |
let mut ibu_kota = HashMap::new(); | |
ibu_kota.insert("Jawa Timur", "Surabaya"); | |
ibu_kota.insert("Jawa Barat", "Bandung"); | |
assert_eq!(2, ibu_kota.len()); | |
} | |
#[test] | |
fn getting_value() { | |
let mut ibu_kota = HashMap::new(); | |
ibu_kota.insert("Jawa Timur", "Surabaya"); | |
ibu_kota.insert("Jawa Barat", "Bandung"); | |
match ibu_kota.get(&"Jawa Timur") { | |
Some(&province) => assert_eq!("Surabaya", province), | |
_ => assert!(true, false), | |
} | |
} | |
#[test] | |
fn removing_an_element() { | |
let mut ibu_kota = HashMap::new(); | |
ibu_kota.insert("Jawa Timur", "Surabaya"); | |
ibu_kota.insert("Jawa Barat", "Bandung"); | |
ibu_kota.remove(&"Jawa Timur"); | |
assert_eq!(1, ibu_kota.len()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment