Skip to content

Commit 9c716af

Browse files
authored
Update Swift tools version to 6.0 and improve test templates across exercises (#797)
1 parent 37ea13e commit 9c716af

File tree

17 files changed

+251
-237
lines changed

17 files changed

+251
-237
lines changed
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import XCTest
1+
import Testing
2+
import Foundation
23
@testable import {{exercise|camelCase}}
3-
class {{exercise|camelCase}}Tests: XCTestCase {
4-
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
54

5+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
6+
7+
@Suite struct {{exercise|camelCase}}Tests {
68
{% for case in cases %}
79
{% if forloop.first -%}
8-
func test{{case.description |camelCase }}() {
10+
@Test("{{case.description}}")
911
{% else -%}
10-
func test{{case.description |camelCase }}() throws {
11-
try XCTSkipIf(true && !runAll) // change true to false to run this test
12+
@Test("{{case.description}}", .enabled(if: RUNALL))
1213
{% endif -%}
13-
XCTAssertEqual(primeFactors({{case.input.value}}), {{case.expected}})
14+
func test{{case.description |camelCase }}() {
15+
#expect(primeFactors({{case.input.value}}) == {{case.expected}})
1416
}
1517
{% endfor -%}
1618
}

exercises/practice/prime-factors/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:6.0
22

33
import PackageDescription
44

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,69 @@
1-
import XCTest
1+
import Foundation
2+
import Testing
23

34
@testable import PrimeFactors
45

5-
class PrimeFactorsTests: XCTestCase {
6-
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
6+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
77

8+
@Suite struct PrimeFactorsTests {
9+
10+
@Test("no factors")
811
func testNoFactors() {
9-
XCTAssertEqual(primeFactors(1), [])
12+
#expect(primeFactors(1) == [])
1013
}
1114

12-
func testPrimeNumber() throws {
13-
try XCTSkipIf(true && !runAll) // change true to false to run this test
14-
XCTAssertEqual(primeFactors(2), [2])
15+
@Test("prime number", .enabled(if: RUNALL))
16+
func testPrimeNumber() {
17+
#expect(primeFactors(2) == [2])
1518
}
1619

17-
func testAnotherPrimeNumber() throws {
18-
try XCTSkipIf(true && !runAll) // change true to false to run this test
19-
XCTAssertEqual(primeFactors(3), [3])
20+
@Test("another prime number", .enabled(if: RUNALL))
21+
func testAnotherPrimeNumber() {
22+
#expect(primeFactors(3) == [3])
2023
}
2124

22-
func testSquareOfAPrime() throws {
23-
try XCTSkipIf(true && !runAll) // change true to false to run this test
24-
XCTAssertEqual(primeFactors(9), [3, 3])
25+
@Test("square of a prime", .enabled(if: RUNALL))
26+
func testSquareOfAPrime() {
27+
#expect(primeFactors(9) == [3, 3])
2528
}
2629

27-
func testProductOfFirstPrime() throws {
28-
try XCTSkipIf(true && !runAll) // change true to false to run this test
29-
XCTAssertEqual(primeFactors(4), [2, 2])
30+
@Test("product of first prime", .enabled(if: RUNALL))
31+
func testProductOfFirstPrime() {
32+
#expect(primeFactors(4) == [2, 2])
3033
}
3134

32-
func testCubeOfAPrime() throws {
33-
try XCTSkipIf(true && !runAll) // change true to false to run this test
34-
XCTAssertEqual(primeFactors(8), [2, 2, 2])
35+
@Test("cube of a prime", .enabled(if: RUNALL))
36+
func testCubeOfAPrime() {
37+
#expect(primeFactors(8) == [2, 2, 2])
3538
}
3639

37-
func testProductOfSecondPrime() throws {
38-
try XCTSkipIf(true && !runAll) // change true to false to run this test
39-
XCTAssertEqual(primeFactors(27), [3, 3, 3])
40+
@Test("product of second prime", .enabled(if: RUNALL))
41+
func testProductOfSecondPrime() {
42+
#expect(primeFactors(27) == [3, 3, 3])
4043
}
4144

42-
func testProductOfThirdPrime() throws {
43-
try XCTSkipIf(true && !runAll) // change true to false to run this test
44-
XCTAssertEqual(primeFactors(625), [5, 5, 5, 5])
45+
@Test("product of third prime", .enabled(if: RUNALL))
46+
func testProductOfThirdPrime() {
47+
#expect(primeFactors(625) == [5, 5, 5, 5])
4548
}
4649

47-
func testProductOfFirstAndSecondPrime() throws {
48-
try XCTSkipIf(true && !runAll) // change true to false to run this test
49-
XCTAssertEqual(primeFactors(6), [2, 3])
50+
@Test("product of first and second prime", .enabled(if: RUNALL))
51+
func testProductOfFirstAndSecondPrime() {
52+
#expect(primeFactors(6) == [2, 3])
5053
}
5154

52-
func testProductOfPrimesAndNonPrimes() throws {
53-
try XCTSkipIf(true && !runAll) // change true to false to run this test
54-
XCTAssertEqual(primeFactors(12), [2, 2, 3])
55+
@Test("product of primes and non-primes", .enabled(if: RUNALL))
56+
func testProductOfPrimesAndNonPrimes() {
57+
#expect(primeFactors(12) == [2, 2, 3])
5558
}
5659

57-
func testProductOfPrimes() throws {
58-
try XCTSkipIf(true && !runAll) // change true to false to run this test
59-
XCTAssertEqual(primeFactors(901255), [5, 17, 23, 461])
60+
@Test("product of primes", .enabled(if: RUNALL))
61+
func testProductOfPrimes() {
62+
#expect(primeFactors(901255) == [5, 17, 23, 461])
6063
}
6164

62-
func testFactorsIncludeALargePrime() throws {
63-
try XCTSkipIf(true && !runAll) // change true to false to run this test
64-
XCTAssertEqual(primeFactors(93_819_012_551), [11, 9539, 894119])
65+
@Test("factors include a large prime", .enabled(if: RUNALL))
66+
func testFactorsIncludeALargePrime() {
67+
#expect(primeFactors(93_819_012_551) == [11, 9539, 894119])
6568
}
6669
}

exercises/practice/protein-translation/.docs/instructions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
Translate RNA sequences into proteins.
44

5-
RNA can be broken into three nucleotide sequences called codons, and then translated to a polypeptide like so:
5+
RNA can be broken into three-nucleotide sequences called codons, and then translated to a protein like so:
66

77
RNA: `"AUGUUUUCU"` => translates to
88

99
Codons: `"AUG", "UUU", "UCU"`
10-
=> which become a polypeptide with the following sequence =>
10+
=> which become a protein with the following sequence =>
1111

1212
Protein: `"Methionine", "Phenylalanine", "Serine"`
1313

@@ -27,9 +27,9 @@ Protein: `"Methionine", "Phenylalanine", "Serine"`
2727

2828
Note the stop codon `"UAA"` terminates the translation and the final methionine is not translated into the protein sequence.
2929

30-
Below are the codons and resulting Amino Acids needed for the exercise.
30+
Below are the codons and resulting amino acids needed for the exercise.
3131

32-
| Codon | Protein |
32+
| Codon | Amino Acid |
3333
| :----------------- | :------------ |
3434
| AUG | Methionine |
3535
| UUU, UUC | Phenylalanine |

exercises/practice/protein-translation/.meta/template.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import XCTest
1+
import Testing
2+
import Foundation
23
@testable import {{exercise|camelCase}}
3-
class {{exercise|camelCase}}Tests: XCTestCase {
4-
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
54

5+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
6+
7+
@Suite struct {{exercise|camelCase}}Tests {
68
{% for case in cases %}
79
{% if forloop.first -%}
8-
func test{{case.description |camelCase }}() {
10+
@Test("{{case.description}}")
911
{% else -%}
10-
func test{{case.description |camelCase }}() throws {
11-
try XCTSkipIf(true && !runAll) // change true to false to run this test
12+
@Test("{{case.description}}", .enabled(if: RUNALL))
1213
{% endif -%}
14+
func test{{case.description |camelCase }}() {
1315
{%- ifnot case.expected.error -%}
14-
XCTAssertEqual({{case.expected | toStringArray}}, try! translationOfRNA(rna: "{{case.input.strand}}"))
16+
#expect(try! translationOfRNA(rna: "{{case.input.strand}}") == {{case.expected | toStringArray}})
1517
{%- else -%}
16-
XCTAssertThrowsError(try translationOfRNA(rna: "{{case.input.strand}}")) { error in
17-
XCTAssertEqual(error as? TranslationError, TranslationError.invalidCodon)
18+
#expect(throws: TranslationError.invalidCodon) {
19+
try translationOfRNA(rna: "{{case.input.strand}}")
1820
}
1921
{%- endif -%}
2022
}

exercises/practice/protein-translation/.meta/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,15 @@ description = "Translation stops if STOP codon in middle of three-codon sequence
8787
[2c2a2a60-401f-4a80-b977-e0715b23b93d]
8888
description = "Translation stops if STOP codon in middle of six-codon sequence"
8989

90+
[f6f92714-769f-4187-9524-e353e8a41a80]
91+
description = "Sequence of two non-STOP codons does not translate to a STOP codon"
92+
9093
[1e75ea2a-f907-4994-ae5c-118632a1cb0f]
9194
description = "Non-existing codon can't translate"
9295

9396
[9eac93f3-627a-4c90-8653-6d0a0595bc6f]
9497
description = "Unknown amino acids, not part of a codon, can't translate"
98+
reimplements = "1e75ea2a-f907-4994-ae5c-118632a1cb0f"
9599

96100
[9d73899f-e68e-4291-b1e2-7bf87c00f024]
97101
description = "Incomplete RNA sequence can't translate"

exercises/practice/protein-translation/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:6.0
22

33
import PackageDescription
44

0 commit comments

Comments
 (0)