Skip to content

Commit

Permalink
Update test framework to use new Testing module and Swift tools version
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 committed Dec 17, 2024
1 parent 98b42de commit 9b2cf81
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 143 deletions.
16 changes: 9 additions & 7 deletions exercises/practice/acronym/.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("{{case.expected}}", Acronym.abbreviate("{{case.input.phrase}}"))
func test{{case.description |camelCase }}() {
#expect("{{case.expected}}" == Acronym.abbreviate("{{case.input.phrase}}"))
}
{% endfor -%}
}
2 changes: 1 addition & 1 deletion exercises/practice/acronym/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
65 changes: 34 additions & 31 deletions exercises/practice/acronym/Tests/AcronymTests/AcronymTests.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
import XCTest
import Foundation
import Testing

@testable import Acronym

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

@Suite struct AcronymTests {

@Test("basic")
func testBasic() {
XCTAssertEqual("PNG", Acronym.abbreviate("Portable Network Graphics"))
#expect("PNG" == Acronym.abbreviate("Portable Network Graphics"))
}

func testLowercaseWords() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("ROR", Acronym.abbreviate("Ruby on Rails"))
@Test("lowercase words", .enabled(if: RUNALL))
func testLowercaseWords() {
#expect("ROR" == Acronym.abbreviate("Ruby on Rails"))
}

func testPunctuation() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("FIFO", Acronym.abbreviate("First In, First Out"))
@Test("punctuation", .enabled(if: RUNALL))
func testPunctuation() {
#expect("FIFO" == Acronym.abbreviate("First In, First Out"))
}

func testAllCapsWord() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("GIMP", Acronym.abbreviate("GNU Image Manipulation Program"))
@Test("all caps word", .enabled(if: RUNALL))
func testAllCapsWord() {
#expect("GIMP" == Acronym.abbreviate("GNU Image Manipulation Program"))
}

func testPunctuationWithoutWhitespace() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("CMOS", Acronym.abbreviate("Complementary metal-oxide semiconductor"))
@Test("punctuation without whitespace", .enabled(if: RUNALL))
func testPunctuationWithoutWhitespace() {
#expect("CMOS" == Acronym.abbreviate("Complementary metal-oxide semiconductor"))
}

func testVeryLongAbbreviation() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
"ROTFLSHTMDCOALM",
Acronym.abbreviate(
"Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"))
@Test("very long abbreviation", .enabled(if: RUNALL))
func testVeryLongAbbreviation() {
#expect(
"ROTFLSHTMDCOALM"
== Acronym.abbreviate(
"Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"))
}

func testConsecutiveDelimiters() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("SIMUFTA", Acronym.abbreviate("Something - I made up from thin air"))
@Test("consecutive delimiters", .enabled(if: RUNALL))
func testConsecutiveDelimiters() {
#expect("SIMUFTA" == Acronym.abbreviate("Something - I made up from thin air"))
}

func testApostrophes() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("HC", Acronym.abbreviate("Halley's Comet"))
@Test("apostrophes", .enabled(if: RUNALL))
func testApostrophes() {
#expect("HC" == Acronym.abbreviate("Halley's Comet"))
}

func testUnderscoreEmphasis() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("TRNT", Acronym.abbreviate("The Road _Not_ Taken"))
@Test("underscore emphasis", .enabled(if: RUNALL))
func testUnderscoreEmphasis() {
#expect("TRNT" == Acronym.abbreviate("The Road _Not_ Taken"))
}
}
28 changes: 15 additions & 13 deletions exercises/practice/all-your-base/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
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 Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}})) { error in
{%- if case.input.inputBase < 2 %}
XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase)
#expect(throws: BaseError.invalidInputBase)
{%- elif case.input.outputBase < 2 %}
XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase)
#expect(throws: BaseError.invalidOutputBase)
{%- elif case.input.digits | any:"isNegative" %}
XCTAssertEqual(error as? BaseError, BaseError.negativeDigit)
#expect(throws: BaseError.negativeDigit)
{%- else %}
XCTAssertEqual(error as? BaseError, BaseError.invalidPositiveDigit)
#expect(throws: BaseError.invalidPositiveDigit)
{%- endif -%}
}
{try Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}})}
{%- else -%}
XCTAssertEqual(try! Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}}), {{case.expected}})
#expect(try! Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}}) == {{case.expected}})
{%- endif -%}
}
{% endfor -%}
Expand Down
Loading

0 comments on commit 9b2cf81

Please sign in to comment.