Skip to content

Commit bec2a9b

Browse files
committed
feat: migrate size tests to proptest
1 parent 1423f09 commit bec2a9b

File tree

4 files changed

+41
-102
lines changed

4 files changed

+41
-102
lines changed

crates/league-modpkg/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ rmp-serde = "1.3.0"
1212
twox-hash = { version = "2.0.1", features = ["xxhash64"] }
1313
binrw = "0.14.1"
1414
itertools = "0.14.0"
15+
proptest = "1.6.0"
16+
17+
[dev-dependencies]
18+
proptest-derive = "0.5.1"
Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use binrw::{binrw, NullString};
21
use binrw::binrw;
32

43
use crate::utils::{nullstr_read, nullstr_write};
@@ -40,59 +39,15 @@ impl ModpkgLicense {
4039
}
4140
}
4241

43-
// TODO: use proptest here
4442
#[cfg(test)]
4543
mod tests {
46-
use std::io::Cursor;
47-
48-
use binrw::BinWrite;
49-
5044
use super::*;
51-
52-
#[test]
53-
fn test_none_size() {
54-
let license = ModpkgLicense::default();
55-
let mut buf = Cursor::new(Vec::with_capacity(license.size() + 512));
56-
license.write(&mut buf).unwrap();
57-
println!("{:x?}", buf.clone().into_inner());
58-
59-
assert_eq!(
60-
license.size(),
61-
buf.into_inner().len(),
62-
"comparing reported size with real size"
63-
);
64-
}
65-
45+
use crate::utils::test;
46+
use proptest::prelude::*;
47+
proptest! {
6648
#[test]
67-
fn test_spdx_size() {
68-
let license = ModpkgLicense::Spdx {
69-
spdx_id: "test".to_string().into(),
70-
};
71-
72-
let mut buf = Cursor::new(Vec::with_capacity(license.size() + 512));
73-
license.write(&mut buf).unwrap();
74-
75-
assert_eq!(
76-
license.size(),
77-
buf.into_inner().len(),
78-
"comparing reported size with real size"
79-
);
80-
}
81-
#[test]
82-
fn test_custom_size() {
83-
let license = ModpkgLicense::Custom {
84-
name: "customName".into(),
85-
url: "http://fake.url/".into(),
86-
};
87-
let mut buf = Cursor::new(Vec::with_capacity(license.size() + 512));
88-
license.write(&mut buf).unwrap();
89-
90-
println!("{:x?}", buf.clone().into_inner());
91-
92-
assert_eq!(
93-
license.size(),
94-
buf.into_inner().len(),
95-
"comparing reported size with real size"
96-
);
49+
fn test_license_size(license: ModpkgLicense) {
50+
test::written_size(&license, license.size());
51+
}
9752
}
9853
}

crates/league-modpkg/src/metadata.rs

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
99
#[binrw]
1010
#[brw(little)]
1111
#[derive(Debug, PartialEq, Default)]
12+
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
1213
pub struct ModpkgMetadata {
1314
#[br(temp)]
1415
#[bw(calc = name.len() as u32)]
@@ -95,6 +96,7 @@ impl ModpkgMetadata {
9596
#[binrw]
9697
#[brw(little)]
9798
#[derive(Debug, PartialEq, Serialize, Deserialize)]
99+
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
98100
pub struct ModpkgAuthor {
99101
#[br(temp)]
100102
#[bw( calc = name.len() as u32 )]
@@ -128,61 +130,19 @@ impl ModpkgAuthor {
128130
}
129131
}
130132

131-
// TODO: use proptest here
132133
#[cfg(test)]
133134
mod tests {
134-
use std::io::Cursor;
135-
136-
use binrw::BinWrite;
135+
use crate::utils::test;
137136

138137
use super::*;
139-
140-
#[test]
141-
fn test_empty_metadata_size() {
142-
let metadata = ModpkgMetadata::default();
143-
144-
let mut buf = Cursor::new(Vec::with_capacity(metadata.size() + 512));
145-
metadata.write(&mut buf).unwrap();
146-
147-
assert_eq!(
148-
metadata.size(),
149-
buf.into_inner().len(),
150-
"comparing reported size with real size"
151-
);
152-
}
153-
138+
use proptest::prelude::*;
139+
proptest! {
154140
#[test]
155-
fn test_metadata_size() {
156-
let mod_name = "test".to_string();
157-
let display_name = "test".to_string();
158-
let description = "test".to_string();
159-
let version = "test".to_string();
160-
let distributor = "test".to_string();
161-
let author_name = "test".to_string();
162-
let author_role = "test".to_string();
163-
let license = ModpkgLicense::Spdx {
164-
spdx_id: "test".to_string().into(),
165-
};
166-
167-
let metadata = ModpkgMetadata {
168-
name: mod_name,
169-
display_name,
170-
description: Some(description),
171-
version,
172-
distributor: Some(distributor),
173-
authors: vec![ModpkgAuthor {
174-
name: author_name,
175-
role: Some(author_role),
176-
}],
177-
license,
178-
};
179-
let mut buf = Cursor::new(Vec::with_capacity(metadata.size() + 512));
180-
metadata.write(&mut buf).unwrap();
181-
182-
assert_eq!(
183-
metadata.size(),
184-
buf.into_inner().len(),
185-
"comparing reported size with real size"
186-
);
141+
fn test_metadata_size(metadata: ModpkgMetadata) {
142+
test::written_size(&metadata, metadata.size());
143+
}
144+
fn test_author_size(author: ModpkgAuthor) {
145+
test::written_size(&author, author.size());
146+
}
187147
}
188148
}

crates/league-modpkg/src/utils.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,23 @@ pub fn nullstr_read(s: NullString) -> Result<String, std::string::FromUtf8Error>
1818
pub fn nullstr_write<'a>(s: impl Into<&'a String>) -> NullString {
1919
NullString::from(s.into().as_str())
2020
}
21+
22+
#[cfg(test)]
23+
pub mod test {
24+
use binrw::{meta, BinWrite};
25+
use std::io::Cursor;
26+
27+
pub fn written_size<I>(item: &I, expected_size: usize)
28+
where
29+
for<'a> I: BinWrite<Args<'a> = ()> + meta::WriteEndian,
30+
{
31+
let mut buf = Cursor::new(Vec::with_capacity(expected_size + 64));
32+
item.write(&mut buf).unwrap();
33+
34+
assert_eq!(
35+
expected_size,
36+
buf.into_inner().len(),
37+
"comparing reported size with real size"
38+
);
39+
}
40+
}

0 commit comments

Comments
 (0)