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 21, 2024
1 parent 30f8e4c commit 998980b
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 162 deletions.
18 changes: 10 additions & 8 deletions exercises/practice/two-fer/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -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 -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/two-fer/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
25 changes: 11 additions & 14 deletions exercises/practice/two-fer/Tests/TwoFerTests/TwoFerTests.swift
Original file line number Diff line number Diff line change
@@ -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.") }
}
16 changes: 9 additions & 7 deletions exercises/practice/word-count/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -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 -%}
}
2 changes: 1 addition & 1 deletion exercises/practice/word-count/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,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)
}
}
20 changes: 11 additions & 9 deletions exercises/practice/wordy/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -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 -%}
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/wordy/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 998980b

Please sign in to comment.