Skip to content

Commit

Permalink
add tests for Serialize and Deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
drash-course committed Jul 4, 2024
1 parent ec28664 commit 1f6132c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde = ["dep:serde"]

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
serde_json = "1.0"

[[bench]]
name = "istring-benches"
Expand Down
40 changes: 38 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,53 @@ mod tests {
}

#[test]
fn test_send() {
fn it_is_send() {
fn assert_send<T: Send>() {}
assert_send::<IString>();
}

#[test]
fn test_sync() {
fn it_is_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<IString>();
}

#[cfg(feature = "serde")]
#[test]
fn it_serializes() {
with_exclusive_use_of_shared_storage(|| {
use serde::Serialize;

#[derive(Serialize)]
struct ExampleDTO {
favorite_dish: IString
}

let dto = ExampleDTO { favorite_dish: "pasta".intern() };

assert_eq!(serde_json::to_string(&dto).unwrap(), "{\"favorite_dish\":\"pasta\"}");
});
}

#[cfg(feature = "serde")]
#[test]
fn it_deserializes() {
with_exclusive_use_of_shared_storage(|| {
use serde::Deserialize;

#[derive(Deserialize, PartialEq, Debug)]
struct ExampleDTO {
favorite_dish: IString
}

let input = "{\"favorite_dish\":\"pasta\"}";

let dto: Result<ExampleDTO, _> = serde_json::from_str(input);

assert_eq!(dto.unwrap(), ExampleDTO { favorite_dish: "pasta".into() });
});
}

fn assert_string_count_in_storage(count: usize) {
let guard = SHARED_STORAGE.read_handle.lock().unwrap();
let read_handle = guard.enter().unwrap();
Expand Down

0 comments on commit 1f6132c

Please sign in to comment.