|
7 | 7 | //
|
8 | 8 |
|
9 | 9 | import XCTest
|
10 |
| -@testable import WTUserDefaults |
11 | 10 |
|
12 | 11 | class WTUserDefaultsTests: XCTestCase {
|
| 12 | + var userDefaults:WTUserDefaultsHelper? |
13 | 13 |
|
14 | 14 | override func setUp() {
|
15 | 15 | super.setUp()
|
16 | 16 | // Put setup code here. This method is called before the invocation of each test method in the class.
|
| 17 | + self.userDefaults = WTUserDefaultsHelper() |
17 | 18 | }
|
18 | 19 |
|
19 | 20 | override func tearDown() {
|
20 | 21 | // Put teardown code here. This method is called after the invocation of each test method in the class.
|
21 | 22 | super.tearDown()
|
22 | 23 | }
|
23 | 24 |
|
24 |
| - func testExample() { |
25 |
| - // This is an example of a functional test case. |
26 |
| - // Use XCTAssert and related functions to verify your tests produce the correct results. |
| 25 | + func testSetAndGetBool() { |
| 26 | + guard let defaults = userDefaults else { return } |
| 27 | + let stringKey = "ABC" |
| 28 | + defaults.set(true, forKey: stringKey) |
| 29 | + var ret = defaults.getBool(forKey: stringKey) |
| 30 | + XCTAssert(ret) |
| 31 | + defaults.set(false, forKey: stringKey) |
| 32 | + ret = defaults.getBool(forKey: stringKey) |
| 33 | + XCTAssert(false == ret) |
27 | 34 | }
|
28 | 35 |
|
29 |
| - func testPerformanceExample() { |
30 |
| - // This is an example of a performance test case. |
31 |
| - self.measure { |
32 |
| - // Put the code you want to measure the time of here. |
33 |
| - } |
| 36 | + func testSetAndGetInteger() { |
| 37 | + guard let defaults = userDefaults else { return } |
| 38 | + let stringKey = "123" |
| 39 | + defaults.set(1, forKey: stringKey) |
| 40 | + var ret = defaults.getInteger(forKey: stringKey) |
| 41 | + XCTAssert(1 == ret) |
| 42 | + defaults.set(2, forKey: stringKey) |
| 43 | + ret = defaults.getInteger(forKey: stringKey) |
| 44 | + XCTAssert(2 == ret) |
| 45 | + defaults.set(3, forKey: stringKey) |
| 46 | + ret = defaults.getInteger(forKey: stringKey) |
| 47 | + XCTAssert(3 == ret) |
| 48 | + } |
| 49 | + |
| 50 | + func testSetAndGetObject() { |
| 51 | + guard let defaults = userDefaults else { return } |
| 52 | + let stringKey = "KeyString" |
| 53 | + defaults.set("ABC", forKey: stringKey) |
| 54 | + var ret = defaults.getObject(forKey: stringKey) as? String |
| 55 | + XCTAssert("ABC" == ret) |
| 56 | + defaults.set("CDE", forKey: stringKey) |
| 57 | + ret = defaults.getObject(forKey: stringKey) as? String |
| 58 | + XCTAssert("CDE" == ret) |
| 59 | + defaults.set("QWER", forKey: stringKey) |
| 60 | + ret = defaults.getObject(forKey: stringKey) as? String |
| 61 | + XCTAssert("QWER" == ret) |
| 62 | + } |
| 63 | + |
| 64 | + func testSetAndGetArray() { |
| 65 | + guard let defaults = userDefaults else { return } |
| 66 | + let stringKey = "ArrayTEST" |
| 67 | + var array = ["ABC"] |
| 68 | + defaults.set(array, forKey: stringKey) |
| 69 | + var ret = defaults.getArray(forKey: stringKey) as? [String] |
| 70 | + XCTAssert(1 == ret?.count) |
| 71 | + XCTAssert("ABC" == ret?[0]) |
| 72 | + |
| 73 | + array = ["ABC", "CDE"] |
| 74 | + defaults.set(array, forKey: stringKey) |
| 75 | + ret = defaults.getArray(forKey: stringKey) as? [String] |
| 76 | + XCTAssert(2 == ret?.count) |
| 77 | + XCTAssert("CDE" == ret?[1]) |
| 78 | + |
| 79 | + array = ["ABC", "CDE", "123"] |
| 80 | + defaults.set(array, forKey: stringKey) |
| 81 | + ret = defaults.getArray(forKey: stringKey) as? [String] |
| 82 | + XCTAssert(3 == ret?.count) |
| 83 | + XCTAssert("123" == ret?[2]) |
| 84 | + |
| 85 | + array = ["ABC", "CDE", "123", "!@#"] |
| 86 | + defaults.set(array, forKey: stringKey) |
| 87 | + ret = defaults.getArray(forKey: stringKey) as? [String] |
| 88 | + XCTAssert(4 == ret?.count) |
| 89 | + XCTAssert("!@#" == ret?[3]) |
| 90 | + |
34 | 91 | }
|
35 | 92 |
|
36 | 93 | }
|
0 commit comments