-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all escape tests into integration tests
- Loading branch information
Showing
2 changed files
with
102 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use pretty_assertions::assert_eq; | ||
use quick_xml::escape; | ||
use std::borrow::Cow; | ||
|
||
#[test] | ||
fn escape() { | ||
let unchanged = escape::escape("test"); | ||
// assert_eq does not check that Cow is borrowed, but we explicitly use Cow | ||
// because it influences diff | ||
// TODO: use assert_matches! when stabilized and other features will bump MSRV | ||
assert_eq!(unchanged, Cow::Borrowed("test")); | ||
assert!(matches!(unchanged, Cow::Borrowed(_))); | ||
|
||
assert_eq!(escape::escape("<&\"'>"), "<&"'>"); | ||
assert_eq!(escape::escape("<test>"), "<test>"); | ||
assert_eq!(escape::escape("\"a\"bc"), ""a"bc"); | ||
assert_eq!(escape::escape("\"a\"b&c"), ""a"b&c"); | ||
assert_eq!( | ||
escape::escape("prefix_\"a\"b&<>c"), | ||
"prefix_"a"b&<>c" | ||
); | ||
} | ||
|
||
#[test] | ||
fn partial_escape() { | ||
let unchanged = escape::partial_escape("test"); | ||
// assert_eq does not check that Cow is borrowed, but we explicitly use Cow | ||
// because it influences diff | ||
// TODO: use assert_matches! when stabilized and other features will bump MSRV | ||
assert_eq!(unchanged, Cow::Borrowed("test")); | ||
assert!(matches!(unchanged, Cow::Borrowed(_))); | ||
|
||
assert_eq!(escape::partial_escape("<&\"'>"), "<&\"'>"); | ||
assert_eq!(escape::partial_escape("<test>"), "<test>"); | ||
assert_eq!(escape::partial_escape("\"a\"bc"), "\"a\"bc"); | ||
assert_eq!(escape::partial_escape("\"a\"b&c"), "\"a\"b&c"); | ||
assert_eq!( | ||
escape::partial_escape("prefix_\"a\"b&<>c"), | ||
"prefix_\"a\"b&<>c" | ||
); | ||
} | ||
|
||
#[test] | ||
fn minimal_escape() { | ||
assert_eq!(escape::minimal_escape("test"), Cow::Borrowed("test")); | ||
assert_eq!(escape::minimal_escape("<&\"'>"), "<&\"'>"); | ||
assert_eq!(escape::minimal_escape("<test>"), "<test>"); | ||
assert_eq!(escape::minimal_escape("\"a\"bc"), "\"a\"bc"); | ||
assert_eq!(escape::minimal_escape("\"a\"b&c"), "\"a\"b&c"); | ||
assert_eq!( | ||
escape::minimal_escape("prefix_\"a\"b&<>c"), | ||
"prefix_\"a\"b&<>c" | ||
); | ||
} | ||
|
||
#[test] | ||
fn unescape() { | ||
let unchanged = escape::unescape("test"); | ||
// assert_eq does not check that Cow is borrowed, but we explicitly use Cow | ||
// because it influences diff | ||
// TODO: use assert_matches! when stabilized and other features will bump MSRV | ||
assert_eq!(unchanged, Ok(Cow::Borrowed("test"))); | ||
assert!(matches!(unchanged, Ok(Cow::Borrowed(_)))); | ||
|
||
assert_eq!( | ||
escape::unescape("<&test'">"), | ||
Ok("<&test'\">".into()) | ||
); | ||
assert_eq!(escape::unescape("0"), Ok("0".into())); | ||
assert_eq!(escape::unescape("0"), Ok("0".into())); | ||
assert!(escape::unescape("&foo;").is_err()); | ||
} | ||
|
||
#[test] | ||
fn unescape_with() { | ||
let custom_entities = |ent: &str| match ent { | ||
"foo" => Some("BAR"), | ||
_ => None, | ||
}; | ||
|
||
let unchanged = escape::unescape_with("test", custom_entities); | ||
// assert_eq does not check that Cow is borrowed, but we explicitly use Cow | ||
// because it influences diff | ||
// TODO: use assert_matches! when stabilized and other features will bump MSRV | ||
assert_eq!(unchanged, Ok(Cow::Borrowed("test"))); | ||
assert!(matches!(unchanged, Ok(Cow::Borrowed(_)))); | ||
|
||
assert!(escape::unescape_with("<", custom_entities).is_err()); | ||
assert_eq!( | ||
escape::unescape_with("0", custom_entities), | ||
Ok("0".into()) | ||
); | ||
assert_eq!( | ||
escape::unescape_with("0", custom_entities), | ||
Ok("0".into()) | ||
); | ||
assert_eq!( | ||
escape::unescape_with("&foo;", custom_entities), | ||
Ok("BAR".into()) | ||
); | ||
assert!(escape::unescape_with("&fop;", custom_entities).is_err()); | ||
} |