Skip to content

Commit f601943

Browse files
committed
[#213] Add semantic string tests
1 parent a7c7a07 commit f601943

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

iceoryx2-bb/container/src/semantic_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ enum_gen! {
8080

8181
impl std::fmt::Display for SemanticStringError {
8282
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83-
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
83+
std::write!(f, "SemanticStringError::{:?}", self)
8484
}
8585
}
8686

iceoryx2-bb/trait-tests/tests/semantic_string_tests.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ use iceoryx2_bb_system_types::file_path::*;
1717
use iceoryx2_bb_system_types::group_name::*;
1818
use iceoryx2_bb_system_types::path::*;
1919
use iceoryx2_bb_system_types::user_name::*;
20+
use iceoryx2_bb_testing::assert_that;
21+
22+
#[test]
23+
fn display_error_enum_works() {
24+
assert_that!(format!("{}", SemanticStringError::InvalidContent), eq "SemanticStringError::InvalidContent");
25+
assert_that!(format!("{}", SemanticStringError::ExceedsMaximumLength), eq "SemanticStringError::ExceedsMaximumLength");
26+
}
2027

2128
#[generic_tests::define]
2229
mod semantic_string {
23-
use iceoryx2_bb_testing::assert_that;
2430

2531
use super::*;
2632

@@ -130,6 +136,13 @@ mod semantic_string {
130136

131137
assert_that!(sut.strip_prefix(b"a0123"), eq Ok(false));
132138
assert_that!(sut.as_bytes(), eq b"a4567");
139+
140+
let result = sut.strip_prefix(b"a45");
141+
if result.is_ok() {
142+
assert_that!(sut.as_bytes(), eq b"67");
143+
} else {
144+
assert_that!(result.err().unwrap(), eq SemanticStringError::InvalidContent);
145+
}
133146
}
134147

135148
#[test]
@@ -140,6 +153,13 @@ mod semantic_string {
140153

141154
assert_that!(sut.strip_suffix(b"a4567"), eq Ok(false));
142155
assert_that!(sut.as_bytes(), eq b"a0123");
156+
157+
let result = sut.strip_suffix(b"a0123");
158+
if result.is_ok() {
159+
assert_that!(sut.as_bytes(), eq b"");
160+
} else {
161+
assert_that!(result.err().unwrap(), eq SemanticStringError::InvalidContent);
162+
}
143163
}
144164

145165
#[test]
@@ -153,6 +173,13 @@ mod semantic_string {
153173
assert_that!(sut.truncate(6), is_ok);
154174
assert_that!(sut, len 4);
155175
assert_that!(sut.as_bytes(), eq b"a012");
176+
177+
let result = sut.truncate(0);
178+
if result.is_ok() {
179+
assert_that!(sut, is_empty);
180+
} else {
181+
assert_that!(result.err().unwrap(), eq SemanticStringError::InvalidContent);
182+
}
156183
}
157184

158185
#[test]
@@ -164,6 +191,56 @@ mod semantic_string {
164191
assert_that!(sut, is_err);
165192
}
166193

194+
#[test]
195+
fn is_full_works<const CAPACITY: usize, Sut: SemanticString<CAPACITY>>() {
196+
let sut = Sut::new(b"a01234567").unwrap();
197+
assert_that!(sut.is_full(), eq false);
198+
}
199+
200+
#[test]
201+
fn capacity_works<const CAPACITY: usize, Sut: SemanticString<CAPACITY>>() {
202+
let sut = Sut::new(b"a01234567").unwrap();
203+
assert_that!(sut.capacity(), eq CAPACITY);
204+
}
205+
206+
#[test]
207+
fn insert_too_much_bytes_fails<const CAPACITY: usize, Sut: SemanticString<CAPACITY>>() {
208+
let mut sut = Sut::new(b"a01234567").unwrap();
209+
let mut bytes = vec![];
210+
for _ in 0..8192 {
211+
bytes.push(b'a')
212+
}
213+
214+
let result = sut.insert_bytes(0, &bytes);
215+
assert_that!(result, is_err);
216+
assert_that!(
217+
result.err().unwrap(), eq
218+
SemanticStringError::ExceedsMaximumLength
219+
);
220+
}
221+
222+
#[test]
223+
fn pop_until_empty_works<const CAPACITY: usize, Sut: SemanticString<CAPACITY>>() {
224+
let mut sut = Sut::new(b"aaa").unwrap();
225+
226+
let mut do_pop = || {
227+
let result = sut.pop();
228+
if result.is_ok() {
229+
assert_that!(result.unwrap().unwrap(), eq b'a');
230+
} else {
231+
assert_that!(result.err().unwrap(), eq SemanticStringError::InvalidContent);
232+
}
233+
};
234+
235+
do_pop();
236+
do_pop();
237+
do_pop();
238+
239+
if sut.is_empty() {
240+
assert_that!(sut.pop().unwrap(), eq None);
241+
}
242+
}
243+
167244
#[instantiate_tests(<{FileName::max_len()}, FileName>)]
168245
mod file_name {}
169246

0 commit comments

Comments
 (0)