Skip to content

Commit

Permalink
Use URLSession.shared.data in tests in swift 6 or later (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Aug 20, 2024
1 parent f13bd83 commit c3beb4a
Showing 1 changed file with 54 additions and 40 deletions.
94 changes: 54 additions & 40 deletions Tests/MustacheTests/SpecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
//===----------------------------------------------------------------------===//

import Foundation
import Mustache
import XCTest

#if os(Linux) || os(Windows)
import FoundationNetworking
#endif
import Mustache
import XCTest

public struct AnyDecodable: Decodable {
public let value: Any
Expand All @@ -27,8 +28,8 @@ public struct AnyDecodable: Decodable {
}
}

public extension AnyDecodable {
init(from decoder: Decoder) throws {
extension AnyDecodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

if container.decodeNil() {
Expand All @@ -48,7 +49,9 @@ public extension AnyDecodable {
} else if let dictionary = try? container.decode([String: AnyDecodable].self) {
self.init(dictionary.mapValues { $0.value })
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "AnyDecodable value cannot be decoded")
throw DecodingError.dataCorruptedError(
in: container, debugDescription: "AnyDecodable value cannot be decoded"
)
}
}
}
Expand Down Expand Up @@ -86,19 +89,20 @@ final class MustacheSpecTests: XCTestCase {

func XCTAssertSpecEqual(_ result: String?, _ test: Spec.Test) {
if result != test.expected {
XCTFail("""
\(test.name)
\(test.desc)
template:
\(test.template)
data:
\(test.data.value)
\(test.partials.map { "partials:\n\($0)" } ?? "")
result:
\(result ?? "nil")
expected:
\(test.expected)
""")
XCTFail(
"""
\(test.name)
\(test.desc)
template:
\(test.template)
data:
\(test.data.value)
\(test.partials.map { "partials:\n\($0)" } ?? "")
result:
\(result ?? "nil")
expected:
\(test.expected)
""")
}
}
}
Expand All @@ -107,13 +111,18 @@ final class MustacheSpecTests: XCTestCase {
let tests: [Test]
}

func testSpec(name: String, ignoring: [String] = []) throws {
let url = URL(string: "https://raw.githubusercontent.com/mustache/spec/master/specs/\(name).json")!
try testSpec(url: url, ignoring: ignoring)
func testSpec(name: String, ignoring: [String] = []) async throws {
let url = URL(
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)
#endif
let spec = try JSONDecoder().decode(Spec.self, from: data)

let date = Date()
Expand All @@ -124,13 +133,18 @@ final class MustacheSpecTests: XCTestCase {
print(-date.timeIntervalSinceNow)
}

func testSpec(name: String, only: [String]) throws {
let url = URL(string: "https://raw.githubusercontent.com/mustache/spec/master/specs/\(name).json")!
try testSpec(url: url, only: only)
func testSpec(name: String, only: [String]) async throws {
let url = URL(
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)
#endif
let spec = try JSONDecoder().decode(Spec.self, from: data)

let date = Date()
Expand All @@ -141,32 +155,32 @@ final class MustacheSpecTests: XCTestCase {
print(-date.timeIntervalSinceNow)
}

func testCommentsSpec() throws {
try self.testSpec(name: "comments")
func testCommentsSpec() async throws {
try await self.testSpec(name: "comments")
}

func testDelimitersSpec() throws {
try self.testSpec(name: "delimiters")
func testDelimitersSpec() async throws {
try await self.testSpec(name: "delimiters")
}

func testInterpolationSpec() throws {
try self.testSpec(name: "interpolation")
func testInterpolationSpec() async throws {
try await self.testSpec(name: "interpolation")
}

func testInvertedSpec() throws {
try self.testSpec(name: "inverted")
func testInvertedSpec() async throws {
try await self.testSpec(name: "inverted")
}

func testPartialsSpec() throws {
try self.testSpec(name: "partials")
func testPartialsSpec() async throws {
try await self.testSpec(name: "partials")
}

func testSectionsSpec() throws {
try self.testSpec(name: "sections")
func testSectionsSpec() async throws {
try await self.testSpec(name: "sections")
}

func testInheritanceSpec() throws {
try self.testSpec(
func testInheritanceSpec() async throws {
try await self.testSpec(
name: "~inheritance",
ignoring: [
"Intrinsic indentation",
Expand Down

0 comments on commit c3beb4a

Please sign in to comment.