|
| 1 | +@testable import JMESPath |
| 2 | +import XCTest |
| 3 | + |
| 4 | +final class MirrorTests: XCTestCase { |
| 5 | + func testInterpreter<Value: Equatable>(_ expression: String, data: Any, result: Value) { |
| 6 | + do { |
| 7 | + let expression = try Expression.compile(expression) |
| 8 | + let value = try XCTUnwrap(expression.search(data)) |
| 9 | + XCTAssertEqual(value as? Value, result) |
| 10 | + } catch { |
| 11 | + XCTFail("\(error)") |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + func testString() { |
| 16 | + struct TestString { |
| 17 | + let s: String |
| 18 | + } |
| 19 | + let test = TestString(s: "hello") |
| 20 | + self.testInterpreter("s", data: test, result: "hello") |
| 21 | + } |
| 22 | + |
| 23 | + func testOptional() { |
| 24 | + struct TestString { |
| 25 | + let s: String? |
| 26 | + } |
| 27 | + let test = TestString(s: "hello") |
| 28 | + self.testInterpreter("s", data: test, result: "hello") |
| 29 | + } |
| 30 | + |
| 31 | + func testNumbers() { |
| 32 | + struct TestNumbers { |
| 33 | + let i: Int |
| 34 | + let d: Double |
| 35 | + let f: Float |
| 36 | + let b: Bool |
| 37 | + } |
| 38 | + let test = TestNumbers(i:34, d: 1.4, f: 2.5, b: true) |
| 39 | + self.testInterpreter("i", data: test, result: 34) |
| 40 | + self.testInterpreter("d", data: test, result: 1.4) |
| 41 | + self.testInterpreter("f", data: test, result: 2.5) |
| 42 | + self.testInterpreter("b", data: test, result: true) |
| 43 | + } |
| 44 | + |
| 45 | + func testArray() { |
| 46 | + struct TestArray { |
| 47 | + let a: [Int] |
| 48 | + } |
| 49 | + let test = TestArray(a: [1,2,3,4,5]) |
| 50 | + self.testInterpreter("a[2]", data: test, result: 3) |
| 51 | + self.testInterpreter("a[-2]", data: test, result: 4) |
| 52 | + self.testInterpreter("a[1]", data: test, result: 2) |
| 53 | + } |
| 54 | + |
| 55 | + func testObjects() { |
| 56 | + struct TestObject { |
| 57 | + struct TestSubObject { |
| 58 | + let a: String |
| 59 | + } |
| 60 | + let sub: TestSubObject |
| 61 | + } |
| 62 | + let test = TestObject(sub: .init(a: "hello")) |
| 63 | + self.testInterpreter("sub.a", data: test, result: "hello") |
| 64 | + } |
| 65 | +} |
0 commit comments