Skip to content

Commit 142a5d1

Browse files
committed
Test the macros in id_macros module
So that the tests are near the things they test. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent a4f7f94 commit 142a5d1

2 files changed

Lines changed: 65 additions & 16 deletions

File tree

src/id_macros.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,68 @@ macro_rules! str_id {
112112
}
113113
};
114114
}
115+
116+
#[cfg(test)]
117+
mod tests {
118+
use std::borrow::Borrow;
119+
use std::fmt;
120+
use std::iter;
121+
use std::ops::Deref;
122+
123+
use crate::errors::{Error, ErrorKind};
124+
use crate::result::{DmError, DmResult};
125+
126+
const TYPE_LEN: usize = 12;
127+
str_id!(Id, IdBuf, TYPE_LEN);
128+
129+
#[test]
130+
/// Test for errors on an empty name.
131+
fn test_empty_name() {
132+
assert!(match Id::new("") {
133+
Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true,
134+
_ => false,
135+
});
136+
assert!(match IdBuf::new("".into()) {
137+
Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true,
138+
_ => false,
139+
})
140+
}
141+
142+
#[test]
143+
/// Test for errors on an overlong name.
144+
fn test_too_long_name() {
145+
let name = iter::repeat('a').take(TYPE_LEN + 1).collect::<String>();
146+
assert!(match Id::new(&name) {
147+
Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true,
148+
_ => false,
149+
});
150+
assert!(match IdBuf::new(name) {
151+
Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true,
152+
_ => false,
153+
})
154+
}
155+
156+
#[test]
157+
/// Test the concrete methods and traits of the interface.
158+
fn test_interface() {
159+
let id = Id::new("id").expect("is valid id");
160+
let id_buf = IdBuf::new("id".into()).expect("is valid id");
161+
162+
// Test as_bytes.
163+
assert_eq!(id.as_bytes(), &[105u8, 100u8]);
164+
assert_eq!(id_buf.as_bytes(), &[105u8, 100u8]);
165+
166+
// Test ToOwned implementation.
167+
// $B.to_owned() == $O
168+
assert_eq!(id.to_owned(), id_buf);
169+
170+
// Test Display implementation
171+
// X.to_string() = (*X).to_string()
172+
assert_eq!(id.to_string(), (*id).to_string());
173+
assert_eq!(id_buf.to_string(), (*id_buf).to_string());
174+
175+
// Test Deref
176+
assert_eq!(id_buf.deref(), id);
177+
assert_eq!(*id_buf, *id);
178+
}
179+
}

src/types.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,3 @@ impl<'a> fmt::Display for DevId<'a> {
9494
const DM_TARGET_TYPE_LEN: usize = 16;
9595

9696
str_id!(TargetType, TargetTypeBuf, DM_TARGET_TYPE_LEN);
97-
98-
#[cfg(test)]
99-
mod tests {
100-
use crate::errors::Error;
101-
102-
use super::*;
103-
104-
#[test]
105-
/// Verify that creating an empty DmName is an error.
106-
pub fn test_empty_name() {
107-
assert!(match DmName::new("") {
108-
Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true,
109-
_ => false,
110-
})
111-
}
112-
}

0 commit comments

Comments
 (0)