From 9b2cf815dff5193bf892a996c7574a6f9fe5436a Mon Sep 17 00:00:00 2001 From: meatball Date: Tue, 17 Dec 2024 23:03:37 +0100 Subject: [PATCH] Update test framework to use new Testing module and Swift tools version --- .../practice/acronym/.meta/template.swift | 16 +- exercises/practice/acronym/Package.swift | 2 +- .../Tests/AcronymTests/AcronymTests.swift | 65 ++++--- .../all-your-base/.meta/template.swift | 28 +-- .../AllYourBaseTests/AllYourBaseTests.swift | 176 +++++++++--------- 5 files changed, 144 insertions(+), 143 deletions(-) diff --git a/exercises/practice/acronym/.meta/template.swift b/exercises/practice/acronym/.meta/template.swift index 252d0ccee..f5aaa0bd7 100644 --- a/exercises/practice/acronym/.meta/template.swift +++ b/exercises/practice/acronym/.meta/template.swift @@ -1,16 +1,18 @@ -import XCTest +import Testing +import Foundation @testable import {{exercise|camelCase}} -class {{exercise|camelCase}}Tests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false + +@Suite struct {{exercise|camelCase}}Tests { {% for case in cases %} {% if forloop.first -%} - func test{{case.description |camelCase }}() { + @Test("{{case.description}}") {% else -%} - func test{{case.description |camelCase }}() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("{{case.description}}", .enabled(if: RUNALL)) {% endif -%} - XCTAssertEqual("{{case.expected}}", Acronym.abbreviate("{{case.input.phrase}}")) + func test{{case.description |camelCase }}() { + #expect("{{case.expected}}" == Acronym.abbreviate("{{case.input.phrase}}")) } {% endfor -%} } diff --git a/exercises/practice/acronym/Package.swift b/exercises/practice/acronym/Package.swift index 7a686a97f..058d816c5 100644 --- a/exercises/practice/acronym/Package.swift +++ b/exercises/practice/acronym/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/acronym/Tests/AcronymTests/AcronymTests.swift b/exercises/practice/acronym/Tests/AcronymTests/AcronymTests.swift index f9385977c..b82680772 100644 --- a/exercises/practice/acronym/Tests/AcronymTests/AcronymTests.swift +++ b/exercises/practice/acronym/Tests/AcronymTests/AcronymTests.swift @@ -1,54 +1,57 @@ -import XCTest +import Foundation +import Testing @testable import Acronym -class AcronymTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +@Suite struct AcronymTests { + + @Test("basic") func testBasic() { - XCTAssertEqual("PNG", Acronym.abbreviate("Portable Network Graphics")) + #expect("PNG" == Acronym.abbreviate("Portable Network Graphics")) } - func testLowercaseWords() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual("ROR", Acronym.abbreviate("Ruby on Rails")) + @Test("lowercase words", .enabled(if: RUNALL)) + func testLowercaseWords() { + #expect("ROR" == Acronym.abbreviate("Ruby on Rails")) } - func testPunctuation() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual("FIFO", Acronym.abbreviate("First In, First Out")) + @Test("punctuation", .enabled(if: RUNALL)) + func testPunctuation() { + #expect("FIFO" == Acronym.abbreviate("First In, First Out")) } - func testAllCapsWord() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual("GIMP", Acronym.abbreviate("GNU Image Manipulation Program")) + @Test("all caps word", .enabled(if: RUNALL)) + func testAllCapsWord() { + #expect("GIMP" == Acronym.abbreviate("GNU Image Manipulation Program")) } - func testPunctuationWithoutWhitespace() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual("CMOS", Acronym.abbreviate("Complementary metal-oxide semiconductor")) + @Test("punctuation without whitespace", .enabled(if: RUNALL)) + func testPunctuationWithoutWhitespace() { + #expect("CMOS" == Acronym.abbreviate("Complementary metal-oxide semiconductor")) } - func testVeryLongAbbreviation() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - "ROTFLSHTMDCOALM", - Acronym.abbreviate( - "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me")) + @Test("very long abbreviation", .enabled(if: RUNALL)) + func testVeryLongAbbreviation() { + #expect( + "ROTFLSHTMDCOALM" + == Acronym.abbreviate( + "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me")) } - func testConsecutiveDelimiters() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual("SIMUFTA", Acronym.abbreviate("Something - I made up from thin air")) + @Test("consecutive delimiters", .enabled(if: RUNALL)) + func testConsecutiveDelimiters() { + #expect("SIMUFTA" == Acronym.abbreviate("Something - I made up from thin air")) } - func testApostrophes() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual("HC", Acronym.abbreviate("Halley's Comet")) + @Test("apostrophes", .enabled(if: RUNALL)) + func testApostrophes() { + #expect("HC" == Acronym.abbreviate("Halley's Comet")) } - func testUnderscoreEmphasis() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual("TRNT", Acronym.abbreviate("The Road _Not_ Taken")) + @Test("underscore emphasis", .enabled(if: RUNALL)) + func testUnderscoreEmphasis() { + #expect("TRNT" == Acronym.abbreviate("The Road _Not_ Taken")) } } diff --git a/exercises/practice/all-your-base/.meta/template.swift b/exercises/practice/all-your-base/.meta/template.swift index b8cb33eaa..261e3c47f 100644 --- a/exercises/practice/all-your-base/.meta/template.swift +++ b/exercises/practice/all-your-base/.meta/template.swift @@ -1,29 +1,31 @@ -import XCTest +import Testing +import Foundation @testable import {{exercise|camelCase}} -class {{exercise|camelCase}}Tests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false + +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false + +@Suite struct {{exercise|camelCase}}Tests { {% for case in cases %} {% if forloop.first -%} - func test{{case.description |camelCase }}() { + @Test("{{case.description}}") {% else -%} - func test{{case.description |camelCase }}() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("{{case.description}}", .enabled(if: RUNALL)) {% endif -%} + func test{{case.description |camelCase }}() { {%- if case.expected.error -%} - XCTAssertThrowsError(try Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}})) { error in {%- if case.input.inputBase < 2 %} - XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase) + #expect(throws: BaseError.invalidInputBase) {%- elif case.input.outputBase < 2 %} - XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase) + #expect(throws: BaseError.invalidOutputBase) {%- elif case.input.digits | any:"isNegative" %} - XCTAssertEqual(error as? BaseError, BaseError.negativeDigit) + #expect(throws: BaseError.negativeDigit) {%- else %} - XCTAssertEqual(error as? BaseError, BaseError.invalidPositiveDigit) + #expect(throws: BaseError.invalidPositiveDigit) {%- endif -%} - } + {try Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}})} {%- else -%} - XCTAssertEqual(try! Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}}), {{case.expected}}) + #expect(try! Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}}) == {{case.expected}}) {%- endif -%} } {% endfor -%} diff --git a/exercises/practice/all-your-base/Tests/AllYourBaseTests/AllYourBaseTests.swift b/exercises/practice/all-your-base/Tests/AllYourBaseTests/AllYourBaseTests.swift index 95db39fa8..c786d9195 100644 --- a/exercises/practice/all-your-base/Tests/AllYourBaseTests/AllYourBaseTests.swift +++ b/exercises/practice/all-your-base/Tests/AllYourBaseTests/AllYourBaseTests.swift @@ -1,150 +1,144 @@ -import XCTest +import Foundation +import Testing @testable import AllYourBase -class AllYourBaseTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +@Suite struct AllYourBaseTests { + + @Test("single bit one to decimal") func testSingleBitOneToDecimal() { - XCTAssertEqual(try! Base.outputDigits(inputBase: 2, inputDigits: [1], outputBase: 10), [1]) + #expect(try! Base.outputDigits(inputBase: 2, inputDigits: [1], outputBase: 10) == [1]) } - func testBinaryToSingleDecimal() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1], outputBase: 10), [5]) + @Test("binary to single decimal", .enabled(if: RUNALL)) + func testBinaryToSingleDecimal() { + #expect(try! Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1], outputBase: 10) == [5]) } - func testSingleDecimalToBinary() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 10, inputDigits: [5], outputBase: 2), [1, 0, 1]) + @Test("single decimal to binary", .enabled(if: RUNALL)) + func testSingleDecimalToBinary() { + #expect(try! Base.outputDigits(inputBase: 10, inputDigits: [5], outputBase: 2) == [1, 0, 1]) } - func testBinaryToMultipleDecimal() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1, 0, 1, 0], outputBase: 10), [4, 2]) + @Test("binary to multiple decimal", .enabled(if: RUNALL)) + func testBinaryToMultipleDecimal() { + #expect( + try! Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1, 0, 1, 0], outputBase: 10) == [ + 4, 2, + ]) } - func testDecimalToBinary() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 10, inputDigits: [4, 2], outputBase: 2), [1, 0, 1, 0, 1, 0]) + @Test("decimal to binary", .enabled(if: RUNALL)) + func testDecimalToBinary() { + #expect( + try! Base.outputDigits(inputBase: 10, inputDigits: [4, 2], outputBase: 2) == [ + 1, 0, 1, 0, 1, 0, + ]) } - func testTrinaryToHexadecimal() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 3, inputDigits: [1, 1, 2, 0], outputBase: 16), [2, 10]) + @Test("trinary to hexadecimal", .enabled(if: RUNALL)) + func testTrinaryToHexadecimal() { + #expect( + try! Base.outputDigits(inputBase: 3, inputDigits: [1, 1, 2, 0], outputBase: 16) == [2, 10]) } - func testHexadecimalToTrinary() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 16, inputDigits: [2, 10], outputBase: 3), [1, 1, 2, 0]) + @Test("hexadecimal to trinary", .enabled(if: RUNALL)) + func testHexadecimalToTrinary() { + #expect( + try! Base.outputDigits(inputBase: 16, inputDigits: [2, 10], outputBase: 3) == [1, 1, 2, 0]) } - func test15BitInteger() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 97, inputDigits: [3, 46, 60], outputBase: 73), [6, 10, 45]) + @Test("15-bit integer", .enabled(if: RUNALL)) + func test15BitInteger() { + #expect( + try! Base.outputDigits(inputBase: 97, inputDigits: [3, 46, 60], outputBase: 73) == [ + 6, 10, 45, + ]) } - func testEmptyList() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! Base.outputDigits(inputBase: 2, inputDigits: [], outputBase: 10), [0]) + @Test("empty list", .enabled(if: RUNALL)) + func testEmptyList() { + #expect(try! Base.outputDigits(inputBase: 2, inputDigits: [], outputBase: 10) == [0]) } - func testSingleZero() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! Base.outputDigits(inputBase: 10, inputDigits: [0], outputBase: 2), [0]) + @Test("single zero", .enabled(if: RUNALL)) + func testSingleZero() { + #expect(try! Base.outputDigits(inputBase: 10, inputDigits: [0], outputBase: 2) == [0]) } - func testMultipleZeros() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 10, inputDigits: [0, 0, 0], outputBase: 2), [0]) + @Test("multiple zeros", .enabled(if: RUNALL)) + func testMultipleZeros() { + #expect(try! Base.outputDigits(inputBase: 10, inputDigits: [0, 0, 0], outputBase: 2) == [0]) } - func testLeadingZeros() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual( - try! Base.outputDigits(inputBase: 7, inputDigits: [0, 6, 0], outputBase: 10), [4, 2]) + @Test("leading zeros", .enabled(if: RUNALL)) + func testLeadingZeros() { + #expect( + try! Base.outputDigits(inputBase: 7, inputDigits: [0, 6, 0], outputBase: 10) == [4, 2]) } - func testInputBaseIsOne() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try Base.outputDigits(inputBase: 1, inputDigits: [0], outputBase: 10)) { - error in - XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase) + @Test("input base is one", .enabled(if: RUNALL)) + func testInputBaseIsOne() { + #expect(throws: BaseError.invalidInputBase) { + try Base.outputDigits(inputBase: 1, inputDigits: [0], outputBase: 10) } } - func testInputBaseIsZero() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try Base.outputDigits(inputBase: 0, inputDigits: [], outputBase: 10)) { - error in - XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase) + @Test("input base is zero", .enabled(if: RUNALL)) + func testInputBaseIsZero() { + #expect(throws: BaseError.invalidInputBase) { + try Base.outputDigits(inputBase: 0, inputDigits: [], outputBase: 10) } } - func testInputBaseIsNegative() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try Base.outputDigits(inputBase: -2, inputDigits: [1], outputBase: 10)) { - error in - XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase) + @Test("input base is negative", .enabled(if: RUNALL)) + func testInputBaseIsNegative() { + #expect(throws: BaseError.invalidInputBase) { + try Base.outputDigits(inputBase: -2, inputDigits: [1], outputBase: 10) } } - func testNegativeDigit() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError( + @Test("negative digit", .enabled(if: RUNALL)) + func testNegativeDigit() { + #expect(throws: BaseError.negativeDigit) { try Base.outputDigits(inputBase: 2, inputDigits: [1, -1, 1, 0, 1, 0], outputBase: 10) - ) { error in - XCTAssertEqual(error as? BaseError, BaseError.negativeDigit) } } - func testInvalidPositiveDigit() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError( + @Test("invalid positive digit", .enabled(if: RUNALL)) + func testInvalidPositiveDigit() { + #expect(throws: BaseError.invalidPositiveDigit) { try Base.outputDigits(inputBase: 2, inputDigits: [1, 2, 1, 0, 1, 0], outputBase: 10) - ) { error in - XCTAssertEqual(error as? BaseError, BaseError.invalidPositiveDigit) } } - func testOutputBaseIsOne() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError( + @Test("output base is one", .enabled(if: RUNALL)) + func testOutputBaseIsOne() { + #expect(throws: BaseError.invalidOutputBase) { try Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1, 0, 1, 0], outputBase: 1) - ) { error in - XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase) } } - func testOutputBaseIsZero() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try Base.outputDigits(inputBase: 10, inputDigits: [7], outputBase: 0)) { - error in - XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase) + @Test("output base is zero", .enabled(if: RUNALL)) + func testOutputBaseIsZero() { + #expect(throws: BaseError.invalidOutputBase) { + try Base.outputDigits(inputBase: 10, inputDigits: [7], outputBase: 0) } } - func testOutputBaseIsNegative() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try Base.outputDigits(inputBase: 2, inputDigits: [1], outputBase: -7)) { - error in - XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase) + @Test("output base is negative", .enabled(if: RUNALL)) + func testOutputBaseIsNegative() { + #expect(throws: BaseError.invalidOutputBase) { + try Base.outputDigits(inputBase: 2, inputDigits: [1], outputBase: -7) } } - func testBothBasesAreNegative() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try Base.outputDigits(inputBase: -2, inputDigits: [1], outputBase: -7)) { - error in - XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase) + @Test("both bases are negative", .enabled(if: RUNALL)) + func testBothBasesAreNegative() { + #expect(throws: BaseError.invalidInputBase) { + try Base.outputDigits(inputBase: -2, inputDigits: [1], outputBase: -7) } } }