Skip to content

Commit

Permalink
Merge pull request #10 from to4iki/replace_swift-testing
Browse files Browse the repository at this point in the history
Replace to swift-testing
  • Loading branch information
to4iki authored Sep 25, 2024
2 parents 5dffb40 + f4c0d60 commit b89f044
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 290 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
xcode: ['15.4']
xcode: ['16.0']
config: ['debug', 'release']
steps:
- uses: actions/checkout@v3
Expand Down
162 changes: 68 additions & 94 deletions Tests/ForcibleValueTests/ForcibleBoolTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import XCTest
import Foundation
import Testing

@testable import ForcibleValue

final class ForcibleBoolTests: XCTestCase {
@Suite
struct ForcibleBoolTests {
private struct Target: Decodable {
@ForcibleBool var value: Bool
}
Expand All @@ -15,107 +17,79 @@ final class ForcibleBoolTests: XCTestCase {
@ForcibleDefault.False var value: Bool
}

func testDecodeSuccess() throws {
let testCases: [ParameterizedTestCase<Any, Bool>] = [
.init(input: false, output: false),
.init(input: true, output: true),
.init(input: 0, output: false),
.init(input: 1, output: true),
.init(input: "\"false\"", output: false),
.init(input: "\"true\"", output: true),
]

for testCase in testCases {
let json = """
{
"value": \(testCase.input)
}
""".data(using: .utf8)

do {
let target = try JSONDecoder().decode(Target.self, from: json!)
XCTAssertEqual(target.value, testCase.output)
} catch {
XCTFail(error.localizedDescription)
@Test(arguments: [
TestCase(input: false, output: false),
TestCase(input: true, output: true),
TestCase(input: 0, output: false),
TestCase(input: 1, output: true),
TestCase(input: "\"false\"", output: false),
TestCase(input: "\"true\"", output: true),
])
func decodeSuccess(testCase: TestCase<Any, Bool>) throws {
let json = """
{
"value": \(testCase.input)
}
}
""".data(using: .utf8)
let target = try JSONDecoder().decode(Target.self, from: json!)
#expect(target.value == testCase.output)
}

func testDecodeError() throws {
let testCases: [ParameterizedTestCase<Any, Void>] = [
.init(input: -1, output: ()),
.init(input: 2, output: ()),
.init(input: "\"f\"", output: ()),
.init(input: "\"t\"", output: ()),
.init(input: 1.1, output: ()),
]

for testCase in testCases {
let json = """
{
"value": \(testCase.input)
}
""".data(using: .utf8)

XCTAssertThrowsError(
try JSONDecoder().decode(Target.self, from: json!)
)
@Test(arguments: [
TestCase(input: -1, output: ()),
TestCase(input: 2, output: ()),
TestCase(input: "\"f\"", output: ()),
TestCase(input: "\"t\"", output: ()),
TestCase(input: 1.1, output: ()),
])
func decodeError(testCase: TestCase<Any, Void>) throws {
let json = """
{
"value": \(testCase.input)
}
""".data(using: .utf8)
#expect(throws: (any Error).self) {
try JSONDecoder().decode(Target.self, from: json!)
}
}

func testOptionDecode() throws {
let testCases: [ParameterizedTestCase<Any?, Bool?>] = [
.init(input: nil, output: nil),
.init(input: 0, output: false),
]

for testCase in testCases {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}()

do {
let target = try JSONDecoder().decode(OptionTarget.self, from: json!)
XCTAssertEqual(target.value, testCase.output)
} catch {
XCTFail(error.localizedDescription)
@Test(arguments: [
TestCase(input: nil, output: nil),
TestCase(input: 0, output: false),
])
func decodeOptionSuccess(testCase: TestCase<Any?, Bool?>) throws {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}
}()
let target = try JSONDecoder().decode(OptionTarget.self, from: json!)
#expect(target.value == testCase.output)
}

func testDefaultValueDecode() throws {
let testCases: [ParameterizedTestCase<Any?, Bool>] = [
.init(input: nil, output: false),
.init(input: 1, output: true),
]

for testCase in testCases {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}()

do {
let target = try JSONDecoder().decode(DefaultTarget.self, from: json!)
XCTAssertEqual(target.value, testCase.output)
} catch {
XCTFail(error.localizedDescription)
@Test(arguments: [
TestCase(input: nil, output: false),
TestCase(input: 1, output: true),
])
func decodeDefaultSuccess(testCase: TestCase<Any?, Bool>) throws {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}
}()
let target = try JSONDecoder().decode(DefaultTarget.self, from: json!)
#expect(target.value == testCase.output)
}
}
146 changes: 60 additions & 86 deletions Tests/ForcibleValueTests/ForcibleNumberTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import XCTest
import Foundation
import Testing

@testable import ForcibleValue

final class ForcibleNumberTests: XCTestCase {
@Suite
struct ForcibleNumberTests {
private struct Target: Decodable {
@ForcibleNumber var value: Int
}
Expand All @@ -15,99 +17,71 @@ final class ForcibleNumberTests: XCTestCase {
@ForcibleDefault.Zero var value: Int
}

func testDecodeSuccess() throws {
let testCases: [ParameterizedTestCase<Any, Int>] = [
.init(input: 1, output: 1),
.init(input: "\"1\"", output: 1),
]

for testCase in testCases {
let json = """
{
"value": \(testCase.input)
}
""".data(using: .utf8)

do {
let target = try JSONDecoder().decode(Target.self, from: json!)
XCTAssertEqual(target.value, testCase.output)
} catch {
XCTFail(error.localizedDescription)
@Test(arguments: [
TestCase(input: 1, output: 1),
TestCase(input: "\"1\"", output: 1),
])
func decodeSuccess(testCase: TestCase<Any, Int>) throws {
let json = """
{
"value": \(testCase.input)
}
}
""".data(using: .utf8)
let target = try JSONDecoder().decode(Target.self, from: json!)
#expect(target.value == testCase.output)
}

func testDecodeError() throws {
let testCases: [ParameterizedTestCase<Any, Void>] = [
.init(input: "\"abc\"", output: ())
]

for testCase in testCases {
let json = """
{
"value": \(testCase.input)
}
""".data(using: .utf8)

XCTAssertThrowsError(
try JSONDecoder().decode(Target.self, from: json!)
)
@Test(arguments: [
TestCase(input: "\"abc\"", output: ())
])
func decodeError(testCase: TestCase<Any, Void>) throws {
let json = """
{
"value": \(testCase.input)
}
""".data(using: .utf8)
#expect(throws: (any Error).self) {
try JSONDecoder().decode(Target.self, from: json!)
}
}

func testOptionDecode() throws {
let testCases: [ParameterizedTestCase<Any?, Int?>] = [
.init(input: nil, output: nil),
.init(input: "\"1\"", output: 1),
]

for testCase in testCases {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}()

do {
let target = try JSONDecoder().decode(OptionTarget.self, from: json!)
XCTAssertEqual(target.value, testCase.output)
} catch {
XCTFail(error.localizedDescription)
@Test(arguments: [
TestCase(input: nil, output: nil),
TestCase(input: "\"1\"", output: 1),
])
func decodeOptionSuccess(testCase: TestCase<Any?, Int?>) throws {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}
}()
let target = try JSONDecoder().decode(OptionTarget.self, from: json!)
#expect(target.value == testCase.output)
}

func testDefaultValueDecode() throws {
let testCases: [ParameterizedTestCase<Any?, Int>] = [
.init(input: nil, output: 0),
.init(input: "\"1\"", output: 1),
]

for testCase in testCases {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}()

do {
let target = try JSONDecoder().decode(DefaultTarget.self, from: json!)
XCTAssertEqual(target.value, testCase.output)
} catch {
XCTFail(error.localizedDescription)
@Test(arguments: [
TestCase(input: nil, output: 0),
TestCase(input: "\"1\"", output: 1),
])
func decodeDefaultSuccess(testCase: TestCase<Any?, Int>) throws {
let json: Data? = {
if let input = testCase.input {
return """
{
"value": \(input)
}
""".data(using: .utf8)
} else {
return "{}".data(using: .utf8)
}
}
}()
let target = try JSONDecoder().decode(DefaultTarget.self, from: json!)
#expect(target.value == testCase.output)
}
}
Loading

0 comments on commit b89f044

Please sign in to comment.