Skip to content

Commit faf19dc

Browse files
committed
Add some unit tests (rustc copy)
Same as rust-lang/rust#56841
1 parent 25b681f commit faf19dc

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/header.rs

+26
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,29 @@ fn parse_normalization_string(line: &mut &str) -> Option<String> {
642642
*line = &line[end+1..];
643643
Some(result)
644644
}
645+
646+
#[test]
647+
fn test_parse_normalization_string() {
648+
let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
649+
let first = parse_normalization_string(&mut s);
650+
assert_eq!(first, Some("something (32 bits)".to_owned()));
651+
assert_eq!(s, " -> \"something ($WORD bits)\".");
652+
653+
// Nothing to normalize (No quotes)
654+
let mut s = "normalize-stderr-32bit: something (32 bits) -> something ($WORD bits).";
655+
let first = parse_normalization_string(&mut s);
656+
assert_eq!(first, None);
657+
assert_eq!(s, r#"normalize-stderr-32bit: something (32 bits) -> something ($WORD bits)."#);
658+
659+
// Nothing to normalize (Only a single quote)
660+
let mut s = "normalize-stderr-32bit: \"something (32 bits) -> something ($WORD bits).";
661+
let first = parse_normalization_string(&mut s);
662+
assert_eq!(first, None);
663+
assert_eq!(s, "normalize-stderr-32bit: \"something (32 bits) -> something ($WORD bits).");
664+
665+
// Nothing to normalize (Three quotes)
666+
let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits).";
667+
let first = parse_normalization_string(&mut s);
668+
assert_eq!(first, Some("something (32 bits)".to_owned()));
669+
assert_eq!(s, " -> \"something ($WORD bits).");
670+
}

src/util.rs

+28
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub fn matches_os(triple: &str, name: &str) -> bool {
6464
}
6565
panic!("Cannot determine OS from triple");
6666
}
67+
68+
/// Determine the architecture from `triple`
6769
pub fn get_arch(triple: &str) -> &'static str {
6870
for &(triple_arch, arch) in ARCH_TABLE {
6971
if triple.contains(triple_arch) {
@@ -108,3 +110,29 @@ pub fn logv(config: &Config, s: String) {
108110
println!("{}", s);
109111
}
110112
}
113+
114+
#[test]
115+
#[should_panic(expected = "Cannot determine Architecture from triple")]
116+
fn test_get_arch_failure() {
117+
get_arch("abc");
118+
}
119+
120+
#[test]
121+
fn test_get_arch() {
122+
assert_eq!("x86_64", get_arch("x86_64-unknown-linux-gnu"));
123+
assert_eq!("x86_64", get_arch("amd64"));
124+
}
125+
126+
#[test]
127+
#[should_panic(expected = "Cannot determine OS from triple")]
128+
fn test_matches_os_failure() {
129+
matches_os("abc", "abc");
130+
}
131+
132+
#[test]
133+
fn test_matches_os() {
134+
assert!(matches_os("x86_64-unknown-linux-gnu", "linux"));
135+
assert!(matches_os("wasm32-unknown-unknown", "emscripten"));
136+
assert!(matches_os("wasm32-unknown-unknown", "wasm32-bare"));
137+
assert!(!matches_os("wasm32-unknown-unknown", "windows"));
138+
}

0 commit comments

Comments
 (0)