From 4a3a25ce4b32743128c9c2ad370d4c5e6df3b9bc Mon Sep 17 00:00:00 2001 From: meatball Date: Fri, 20 Dec 2024 20:59:09 +0100 Subject: [PATCH 1/2] Update Swift tools version to 6.0 and refactor test cases to use new testing framework --- .../isbn-verifier/.meta/template.swift | 18 +-- .../practice/isbn-verifier/Package.swift | 2 +- .../IsbnVerifierTests/IsbnVerifierTests.swift | 121 +++++++----------- .../practice/isogram/.meta/template.swift | 18 +-- exercises/practice/isogram/Package.swift | 2 +- .../Tests/IsogramTests/IsogramTests.swift | 89 ++++++------- .../kindergarten-garden/.meta/template.swift | 22 ++-- .../kindergarten-garden/Package.swift | 2 +- .../KindergartenGardenTests.swift | 107 ++++++++-------- .../practice/knapsack/.meta/template.swift | 37 +++--- exercises/practice/knapsack/Package.swift | 2 +- .../Tests/KnapsackTests/KnapsackTests.swift | 54 ++++---- 12 files changed, 229 insertions(+), 245 deletions(-) diff --git a/exercises/practice/isbn-verifier/.meta/template.swift b/exercises/practice/isbn-verifier/.meta/template.swift index 095e8c118..a104812d6 100644 --- a/exercises/practice/isbn-verifier/.meta/template.swift +++ b/exercises/practice/isbn-verifier/.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.expected -%} - XCTAssertTrue(IsbnVerifier.isValid("{{case.input.isbn}}")) + #expect(IsbnVerifier.isValid("{{case.input.isbn}}")) {%- else -%} - XCTAssertFalse(IsbnVerifier.isValid("{{case.input.isbn}}")) + #expect(!IsbnVerifier.isValid("{{case.input.isbn}}")) {%- endif -%} } {% endfor -%} diff --git a/exercises/practice/isbn-verifier/Package.swift b/exercises/practice/isbn-verifier/Package.swift index bc13ad6b0..674603ab7 100644 --- a/exercises/practice/isbn-verifier/Package.swift +++ b/exercises/practice/isbn-verifier/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/isbn-verifier/Tests/IsbnVerifierTests/IsbnVerifierTests.swift b/exercises/practice/isbn-verifier/Tests/IsbnVerifierTests/IsbnVerifierTests.swift index 2c9689b54..2048f65c1 100644 --- a/exercises/practice/isbn-verifier/Tests/IsbnVerifierTests/IsbnVerifierTests.swift +++ b/exercises/practice/isbn-verifier/Tests/IsbnVerifierTests/IsbnVerifierTests.swift @@ -1,101 +1,76 @@ -import XCTest +import Foundation +import Testing @testable import IsbnVerifier -class IsbnVerifierTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false - func testValidIsbn() { - XCTAssertTrue(IsbnVerifier.isValid("3-598-21508-8")) - } +@Suite struct IsbnVerifierTests { - func testInvalidIsbnCheckDigit() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3-598-21508-9")) - } + @Test("valid isbn") + func testValidIsbn() { #expect(IsbnVerifier.isValid("3-598-21508-8")) } - func testValidIsbnWithACheckDigitOf10() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(IsbnVerifier.isValid("3-598-21507-X")) - } + @Test("invalid isbn check digit", .enabled(if: RUNALL)) + func testInvalidIsbnCheckDigit() { #expect(!IsbnVerifier.isValid("3-598-21508-9")) } - func testCheckDigitIsACharacterOtherThanX() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3-598-21507-A")) - } + @Test("valid isbn with a check digit of 10", .enabled(if: RUNALL)) + func testValidIsbnWithACheckDigitOf10() { #expect(IsbnVerifier.isValid("3-598-21507-X")) } - func testInvalidCheckDigitInIsbnIsNotTreatedAsZero() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("4-598-21507-B")) - } + @Test("check digit is a character other than X", .enabled(if: RUNALL)) + func testCheckDigitIsACharacterOtherThanX() { #expect(!IsbnVerifier.isValid("3-598-21507-A")) } - func testInvalidCharacterInIsbnIsNotTreatedAsZero() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3-598-P1581-X")) + @Test("invalid check digit in isbn is not treated as zero", .enabled(if: RUNALL)) + func testInvalidCheckDigitInIsbnIsNotTreatedAsZero() { + #expect(!IsbnVerifier.isValid("4-598-21507-B")) } - func testXIsOnlyValidAsACheckDigit() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3-598-2X507-9")) + @Test("invalid character in isbn is not treated as zero", .enabled(if: RUNALL)) + func testInvalidCharacterInIsbnIsNotTreatedAsZero() { + #expect(!IsbnVerifier.isValid("3-598-P1581-X")) } - func testValidIsbnWithoutSeparatingDashes() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(IsbnVerifier.isValid("3598215088")) - } + @Test("X is only valid as a check digit", .enabled(if: RUNALL)) + func testXIsOnlyValidAsACheckDigit() { #expect(!IsbnVerifier.isValid("3-598-2X507-9")) } - func testIsbnWithoutSeparatingDashesAndXAsCheckDigit() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(IsbnVerifier.isValid("359821507X")) - } + @Test("valid isbn without separating dashes", .enabled(if: RUNALL)) + func testValidIsbnWithoutSeparatingDashes() { #expect(IsbnVerifier.isValid("3598215088")) } - func testIsbnWithoutCheckDigitAndDashes() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("359821507")) + @Test("isbn without separating dashes and X as check digit", .enabled(if: RUNALL)) + func testIsbnWithoutSeparatingDashesAndXAsCheckDigit() { + #expect(IsbnVerifier.isValid("359821507X")) } - func testTooLongIsbnAndNoDashes() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3598215078X")) - } + @Test("isbn without check digit and dashes", .enabled(if: RUNALL)) + func testIsbnWithoutCheckDigitAndDashes() { #expect(!IsbnVerifier.isValid("359821507")) } - func testTooShortIsbn() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("00")) - } + @Test("too long isbn and no dashes", .enabled(if: RUNALL)) + func testTooLongIsbnAndNoDashes() { #expect(!IsbnVerifier.isValid("3598215078X")) } - func testIsbnWithoutCheckDigit() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3-598-21507")) - } + @Test("too short isbn", .enabled(if: RUNALL)) + func testTooShortIsbn() { #expect(!IsbnVerifier.isValid("00")) } - func testCheckDigitOfXShouldNotBeUsedFor0() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3-598-21515-X")) - } + @Test("isbn without check digit", .enabled(if: RUNALL)) + func testIsbnWithoutCheckDigit() { #expect(!IsbnVerifier.isValid("3-598-21507")) } - func testEmptyIsbn() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("")) - } + @Test("check digit of X should not be used for 0", .enabled(if: RUNALL)) + func testCheckDigitOfXShouldNotBeUsedFor0() { #expect(!IsbnVerifier.isValid("3-598-21515-X")) } - func testInputIs9Characters() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("134456729")) - } + @Test("empty isbn", .enabled(if: RUNALL)) + func testEmptyIsbn() { #expect(!IsbnVerifier.isValid("")) } - func testInvalidCharactersAreNotIgnoredAfterCheckingLength() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3132P34035")) - } + @Test("input is 9 characters", .enabled(if: RUNALL)) + func testInputIs9Characters() { #expect(!IsbnVerifier.isValid("134456729")) } - func testInvalidCharactersAreNotIgnoredBeforeCheckingLength() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("3598P215088")) + @Test("invalid characters are not ignored after checking length", .enabled(if: RUNALL)) + func testInvalidCharactersAreNotIgnoredAfterCheckingLength() { + #expect(!IsbnVerifier.isValid("3132P34035")) } - func testInputIsTooLongButContainsAValidIsbn() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(IsbnVerifier.isValid("98245726788")) + @Test("invalid characters are not ignored before checking length", .enabled(if: RUNALL)) + func testInvalidCharactersAreNotIgnoredBeforeCheckingLength() { + #expect(!IsbnVerifier.isValid("3598P215088")) } + + @Test("input is too long but contains a valid isbn", .enabled(if: RUNALL)) + func testInputIsTooLongButContainsAValidIsbn() { #expect(!IsbnVerifier.isValid("98245726788")) } } diff --git a/exercises/practice/isogram/.meta/template.swift b/exercises/practice/isogram/.meta/template.swift index a5e3303d9..f49ccd0cb 100644 --- a/exercises/practice/isogram/.meta/template.swift +++ b/exercises/practice/isogram/.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.expected -%} - XCTAssertTrue(isIsogram("{{case.input.phrase}}")) + #expect(isIsogram("{{case.input.phrase}}")) {%- else -%} - XCTAssertFalse(isIsogram("{{case.input.phrase}}")) + #expect(!isIsogram("{{case.input.phrase}}")) {%- endif %} } {% endfor -%} diff --git a/exercises/practice/isogram/Package.swift b/exercises/practice/isogram/Package.swift index feda1f7cd..81655da0d 100644 --- a/exercises/practice/isogram/Package.swift +++ b/exercises/practice/isogram/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/isogram/Tests/IsogramTests/IsogramTests.swift b/exercises/practice/isogram/Tests/IsogramTests/IsogramTests.swift index e07be154c..82eaf194e 100644 --- a/exercises/practice/isogram/Tests/IsogramTests/IsogramTests.swift +++ b/exercises/practice/isogram/Tests/IsogramTests/IsogramTests.swift @@ -1,76 +1,79 @@ -import XCTest +import Foundation +import Testing @testable import Isogram -class IsogramTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +@Suite struct IsogramTests { + + @Test("empty string") func testEmptyString() { - XCTAssertTrue(isIsogram("")) + #expect(isIsogram("")) } - func testIsogramWithOnlyLowerCaseCharacters() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isIsogram("isogram")) + @Test("isogram with only lower case characters", .enabled(if: RUNALL)) + func testIsogramWithOnlyLowerCaseCharacters() { + #expect(isIsogram("isogram")) } - func testWordWithOneDuplicatedCharacter() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isIsogram("eleven")) + @Test("word with one duplicated character", .enabled(if: RUNALL)) + func testWordWithOneDuplicatedCharacter() { + #expect(!isIsogram("eleven")) } - func testWordWithOneDuplicatedCharacterFromTheEndOfTheAlphabet() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isIsogram("zzyzx")) + @Test("word with one duplicated character from the end of the alphabet", .enabled(if: RUNALL)) + func testWordWithOneDuplicatedCharacterFromTheEndOfTheAlphabet() { + #expect(!isIsogram("zzyzx")) } - func testLongestReportedEnglishIsogram() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isIsogram("subdermatoglyphic")) + @Test("longest reported english isogram", .enabled(if: RUNALL)) + func testLongestReportedEnglishIsogram() { + #expect(isIsogram("subdermatoglyphic")) } - func testWordWithDuplicatedCharacterInMixedCase() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isIsogram("Alphabet")) + @Test("word with duplicated character in mixed case", .enabled(if: RUNALL)) + func testWordWithDuplicatedCharacterInMixedCase() { + #expect(!isIsogram("Alphabet")) } - func testWordWithDuplicatedCharacterInMixedCaseLowercaseFirst() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isIsogram("alphAbet")) + @Test("word with duplicated character in mixed case, lowercase first", .enabled(if: RUNALL)) + func testWordWithDuplicatedCharacterInMixedCaseLowercaseFirst() { + #expect(!isIsogram("alphAbet")) } - func testHypotheticalIsogrammicWordWithHyphen() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isIsogram("thumbscrew-japingly")) + @Test("hypothetical isogrammic word with hyphen", .enabled(if: RUNALL)) + func testHypotheticalIsogrammicWordWithHyphen() { + #expect(isIsogram("thumbscrew-japingly")) } - func testHypotheticalWordWithDuplicatedCharacterFollowingHyphen() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isIsogram("thumbscrew-jappingly")) + @Test("hypothetical word with duplicated character following hyphen", .enabled(if: RUNALL)) + func testHypotheticalWordWithDuplicatedCharacterFollowingHyphen() { + #expect(!isIsogram("thumbscrew-jappingly")) } - func testIsogramWithDuplicatedHyphen() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isIsogram("six-year-old")) + @Test("isogram with duplicated hyphen", .enabled(if: RUNALL)) + func testIsogramWithDuplicatedHyphen() { + #expect(isIsogram("six-year-old")) } - func testMadeUpNameThatIsAnIsogram() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isIsogram("Emily Jung Schwartzkopf")) + @Test("made-up name that is an isogram", .enabled(if: RUNALL)) + func testMadeUpNameThatIsAnIsogram() { + #expect(isIsogram("Emily Jung Schwartzkopf")) } - func testDuplicatedCharacterInTheMiddle() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isIsogram("accentor")) + @Test("duplicated character in the middle", .enabled(if: RUNALL)) + func testDuplicatedCharacterInTheMiddle() { + #expect(!isIsogram("accentor")) } - func testSameFirstAndLastCharacters() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isIsogram("angola")) + @Test("same first and last characters", .enabled(if: RUNALL)) + func testSameFirstAndLastCharacters() { + #expect(!isIsogram("angola")) } - func testWordWithDuplicatedCharacterAndWithTwoHyphens() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isIsogram("up-to-date")) + @Test("word with duplicated character and with two hyphens", .enabled(if: RUNALL)) + func testWordWithDuplicatedCharacterAndWithTwoHyphens() { + #expect(!isIsogram("up-to-date")) } } diff --git a/exercises/practice/kindergarten-garden/.meta/template.swift b/exercises/practice/kindergarten-garden/.meta/template.swift index 65917e730..6305849c6 100644 --- a/exercises/practice/kindergarten-garden/.meta/template.swift +++ b/exercises/practice/kindergarten-garden/.meta/template.swift @@ -1,27 +1,29 @@ -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 { {% outer: for case in cases %} {%- for subCases in case.cases %} {%- if subCases.cases %} {%- for subSubCases in subCases.cases %} - func test{{subSubCases.description |camelCase }}() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("{{subSubCases.description}}", .enabled(if: RUNALL)) + func test{{subSubCases.description |camelCase }}() { let garden = Garden("{{subSubCases.input.diagram | inspect}}") - XCTAssertEqual(garden.plantsForChild("{{subSubCases.input.student}}"), {{subSubCases.expected | toEnumArray}}) + #expect(garden.plantsForChild("{{subSubCases.input.student}}") == {{subSubCases.expected | toEnumArray}}) } {% endfor -%} {%- else %} {%- if forloop.outer.first and forloop.first %} - func test{{subCases.description |camelCase }}() { + @Test("{{subCases.description}}") {%- else %} - func test{{subCases.description |camelCase }}() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("{{subCases.description}}", .enabled(if: RUNALL)) {%- endif %} + func test{{subCases.description |camelCase }}() { let garden = Garden("{{subCases.input.diagram | inspect}}") - XCTAssertEqual(garden.plantsForChild("{{subCases.input.student}}"), {{subCases.expected | toEnumArray}}) + #expect(garden.plantsForChild("{{subCases.input.student}}") == {{subCases.expected | toEnumArray}}) } {%- endif %} {% endfor -%} diff --git a/exercises/practice/kindergarten-garden/Package.swift b/exercises/practice/kindergarten-garden/Package.swift index f6c43ec1b..4efee6b6c 100644 --- a/exercises/practice/kindergarten-garden/Package.swift +++ b/exercises/practice/kindergarten-garden/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/kindergarten-garden/Tests/KindergartenGardenTests/KindergartenGardenTests.swift b/exercises/practice/kindergarten-garden/Tests/KindergartenGardenTests/KindergartenGardenTests.swift index a3d335d90..46157c0a7 100644 --- a/exercises/practice/kindergarten-garden/Tests/KindergartenGardenTests/KindergartenGardenTests.swift +++ b/exercises/practice/kindergarten-garden/Tests/KindergartenGardenTests/KindergartenGardenTests.swift @@ -1,108 +1,111 @@ -import XCTest +import Foundation +import Testing @testable import KindergartenGarden -class KindergartenGardenTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "true"]) ?? false +@Suite struct KindergartenGardenTests { + + @Test("garden with single student") func testGardenWithSingleStudent() { let garden = Garden("RC\nGG") - XCTAssertEqual(garden.plantsForChild("Alice"), [.radishes, .clover, .grass, .grass]) + #expect(garden.plantsForChild("Alice") == [.radishes, .clover, .grass, .grass]) } - func testDifferentGardenWithSingleStudent() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("different garden with single student", .enabled(if: RUNALL)) + func testDifferentGardenWithSingleStudent() { let garden = Garden("VC\nRC") - XCTAssertEqual(garden.plantsForChild("Alice"), [.violets, .clover, .radishes, .clover]) + #expect(garden.plantsForChild("Alice") == [.violets, .clover, .radishes, .clover]) } - func testGardenWithTwoStudents() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("garden with two students", .enabled(if: RUNALL)) + func testGardenWithTwoStudents() { let garden = Garden("VVCG\nVVRC") - XCTAssertEqual(garden.plantsForChild("Bob"), [.clover, .grass, .radishes, .clover]) + #expect(garden.plantsForChild("Bob") == [.clover, .grass, .radishes, .clover]) } - func testSecondStudentsGarden() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("second student's garden", .enabled(if: RUNALL)) + func testSecondStudentsGarden() { let garden = Garden("VVCCGG\nVVCCGG") - XCTAssertEqual(garden.plantsForChild("Bob"), [.clover, .clover, .clover, .clover]) + #expect(garden.plantsForChild("Bob") == [.clover, .clover, .clover, .clover]) } - func testThirdStudentsGarden() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("third student's garden", .enabled(if: RUNALL)) + func testThirdStudentsGarden() { let garden = Garden("VVCCGG\nVVCCGG") - XCTAssertEqual(garden.plantsForChild("Charlie"), [.grass, .grass, .grass, .grass]) + #expect(garden.plantsForChild("Charlie") == [.grass, .grass, .grass, .grass]) } - func testForAliceFirstStudentsGarden() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Alice, first student's garden", .enabled(if: RUNALL)) + func testForAliceFirstStudentsGarden() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Alice"), [.violets, .radishes, .violets, .radishes]) + #expect(garden.plantsForChild("Alice") == [.violets, .radishes, .violets, .radishes]) } - func testForBobSecondStudentsGarden() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Bob, second student's garden", .enabled(if: RUNALL)) + func testForBobSecondStudentsGarden() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Bob"), [.clover, .grass, .clover, .clover]) + #expect(garden.plantsForChild("Bob") == [.clover, .grass, .clover, .clover]) } - func testForCharlie() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Charlie", .enabled(if: RUNALL)) + func testForCharlie() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Charlie"), [.violets, .violets, .clover, .grass]) + #expect(garden.plantsForChild("Charlie") == [.violets, .violets, .clover, .grass]) } - func testForDavid() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for David", .enabled(if: RUNALL)) + func testForDavid() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("David"), [.radishes, .violets, .clover, .radishes]) + #expect(garden.plantsForChild("David") == [.radishes, .violets, .clover, .radishes]) } - func testForEve() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Eve", .enabled(if: RUNALL)) + func testForEve() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Eve"), [.clover, .grass, .radishes, .grass]) + #expect(garden.plantsForChild("Eve") == [.clover, .grass, .radishes, .grass]) } - func testForFred() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Fred", .enabled(if: RUNALL)) + func testForFred() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Fred"), [.grass, .clover, .violets, .clover]) + #expect(garden.plantsForChild("Fred") == [.grass, .clover, .violets, .clover]) } - func testForGinny() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Ginny", .enabled(if: RUNALL)) + func testForGinny() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Ginny"), [.clover, .grass, .grass, .clover]) + #expect(garden.plantsForChild("Ginny") == [.clover, .grass, .grass, .clover]) } - func testForHarriet() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Harriet", .enabled(if: RUNALL)) + func testForHarriet() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Harriet"), [.violets, .radishes, .radishes, .violets]) + #expect(garden.plantsForChild("Harriet") == [.violets, .radishes, .radishes, .violets]) } - func testForIleana() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Ileana", .enabled(if: RUNALL)) + func testForIleana() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Ileana"), [.grass, .clover, .violets, .clover]) + #expect(garden.plantsForChild("Ileana") == [.grass, .clover, .violets, .clover]) } - func testForJoseph() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Joseph", .enabled(if: RUNALL)) + func testForJoseph() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Joseph"), [.violets, .clover, .violets, .grass]) + #expect(garden.plantsForChild("Joseph") == [.violets, .clover, .violets, .grass]) } - func testForKincaidSecondToLastStudentsGarden() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Kincaid, second to last student's garden", .enabled(if: RUNALL)) + func testForKincaidSecondToLastStudentsGarden() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Kincaid"), [.grass, .clover, .clover, .grass]) + #expect(garden.plantsForChild("Kincaid") == [.grass, .clover, .clover, .grass]) } - func testForLarryLastStudentsGarden() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("for Larry, last student's garden", .enabled(if: RUNALL)) + func testForLarryLastStudentsGarden() { let garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") - XCTAssertEqual(garden.plantsForChild("Larry"), [.grass, .violets, .clover, .violets]) + #expect(garden.plantsForChild("Larry") == [.grass, .violets, .clover, .violets]) } } diff --git a/exercises/practice/knapsack/.meta/template.swift b/exercises/practice/knapsack/.meta/template.swift index c87d96e9f..f95f1a0c1 100644 --- a/exercises/practice/knapsack/.meta/template.swift +++ b/exercises/practice/knapsack/.meta/template.swift @@ -1,22 +1,23 @@ -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}}() { - {%- else %} - func test{{case.description |camelCase}}() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - {%- endif %} - - let items : [Item] = [ - {%- for item in case.input.items %} - {{item | knapsackItem}}, - {%- endfor -%} - ] - XCTAssertEqual(Knapsack.maximumValue(items, {{case.input.maximumWeight}}), {{case.expected}}) - } + {% if forloop.first -%} + @Test("{{case.description}}") + {% else -%} + @Test("{{case.description}}", .enabled(if: RUNALL)) + {% endif -%} + func test{{case.description |camelCase }}() { + let items : [Item] = [ + {%- for item in case.input.items %} + {{item | knapsackItem}}, + {%- endfor -%} + ] + #expect(Knapsack.maximumValue(items, {{case.input.maximumWeight}}) == {{case.expected}}) + } {% endfor -%} } diff --git a/exercises/practice/knapsack/Package.swift b/exercises/practice/knapsack/Package.swift index 29c95bcde..c3b135e52 100644 --- a/exercises/practice/knapsack/Package.swift +++ b/exercises/practice/knapsack/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/knapsack/Tests/KnapsackTests/KnapsackTests.swift b/exercises/practice/knapsack/Tests/KnapsackTests/KnapsackTests.swift index a64771bdc..fc699f22e 100644 --- a/exercises/practice/knapsack/Tests/KnapsackTests/KnapsackTests.swift +++ b/exercises/practice/knapsack/Tests/KnapsackTests/KnapsackTests.swift @@ -1,28 +1,28 @@ -import XCTest +import Foundation +import Testing @testable import Knapsack -class KnapsackTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false - func testNoItems() { +@Suite struct KnapsackTests { + @Test("no items") + func testNoItems() { let items: [Item] = [] - XCTAssertEqual(Knapsack.maximumValue(items, 100), 0) + #expect(Knapsack.maximumValue(items, 100) == 0) } - func testOneItemTooHeavy() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - + @Test("one item, too heavy", .enabled(if: RUNALL)) + func testOneItemTooHeavy() { let items: [Item] = [ Item(weight: 100, value: 1) ] - XCTAssertEqual(Knapsack.maximumValue(items, 10), 0) + #expect(Knapsack.maximumValue(items, 10) == 0) } - func testFiveItemsCannotBeGreedyByWeight() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - + @Test("five items (cannot be greedy by weight)", .enabled(if: RUNALL)) + func testFiveItemsCannotBeGreedyByWeight() { let items: [Item] = [ Item(weight: 2, value: 5), Item(weight: 2, value: 5), @@ -30,12 +30,11 @@ class KnapsackTests: XCTestCase { Item(weight: 2, value: 5), Item(weight: 10, value: 21), ] - XCTAssertEqual(Knapsack.maximumValue(items, 10), 21) + #expect(Knapsack.maximumValue(items, 10) == 21) } - func testFiveItemsCannotBeGreedyByValue() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - + @Test("five items (cannot be greedy by value)", .enabled(if: RUNALL)) + func testFiveItemsCannotBeGreedyByValue() { let items: [Item] = [ Item(weight: 2, value: 20), Item(weight: 2, value: 20), @@ -43,24 +42,22 @@ class KnapsackTests: XCTestCase { Item(weight: 2, value: 20), Item(weight: 10, value: 50), ] - XCTAssertEqual(Knapsack.maximumValue(items, 10), 80) + #expect(Knapsack.maximumValue(items, 10) == 80) } - func testExampleKnapsack() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - + @Test("example knapsack", .enabled(if: RUNALL)) + func testExampleKnapsack() { let items: [Item] = [ Item(weight: 5, value: 10), Item(weight: 4, value: 40), Item(weight: 6, value: 30), Item(weight: 4, value: 50), ] - XCTAssertEqual(Knapsack.maximumValue(items, 10), 90) + #expect(Knapsack.maximumValue(items, 10) == 90) } - func test8Items() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - + @Test("8 items", .enabled(if: RUNALL)) + func test8Items() { let items: [Item] = [ Item(weight: 25, value: 350), Item(weight: 35, value: 400), @@ -71,12 +68,11 @@ class KnapsackTests: XCTestCase { Item(weight: 2, value: 5), Item(weight: 2, value: 5), ] - XCTAssertEqual(Knapsack.maximumValue(items, 104), 900) + #expect(Knapsack.maximumValue(items, 104) == 900) } - func test15Items() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - + @Test("15 items", .enabled(if: RUNALL)) + func test15Items() { let items: [Item] = [ Item(weight: 70, value: 135), Item(weight: 73, value: 139), @@ -94,6 +90,6 @@ class KnapsackTests: XCTestCase { Item(weight: 118, value: 229), Item(weight: 120, value: 240), ] - XCTAssertEqual(Knapsack.maximumValue(items, 750), 1458) + #expect(Knapsack.maximumValue(items, 750) == 1458) } } From 0946ac940604ac06d52ee76b6d0e33f9b7f7e94f Mon Sep 17 00:00:00 2001 From: meatball Date: Fri, 3 Jan 2025 23:18:06 +0100 Subject: [PATCH 2/2] Re generate test file --- .../Tests/KindergartenGardenTests/KindergartenGardenTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/kindergarten-garden/Tests/KindergartenGardenTests/KindergartenGardenTests.swift b/exercises/practice/kindergarten-garden/Tests/KindergartenGardenTests/KindergartenGardenTests.swift index 46157c0a7..f0b417de8 100644 --- a/exercises/practice/kindergarten-garden/Tests/KindergartenGardenTests/KindergartenGardenTests.swift +++ b/exercises/practice/kindergarten-garden/Tests/KindergartenGardenTests/KindergartenGardenTests.swift @@ -3,7 +3,7 @@ import Testing @testable import KindergartenGarden -let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "true"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false @Suite struct KindergartenGardenTests {