-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCBORValueTests.swift
193 lines (159 loc) · 5.7 KB
/
CBORValueTests.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// CBORValueTests.swift
// PotentCodables
//
// Copyright © 2021 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//
import Foundation
import PotentCBOR
import XCTest
class CBORValueTests: XCTestCase {
func testNilLiteralInitializer() {
let cbor: CBOR = nil
XCTAssertTrue(cbor.isNull)
}
func testBoolLiteralInitializer() {
let cbor: CBOR = true
XCTAssertEqual(cbor.booleanValue, true)
}
func testStringLiteralInitializer() {
let cbor: CBOR = "Hello World!"
XCTAssertEqual(cbor.utf8StringValue, "Hello World!")
}
func testFloatLiteralInitializer() {
let cbor: CBOR = 1.234
XCTAssertEqual(cbor.doubleValue, 1.234)
}
func testArrayLiteralInitializer() {
let cbor: CBOR = [1, 2, 3]
XCTAssertEqual(cbor.arrayValue, [1, 2, 3])
}
func testDictionaryLiteralInitializer() {
let cbor: CBOR = ["b": 1, "a": 2, "c": 3]
XCTAssertEqual(cbor.mapValue, ["b": 1, "a": 2, "c": 3])
}
func testDataInitializer() {
let cbor = CBOR(Data([1, 2, 3, 4]))
XCTAssertEqual(cbor.bytesStringValue, Data([1, 2, 3, 4]))
}
func testUIntInitializer() {
let cbor = CBOR(UInt(5))
XCTAssertEqual(cbor.integerValue() as UInt?, 5)
}
func testHalfInitializer() {
let cbor = CBOR(CBOR.Half(1.5))
XCTAssertEqual(cbor.floatingPointValue() as CBOR.Half?, 1.5)
}
func testFloatInitializer() {
let cbor = CBOR(Float(1.5))
XCTAssertEqual(cbor.floatingPointValue() as Float?, 1.5)
}
func testCBORNumberFloatLiteralInitializer() {
let number: CBOR = 1.234
XCTAssertEqual(number.doubleValue, 1.234)
}
func testDynamicMemberSubscript() {
let map: CBOR = [
"aaa": 1,
]
XCTAssertEqual(map.aaa, .unsignedInt(1))
XCTAssertEqual(map.bbb, nil)
XCTAssertEqual(CBOR.null.aaa, nil)
}
func testIntSubscript() {
var array: CBOR = [
"a", "b", "c"
]
XCTAssertEqual(array[1], .utf8String("b"))
XCTAssertEqual(array[3], nil)
XCTAssertEqual(CBOR.null[1], nil)
array[1] = .utf8String("d")
XCTAssertEqual(array[1], .utf8String("d"))
}
func testStringSubscript() {
var map: CBOR = [
"aaa": 1,
]
XCTAssertEqual(map["aaa"], .unsignedInt(1))
XCTAssertEqual(map["bbb"], nil)
XCTAssertEqual(CBOR.null["aaa"], nil)
map["aaa"] = .unsignedInt(5)
XCTAssertEqual(map["aaa"], .unsignedInt(5))
}
func testNullAccessor() {
XCTAssertTrue(CBOR.null.isNull)
XCTAssertFalse(CBOR.unsignedInt(5).isNull)
}
func testStringAccessor() {
XCTAssertEqual(CBOR.utf8String("Hello World!").utf8StringValue, "Hello World!")
XCTAssertNil(CBOR.unsignedInt(5).utf8StringValue)
}
func testByteStringAccessor() {
XCTAssertEqual(CBOR.byteString(Data([1, 2, 3, 4])).bytesStringValue, Data([1, 2, 3, 4]))
XCTAssertNil(CBOR.unsignedInt(5).bytesStringValue)
}
func testBoolAccessor() {
XCTAssertEqual(CBOR.boolean(true).booleanValue, true)
XCTAssertNil(CBOR.unsignedInt(5).booleanValue)
}
func testIntegerAccessor() {
XCTAssertEqual(CBOR.unsignedInt(5).integerValue() as Int?, 5)
XCTAssertEqual(CBOR.negativeInt(4).integerValue() as Int?, -5)
XCTAssertNil(CBOR.double(5.3).integerValue() as Int?)
XCTAssertNil(CBOR.boolean(false).integerValue() as Int?)
}
func testSimpleAccessor() {
XCTAssertEqual(CBOR.simple(5).simpleValue, 5)
XCTAssertNil(CBOR.boolean(false).simpleValue)
}
func testHalfAccessor() {
XCTAssertEqual(CBOR.half(5.3).halfValue, 5.3)
XCTAssertNil(CBOR.boolean(false).halfValue)
}
func testFloatAccessor() {
XCTAssertEqual(CBOR.float(5.3).floatValue, 5.3)
XCTAssertNil(CBOR.boolean(false).floatValue)
}
func testDoubleAccessor() {
XCTAssertEqual(CBOR.double(5.3).doubleValue, 5.3)
XCTAssertNil(CBOR.boolean(false).doubleValue)
}
func testFloatingAccessor() {
XCTAssertEqual(CBOR.unsignedInt(5).floatingPointValue() as Float?, 5.0)
XCTAssertEqual(CBOR.negativeInt(4).floatingPointValue() as Float?, -5.0)
XCTAssertEqual(CBOR.half(5.3).floatingPointValue() as CBOR.Half?, 5.3)
XCTAssertEqual(CBOR.float(5.3).floatingPointValue() as Float?, 5.3)
XCTAssertEqual(CBOR.double(5.3).floatingPointValue() as Double?, 5.3)
XCTAssertNil(CBOR.boolean(false).floatingPointValue() as Double?)
}
func testArrayAccessor() {
XCTAssertEqual(CBOR.array([1, 2, 3]).arrayValue, [1, 2, 3] as CBOR.Array)
XCTAssertNil(CBOR.boolean(false).arrayValue)
}
func testObjectAccessor() {
XCTAssertEqual(CBOR.map(["a": 1, "b": 2, "c": 3]).mapValue, ["a": 1, "b": 2, "c": 3] as CBOR.Map)
XCTAssertNil(CBOR.boolean(false).mapValue)
}
func testUnwrap() {
XCTAssertNil(CBOR.null.unwrapped)
XCTAssertNil(CBOR.undefined.unwrapped)
XCTAssertEqual(CBOR.boolean(true).unwrapped as? Bool, true)
XCTAssertEqual(CBOR.byteString(Data([1, 2, 3, 4])).unwrapped as? Data, Data([1, 2, 3, 4]))
XCTAssertEqual(CBOR.simple(5).unwrapped as? UInt8, 5)
XCTAssertEqual(CBOR.utf8String("Hello World!").unwrapped as? String, "Hello World!")
XCTAssertEqual(CBOR.unsignedInt(5).unwrapped as? UInt64, 5)
XCTAssertEqual(CBOR.negativeInt(4).unwrapped as? Int64, -5)
XCTAssertEqual(CBOR.half(1.5).unwrapped as? CBOR.Half, 1.5)
XCTAssertEqual(CBOR.float(1.5).unwrapped as? Float, 1.5)
XCTAssertEqual(CBOR.double(1.5).unwrapped as? Double, 1.5)
XCTAssertEqual(CBOR.array([1, 2, 3]).unwrapped as? [UInt64], [1, 2, 3])
XCTAssertEqual(CBOR.map(["a": 1, "b": 2, "c": 3]).unwrapped as? [String: UInt64], ["a": 1, "b": 2, "c": 3])
XCTAssertEqual(CBOR.tagged(CBOR.Tag(rawValue: 5), CBOR.unsignedInt(5)).unwrapped as? UInt64, 5)
}
func testUntagged() {
XCTAssertEqual(CBOR.tagged(CBOR.Tag(rawValue: 5), CBOR.unsignedInt(5)).untagged, CBOR.unsignedInt(5))
}
}