-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONPathTestCase.swift
93 lines (86 loc) · 3.42 KB
/
JSONPathTestCase.swift
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// JSONPathTestCase.swift
// DynamicJSONTests
//
// Created by Matthias Zenger on 06/03/2024.
// Copyright © 2024 Matthias Zenger. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import XCTest
import DynamicJSON
class JSONPathTestCase: XCTestCase {
enum JSONPathTestError: Error {
case testSuiteNotFound
}
public func loadComplianceTests(from filename: String) throws -> JSONPathComplianceTests {
let bundle = Bundle(for: type(of: self))
if let url = bundle.url(forResource: filename, withExtension: "json") {
let data = try Data(contentsOf: url)
return try JSONDecoder().decode(JSONPathComplianceTests.self, from: data)
} else {
let url = URL(fileURLWithPath: "Tests/DynamicJSONTests/ComplianceTests/JSONPath/\(filename).json")
if FileManager.default.fileExists(atPath: url.path) {
let data = try Data(contentsOf: url)
return try JSONDecoder().decode(JSONPathComplianceTests.self, from: data)
} else {
throw JSONPathTestError.testSuiteNotFound
}
}
}
private func parse(_ str: String) -> JSONPath? {
var parser = JSONPathParser(string: str)
return try? parser.parse()
}
public func execute(name: String, tests: [JSONPathComplianceTest]) {
for test in tests {
do {
print("✅ \(name)/\(test.name)")
if test.ignore ?? false {
// Don't do anything
} else if test.invalid_selector ?? false {
if let document = test.document {
XCTAssertThrowsError(try document.query(values: test.selector),
"🛑 \(name)/\(test.name): did not fail")
} else {
var parser = JSONPathParser(string: test.selector)
XCTAssertThrowsError(try parser.parse(), "🛑 \(name)/\(test.name): did not fail")
}
} else if let document = test.document, case .some(.array(let res)) = test.result {
let resultset = try document.query(test.selector)
let result = Set(res)
let computed = Set(resultset.values)
XCTAssertEqual(computed, result, "🛑 \(name)/\(test.name): value mismatch")
for res in resultset {
XCTAssertEqual(res.value,
document[ref: res.location],
"🛑 \(name)/\(test.name): location mismatch for \(res.location)")
}
} else {
XCTFail("🛑 \(name)/\(test.name): invalid test case")
}
} catch let e {
XCTFail("🛑 \(name)/\(test.name): query failed (\(e.localizedDescription))")
}
}
}
public func execute(suite filename: String) {
do {
let complianceTests = try self.loadComplianceTests(from: filename)
self.execute(name: filename, tests: complianceTests.tests)
} catch let e {
XCTFail("🛑 \(filename): cannot load JSONPath test suite (\(e.localizedDescription))")
}
}
}