Skip to content

Commit

Permalink
move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 17, 2024
1 parent fb885f9 commit 18722b0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 63 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 14 additions & 61 deletions lib/komainu/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ impl<K, V> ParamStorage<K, V>
where
K: Eq + Hash,
{
pub(crate) fn new() -> Self {
#[must_use]
pub fn new() -> Self {
Self {
inner: HashMap::new(),
}
}

#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Expand All @@ -42,14 +44,24 @@ where
})
}

fn insert(&mut self, key: K, value: V) {
#[inline]
pub fn insert(&mut self, key: K, value: V) {
self.inner
.entry(key)
.and_modify(|val| *val = Container::Overoccupied)
.or_insert(Container::Set(value));
}
}

impl<K, V> Default for ParamStorage<K, V>
where
K: Eq + Hash,
{
fn default() -> Self {
Self::new()
}

Check warning on line 62 in lib/komainu/src/params.rs

View check run for this annotation

Codecov / codecov/patch

lib/komainu/src/params.rs#L60-L62

Added lines #L60 - L62 were not covered by tests
}

impl<'de, K, V> Deserialize<'de> for ParamStorage<K, V>
where
K: Deserialize<'de> + Eq + Hash,
Expand Down Expand Up @@ -90,62 +102,3 @@ where
deserializer.deserialize_map(Visitor { _owo: PhantomData })
}
}

#[cfg(test)]
mod test {
use serde_test::Token;

use super::ParamStorage;

#[test]
fn insert_get_works() {
let mut map = ParamStorage::new();
map.insert("hello", "world");
assert_eq!(map.get("hello"), Some(&"world"));
}

#[test]
fn multi_insert_empty() {
let mut map = ParamStorage::new();
map.insert("hello", "world");
map.insert("hello", "owo");
assert_eq!(map.get("hello"), None);

map.insert("hello", "uwu");
assert_eq!(map.get("hello"), None);
}

#[test]
fn deserialize_impl() {
let mut map1 = ParamStorage::new();
map1.insert("hello", "world");

serde_test::assert_de_tokens(
&map1,
&[
Token::Map { len: Some(1) },
Token::BorrowedStr("hello"),
Token::BorrowedStr("world"),
Token::MapEnd,
],
);

let mut map2 = ParamStorage::new();
map2.insert("hello", "world");
map2.insert("hello", "owo");

assert!(map2.get("hello").is_none());

serde_test::assert_de_tokens(
&map2,
&[
Token::Map { len: Some(2) },
Token::BorrowedStr("hello"),
Token::BorrowedStr("world"),
Token::BorrowedStr("hello"),
Token::BorrowedStr("owo"),
Token::MapEnd,
],
);
}
}
1 change: 1 addition & 0 deletions lib/komainu/tests/basic_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn emptiness(#[case] username: &str, #[case] password: &str) {
#[case("pwease?? 🥺", "enter?? 👉👈")]
#[case("hällö 💀", "tschüß 😔")]
#[case("안녕하세요", "안녕")]
#[case("مرحبًا", "مع السلامة")]
#[case("🏳️‍⚧️", "🏳‍🌈")]
fn unicode_works(#[case] username: &str, #[case] password: &str) {
let creds = format!("{username}:{password}");
Expand Down
54 changes: 54 additions & 0 deletions lib/komainu/tests/param_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use komainu::params::ParamStorage;
use serde_test::Token;

#[test]
fn insert_get_works() {
let mut map = ParamStorage::new();
map.insert("hello", "world");
assert_eq!(map.get("hello"), Some(&"world"));
}

#[test]
fn multi_insert_empty() {
let mut map = ParamStorage::new();
map.insert("hello", "world");
map.insert("hello", "owo");
assert_eq!(map.get("hello"), None);

map.insert("hello", "uwu");
assert_eq!(map.get("hello"), None);
}

#[test]
fn deserialize_impl() {
let mut map1 = ParamStorage::new();
map1.insert("hello", "world");

serde_test::assert_de_tokens(
&map1,
&[
Token::Map { len: Some(1) },
Token::BorrowedStr("hello"),
Token::BorrowedStr("world"),
Token::MapEnd,
],
);

let mut map2 = ParamStorage::new();
map2.insert("hello", "world");
map2.insert("hello", "owo");

assert!(map2.get("hello").is_none());

serde_test::assert_de_tokens(
&map2,
&[
Token::Map { len: Some(2) },
Token::BorrowedStr("hello"),
Token::BorrowedStr("world"),
Token::BorrowedStr("hello"),
Token::BorrowedStr("owo"),
Token::MapEnd,
],
);
}

0 comments on commit 18722b0

Please sign in to comment.