Skip to content

Commit fea4b87

Browse files
committed
Added: Cache methods and tests.
1 parent e8122da commit fea4b87

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

Sources/DiffableTextKit/Support/Cache.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,21 @@ public final class Cache<Key, Value> where Key: Hashable, Value: AnyObject {
3434
}
3535

3636
//=------------------------------------------------------------------------=
37-
// MARK: Search or Insert
37+
// MARK: Utilities
3838
//=------------------------------------------------------------------------=
3939

40+
@inlinable public func access(key: Key) -> Value? {
41+
nscache.object(forKey: Wrapper(key))
42+
}
43+
44+
@inlinable public func insert(_ value: Value, key: Key) {
45+
nscache.setObject(value, forKey: Wrapper(key))
46+
}
47+
48+
@inlinable public func remove(key: Key) {
49+
nscache.removeObject(forKey: Wrapper(key))
50+
}
51+
4052
@inlinable public func reuseable(_ key: Key, make: @autoclosure () throws -> Value) rethrows -> Value {
4153
let key = Wrapper(key)
4254
//=--------------------------------------=

Sources/DiffableTextStylesXWrapper/Constant.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,3 @@ DiffableTextStyleXiOS where
8686
Style: DiffableTextStyleXiOS { }
8787

8888
#endif
89-

Sources/DiffableTextStylesXWrapper/Wrapper.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,3 @@ extension WrapperTextStyleXiOS {
103103
}
104104

105105
#endif
106-

Tests/DiffableTextKitTests/Helpers/HelpersTests+Direction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#if DEBUG
1111

12-
import XCTest
12+
import DiffableTestKit
1313
@testable import DiffableTextKit
1414

1515
//*============================================================================*
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//=----------------------------------------------------------------------------=
2+
// This source file is part of the DiffableTextViews open source project.
3+
//
4+
// Copyright (c) 2022 Oscar Byström Ericsson
5+
// Licensed under Apache License, Version 2.0
6+
//
7+
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
8+
//=----------------------------------------------------------------------------=
9+
10+
#if DEBUG
11+
12+
import DiffableTestKit
13+
@testable import DiffableTextKit
14+
15+
//*============================================================================*
16+
// MARK: * HelpersTests x Direction
17+
//*============================================================================*
18+
19+
final class SupportTests_Cache: XCTestCase {
20+
21+
//=------------------------------------------------------------------------=
22+
// MARK: Tests
23+
//=------------------------------------------------------------------------=
24+
25+
func testObjectForKey() {
26+
let storage = Cache<Int, Value>()
27+
storage.insert(Value("ABC"), key: 123)
28+
XCTAssertEqual(Value("ABC"), storage.access(key: 123))
29+
}
30+
31+
//*========================================================================*
32+
// MARK: * Value
33+
//*========================================================================*
34+
35+
final class Value: Equatable {
36+
37+
//=------------------------------------------------------------------------=
38+
// MARK: State
39+
//=------------------------------------------------------------------------=
40+
41+
let content: String
42+
43+
//=------------------------------------------------------------------------=
44+
// MARK: Initializers
45+
//=------------------------------------------------------------------------=
46+
47+
init(_ content: String) { self.content = content }
48+
49+
//=------------------------------------------------------------------------=
50+
// MARK: Comparisons
51+
//=------------------------------------------------------------------------=
52+
53+
static func == (lhs: Value, rhs: Value) -> Bool {
54+
lhs.content == rhs.content
55+
}
56+
}
57+
}
58+
59+
#endif

0 commit comments

Comments
 (0)