-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathResolverTests.swift
140 lines (122 loc) · 6.27 KB
/
ResolverTests.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// ResolverTests.swift
// Yams
//
// Created by Norio Nomura on 12/15/16.
// Copyright (c) 2016 Yams. All rights reserved.
//
import XCTest
import Yams
class ResolverTests: XCTestCase {
func testBasic() {
let resolver = Resolver.basic
XCTAssertEqual(resolver.resolveTag(of: "null"), .str)
XCTAssertEqual(resolver.resolveTag(of: "Null"), .str)
XCTAssertEqual(resolver.resolveTag(of: "NULL"), .str)
XCTAssertEqual(resolver.resolveTag(of: "~"), .str)
XCTAssertEqual(resolver.resolveTag(of: ""), .str)
XCTAssertEqual(resolver.resolveTag(of: "true"), .str)
XCTAssertEqual(resolver.resolveTag(of: "True"), .str)
XCTAssertEqual(resolver.resolveTag(of: "TRUE"), .str)
XCTAssertEqual(resolver.resolveTag(of: "false"), .str)
XCTAssertEqual(resolver.resolveTag(of: "False"), .str)
XCTAssertEqual(resolver.resolveTag(of: "FALSE"), .str)
XCTAssertEqual(resolver.resolveTag(of: "0"), .str)
XCTAssertEqual(resolver.resolveTag(of: "+1"), .str)
XCTAssertEqual(resolver.resolveTag(of: "0o7"), .str)
XCTAssertEqual(resolver.resolveTag(of: "0x3A"), .str)
XCTAssertEqual(resolver.resolveTag(of: "-19"), .str)
XCTAssertEqual(resolver.resolveTag(of: "0."), .str)
XCTAssertEqual(resolver.resolveTag(of: "-0.0"), .str)
XCTAssertEqual(resolver.resolveTag(of: ".5"), .str)
XCTAssertEqual(resolver.resolveTag(of: "+12e03"), .str)
XCTAssertEqual(resolver.resolveTag(of: "-2E+05"), .str)
XCTAssertEqual(resolver.resolveTag(of: ".inf"), .str)
XCTAssertEqual(resolver.resolveTag(of: ".Inf"), .str)
XCTAssertEqual(resolver.resolveTag(of: ".INF"), .str)
XCTAssertEqual(resolver.resolveTag(of: "+.inf"), .str)
XCTAssertEqual(resolver.resolveTag(of: "+.Inf"), .str)
XCTAssertEqual(resolver.resolveTag(of: "+.INF"), .str)
XCTAssertEqual(resolver.resolveTag(of: "-.inf"), .str)
XCTAssertEqual(resolver.resolveTag(of: "-.Inf"), .str)
XCTAssertEqual(resolver.resolveTag(of: "-.INF"), .str)
XCTAssertEqual(resolver.resolveTag(of: ".nan"), .str)
XCTAssertEqual(resolver.resolveTag(of: ".NaN"), .str)
XCTAssertEqual(resolver.resolveTag(of: ".NAN"), .str)
}
func testDefault() {
let resolver = Resolver.default
XCTAssertEqual(resolver.resolveTag(of: "null"), .null)
XCTAssertEqual(resolver.resolveTag(of: "Null"), .null)
XCTAssertEqual(resolver.resolveTag(of: "NULL"), .null)
XCTAssertEqual(resolver.resolveTag(of: "~"), .null)
XCTAssertEqual(resolver.resolveTag(of: ""), .null)
XCTAssertEqual(resolver.resolveTag(of: "true"), .bool)
XCTAssertEqual(resolver.resolveTag(of: "True"), .bool)
XCTAssertEqual(resolver.resolveTag(of: "TRUE"), .bool)
XCTAssertEqual(resolver.resolveTag(of: "false"), .bool)
XCTAssertEqual(resolver.resolveTag(of: "False"), .bool)
XCTAssertEqual(resolver.resolveTag(of: "FALSE"), .bool)
XCTAssertEqual(resolver.resolveTag(of: "0"), .int)
XCTAssertEqual(resolver.resolveTag(of: "+1"), .int)
XCTAssertEqual(resolver.resolveTag(of: "0o7"), .int)
XCTAssertEqual(resolver.resolveTag(of: "0x3A"), .int)
XCTAssertEqual(resolver.resolveTag(of: "-19"), .int)
XCTAssertEqual(resolver.resolveTag(of: "0."), .float)
XCTAssertEqual(resolver.resolveTag(of: "-0.0"), .float)
XCTAssertEqual(resolver.resolveTag(of: ".5"), .float)
XCTAssertEqual(resolver.resolveTag(of: "+12e03"), .float)
XCTAssertEqual(resolver.resolveTag(of: "-2E+05"), .float)
XCTAssertEqual(resolver.resolveTag(of: ".inf"), .float)
XCTAssertEqual(resolver.resolveTag(of: ".Inf"), .float)
XCTAssertEqual(resolver.resolveTag(of: ".INF"), .float)
XCTAssertEqual(resolver.resolveTag(of: "+.inf"), .float)
XCTAssertEqual(resolver.resolveTag(of: "+.Inf"), .float)
XCTAssertEqual(resolver.resolveTag(of: "+.INF"), .float)
XCTAssertEqual(resolver.resolveTag(of: "-.inf"), .float)
XCTAssertEqual(resolver.resolveTag(of: "-.Inf"), .float)
XCTAssertEqual(resolver.resolveTag(of: "-.INF"), .float)
XCTAssertEqual(resolver.resolveTag(of: ".nan"), .float)
XCTAssertEqual(resolver.resolveTag(of: ".NaN"), .float)
XCTAssertEqual(resolver.resolveTag(of: ".NAN"), .float)
}
func testCustomize() throws {
// appending rule
XCTAssertEqual(Resolver.basic.rules.count, 0)
let basicAppendingBool = Resolver.basic.appending(.bool)
XCTAssertEqual(basicAppendingBool.rules.count, 1)
XCTAssertEqual(basicAppendingBool.resolveTag(of: "true"), .bool)
// replacing rule with pattern string
XCTAssertEqual(Resolver.default.resolveTag(of: "はい"), .str)
let resolverRecognizingJapaneseYesNo = try Resolver.default.replacing(.bool, with: "(はい|いいえ)")
XCTAssertEqual(resolverRecognizingJapaneseYesNo.resolveTag(of: "はい"), .bool)
// replacing rule with Rule
let ruleForOuiNon = try Resolver.Rule(.bool, "(Oui|Non)")
XCTAssertEqual(resolverRecognizingJapaneseYesNo.replacing(ruleForOuiNon).resolveTag(of: "Oui"), .bool)
// removing
XCTAssertEqual(Resolver.default.rules.count, 7)
let defaultRemovingBool = Resolver.default.removing(.bool)
XCTAssertEqual(defaultRemovingBool.rules.count, 6)
XCTAssertEqual(defaultRemovingBool.resolveTag(of: "true"), .str)
// custom tag and rule
let xml: Tag.Name = "XML"
let defaultAppendingXML = try Resolver.default.appending(xml, "<[^>]*>")
XCTAssertEqual(defaultAppendingXML.resolveTag(of: "<XML>"), xml)
// Use customized Resolver on Yams.load()
let bool = try Yams.load(yaml: "true")
XCTAssertTrue(bool is Bool)
XCTAssertFalse(bool is String)
let string = try Yams.load(yaml: "true", Resolver.default.removing(.bool))
XCTAssertFalse(string is Bool)
XCTAssertTrue(string is String)
}
}
extension ResolverTests {
static var allTests: [(String, (ResolverTests) -> () throws -> Void)] {
return [
("testBasic", testBasic),
("testDefault", testDefault),
("testCustomize", testCustomize)
]
}
}