Skip to content

Commit

Permalink
Add a serialization test
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Oct 1, 2024
1 parent 3e7f324 commit 7fd0507
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ dependencies = [
"env_logger",
"itertools",
"log",
"ron",
"serde",
"serde_json",
]

[[package]]
Expand Down Expand Up @@ -831,6 +833,12 @@ dependencies = [
"either",
]

[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"

[[package]]
name = "jni"
version = "0.21.1"
Expand Down Expand Up @@ -1482,6 +1490,12 @@ dependencies = [
"windows-sys 0.52.0",
]

[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"

[[package]]
name = "same-file"
version = "1.0.6"
Expand Down Expand Up @@ -1523,6 +1537,18 @@ dependencies = [
"syn",
]

[[package]]
name = "serde_json"
version = "1.0.128"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]

[[package]]
name = "simd-adler32"
version = "0.3.7"
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ log = { version = "0.4", features = ["std"] }
serde = { version = "1", features = ["derive"], optional = true }


# For the example:
[dev-dependencies]
# For the example:
eframe = { version = "0.29", default-features = false, features = [
"default_fonts",
"glow",
Expand All @@ -48,6 +48,10 @@ env_logger = { version = "0.10", default-features = false, features = [
"humantime",
] }

# For tests:
serde_json = "1"
ron = "0.8"



[patch.crates-io]
Expand Down
48 changes: 48 additions & 0 deletions tests/serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use egui_tiles::{Tiles, Tree};

#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]

Check failure on line 3 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

failed to resolve: use of undeclared crate or module `serde`

Check failure on line 3 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

failed to resolve: use of undeclared crate or module `serde`

Check failure on line 3 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

failed to resolve: use of undeclared crate or module `serde`

Check failure on line 3 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

failed to resolve: use of undeclared crate or module `serde`
struct Pane {
nr: usize,
}

fn create_tree() -> Tree<Pane> {
let mut next_view_nr = 0;
let mut gen_pane = || {
let pane = Pane { nr: next_view_nr };
next_view_nr += 1;
pane
};

let mut tiles = Tiles::default();

let mut tabs = vec![];
tabs.push({
let children = (0..7).map(|_| tiles.insert_pane(gen_pane())).collect();
tiles.insert_horizontal_tile(children)
});
tabs.push({
let cells = (0..11).map(|_| tiles.insert_pane(gen_pane())).collect();
tiles.insert_grid_tile(cells)
});
tabs.push(tiles.insert_pane(gen_pane()));

let root = tiles.insert_tab_tile(tabs);

Tree::new("my_tree", root, tiles)
}

#[test]
fn test_serialize_json() {
let original = create_tree();
let json = serde_json::to_string(&original).expect("json serialize");

Check failure on line 37 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

the trait bound `Tree<Pane>: serde::ser::Serialize` is not satisfied

Check failure on line 37 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

the trait bound `Tree<Pane>: serde::ser::Serialize` is not satisfied
let restored = serde_json::from_str(&json).expect("json deserialize");

Check failure on line 38 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

the trait bound `Tree<Pane>: serde::de::Deserialize<'_>` is not satisfied

Check failure on line 38 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

the trait bound `Tree<Pane>: serde::de::Deserialize<'_>` is not satisfied
assert_eq!(original, restored, "JSON did not round-trip");
}

#[test]
fn test_serialize_ron() {
let original = create_tree();
let ron = ron::to_string(&original).expect("ron serialize");

Check failure on line 45 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

the trait bound `Tree<Pane>: serde::ser::Serialize` is not satisfied

Check failure on line 45 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

the trait bound `Tree<Pane>: serde::ser::Serialize` is not satisfied
let restored = ron::from_str(&ron).expect("ron deserialize");

Check failure on line 46 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

the trait bound `Tree<Pane>: serde::de::Deserialize<'_>` is not satisfied

Check failure on line 46 in tests/serialize.rs

View workflow job for this annotation

GitHub Actions / Rust

the trait bound `Tree<Pane>: serde::de::Deserialize<'_>` is not satisfied
assert_eq!(original, restored, "RON did not round-trip");
}

0 comments on commit 7fd0507

Please sign in to comment.