Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import Shared
7
import XCTest
8
9
/**
10
* Test for our own utils.
11
*/
12
class UtilsTests: XCTestCase {
13
func testMapUtils() {
14
let m: [String: Int] = ["foo": 123, "bar": 456]
15
let f: (Int) -> Int? = { v in
16
return (v > 200) ? 999 : nil
17
}
18
19
let o = mapValues(m, f: f)
20
XCTAssertEqual(2, o.count)
21
XCTAssertTrue(o["foo"]! == nil)
22
XCTAssertTrue(o["bar"]! == 999)
23
24
let filtered = optFilter(o)
25
XCTAssertEqual(1, filtered.count)
26
XCTAssertTrue(filtered["bar"] == 999)
27
}
28
29
func testOptFilter() {
30
let a: [Int?] = [nil, 1, nil, 2, 3, 4]
31
let b = optFilter(a)
32
XCTAssertEqual(4, b.count)
33
XCTAssertEqual([1, 2, 3, 4], b)
34
}
35
36
func testOptArrayEqual() {
37
let x: [String] = ["a", "b", "c"]
38
let y: [String]? = ["a", "b", "c"]
39
let z: [String]? = nil
40
41
XCTAssertTrue(optArrayEqual(x, rhs: y))
42
XCTAssertTrue(optArrayEqual(x, rhs: x))
43
XCTAssertTrue(optArrayEqual(y, rhs: y))
44
XCTAssertTrue(optArrayEqual(z, rhs: z))
45
XCTAssertFalse(optArrayEqual(x, rhs: z))
46
XCTAssertFalse(optArrayEqual(z, rhs: y))
47
}
48
49
func testChunk() {
50
let examples: [([Int], Int, [[Int]])] = [
51
([], 2, []),
52
([1, 2], 0, [[1], [2]]),
53
([1, 2], 1, [[1], [2]]),
54
([1, 2, 3], 2, [[1, 2], [3]]),
55
([1, 2], 3, [[1, 2]]),
56
([1, 2, 3], 1, [[1], [2], [3]]),
57
]
58
for (arr, by, expected) in examples {
59
// Turn the ArraySlices back into Arrays for comparison.
60
let actual = chunk(arr as [Int], by: by).map { Array($0) }
61
XCTAssertEqual(expected as NSArray, actual as NSArray) //wtf. why is XCTAssert being so weeird
62
}
63
}
64
65
func testChunkCollection() {
66
let examples: [([Int], Int, [[Int]])] = [
67
([], 2, []),
68
([1, 2], 0, [[1], [2]]),
69
([1, 2], 1, [[1], [2]]),
70
([1, 2, 3], 2, [[1, 2], [3]]),
71
([1, 2], 3, [[1, 2]]),
72
([1, 2, 3], 1, [[1], [2], [3]]),
73
]
74
for (arr, by, expected) in examples {
75
let actual = chunkCollection(arr, by: by) { xs in [xs] }
76
XCTAssertEqual(expected as NSArray, actual as NSArray)
77
}
78
}
79
80
func testParseTimestamps() {
81
let millis = "1492316843992" // Firefox for iOS produced millisecond timestamps. Oops.
82
let decimal = "1492316843.99"
83
let truncated = "1492316843"
84
let huge = "1844674407370955161512"
85
86
XCTAssertNil(decimalSecondsStringToTimestamp(""))
87
XCTAssertNil(decimalSecondsStringToTimestamp(huge))
88
XCTAssertNil(decimalSecondsStringToTimestamp("foo"))
89
90
XCTAssertNil(someKindOfTimestampStringToTimestamp(""))
91
XCTAssertNil(someKindOfTimestampStringToTimestamp(huge))
92
XCTAssertNil(someKindOfTimestampStringToTimestamp("foo"))
93
94
let ts1: Timestamp = 1492316843990
95
XCTAssertEqual(decimalSecondsStringToTimestamp(decimal) ?? 0, ts1)
96
XCTAssertEqual(someKindOfTimestampStringToTimestamp(decimal) ?? 0, ts1)
97
98
let ts2: Timestamp = 1492316843000
99
XCTAssertEqual(decimalSecondsStringToTimestamp(truncated) ?? 0, ts2)
100
XCTAssertEqual(someKindOfTimestampStringToTimestamp(truncated) ?? 0, ts2)
101
102
let ts3: Timestamp = 1492316843992000
103
XCTAssertEqual(decimalSecondsStringToTimestamp(millis) ?? 0, ts3) // Oops.
104
105
let ts4: Timestamp = 1492316843992
106
XCTAssertEqual(someKindOfTimestampStringToTimestamp(millis) ?? 0, ts4)
107
}
108
}