forked from segmentio/analytics-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserDefaultsStorageTest.swift
103 lines (86 loc) · 3.23 KB
/
UserDefaultsStorageTest.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
//
// UserDefaultsStorageTest.swift
// Analytics
//
// Copyright © 2016 Segment. All rights reserved.
//
import Quick
import Nimble
import Analytics
class UserDefaultsStorageTest : QuickSpec {
override func spec() {
var storage : SEGUserDefaultsStorage!
beforeEach {
// let crypto = SEGAES256Crypto(password: "thetrees")
// storage = SEGUserDefaultsStorage(defaults: NSUserDefaults.standardUserDefaults(), namespacePrefix: "segment", crypto: crypto)
// storage = SEGUserDefaultsStorage(defaults: NSUserDefaults.standardUserDefaults(), namespacePrefix: nil, crypto: crypto)
storage = SEGUserDefaultsStorage(defaults: UserDefaults.standard, namespacePrefix: nil, crypto: nil)
}
it("persists and loads data") {
let dataIn = "segment".data(using: String.Encoding.utf8)!
storage.setData(dataIn, forKey: "mydata")
let dataOut = storage.data(forKey: "mydata")
expect(dataOut) == dataIn
let strOut = String(data: dataOut!, encoding: .utf8)
expect(strOut) == "segment"
}
it("persists and loads string") {
let str = "san francisco"
storage.setString(str, forKey: "city")
expect(storage.string(forKey: "city")) == str
storage.removeKey("city")
expect(storage.string(forKey: "city")).to(beNil())
}
it("persists and loads array") {
let array = [
"san francisco",
"new york",
"tallinn",
]
storage.setArray(array, forKey: "cities")
expect(storage.array(forKey: "cities") as? Array<String>) == array
storage.removeKey("cities")
expect(storage.array(forKey: "cities")).to(beNil())
}
it("persists and loads dictionary") {
let dict = [
"san francisco": "tech",
"new york": "finance",
"paris": "fashion",
]
storage.setDictionary(dict, forKey: "cityMap")
expect(storage.dictionary(forKey: "cityMap") as? Dictionary<String, String>) == dict
storage.removeKey("cityMap")
expect(storage.dictionary(forKey: "cityMap")).to(beNil())
}
it("should work with crypto") {
let crypto = SEGAES256Crypto(password: "thetrees")
let s = SEGUserDefaultsStorage(defaults: UserDefaults.standard, namespacePrefix: nil, crypto: crypto)
let dict = [
"san francisco": "tech",
"new york": "finance",
"paris": "fashion",
]
s.setDictionary(dict, forKey: "cityMap")
expect(s.dictionary(forKey: "cityMap") as? Dictionary<String, String>) == dict
s.removeKey("cityMap")
expect(s.dictionary(forKey: "cityMap")).to(beNil())
}
it("should work with namespace") {
let crypto = SEGAES256Crypto(password: "thetrees")
let s = SEGUserDefaultsStorage(defaults: UserDefaults.standard, namespacePrefix: "segment", crypto: crypto)
let dict = [
"san francisco": "tech",
"new york": "finance",
"paris": "fashion",
]
s.setDictionary(dict, forKey: "cityMap")
expect(s.dictionary(forKey: "cityMap") as? Dictionary<String, String>) == dict
s.removeKey("cityMap")
expect(s.dictionary(forKey: "cityMap")).to(beNil())
}
afterEach {
storage.resetAll()
}
}
}