From 998980bf32bc22f2c4291fc7d7a6a5f22467d20e Mon Sep 17 00:00:00 2001 From: meatball Date: Sat, 21 Dec 2024 22:31:36 +0100 Subject: [PATCH] Update Swift tools version to 6.0 and refactor test cases to use new testing framework --- .../practice/two-fer/.meta/template.swift | 18 +- exercises/practice/two-fer/Package.swift | 2 +- .../Tests/TwoFerTests/TwoFerTests.swift | 25 ++- .../practice/word-count/.meta/template.swift | 16 +- exercises/practice/word-count/Package.swift | 2 +- .../Tests/WordCountTests/WordCountTests.swift | 89 +++++----- exercises/practice/wordy/.meta/template.swift | 20 ++- exercises/practice/wordy/Package.swift | 2 +- .../wordy/Tests/WordyTests/WordyTests.swift | 159 +++++++++--------- 9 files changed, 171 insertions(+), 162 deletions(-) diff --git a/exercises/practice/two-fer/.meta/template.swift b/exercises/practice/two-fer/.meta/template.swift index 198f3ca90..f72360621 100644 --- a/exercises/practice/two-fer/.meta/template.swift +++ b/exercises/practice/two-fer/.meta/template.swift @@ -1,19 +1,21 @@ -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.input.name | isNull -%} - XCTAssertEqual(twoFer(), "{{case.expected}}") + #expect(twoFer() == "{{case.expected}}") {%- else -%} - XCTAssertEqual(twoFer(name: "{{case.input.name}}"), "{{case.expected}}") + #expect(twoFer(name: "{{case.input.name}}") == "{{case.expected}}") {%- endif -%} } {% endfor -%} diff --git a/exercises/practice/two-fer/Package.swift b/exercises/practice/two-fer/Package.swift index a71cea0cd..1e5aaa0b9 100644 --- a/exercises/practice/two-fer/Package.swift +++ b/exercises/practice/two-fer/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/two-fer/Tests/TwoFerTests/TwoFerTests.swift b/exercises/practice/two-fer/Tests/TwoFerTests/TwoFerTests.swift index 4d4e5f23d..d43c49a7c 100644 --- a/exercises/practice/two-fer/Tests/TwoFerTests/TwoFerTests.swift +++ b/exercises/practice/two-fer/Tests/TwoFerTests/TwoFerTests.swift @@ -1,21 +1,18 @@ -import XCTest +import Foundation +import Testing @testable import TwoFer -class TwoFerTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false - func testNoNameGiven() { - XCTAssertEqual(twoFer(), "One for you, one for me.") - } +@Suite struct TwoFerTests { - func testANameGiven() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(twoFer(name: "Alice"), "One for Alice, one for me.") - } + @Test("no name given") + func testNoNameGiven() { #expect(twoFer() == "One for you, one for me.") } - func testAnotherNameGiven() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(twoFer(name: "Bob"), "One for Bob, one for me.") - } + @Test("a name given", .enabled(if: RUNALL)) + func testANameGiven() { #expect(twoFer(name: "Alice") == "One for Alice, one for me.") } + + @Test("another name given", .enabled(if: RUNALL)) + func testAnotherNameGiven() { #expect(twoFer(name: "Bob") == "One for Bob, one for me.") } } diff --git a/exercises/practice/word-count/.meta/template.swift b/exercises/practice/word-count/.meta/template.swift index b892d614f..0016090d4 100644 --- a/exercises/practice/word-count/.meta/template.swift +++ b/exercises/practice/word-count/.meta/template.swift @@ -1,18 +1,20 @@ -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 }}() { let words = WordCount(words: "{{case.input.sentence | inspect}}") let expected = {{case.expected | toStringDictionary}} - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } {% endfor -%} } diff --git a/exercises/practice/word-count/Package.swift b/exercises/practice/word-count/Package.swift index 5c9d53e12..c8e3ae3a9 100644 --- a/exercises/practice/word-count/Package.swift +++ b/exercises/practice/word-count/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/word-count/Tests/WordCountTests/WordCountTests.swift b/exercises/practice/word-count/Tests/WordCountTests/WordCountTests.swift index 2a46f1b9b..93b945333 100644 --- a/exercises/practice/word-count/Tests/WordCountTests/WordCountTests.swift +++ b/exercises/practice/word-count/Tests/WordCountTests/WordCountTests.swift @@ -1,108 +1,111 @@ -import XCTest +import Foundation +import Testing @testable import WordCount -class WordCountTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +@Suite struct WordCountTests { + + @Test("count one word") func testCountOneWord() { let words = WordCount(words: "word") let expected = ["word": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testCountOneOfEachWord() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("count one of each word", .enabled(if: RUNALL)) + func testCountOneOfEachWord() { let words = WordCount(words: "one of each") let expected = ["each": 1, "of": 1, "one": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testMultipleOccurrencesOfAWord() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("multiple occurrences of a word", .enabled(if: RUNALL)) + func testMultipleOccurrencesOfAWord() { let words = WordCount(words: "one fish two fish red fish blue fish") let expected = ["blue": 1, "fish": 4, "one": 1, "red": 1, "two": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testHandlesCrampedLists() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("handles cramped lists", .enabled(if: RUNALL)) + func testHandlesCrampedLists() { let words = WordCount(words: "one,two,three") let expected = ["one": 1, "three": 1, "two": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testHandlesExpandedLists() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("handles expanded lists", .enabled(if: RUNALL)) + func testHandlesExpandedLists() { let words = WordCount(words: "one,\ntwo,\nthree") let expected = ["one": 1, "three": 1, "two": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testIgnorePunctuation() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("ignore punctuation", .enabled(if: RUNALL)) + func testIgnorePunctuation() { let words = WordCount(words: "car: carpet as java: javascript!!&@$%^&") let expected = ["as": 1, "car": 1, "carpet": 1, "java": 1, "javascript": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testIncludeNumbers() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("include numbers", .enabled(if: RUNALL)) + func testIncludeNumbers() { let words = WordCount(words: "testing, 1, 2 testing") let expected = ["1": 1, "2": 1, "testing": 2] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testNormalizeCase() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("normalize case", .enabled(if: RUNALL)) + func testNormalizeCase() { let words = WordCount(words: "go Go GO Stop stop") let expected = ["go": 3, "stop": 2] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testWithApostrophes() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("with apostrophes", .enabled(if: RUNALL)) + func testWithApostrophes() { let words = WordCount(words: "'First: don't laugh. Then: don't cry. You're getting it.'") let expected = [ "cry": 1, "don't": 2, "first": 1, "getting": 1, "it": 1, "laugh": 1, "then": 1, "you're": 1, ] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testWithQuotations() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("with quotations", .enabled(if: RUNALL)) + func testWithQuotations() { let words = WordCount(words: "Joe can't tell between 'large' and large.") let expected = ["and": 1, "between": 1, "can't": 1, "joe": 1, "large": 2, "tell": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testSubstringsFromTheBeginning() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("substrings from the beginning", .enabled(if: RUNALL)) + func testSubstringsFromTheBeginning() { let words = WordCount(words: "Joe can't tell between app, apple and a.") let expected = [ "a": 1, "and": 1, "app": 1, "apple": 1, "between": 1, "can't": 1, "joe": 1, "tell": 1, ] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testMultipleSpacesNotDetectedAsAWord() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("multiple spaces not detected as a word", .enabled(if: RUNALL)) + func testMultipleSpacesNotDetectedAsAWord() { let words = WordCount(words: " multiple whitespaces") let expected = ["multiple": 1, "whitespaces": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testAlternatingWordSeparatorsNotDetectedAsAWord() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("alternating word separators not detected as a word", .enabled(if: RUNALL)) + func testAlternatingWordSeparatorsNotDetectedAsAWord() { let words = WordCount(words: ",\n,one,\n ,two \n 'three'") let expected = ["one": 1, "three": 1, "two": 1] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } - func testQuotationForWordWithApostrophe() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("quotation for word with apostrophe", .enabled(if: RUNALL)) + func testQuotationForWordWithApostrophe() { let words = WordCount(words: "can, can't, 'can't'") let expected = ["can": 1, "can't": 2] - XCTAssertEqual(words.count(), expected) + #expect(words.count() == expected) } } diff --git a/exercises/practice/wordy/.meta/template.swift b/exercises/practice/wordy/.meta/template.swift index f53f4ad12..9e02f4d6e 100644 --- a/exercises/practice/wordy/.meta/template.swift +++ b/exercises/practice/wordy/.meta/template.swift @@ -1,20 +1,22 @@ -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 }}() { {% ifnot case.expected.error -%} - XCTAssertEqual(try! wordyAnswer("{{case.input.question}}"), {{case.expected}}) + #expect(try! wordyAnswer("{{case.input.question}}") == {{case.expected}}) {% else -%} - XCTAssertThrowsError(try wordyAnswer("{{case.input.question}}")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("{{case.input.question}}") } {% endif -%} } diff --git a/exercises/practice/wordy/Package.swift b/exercises/practice/wordy/Package.swift index b6dcc66eb..ca60af2ab 100644 --- a/exercises/practice/wordy/Package.swift +++ b/exercises/practice/wordy/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/wordy/Tests/WordyTests/WordyTests.swift b/exercises/practice/wordy/Tests/WordyTests/WordyTests.swift index ab0577f03..6b88611ae 100644 --- a/exercises/practice/wordy/Tests/WordyTests/WordyTests.swift +++ b/exercises/practice/wordy/Tests/WordyTests/WordyTests.swift @@ -1,137 +1,140 @@ -import XCTest +import Foundation +import Testing @testable import Wordy -class WordyTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +@Suite struct WordyTests { + + @Test("just a number") func testJustANumber() { - XCTAssertEqual(try! wordyAnswer("What is 5?"), 5) + #expect(try! wordyAnswer("What is 5?") == 5) } - func testAddition() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 1 plus 1?"), 2) + @Test("addition", .enabled(if: RUNALL)) + func testAddition() { + #expect(try! wordyAnswer("What is 1 plus 1?") == 2) } - func testMoreAddition() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 53 plus 2?"), 55) + @Test("more addition", .enabled(if: RUNALL)) + func testMoreAddition() { + #expect(try! wordyAnswer("What is 53 plus 2?") == 55) } - func testAdditionWithNegativeNumbers() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is -1 plus -10?"), -11) + @Test("addition with negative numbers", .enabled(if: RUNALL)) + func testAdditionWithNegativeNumbers() { + #expect(try! wordyAnswer("What is -1 plus -10?") == -11) } - func testLargeAddition() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 123 plus 45678?"), 45801) + @Test("large addition", .enabled(if: RUNALL)) + func testLargeAddition() { + #expect(try! wordyAnswer("What is 123 plus 45678?") == 45801) } - func testSubtraction() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 4 minus -12?"), 16) + @Test("subtraction", .enabled(if: RUNALL)) + func testSubtraction() { + #expect(try! wordyAnswer("What is 4 minus -12?") == 16) } - func testMultiplication() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is -3 multiplied by 25?"), -75) + @Test("multiplication", .enabled(if: RUNALL)) + func testMultiplication() { + #expect(try! wordyAnswer("What is -3 multiplied by 25?") == -75) } - func testDivision() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 33 divided by -3?"), -11) + @Test("division", .enabled(if: RUNALL)) + func testDivision() { + #expect(try! wordyAnswer("What is 33 divided by -3?") == -11) } - func testMultipleAdditions() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 1 plus 1 plus 1?"), 3) + @Test("multiple additions", .enabled(if: RUNALL)) + func testMultipleAdditions() { + #expect(try! wordyAnswer("What is 1 plus 1 plus 1?") == 3) } - func testAdditionAndSubtraction() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 1 plus 5 minus -2?"), 8) + @Test("addition and subtraction", .enabled(if: RUNALL)) + func testAdditionAndSubtraction() { + #expect(try! wordyAnswer("What is 1 plus 5 minus -2?") == 8) } - func testMultipleSubtraction() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 20 minus 4 minus 13?"), 3) + @Test("multiple subtraction", .enabled(if: RUNALL)) + func testMultipleSubtraction() { + #expect(try! wordyAnswer("What is 20 minus 4 minus 13?") == 3) } - func testSubtractionThenAddition() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 17 minus 6 plus 3?"), 14) + @Test("subtraction then addition", .enabled(if: RUNALL)) + func testSubtractionThenAddition() { + #expect(try! wordyAnswer("What is 17 minus 6 plus 3?") == 14) } - func testMultipleMultiplication() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is 2 multiplied by -2 multiplied by 3?"), -12) + @Test("multiple multiplication", .enabled(if: RUNALL)) + func testMultipleMultiplication() { + #expect(try! wordyAnswer("What is 2 multiplied by -2 multiplied by 3?") == -12) } - func testAdditionAndMultiplication() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is -3 plus 7 multiplied by -2?"), -8) + @Test("addition and multiplication", .enabled(if: RUNALL)) + func testAdditionAndMultiplication() { + #expect(try! wordyAnswer("What is -3 plus 7 multiplied by -2?") == -8) } - func testMultipleDivision() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertEqual(try! wordyAnswer("What is -12 divided by 2 divided by -3?"), 2) + @Test("multiple division", .enabled(if: RUNALL)) + func testMultipleDivision() { + #expect(try! wordyAnswer("What is -12 divided by 2 divided by -3?") == 2) } - func testUnknownOperation() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try wordyAnswer("What is 52 cubed?")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + @Test("unknown operation", .enabled(if: RUNALL)) + func testUnknownOperation() { + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("What is 52 cubed?") } } - func testNonMathQuestion() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try wordyAnswer("Who is the President of the United States?")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + @Test("Non math question", .enabled(if: RUNALL)) + func testNonMathQuestion() { + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("Who is the President of the United States?") } } - func testRejectProblemMissingAnOperand() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try wordyAnswer("What is 1 plus?")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + @Test("reject problem missing an operand", .enabled(if: RUNALL)) + func testRejectProblemMissingAnOperand() { + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("What is 1 plus?") } } - func testRejectProblemWithNoOperandsOrOperators() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try wordyAnswer("What is?")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + @Test("reject problem with no operands or operators", .enabled(if: RUNALL)) + func testRejectProblemWithNoOperandsOrOperators() { + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("What is?") } } - func testRejectTwoOperationsInARow() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try wordyAnswer("What is 1 plus plus 2?")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + @Test("reject two operations in a row", .enabled(if: RUNALL)) + func testRejectTwoOperationsInARow() { + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("What is 1 plus plus 2?") } } - func testRejectTwoNumbersInARow() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try wordyAnswer("What is 1 plus 2 1?")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + @Test("reject two numbers in a row", .enabled(if: RUNALL)) + func testRejectTwoNumbersInARow() { + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("What is 1 plus 2 1?") } } - func testRejectPostfixNotation() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try wordyAnswer("What is 1 2 plus?")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + @Test("reject postfix notation", .enabled(if: RUNALL)) + func testRejectPostfixNotation() { + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("What is 1 2 plus?") } } - func testRejectPrefixNotation() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertThrowsError(try wordyAnswer("What is plus 1 2?")) { error in - XCTAssertEqual(error as? WordyError, .syntaxError) + @Test("reject prefix notation", .enabled(if: RUNALL)) + func testRejectPrefixNotation() { + #expect(throws: WordyError.syntaxError) { + try wordyAnswer("What is plus 1 2?") } } }