Skip to content

Commit 0251fe6

Browse files
authored
ref: Use strip_suffix for source_root and binary-raw strings in tests (#62)
1 parent 2f864d5 commit 0251fe6

File tree

8 files changed

+67
-67
lines changed

8 files changed

+67
-67
lines changed

src/decoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result<SourceMap> {
189189

190190
let sources = match rsm.source_root {
191191
Some(ref source_root) if !source_root.is_empty() => {
192-
let source_root = if source_root.ends_with('/') {
193-
source_root[..source_root.len() - 1].to_string()
192+
let source_root = if let Some(stripped) = source_root.strip_suffix('/') {
193+
stripped
194194
} else {
195-
source_root.clone()
195+
source_root
196196
};
197197

198198
sources

tests/test_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn test_builder_into_sourcemap() {
1212
assert_eq!(sm.get_source(0), Some("baz.js"));
1313
assert_eq!(sm.get_name(0), Some("x"));
1414

15-
let expected = b"{\"version\":3,\"sources\":[\"baz.js\"],\"sourceRoot\":\"/foo/bar\",\"names\":[\"x\"],\"mappings\":\"\"}";
15+
let expected = br#"{"version":3,"sources":["baz.js"],"sourceRoot":"/foo/bar","names":["x"],"mappings":""}"#;
1616
let mut output: Vec<u8> = vec![];
1717
sm.to_writer(&mut output).unwrap();
1818
assert_eq!(output, expected);

tests/test_decoder.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use sourcemap::{decode_data_url, DecodedMap, SourceMap, Token};
55

66
#[test]
77
fn test_no_header() {
8-
let input: &[_] = b"[1, 2, 3]";
8+
let input: &[_] = br#"[1, 2, 3]"#;
99
let mut reader = io::BufReader::new(input);
1010
let mut text = String::new();
1111
reader.read_line(&mut text).ok();
@@ -14,21 +14,21 @@ fn test_no_header() {
1414

1515
#[test]
1616
fn test_no_header_object() {
17-
let input: &[_] = b"{\"x\":[1, 2, 3]}";
17+
let input: &[_] = br#"{"x": [1, 2, 3]}"#;
1818
let mut reader = io::BufReader::new(input);
1919
let mut text = String::new();
2020
reader.read_line(&mut text).ok();
21-
assert_eq!(text, "{\"x\":[1, 2, 3]}");
21+
assert_eq!(text, r#"{"x": [1, 2, 3]}"#);
2222
}
2323

2424
#[test]
2525
fn test_basic_sourcemap() {
26-
let input: &[_] = b"{
27-
\"version\":3,
28-
\"sources\":[\"coolstuff.js\"],
29-
\"names\":[\"x\",\"alert\"],
30-
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
31-
}";
26+
let input: &[_] = br#"{
27+
"version": 3,
28+
"sources": ["coolstuff.js"],
29+
"names": ["x","alert"],
30+
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
31+
}"#;
3232
let sm = SourceMap::from_reader(input).unwrap();
3333
let mut iter = sm.tokens().filter(Token::has_name);
3434
assert_eq!(
@@ -48,13 +48,13 @@ fn test_basic_sourcemap() {
4848

4949
#[test]
5050
fn test_basic_sourcemap_with_root() {
51-
let input: &[_] = b"{
52-
\"version\":3,
53-
\"sources\":[\"coolstuff.js\"],
54-
\"sourceRoot\":\"x\",
55-
\"names\":[\"x\",\"alert\"],
56-
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
57-
}";
51+
let input: &[_] = br#"{
52+
"version": 3,
53+
"sources": ["coolstuff.js"],
54+
"sourceRoot": "x",
55+
"names": ["x","alert"],
56+
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
57+
}"#;
5858
let sm = SourceMap::from_reader(input).unwrap();
5959
let mut iter = sm.tokens().filter(Token::has_name);
6060
assert_eq!(
@@ -74,13 +74,13 @@ fn test_basic_sourcemap_with_root() {
7474

7575
#[test]
7676
fn test_basic_sourcemap_with_absolute_uri_root() {
77-
let input: &[_] = b"{
78-
\"version\":3,
79-
\"sources\":[\"coolstuff.js\", \"./evencoolerstuff.js\"],
80-
\"sourceRoot\":\"webpack:///\",
81-
\"names\":[\"x\",\"alert\"],
82-
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,ICAIA,GAAK,EAAG,CACVC,MAAM\"
83-
}";
77+
let input: &[_] = br#"{
78+
"version": 3,
79+
"sources": ["coolstuff.js", "./evencoolerstuff.js"],
80+
"sourceRoot": "webpack:///",
81+
"names": ["x","alert"],
82+
"mappings": "AAAA,GAAIA,GAAI,EACR,ICAIA,GAAK,EAAG,CACVC,MAAM"
83+
}"#;
8484
let sm = SourceMap::from_reader(input).unwrap();
8585
let mut iter = sm.tokens().filter(Token::has_name);
8686
assert_eq!(
@@ -129,12 +129,12 @@ fn test_sourcemap_data_url() {
129129

130130
#[test]
131131
fn test_sourcemap_nofiles() {
132-
let input: &[_] = b"{
133-
\"version\":3,
134-
\"sources\":[null],
135-
\"names\":[\"x\",\"alert\"],
136-
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
137-
}";
132+
let input: &[_] = br#"{
133+
"version": 3,
134+
"sources": [null],
135+
"names": ["x","alert"],
136+
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
137+
}"#;
138138
let sm = SourceMap::from_reader(input).unwrap();
139139
let mut iter = sm.tokens().filter(Token::has_name);
140140
assert_eq!(iter.next().unwrap().to_tuple(), ("", 0, 4, Some("x")));

tests/test_detector.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,32 @@ fn test_no_ref() {
4040

4141
#[test]
4242
fn test_detect_basic_sourcemap() {
43-
let input: &[_] = b"{
44-
\"version\":3,
45-
\"sources\":[\"coolstuff.js\"],
46-
\"names\":[\"x\",\"alert\"],
47-
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
48-
}";
43+
let input: &[_] = br#"{
44+
"version": 3,
45+
"sources": ["coolstuff.js"],
46+
"names": ["x","alert"],
47+
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
48+
}"#;
4949
assert!(is_sourcemap_slice(input));
5050
}
5151

5252
#[test]
5353
fn test_detect_bad_sourcemap() {
54-
let input: &[_] = b"{
55-
\"sources\":[\"coolstuff.js\"],
56-
\"names\":[\"x\",\"alert\"]
57-
}";
54+
let input: &[_] = br#"{
55+
"sources": ["coolstuff.js"],
56+
"names": ["x","alert"]
57+
}"#;
5858
assert!(!is_sourcemap_slice(input));
5959
}
6060

6161
#[test]
6262
fn test_detect_basic_sourcemap_with_junk_header() {
63-
let input: &[_] = b")]}garbage\n
63+
let input: &[_] = br#")]}garbage\n
6464
{
65-
\"version\":3,
66-
\"sources\":[\"coolstuff.js\"],
67-
\"names\":[\"x\",\"alert\"],
68-
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
69-
}";
65+
"version": 3,
66+
"sources": ["coolstuff.js"],
67+
"names": ["x","alert"],
68+
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
69+
}"#;
7070
assert!(is_sourcemap_slice(input));
7171
}

tests/test_encoder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use sourcemap::SourceMap;
22

33
#[test]
44
fn test_basic_sourcemap() {
5-
let input: &[_] = b"{
6-
\"version\":3,
7-
\"sources\":[\"coolstuff.js\"],
8-
\"names\":[\"x\",\"alert\"],
9-
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
10-
}";
5+
let input: &[_] = br#"{
6+
"version": 3,
7+
"sources": ["coolstuff.js"],
8+
"names": ["x","alert"],
9+
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
10+
}"#;
1111
let sm = SourceMap::from_reader(input).unwrap();
1212
let mut out: Vec<u8> = vec![];
1313
sm.to_writer(&mut out).unwrap();

tests/test_hermes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use sourcemap::SourceMapHermes;
22

33
#[test]
44
fn test_react_native_hermes() {
5-
let input = include_bytes!("./fixtures/react-native-hermes/output.map");
6-
let sm = SourceMapHermes::from_reader(&input[..]).unwrap();
5+
let input: &[_] = include_bytes!("./fixtures/react-native-hermes/output.map");
6+
let sm = SourceMapHermes::from_reader(input).unwrap();
77
let sm = sm.rewrite(&Default::default()).unwrap();
88

99
// at foo (address at unknown:1:11939)
@@ -23,8 +23,8 @@ fn test_react_native_hermes() {
2323

2424
#[test]
2525
fn test_react_native_metro() {
26-
let input = include_bytes!("./fixtures/react-native-metro/output.js.map");
27-
let sm = SourceMapHermes::from_reader(&input[..]).unwrap();
26+
let input: &[_] = include_bytes!("./fixtures/react-native-metro/output.js.map");
27+
let sm = SourceMapHermes::from_reader(input).unwrap();
2828
// The given map has a bogus `__prelude__` first source, which is being
2929
// dropped (as its not referenced) by rewriting the sourcemap, and thus
3030
// the internal hermes mappings also need to be rewritten accordingly

tests/test_namemap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use sourcemap::{SourceMap, SourceView};
22

33
#[test]
44
fn test_basic_name_mapping() {
5-
let input = br#"{"version":3,"file":"test.min.js","sources":["test.js"],"names":["makeAFailure","testingStuff","Error","onSuccess","data","onFailure","invoke","cb","failed","test","value"],"mappings":"AAAA,GAAIA,cAAe,WACjB,QAASC,KACP,GAAIA,GAAe,EACnB,MAAM,IAAIC,OAAMD,GAGlB,QAASE,GAAUC,GACjBH,IAGF,QAASI,GAAUD,GACjB,KAAM,IAAIF,OAAM,WAGlB,QAASI,GAAOF,GACd,GAAIG,GAAK,IACT,IAAIH,EAAKI,OAAQ,CACfD,EAAKF,MACA,CACLE,EAAKJ,EAEPI,EAAGH,GAGL,QAASK,KACP,GAAIL,IAAQI,OAAQ,KAAME,MAAO,GACjCJ,GAAOF,GAGT,MAAOK","sourcesContent":["var makeAFailure = (function() {\n function testingStuff() {\n var testingStuff = 42;\n throw new Error(testingStuff);\n }\n\n function onSuccess(data) {\n testingStuff();\n }\n\n function onFailure(data) {\n throw new Error('failed!');\n }\n\n function invoke(data) {\n var cb = null;\n if (data.failed) {\n cb = onFailure;\n } else {\n cb = onSuccess;\n }\n cb(data);\n }\n\n function test() {\n var data = {failed: true, value: 42};\n invoke(data);\n }\n\n return test;\n})();\n"]}"#;
5+
let input: &[_] = br#"{"version":3,"file":"test.min.js","sources":["test.js"],"names":["makeAFailure","testingStuff","Error","onSuccess","data","onFailure","invoke","cb","failed","test","value"],"mappings":"AAAA,GAAIA,cAAe,WACjB,QAASC,KACP,GAAIA,GAAe,EACnB,MAAM,IAAIC,OAAMD,GAGlB,QAASE,GAAUC,GACjBH,IAGF,QAASI,GAAUD,GACjB,KAAM,IAAIF,OAAM,WAGlB,QAASI,GAAOF,GACd,GAAIG,GAAK,IACT,IAAIH,EAAKI,OAAQ,CACfD,EAAKF,MACA,CACLE,EAAKJ,EAEPI,EAAGH,GAGL,QAASK,KACP,GAAIL,IAAQI,OAAQ,KAAME,MAAO,GACjCJ,GAAOF,GAGT,MAAOK","sourcesContent":["var makeAFailure = (function() {\n function testingStuff() {\n var testingStuff = 42;\n throw new Error(testingStuff);\n }\n\n function onSuccess(data) {\n testingStuff();\n }\n\n function onFailure(data) {\n throw new Error('failed!');\n }\n\n function invoke(data) {\n var cb = null;\n if (data.failed) {\n cb = onFailure;\n } else {\n cb = onSuccess;\n }\n cb(data);\n }\n\n function test() {\n var data = {failed: true, value: 42};\n invoke(data);\n }\n\n return test;\n})();\n"]}"#;
66
let minified_file = r#"var makeAFailure=function(){function n(){var n=42;throw new Error(n)}function r(r){n()}function e(n){throw new Error("failed!")}function i(n){var i=null;if(n.failed){i=e}else{i=r}i(n)}function u(){var n={failed:true,value:42};i(n)}return u}();"#;
77
let sv = SourceView::new(minified_file);
8-
let sm = SourceMap::from_reader(&input[..]).unwrap();
8+
let sm = SourceMap::from_reader(input).unwrap();
99

1010
let name = sm.get_original_function_name(0, 107, "e", &sv);
1111
assert_eq!(name, Some("onFailure"));

tests/test_regular.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use sourcemap::SourceMap;
22

33
#[test]
44
fn test_basic_sourcemap() {
5-
let input: &[_] = b"{
6-
\"version\":3,
7-
\"sources\":[\"coolstuff.js\"],
8-
\"names\":[\"x\",\"alert\"],
9-
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
10-
}";
5+
let input: &[_] = br#"{
6+
"version": 3,
7+
"sources": ["coolstuff.js"],
8+
"names": ["x","alert"],
9+
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
10+
}"#;
1111
let sm = SourceMap::from_reader(input).unwrap();
1212

1313
assert_eq!(

0 commit comments

Comments
 (0)