forked from getsentry/rust-sourcemap
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_encoder.rs
More file actions
96 lines (84 loc) · 3.04 KB
/
test_encoder.rs
File metadata and controls
96 lines (84 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use swc_sourcemap::SourceMap;
#[test]
fn test_basic_sourcemap() {
let input: &[_] = br#"{
"version": 3,
"sources": ["coolstuff.js"],
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
let sm = SourceMap::from_reader(input).unwrap();
let mut out: Vec<u8> = vec![];
sm.to_writer(&mut out).unwrap();
let sm2 = SourceMap::from_reader(&out[..]).unwrap();
for (tok1, tok2) in sm.tokens().zip(sm2.tokens()) {
assert_eq!(tok1, tok2);
}
}
#[test]
fn test_sourcemap_data_url() {
let input: &[_] = br#"{"version":3,"file":"build/foo.min.js","sources":["src/foo.js"],"names":[],"mappings":"AAAA","sourceRoot":"/"}"#;
let sm = SourceMap::from_reader(input).unwrap();
assert_eq!(sm.to_data_url().unwrap(), "data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJzb3VyY2VSb290IjoiLyIsIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=");
}
#[test]
fn test_basic_range() {
let input: &[_] = br#"{
"version": 3,
"sources": [null],
"names": ["console","log","ab"],
"mappings": "AACAA,QAAQC,GAAG,CAAC,OAAM,OAAM,QACxBD,QAAQC,GAAG,CAAC,QAEZD,QAAQC,GAAG,CAJD;IAACC,IAAI;AAAI,IAKnBF,QAAQC,GAAG,CAAC,YACZD,QAAQC,GAAG,CAAC",
"rangeMappings": "AAB;;g"
}"#;
let sm = SourceMap::from_reader(input).unwrap();
let mut out: Vec<u8> = vec![];
sm.to_writer(&mut out).unwrap();
let sm2 = SourceMap::from_reader(&out[..]).unwrap();
for (tok1, tok2) in sm.tokens().zip(sm2.tokens()) {
assert_eq!(tok1, tok2);
}
}
#[test]
fn test_empty_range() {
let input = r#"{
"version": 3,
"sources": [null],
"names": ["console","log","ab"],
"mappings": "AACAA,QAAQC,GAAG,CAAC,OAAM,OAAM,QACxBD,QAAQC,GAAG,CAAC,QAEZD,QAAQC,GAAG,CAJD;IAACC,IAAI;AAAI,IAKnBF,QAAQC,GAAG,CAAC,YACZD,QAAQC,GAAG,CAAC",
"rangeMappings": ""
}"#;
let sm = SourceMap::from_reader(input.as_bytes()).unwrap();
let mut out: Vec<u8> = vec![];
sm.to_writer(&mut out).unwrap();
let out = String::from_utf8(out).unwrap();
assert!(!out.contains("rangeMappings"));
}
#[test]
fn test_sourcemap_serializes_camel_case_debug_id() {
const DEBUG_ID: &str = "0123456789abcdef0123456789abcdef";
let input = format!(
r#"{{
"version": 3,
"sources": [],
"names": [],
"mappings": "",
"debug_id": "{}"
}}"#,
DEBUG_ID
);
let sm = SourceMap::from_reader(input.as_bytes()).unwrap();
let expected = sm.get_debug_id().expect("debug id parsed").to_string();
let mut out: Vec<u8> = vec![];
sm.to_writer(&mut out).unwrap();
let serialized = String::from_utf8(out).unwrap();
assert!(
serialized.contains(&format!(r#""debugId":"{}""#, expected)),
"expected camelCase debugId in {}",
serialized
);
assert!(
!serialized.contains("debug_id"),
"unexpected snake_case key in {}",
serialized
);
}