This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 320
/
ExSwiftDictionaryTests.swift
111 lines (85 loc) · 2.54 KB
/
ExSwiftDictionaryTests.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
//
// ExSwiftDictionaryTests.swift
// ExSwift
//
// Created by pNre on 04/06/14.
// Copyright (c) 2014 pNre. All rights reserved.
//
import XCTest
class ExSwiftDictionaryTests: XCTestCase {
var dictionary = [
"A": 1,
"B": 2,
"C": 3
]
func testHas() {
XCTAssertTrue(dictionary.has("A"))
XCTAssertFalse(dictionary.has("Z"))
}
func testMapValues() {
let mapped = dictionary.mapValues(mapFunction: { return $1 + 1 })
XCTAssert(mapped == ["A": 2, "B": 3, "C": 4])
}
func testMap() {
let mapped = dictionary.map(mapFunction: { return ($0 + "A", $1 + 1) })
XCTAssert(mapped == ["AA": 2, "BA": 3, "CA": 4])
}
func testFilter() {
let filtered = dictionary.filter {
(key: String, Value: Int) in
return key != "A"
}
XCTAssert(filtered == ["B": 2, "C": 3])
}
func testIsEmpty() {
let e = Dictionary<String, String>()
XCTAssertTrue(e.isEmpty())
XCTAssertFalse(dictionary.isEmpty())
}
func testMerge() {
let a = dictionary.merge([ "D": 4 ])
XCTAssert(a == ["A": 1, "B": 2, "C": 3, "D": 4])
}
func testShift() {
let (key, value) = dictionary.shift()
XCTAssertEqual(2, dictionary.count)
XCTAssertNotNil(key)
XCTAssertNotNil(value)
}
func testGroupBy() {
let group = [
"A": 2,
"B": 4,
"C": 5
]
let g = group.groupBy(groupingFunction: {
(key: String, value: Int) -> Bool in
return (value % 2 == 0)
})
XCTAssert(Array(g.keys) == [false, true])
XCTAssert(Array(g[true]!) == [2, 4])
XCTAssert(Array(g[false]!) == [5])
}
func testAny() {
let any = dictionary.any {
(key: String, value: Int) -> Bool in
return value % 2 == 0
}
XCTAssertTrue(any)
}
func testAll() {
let all = dictionary.all {
(key: String, value: Int) -> Bool in
return value % 2 == 0
}
XCTAssertFalse(all)
}
func testReduce () {
let reduced = dictionary.reduce(Dictionary<Int, String>(), {
(var initial: Dictionary<Int, String>, couple: (String, Int)) in
initial.updateValue(couple.0, forKey: couple.1)
return initial
})
XCTAssert(reduced == [2: "B", 3: "C", 1: "A"])
}
}