Skip to content

Commit fcbb09e

Browse files
committed
Test reading token value in different order
1 parent c78ec28 commit fcbb09e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Sources/Hub/Hub.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public struct Config {
100100
}
101101

102102
/// Tuple of token identifier and string value
103-
public var tokenValue: (UInt, String)? {
104-
switch value {
103+
public var tokenValue: (UInt, String)? {
104+
guard let pair = value as? [Any], pair.count == 2 else { return nil }
105+
switch (pair[0], pair[1]) {
105106
case let (i, t) as (UInt, String): return (i, t)
106107
case let (t, i) as (String, UInt): return (i, t)
107-
case let a as [Any] where a.count == 2: return (a[1], a[0]) as? (UInt, String)
108108
default: return nil
109109
}
110110
}

Tests/HubTests/HubTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,14 @@ class HubTests: XCTestCase {
118118
let vocab_dict = config.dictionary["vocab"] as! [String: Int]
119119
XCTAssertNotEqual(vocab_dict.count, 2)
120120
}
121+
122+
func testConfigTokenValueDifferentOrder() {
123+
let data: Data! = "{\"sep\": [\"</s>\", 2], \"cls\": [0, \"<s>\"]}".data(using: .utf8)
124+
let dict = try! JSONSerialization.jsonObject(with: data, options: []) as! [NSString: Any]
125+
let config = Config(dict)
126+
XCTAssertEqual(config.sep!.tokenValue!.0, 2)
127+
XCTAssertEqual(config.sep!.tokenValue!.1, "</s>")
128+
XCTAssertEqual(config.cls!.tokenValue!.0, 0)
129+
XCTAssertEqual(config.cls!.tokenValue!.1, "<s>")
130+
}
121131
}

0 commit comments

Comments
 (0)