-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestJSON.swift
96 lines (86 loc) · 2.27 KB
/
TestJSON.swift
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
//
// TestJSON.swift
// SourceMapperTests
//
// Licensed under MIT (https://github.com/johnfairh/SourceMapper/blob/main/LICENSE
//
@testable import SourceMapper
import Testing
extension Tag {
@Tag static var json: Self
}
/// JSON code/decode logic and error handling
@Suite(.tags(.json))
final class TestJSON {
@Test
func testMissingFields() throws {
#expect(throws: Swift.DecodingError.self) {
let map = try SourceMap("{}")
print("Bad map decoded: \(map)")
}
}
@Test
func testBadVersion() throws {
let badVersionJSON = """
{
"version": 4,
"sources": [],
"names": [],
"mappings": ""
}
"""
#expect(throws: SourceMapError.invalidFormat(4)) {
let map = try SourceMap(badVersionJSON)
print("Bad map decoded: \(map)")
}
}
@Test
func testInconsistentSources() throws {
let badSourcesJSON = """
{
"version": 3,
"sources": ["a", "b", "c"],
"sourcesContent": ["contents of a", null],
"names": [],
"mappings": ""
}
"""
#expect(throws: SourceMapError.inconsistentSources(sourcesCount: 3, sourcesContentCount: 2)) {
let map = try SourceMap(badSourcesJSON)
print("Bad map decoded: \(map)")
}
}
@Test
func testSourceContent() throws {
let sourcedJSON = """
{
"version": 3,
"sources": ["a", "b"],
"sourcesContent": ["contents of a", null],
"names": [],
"mappings": ""
}
"""
let map = try SourceMap(sourcedJSON)
#expect(map.sources.count == 2)
#expect("contents of a" == map.sources[0].content)
#expect(map.sources[1].content == nil)
let encoded = try map.encode()
let map2 = try SourceMap(encoded)
#expect(map == map2)
}
@Test
func testBadMapping() throws {
let json = """
{
"version": 3,
"sources": ["a"],
"names": [],
"mappings": "AAA"
}
"""
#expect(throws: SourceMapError.invalidVLQStringLength([0,0,0])) {
_ = try SourceMap(json).segments
}
}
}