Use URLSession.shared.data in tests in swift 6 or later (#44)

This commit is contained in:
Adam Fowler
2024-08-20 19:45:19 +01:00
committed by GitHub
parent f13bd839a1
commit c3beb4a327

View File

@@ -13,11 +13,12 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
import Foundation import Foundation
import Mustache
import XCTest
#if os(Linux) || os(Windows) #if os(Linux) || os(Windows)
import FoundationNetworking import FoundationNetworking
#endif #endif
import Mustache
import XCTest
public struct AnyDecodable: Decodable { public struct AnyDecodable: Decodable {
public let value: Any public let value: Any
@@ -27,8 +28,8 @@ public struct AnyDecodable: Decodable {
} }
} }
public extension AnyDecodable { extension AnyDecodable {
init(from decoder: Decoder) throws { public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer() let container = try decoder.singleValueContainer()
if container.decodeNil() { if container.decodeNil() {
@@ -48,7 +49,9 @@ public extension AnyDecodable {
} else if let dictionary = try? container.decode([String: AnyDecodable].self) { } else if let dictionary = try? container.decode([String: AnyDecodable].self) {
self.init(dictionary.mapValues { $0.value }) self.init(dictionary.mapValues { $0.value })
} else { } else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "AnyDecodable value cannot be decoded") throw DecodingError.dataCorruptedError(
in: container, debugDescription: "AnyDecodable value cannot be decoded"
)
} }
} }
} }
@@ -86,19 +89,20 @@ final class MustacheSpecTests: XCTestCase {
func XCTAssertSpecEqual(_ result: String?, _ test: Spec.Test) { func XCTAssertSpecEqual(_ result: String?, _ test: Spec.Test) {
if result != test.expected { if result != test.expected {
XCTFail(""" XCTFail(
\(test.name) """
\(test.desc) \(test.name)
template: \(test.desc)
\(test.template) template:
data: \(test.template)
\(test.data.value) data:
\(test.partials.map { "partials:\n\($0)" } ?? "") \(test.data.value)
result: \(test.partials.map { "partials:\n\($0)" } ?? "")
\(result ?? "nil") result:
expected: \(result ?? "nil")
\(test.expected) expected:
""") \(test.expected)
""")
} }
} }
} }
@@ -107,13 +111,18 @@ final class MustacheSpecTests: XCTestCase {
let tests: [Test] let tests: [Test]
} }
func testSpec(name: String, ignoring: [String] = []) throws { func testSpec(name: String, ignoring: [String] = []) async throws {
let url = URL(string: "https://raw.githubusercontent.com/mustache/spec/master/specs/\(name).json")! let url = URL(
try testSpec(url: url, ignoring: ignoring) string: "https://raw.githubusercontent.com/mustache/spec/master/specs/\(name).json")!
try await testSpec(url: url, ignoring: ignoring)
} }
func testSpec(url: URL, ignoring: [String] = []) throws { func testSpec(url: URL, ignoring: [String] = []) async throws {
#if compiler(>=6.0)
let (data, _) = try await URLSession.shared.data(from: url)
#else
let data = try Data(contentsOf: url) let data = try Data(contentsOf: url)
#endif
let spec = try JSONDecoder().decode(Spec.self, from: data) let spec = try JSONDecoder().decode(Spec.self, from: data)
let date = Date() let date = Date()
@@ -124,13 +133,18 @@ final class MustacheSpecTests: XCTestCase {
print(-date.timeIntervalSinceNow) print(-date.timeIntervalSinceNow)
} }
func testSpec(name: String, only: [String]) throws { func testSpec(name: String, only: [String]) async throws {
let url = URL(string: "https://raw.githubusercontent.com/mustache/spec/master/specs/\(name).json")! let url = URL(
try testSpec(url: url, only: only) string: "https://raw.githubusercontent.com/mustache/spec/master/specs/\(name).json")!
try await testSpec(url: url, only: only)
} }
func testSpec(url: URL, only: [String]) throws { func testSpec(url: URL, only: [String]) async throws {
#if compiler(>=6.0)
let (data, _) = try await URLSession.shared.data(from: url)
#else
let data = try Data(contentsOf: url) let data = try Data(contentsOf: url)
#endif
let spec = try JSONDecoder().decode(Spec.self, from: data) let spec = try JSONDecoder().decode(Spec.self, from: data)
let date = Date() let date = Date()
@@ -141,32 +155,32 @@ final class MustacheSpecTests: XCTestCase {
print(-date.timeIntervalSinceNow) print(-date.timeIntervalSinceNow)
} }
func testCommentsSpec() throws { func testCommentsSpec() async throws {
try self.testSpec(name: "comments") try await self.testSpec(name: "comments")
} }
func testDelimitersSpec() throws { func testDelimitersSpec() async throws {
try self.testSpec(name: "delimiters") try await self.testSpec(name: "delimiters")
} }
func testInterpolationSpec() throws { func testInterpolationSpec() async throws {
try self.testSpec(name: "interpolation") try await self.testSpec(name: "interpolation")
} }
func testInvertedSpec() throws { func testInvertedSpec() async throws {
try self.testSpec(name: "inverted") try await self.testSpec(name: "inverted")
} }
func testPartialsSpec() throws { func testPartialsSpec() async throws {
try self.testSpec(name: "partials") try await self.testSpec(name: "partials")
} }
func testSectionsSpec() throws { func testSectionsSpec() async throws {
try self.testSpec(name: "sections") try await self.testSpec(name: "sections")
} }
func testInheritanceSpec() throws { func testInheritanceSpec() async throws {
try self.testSpec( try await self.testSpec(
name: "~inheritance", name: "~inheritance",
ignoring: [ ignoring: [
"Intrinsic indentation", "Intrinsic indentation",