Skip to content

Commit

Permalink
Update Swift tools version to 6.0 and refactor test cases to use new …
Browse files Browse the repository at this point in the history
…testing framework
  • Loading branch information
meatball133 committed Dec 20, 2024
1 parent 30f8e4c commit 95c5e5b
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 243 deletions.
16 changes: 9 additions & 7 deletions exercises/practice/pascals-triangle/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -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(pascalsTriangle(rows: {{case.input | extractCountKey}}), {{case.expected}})
func test{{case.description |camelCase }}() {
#expect(pascalsTriangle(rows: {{case.input | extractCountKey}}) == {{case.expected}})
}
{% endfor -%}
}
2 changes: 1 addition & 1 deletion exercises/practice/pascals-triangle/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
import XCTest
import Foundation
import Testing

@testable import PascalsTriangle

class PascalsTriangleTests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct PascalsTriangleTests {

@Test("zero rows")
func testZeroRows() {
XCTAssertEqual(pascalsTriangle(rows: 0), [])
#expect(pascalsTriangle(rows: 0) == [])
}

func testSingleRow() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(pascalsTriangle(rows: 1), [[1]])
@Test("single row", .enabled(if: RUNALL))
func testSingleRow() {
#expect(pascalsTriangle(rows: 1) == [[1]])
}

func testTwoRows() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(pascalsTriangle(rows: 2), [[1], [1, 1]])
@Test("two rows", .enabled(if: RUNALL))
func testTwoRows() {
#expect(pascalsTriangle(rows: 2) == [[1], [1, 1]])
}

func testThreeRows() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(pascalsTriangle(rows: 3), [[1], [1, 1], [1, 2, 1]])
@Test("three rows", .enabled(if: RUNALL))
func testThreeRows() {
#expect(pascalsTriangle(rows: 3) == [[1], [1, 1], [1, 2, 1]])
}

func testFourRows() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(pascalsTriangle(rows: 4), [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]])
@Test("four rows", .enabled(if: RUNALL))
func testFourRows() {
#expect(pascalsTriangle(rows: 4) == [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]])
}

func testFiveRows() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
pascalsTriangle(rows: 5), [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]])
@Test("five rows", .enabled(if: RUNALL))
func testFiveRows() {
#expect(
pascalsTriangle(rows: 5) == [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]])
}

func testSixRows() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
pascalsTriangle(rows: 6),
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]])
@Test("six rows", .enabled(if: RUNALL))
func testSixRows() {
#expect(
pascalsTriangle(rows: 6) == [
[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1],
])
}

func testTenRows() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
pascalsTriangle(rows: 10),
[
@Test("ten rows", .enabled(if: RUNALL))
func testTenRows() {
#expect(
pascalsTriangle(rows: 10) == [
[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1],
Expand Down
20 changes: 11 additions & 9 deletions exercises/practice/perfect-numbers/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,21 +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 {
{% outer: for case in cases %}
{%- for subCases in case.cases %}
{%- 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 }}() {
{%- ifnot subCases.expected.error %}
XCTAssertEqual(try! classify(number: {{subCases.input.number}}), .{{subCases.expected}})
#expect(try! classify(number: {{subCases.input.number}}) == .{{subCases.expected}})
{%- else %}
XCTAssertThrowsError(try classify(number: {{subCases.input.number}})) { error in
XCTAssertEqual(error as? ClassificationError, .invalidInput)
#expect(throws: ClassificationError.invalidInput) {
try classify(number: {{subCases.input.number}})
}
{%- endif %}
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/perfect-numbers/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,78 @@
import XCTest
import Foundation
import Testing

@testable import PerfectNumbers

class PerfectNumbersTests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct PerfectNumbersTests {

@Test("Smallest perfect number is classified correctly")
func testSmallestPerfectNumberIsClassifiedCorrectly() {
XCTAssertEqual(try! classify(number: 6), .perfect)
#expect(try! classify(number: 6) == .perfect)
}

func testMediumPerfectNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 28), .perfect)
@Test("Medium perfect number is classified correctly", .enabled(if: RUNALL))
func testMediumPerfectNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 28) == .perfect)
}

func testLargePerfectNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 33_550_336), .perfect)
@Test("Large perfect number is classified correctly", .enabled(if: RUNALL))
func testLargePerfectNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 33_550_336) == .perfect)
}

func testSmallestAbundantNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 12), .abundant)
@Test("Smallest abundant number is classified correctly", .enabled(if: RUNALL))
func testSmallestAbundantNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 12) == .abundant)
}

func testMediumAbundantNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 30), .abundant)
@Test("Medium abundant number is classified correctly", .enabled(if: RUNALL))
func testMediumAbundantNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 30) == .abundant)
}

func testLargeAbundantNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 33_550_335), .abundant)
@Test("Large abundant number is classified correctly", .enabled(if: RUNALL))
func testLargeAbundantNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 33_550_335) == .abundant)
}

func testSmallestPrimeDeficientNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 2), .deficient)
@Test("Smallest prime deficient number is classified correctly", .enabled(if: RUNALL))
func testSmallestPrimeDeficientNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 2) == .deficient)
}

func testSmallestNonPrimeDeficientNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 4), .deficient)
@Test("Smallest non-prime deficient number is classified correctly", .enabled(if: RUNALL))
func testSmallestNonPrimeDeficientNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 4) == .deficient)
}

func testMediumDeficientNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 32), .deficient)
@Test("Medium deficient number is classified correctly", .enabled(if: RUNALL))
func testMediumDeficientNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 32) == .deficient)
}

func testLargeDeficientNumberIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 33_550_337), .deficient)
@Test("Large deficient number is classified correctly", .enabled(if: RUNALL))
func testLargeDeficientNumberIsClassifiedCorrectly() {
#expect(try! classify(number: 33_550_337) == .deficient)
}

func testEdgeCaseNoFactorsOtherThanItselfIsClassifiedCorrectly() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! classify(number: 1), .deficient)
@Test("Edge case (no factors other than itself) is classified correctly", .enabled(if: RUNALL))
func testEdgeCaseNoFactorsOtherThanItselfIsClassifiedCorrectly() {
#expect(try! classify(number: 1) == .deficient)
}

func testZeroIsRejectedAsItIsNotAPositiveInteger() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try classify(number: 0)) { error in
XCTAssertEqual(error as? ClassificationError, .invalidInput)
@Test("Zero is rejected (as it is not a positive integer)", .enabled(if: RUNALL))
func testZeroIsRejectedAsItIsNotAPositiveInteger() {
#expect(throws: ClassificationError.invalidInput) {
try classify(number: 0)
}
}

func testNegativeIntegerIsRejectedAsItIsNotAPositiveInteger() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try classify(number: -1)) { error in
XCTAssertEqual(error as? ClassificationError, .invalidInput)
@Test("Negative integer is rejected (as it is not a positive integer)", .enabled(if: RUNALL))
func testNegativeIntegerIsRejectedAsItIsNotAPositiveInteger() {
#expect(throws: ClassificationError.invalidInput) {
try classify(number: -1)
}
}
}
20 changes: 11 additions & 9 deletions exercises/practice/phone-number/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,21 +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 }}() {
@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 PhoneNumber("{{case.input.phrase}}").clean()) {
XCTAssertEqual($0 as? PhoneNumberError, .invalidPhoneNumber)
#expect(throws: PhoneNumberError.invalidPhoneNumber) {
try PhoneNumber("{{case.input.phrase}}").clean()
}
{% else -%}
XCTAssertEqual(try! PhoneNumber("{{case.input.phrase}}").clean(), "{{case.expected}}")
#expect(try! PhoneNumber("{{case.input.phrase}}").clean() == "{{case.expected}}")
{% endif -%}
}
{% endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/phone-number/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand Down
Loading

0 comments on commit 95c5e5b

Please sign in to comment.