-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollectionsSpec.swift
77 lines (65 loc) · 2.55 KB
/
CollectionsSpec.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
//
// CollectionsSpec.swift
// WeCodePropertiesSwiftTests
//
// Created by Sergio Arroyo Cuevas on 21/02/2019.
// Copyright © 2019 delr3ves. All rights reserved.
//
import Foundation
import XCTest
import SwiftCheck
@testable import WeCodePropertiesSwift
extension Array where Element: Equatable {
func removeAllOccurrences(_ item: Element) -> Array {
return self.filter {value in return value != item}
}
func countAllOccurrences(_ item: Element) -> Int {
return self.filter {value in return value == item}.count
}
func containAll(_ items: [Element]) -> Bool {
return items.filter {value in return !self.contains(value) }.count == 0
}
}
class CollectionsSpec: XCTestCase {
func testAddition() {
property("contain one more element after insertion")
<- forAll { (list: [String], item: String) in
let listWithNewItem = list + [item]
return listWithNewItem.count == list.count + 1
}
property("contain the inserted element")
<- forAll { (list: [String], item: String) in
let listWithNewItem = list + [item]
return listWithNewItem.contains(item)
}
}
func testDeletion() {
property("have an element less after deletion")
<- forAll { (list: [Int], item: Int) in
let numberOfOccurrencesOfItem = list.countAllOccurrences(item)
let listWithNewItem = list + [item]
let listWithNoItemsAtAll = listWithNewItem.removeAllOccurrences(item)
return listWithNoItemsAtAll.count == list.count - numberOfOccurrencesOfItem
}
property("not contain the removed element")
<- forAll { (list: [String], item: String) in
let listWithNewItem = list + [item]
let listWithNoItemsAtAll = listWithNewItem.removeAllOccurrences(item)
return !listWithNoItemsAtAll.contains(item)
}
}
func testConcatenation() {
property("contain the items of the two collections after a concatenation")
<- forAll { (list1: [String], list2: [String]) in
let bigList = list1 + list2
return bigList.containAll(list1) && bigList.containAll(list2)
}
}
func testSetAndDistinct() {
property("set does not contains duplicated elements")
<- forAll { (list: [String]) in
let asSet = Array(Set(list))
return !list.contains {item in return asSet.countAllOccurrences(item) != 1}
}
}
}