Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage #122

Merged
merged 8 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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