stringish/src/test.rs
2022-12-20 22:55:43 -06:00

100 lines
2.3 KiB
Rust

use std::{borrow::Cow, collections::HashMap};
use crate::{Ish, Stringish};
#[test]
fn construction() {
let a = "Hello, world!".ish();
let b = String::from("abracadabra").ish();
let c = Stringish::new();
assert_eq!(a, "Hello, world!");
assert_eq!(b, "abracadabra");
assert!(c.is_empty());
}
#[test]
fn equality() {
let a = "foobar".ish();
let b = String::from("foobar").ish();
assert_eq!(a, b);
assert_eq!(a, "foobar");
assert_eq!(b, "foobar");
assert_eq!(a, *"foobar");
assert_eq!(b, *"foobar");
assert_eq!(a, String::from("foobar"));
assert_eq!(b, String::from("foobar"));
assert_eq!(a, Cow::Borrowed("foobar"));
assert_eq!(b, Cow::Borrowed("foobar"));
assert_eq!(a, Cow::Owned(String::from("foobar")));
assert_eq!(b, Cow::Owned(String::from("foobar")));
}
#[test]
fn clone_hash() {
let a = "sdyajshdiask".ish();
let b = String::from("iujhioasd").ish();
let c = a.clone();
let d = b.clone();
assert_eq!(a, c);
assert_eq!(a.as_ptr(), c.as_ptr());
assert_eq!(b, d);
assert_ne!(b.as_ptr(), d.as_ptr());
let mut map = HashMap::new();
map.insert(a, 1);
map.insert(b, 2);
assert_eq!(map[&c], 1);
assert_eq!(map[&d], 2);
}
#[test]
fn borrowedness() {
let mut a = "abcdef".ish();
let mut b = String::from("abcdef").ish();
let mut c = "".ish();
let mut d = String::new().ish();
assert_eq!(a.is_borrowed(), Some(true));
assert_eq!(b.is_owned(), Some(true));
assert_eq!(c.is_borrowed(), None);
assert_eq!(d.is_owned(), None);
a.make_owned();
b.make_owned();
c.make_owned();
d.make_owned();
assert_eq!(a.is_borrowed(), Some(false));
assert_eq!(b.is_owned(), Some(true));
assert_eq!(c.is_borrowed(), None);
assert_eq!(d.is_owned(), None);
a = "abcdef".ish();
c = "".ish();
assert_eq!(a.into_owned(), "abcdef");
assert_eq!(b.into_owned(), "abcdef");
assert_eq!(c.into_owned(), "");
assert_eq!(d.into_owned(), "");
}
#[test]
fn mutation() {
let mut a = "Hello".ish();
assert_eq!(a, "Hello");
a.mutate().push_str(", world!");
assert_eq!(a, "Hello, world!");
a.mutate_with(|s| {
s.truncate(1);
s.push('i');
s.make_ascii_lowercase();
});
assert_eq!(a, "hi");
}