Skip to content

Commit

Permalink
Improve test coverage (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
SRv6d authored Oct 5, 2024
1 parent 5d8be14 commit a3636f7
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ mod tests {
Attribute::new("ASNumber".parse().unwrap(), "32934".parse().unwrap()),
"ASNumber: 32934\n"
)]
#[case(
Attribute::new("ASNumber".parse().unwrap(), Value::unchecked_single(None)),
"ASNumber: \n"
)]
#[case(
Attribute::new("ASName".parse().unwrap(), "FACEBOOK".parse().unwrap()),
"ASName: FACEBOOK\n"
Expand Down Expand Up @@ -482,6 +486,21 @@ mod tests {
" invalid prefixes from peers and customers.\n",
)
)]
#[case(
Attribute::new(
"remarks".parse().unwrap(),
Value::unchecked_multi(
vec![
None,
None
]
)
),
concat!(
"remarks: \n",
" \n",
)
)]
fn attribute_display_multi_line(#[case] attribute: Attribute, #[case] expected: &str) {
assert_eq!(attribute.to_string(), expected);
}
Expand Down Expand Up @@ -533,6 +552,14 @@ mod tests {
assert_eq!(name_display, "address");
}

#[rstest]
#[case("role")]
#[case("person")]
fn name_deref(#[case] s: &str) {
let name = Name::unchecked(s);
assert_eq!(*name, *s);
}

#[rstest]
#[case("role")]
#[case("person")]
Expand Down
81 changes: 81 additions & 0 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,87 @@ mod tests {
assert_eq!(borrowed.to_string(), expected);
}

#[rstest]
#[case(
Object::new(vec![
Attribute::unchecked_single("role", "ACME Company"),
]),
1
)]
#[case(
Object::new(vec![
Attribute::unchecked_single("role", "ACME Company"),
Attribute::unchecked_single("address", "Packet Street 6"),
Attribute::unchecked_single("address", "128 Series of Tubes"),
Attribute::unchecked_single("address", "Internet"),
Attribute::unchecked_single("email", "[email protected]"),
Attribute::unchecked_single("nic-hdl", "RPSL1-RIPE"),
Attribute::unchecked_single("source", "RIPE"),
]),
7
)]
fn object_len(#[case] object: Object, #[case] expected: usize) {
assert_eq!(object.len(), expected);
}

#[rstest]
#[case(
Object::new(vec![
Attribute::unchecked_single("role", "ACME Company"),
Attribute::unchecked_single("address", "Packet Street 6"),
Attribute::unchecked_single("address", "128 Series of Tubes"),
Attribute::unchecked_single("address", "Internet"),
Attribute::unchecked_single("email", "[email protected]"),
Attribute::unchecked_single("nic-hdl", "RPSL1-RIPE"),
Attribute::unchecked_single("source", "RIPE"),
]),
2,
Attribute::unchecked_single("address", "128 Series of Tubes"),
)]
fn object_index(#[case] object: Object, #[case] index: usize, #[case] expected: Attribute) {
assert_eq!(object[index], expected);
}

#[rstest]
#[case(
vec![
Attribute::unchecked_single("role", "ACME Company"),
Attribute::unchecked_single("address", "Packet Street 6"),
Attribute::unchecked_single("address", "128 Series of Tubes"),
Attribute::unchecked_single("address", "Internet"),
Attribute::unchecked_single("email", "[email protected]"),
Attribute::unchecked_single("nic-hdl", "RPSL1-RIPE"),
Attribute::unchecked_single("source", "RIPE"),
],
)]
fn object_deref(#[case] attributes: Vec<Attribute<'static>>) {
let object = Object::new(attributes.clone());
assert_eq!(*object, attributes);
}

#[rstest]
#[case(
vec![
Attribute::unchecked_single("role", "ACME Company"),
Attribute::unchecked_single("address", "Packet Street 6"),
Attribute::unchecked_single("address", "128 Series of Tubes"),
Attribute::unchecked_single("address", "Internet"),
Attribute::unchecked_single("email", "[email protected]"),
Attribute::unchecked_single("nic-hdl", "RPSL1-RIPE"),
Attribute::unchecked_single("source", "RIPE"),
],
)]
fn object_into_iter(#[case] attributes: Vec<Attribute<'static>>) {
let object = Object::new(attributes.clone());

let attr_iter = attributes.into_iter();
let obj_iter = object.into_iter();

for (a, b) in attr_iter.zip(obj_iter) {
assert_eq!(a, b);
}
}

#[rstest]
#[case(
Object::new(vec![
Expand Down
25 changes: 24 additions & 1 deletion src/parser/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod tests {

#[test]
/// When parsing RPSL, the resulting object contains the original source it was created from.
fn parsed_object_contains_source() {
fn object_block_parsed_object_contains_source() {
let rpsl = &mut concat!(
"email: [email protected]\n",
"nic-hdl: RPSL1-RIPE\n",
Expand All @@ -183,6 +183,29 @@ mod tests {
assert!(parser.parse_next(object).is_err());
}

#[rstest]
#[case(
&mut concat!(
"\n\n",
"email: [email protected]\n",
"nic-hdl: RPSL1-RIPE\n",
"\n",
"\n\n\n"
),
vec![
Attribute::unchecked_single("email", "[email protected]"),
Attribute::unchecked_single("nic-hdl", "RPSL1-RIPE")
]
)]
fn object_block_padded_valid(#[case] given: &mut &str, #[case] attributes: Vec<Attribute>) {
let expected = Object::from_parsed(given, attributes);

let mut parser = object_block_padded::<_, ContextError>(object_block());
let parsed = parser.parse_next(given).unwrap();

assert_eq!(parsed, expected);
}

#[rstest]
#[case(
&mut "% Note: This is a server message\n"
Expand Down

0 comments on commit a3636f7

Please sign in to comment.