File: OperationCacheTests.swift

package info (click to toggle)
golang-github-odeke-em-cache 0.0~git20151107.0.baf8e436-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 80 kB
  • ctags: 26
  • sloc: makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,814 bytes parent folder | download | duplicates (3)
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
//
//  OperationCacheTests.swift
//
//  Created by Indragie Karunaratne on 8/3/15.
//

import XCTest
@testable import OperationCache

class OperationCacheTests: XCTestCase {
    func testInit() {
        let cache = OperationCache<String, String>()
        for (_, _) in cache.snapshot {
            XCTFail("There should be no values")
        }
    }
    
    func testGetAndSet() {
        let cache = OperationCache<String, String>()
        XCTAssertTrue(cache["foo"] == nil)
        
        let expirable = Expirable(expiryDate: NSDate.distantFuture(), value: "bar")
        cache["foo"] = expirable
        XCTAssertTrue(cache["foo"]! == expirable)
        
        let expirable2 = Expirable(expiryDate: NSDate.distantFuture(), value: "baz")
        cache["foo"] = expirable2
        XCTAssertTrue(cache["foo"]! == expirable2)
    }
    
    func testRemove() {
        let cache = OperationCache<String, String>()
        cache["foo"] = Expirable(expiryDate: NSDate.distantFuture(), value: "bar")
        XCTAssertTrue(cache["foo"] != nil)
        
        cache["foo"] = nil
        XCTAssertTrue(cache["foo"] == nil)
    }
    
    func testExpiration() {
        let cache = OperationCache<String, String>()
        cache["foo"] = Expirable(expiryDate: NSDate.distantPast(), value: "bar")
        XCTAssertTrue(cache["foo"] == nil)
    }
    
    func testSnapshot() {
        let cache = OperationCache<String, String>()
        let expirable = Expirable(expiryDate: NSDate.distantFuture(), value: "bar")
        cache["foo"] = expirable
        
        var generator = cache.snapshot.generate()
        var next = generator.next()
        XCTAssertEqual(next!.0, "foo")
        XCTAssertTrue(next!.1 == expirable)
        
        next = generator.next()
        XCTAssertTrue(next == nil)
    }
}