Skip to content

Commit 7686f95

Browse files
authored
chore: Move tests inline with code under test (#58)
1 parent 0fa199a commit 7686f95

22 files changed

Lines changed: 2686 additions & 2771 deletions

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
uses: taiki-e/install-action@cargo-llvm-cov
5252

5353
- name: Generate code coverage
54-
run: cargo llvm-cov --lib --all-features --lcov --ignore-filename-regex tests --output-path lcov.info
54+
run: cargo llvm-cov --lib --all-features --lcov --output-path lcov.info
5555

5656
- name: Filter out tests
5757
uses: scouten/uncover-tests@v1

src/box_type.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,24 @@ pub const DESCRIPTION_BOX_TYPE: BoxType = BoxType(*b"jumd");
6363

6464
/// Box type for JUMBF super box (`b"jumb"`).
6565
pub const SUPER_BOX_TYPE: BoxType = BoxType(*b"jumb");
66+
67+
#[cfg(test)]
68+
mod tests {
69+
#![allow(clippy::expect_used)]
70+
#![allow(clippy::panic)]
71+
#![allow(clippy::unwrap_used)]
72+
73+
use crate::BoxType;
74+
75+
#[test]
76+
fn impl_debug() {
77+
let x = BoxType([1, 2, 3, 4]);
78+
assert_eq!(format!("{x:#?}"), "[0x01, 0x02, 0x03, 0x04]");
79+
80+
let x = BoxType(*b"abcd");
81+
assert_eq!(format!("{x:#?}"), "b\"abcd\"");
82+
83+
let x = BoxType([b'a', b'b', b'c', 0x7f]);
84+
assert_eq!(format!("{x:#?}"), "[0x61, 0x62, 0x63, 0x7f]");
85+
}
86+
}

src/builder/data_box_builder.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,74 @@ impl<'a> ToBox for DataBoxBuilder<'a> {
7373
to_stream.write_all(&self.data)
7474
}
7575
}
76+
77+
#[cfg(test)]
78+
mod tests {
79+
#![allow(clippy::expect_used)]
80+
#![allow(clippy::panic)]
81+
#![allow(clippy::unwrap_used)]
82+
83+
use std::io::Cursor;
84+
85+
use hex_literal::hex;
86+
87+
use crate::{
88+
box_type::DESCRIPTION_BOX_TYPE,
89+
builder::{
90+
to_box::{jumbf_size, write_jumbf},
91+
DataBoxBuilder, ToBox,
92+
},
93+
};
94+
95+
#[test]
96+
fn simple_box_borrowed() {
97+
let expected_jumbf = hex!(
98+
"00000026" // box size
99+
"6a756d64" // box type = 'jumd'
100+
"00000000000000000000000000000000" // UUID
101+
"03" // toggles
102+
"746573742e64657363626f7800" // label
103+
);
104+
105+
let boxx = DataBoxBuilder::from_borrowed(DESCRIPTION_BOX_TYPE, &expected_jumbf[8..]);
106+
107+
assert_eq!(boxx.box_type(), DESCRIPTION_BOX_TYPE);
108+
assert_eq!(boxx.payload_size().unwrap(), 30);
109+
110+
let mut payload = Cursor::new(Vec::<u8>::new());
111+
boxx.write_payload(&mut payload).unwrap();
112+
assert_eq!(*payload.into_inner(), expected_jumbf[8..]);
113+
114+
assert_eq!(jumbf_size(&boxx).unwrap(), 38);
115+
116+
let mut jumbf = Cursor::new(Vec::<u8>::new());
117+
write_jumbf(&boxx, &mut jumbf).unwrap();
118+
assert_eq!(*jumbf.into_inner(), expected_jumbf);
119+
}
120+
121+
#[test]
122+
fn simple_box_owned() {
123+
let expected_jumbf = hex!(
124+
"00000026" // box size
125+
"6a756d64" // box type = 'jumd'
126+
"00000000000000000000000000000000" // UUID
127+
"03" // toggles
128+
"746573742e64657363626f7800" // label
129+
);
130+
131+
let boxx = DataBoxBuilder::from_owned(DESCRIPTION_BOX_TYPE, expected_jumbf[8..].to_owned());
132+
133+
assert_eq!(boxx.box_type(), DESCRIPTION_BOX_TYPE);
134+
assert_eq!(boxx.payload_size().unwrap(), 30);
135+
136+
let mut payload = Cursor::new(Vec::<u8>::new());
137+
boxx.write_payload(&mut payload).unwrap();
138+
assert_eq!(*payload.into_inner(), expected_jumbf[8..]);
139+
140+
assert_eq!(jumbf_size(&boxx).unwrap(), 38);
141+
142+
let mut jumbf = Cursor::new(Vec::<u8>::new());
143+
write_jumbf(&boxx, &mut jumbf).unwrap();
144+
assert_eq!(*jumbf.into_inner(), expected_jumbf);
145+
}
146+
}

src/builder/placeholder_data_box.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,147 @@ impl ToBox for PlaceholderDataBox {
120120
Ok(())
121121
}
122122
}
123+
124+
#[cfg(test)]
125+
mod tests {
126+
#![allow(clippy::expect_used)]
127+
#![allow(clippy::panic)]
128+
#![allow(clippy::unwrap_used)]
129+
130+
use std::io::{Cursor, Write};
131+
132+
use hex_literal::hex;
133+
134+
use crate::{
135+
builder::{
136+
to_box::{jumbf_size, write_jumbf},
137+
PlaceholderDataBox, ToBox,
138+
},
139+
BoxType,
140+
};
141+
142+
const RANDOM_BOX_TYPE: BoxType = BoxType(*b"abcd");
143+
144+
#[test]
145+
fn simple_case() {
146+
let expected_jumbf = hex!(
147+
"00000018" // box size
148+
"61626364" // box type = 'abcd'
149+
"00000000000000000000000000000000" // placeholder
150+
);
151+
152+
let pbox = PlaceholderDataBox::new(RANDOM_BOX_TYPE, 16);
153+
154+
assert_eq!(pbox.box_type(), RANDOM_BOX_TYPE);
155+
assert_eq!(pbox.payload_size().unwrap(), 16);
156+
assert_eq!(jumbf_size(&pbox).unwrap(), 24);
157+
158+
let mut jumbf = Cursor::new(Vec::<u8>::new());
159+
write_jumbf(&pbox, &mut jumbf).unwrap();
160+
assert_eq!(*jumbf.get_ref(), expected_jumbf);
161+
162+
let expected_jumbf = hex!(
163+
"00000018" // box size
164+
"61626364" // box type = 'abcd'
165+
"31323334353637383930000000000000" // replacement payload
166+
);
167+
168+
pbox.replace_payload(&mut jumbf, &expected_jumbf[8..18])
169+
.unwrap();
170+
assert_eq!(*jumbf.get_ref(), expected_jumbf);
171+
}
172+
173+
#[test]
174+
fn error_write_payload_only() {
175+
// PlaceholderDataBox reports an error if its .write_payload() method
176+
// is called by itself.
177+
178+
let pbox = PlaceholderDataBox::new(RANDOM_BOX_TYPE, 16);
179+
180+
let mut payload = Cursor::new(Vec::<u8>::new());
181+
let err = pbox.write_payload(&mut payload).unwrap_err();
182+
assert_eq!(
183+
"Custom { kind: Other, error: \"placeholder stream should have some data already\" }",
184+
format!("{err:?}")
185+
);
186+
}
187+
188+
#[test]
189+
fn error_payload_too_large() {
190+
let expected_jumbf = hex!(
191+
"00000018" // box size
192+
"61626364" // box type = 'abcd'
193+
"00000000000000000000000000000000" // placeholder
194+
);
195+
196+
let pbox = PlaceholderDataBox::new(RANDOM_BOX_TYPE, 16);
197+
198+
let mut jumbf = Cursor::new(Vec::<u8>::new());
199+
write_jumbf(&pbox, &mut jumbf).unwrap();
200+
assert_eq!(*jumbf.get_ref(), expected_jumbf);
201+
202+
let payload_too_large = [1u8; 17];
203+
let err = pbox
204+
.replace_payload(&mut jumbf, &payload_too_large)
205+
.unwrap_err();
206+
207+
assert_eq!(
208+
"Custom { kind: Other, error: \"replace_payload: payload (17 bytes) is larger than reserved capacity (16 bytes)\" }",
209+
format!("{err:?}")
210+
);
211+
212+
// No part of the original JUMBF as written should have been changed.
213+
assert_eq!(*jumbf.get_ref(), expected_jumbf);
214+
}
215+
216+
#[test]
217+
fn error_write_jumbf_not_called() {
218+
let pbox = PlaceholderDataBox::new(RANDOM_BOX_TYPE, 16);
219+
220+
let mut jumbf = Cursor::new(Vec::<u8>::new());
221+
let payload = [1u8; 16];
222+
let err = pbox.replace_payload(&mut jumbf, &payload).unwrap_err();
223+
224+
assert_eq!(
225+
"Custom { kind: Other, error: \"replace_payload: no offset recorded; call write_jumbf() first\" }",
226+
format!("{err:?}")
227+
);
228+
229+
// No part of the original JUMBF as written should have been changed.
230+
assert_eq!(*jumbf.get_ref(), []);
231+
}
232+
233+
#[test]
234+
fn offset() {
235+
let expected_jumbf = hex!(
236+
"41424344" // arbitrary prefix = 'ABCD'
237+
"00000018" // box size
238+
"61626364" // box type = 'abcd'
239+
"00000000000000000000000000000000" // placeholder
240+
);
241+
242+
let pbox = PlaceholderDataBox::new(RANDOM_BOX_TYPE, 16);
243+
244+
assert_eq!(pbox.box_type(), RANDOM_BOX_TYPE);
245+
assert_eq!(pbox.payload_size().unwrap(), 16);
246+
assert_eq!(jumbf_size(&pbox).unwrap(), 24);
247+
248+
let mut jumbf = Cursor::new(Vec::<u8>::new());
249+
jumbf.write_all(b"ABCD").unwrap();
250+
251+
write_jumbf(&pbox, &mut jumbf).unwrap();
252+
assert_eq!(*jumbf.get_ref(), expected_jumbf);
253+
254+
assert_eq!(pbox.offset(), Some(12));
255+
}
256+
257+
#[test]
258+
fn offset_before_write() {
259+
let pbox = PlaceholderDataBox::new(RANDOM_BOX_TYPE, 16);
260+
261+
assert_eq!(pbox.box_type(), RANDOM_BOX_TYPE);
262+
assert_eq!(pbox.payload_size().unwrap(), 16);
263+
assert_eq!(jumbf_size(&pbox).unwrap(), 24);
264+
assert_eq!(pbox.offset(), None);
265+
}
266+
}

0 commit comments

Comments
 (0)