Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift 6]: Update Exercises batch 6 #790

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions exercises/practice/flatten-array/.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 result : [Int] = flattenArray({{case.input.array | toNilArray}})
let expected : [Int] = {{case.expected}}
XCTAssertEqual(expected, result)
#expect(expected == result)
}
{% endfor -%}
}
2 changes: 1 addition & 1 deletion exercises/practice/flatten-array/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,83 +1,90 @@
import XCTest
import Foundation
import Testing

@testable import FlattenArray

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

@Suite struct FlattenArrayTests {

@Test("empty")
func testEmpty() {
let result: [Int] = flattenArray([])
let expected: [Int] = []
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testNoNesting() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("no nesting", .enabled(if: RUNALL))
func testNoNesting() {
let result: [Int] = flattenArray([0, 1, 2])
let expected: [Int] = [0, 1, 2]
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testFlattensANestedArray() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("flattens a nested array", .enabled(if: RUNALL))
func testFlattensANestedArray() {
let result: [Int] = flattenArray([[[]]])
let expected: [Int] = []
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testFlattensArrayWithJustIntegersPresent() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("flattens array with just integers present", .enabled(if: RUNALL))
func testFlattensArrayWithJustIntegersPresent() {
let result: [Int] = flattenArray([1, [2, 3, 4, 5, 6, 7], 8])
let expected: [Int] = [1, 2, 3, 4, 5, 6, 7, 8]
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func test5LevelNesting() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("5 level nesting", .enabled(if: RUNALL))
func test5LevelNesting() {
let result: [Int] = flattenArray([0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2])
let expected: [Int] = [0, 2, 2, 3, 8, 100, 4, 50, -2]
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func test6LevelNesting() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("6 level nesting", .enabled(if: RUNALL))
func test6LevelNesting() {
let result: [Int] = flattenArray([1, [2, [[3]], [4, [[5]]], 6, 7], 8])
let expected: [Int] = [1, 2, 3, 4, 5, 6, 7, 8]
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testNullValuesAreOmittedFromTheFinalResult() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("null values are omitted from the final result", .enabled(if: RUNALL))
func testNullValuesAreOmittedFromTheFinalResult() {
let result: [Int] = flattenArray([1, 2, nil])
let expected: [Int] = [1, 2]
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testConsecutiveNullValuesAtTheFrontOfTheListAreOmittedFromTheFinalResult() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test(
"consecutive null values at the front of the list are omitted from the final result",
.enabled(if: RUNALL))
func testConsecutiveNullValuesAtTheFrontOfTheListAreOmittedFromTheFinalResult() {
let result: [Int] = flattenArray([nil, nil, 3])
let expected: [Int] = [3]
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testConsecutiveNullValuesInTheMiddleOfTheListAreOmittedFromTheFinalResult() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test(
"consecutive null values in the middle of the list are omitted from the final result",
.enabled(if: RUNALL))
func testConsecutiveNullValuesInTheMiddleOfTheListAreOmittedFromTheFinalResult() {
let result: [Int] = flattenArray([1, nil, nil, 4])
let expected: [Int] = [1, 4]
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func test6LevelNestListWithNullValues() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("6 level nest list with null values", .enabled(if: RUNALL))
func test6LevelNestListWithNullValues() {
let result: [Int] = flattenArray([0, 2, [[2, 3], 8, [[100]], nil, [[nil]]], -2])
let expected: [Int] = [0, 2, 2, 3, 8, 100, -2]
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testAllValuesInNestedListAreNull() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("all values in nested list are null", .enabled(if: RUNALL))
func testAllValuesInNestedListAreNull() {
let result: [Int] = flattenArray([nil, [[[nil]]], nil, nil, [[nil, nil], nil], nil])
let expected: [Int] = []
XCTAssertEqual(expected, result)
#expect(expected == result)
}
}
16 changes: 9 additions & 7 deletions exercises/practice/food-chain/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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 expected = "{{case.expected | join:"\n" + ""}}"
XCTAssertEqual(expected, FoodChain.song(start: {{case.input.startVerse}}, end: {{case.input.endVerse}}))
#expect(expected == FoodChain.song(start: {{case.input.startVerse}}, end: {{case.input.endVerse}}))
}
{% endfor -%}
}
2 changes: 1 addition & 1 deletion exercises/practice/food-chain/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,61 +1,64 @@
import XCTest
import Foundation
import Testing

@testable import FoodChain

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

@Suite struct FoodChainTests {

@Test("fly")
func testFly() {
let expected =
"I know an old lady who swallowed a fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die."
XCTAssertEqual(expected, FoodChain.song(start: 1, end: 1))
#expect(expected == FoodChain.song(start: 1, end: 1))
}

func testSpider() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("spider", .enabled(if: RUNALL))
func testSpider() {
let expected =
"I know an old lady who swallowed a spider.\n"
+ "It wriggled and jiggled and tickled inside her.\n"
+ "She swallowed the spider to catch the fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die."
XCTAssertEqual(expected, FoodChain.song(start: 2, end: 2))
#expect(expected == FoodChain.song(start: 2, end: 2))
}

func testBird() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("bird", .enabled(if: RUNALL))
func testBird() {
let expected =
"I know an old lady who swallowed a bird.\n" + "How absurd to swallow a bird!\n"
+ "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"
+ "She swallowed the spider to catch the fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die."
XCTAssertEqual(expected, FoodChain.song(start: 3, end: 3))
#expect(expected == FoodChain.song(start: 3, end: 3))
}

func testCat() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("cat", .enabled(if: RUNALL))
func testCat() {
let expected =
"I know an old lady who swallowed a cat.\n" + "Imagine that, to swallow a cat!\n"
+ "She swallowed the cat to catch the bird.\n"
+ "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"
+ "She swallowed the spider to catch the fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die."
XCTAssertEqual(expected, FoodChain.song(start: 4, end: 4))
#expect(expected == FoodChain.song(start: 4, end: 4))
}

func testDog() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("dog", .enabled(if: RUNALL))
func testDog() {
let expected =
"I know an old lady who swallowed a dog.\n" + "What a hog, to swallow a dog!\n"
+ "She swallowed the dog to catch the cat.\n" + "She swallowed the cat to catch the bird.\n"
+ "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"
+ "She swallowed the spider to catch the fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die."
XCTAssertEqual(expected, FoodChain.song(start: 5, end: 5))
#expect(expected == FoodChain.song(start: 5, end: 5))
}

func testGoat() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("goat", .enabled(if: RUNALL))
func testGoat() {
let expected =
"I know an old lady who swallowed a goat.\n"
+ "Just opened her throat and swallowed a goat!\n"
Expand All @@ -64,29 +67,29 @@ class FoodChainTests: XCTestCase {
+ "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"
+ "She swallowed the spider to catch the fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die."
XCTAssertEqual(expected, FoodChain.song(start: 6, end: 6))
#expect(expected == FoodChain.song(start: 6, end: 6))
}

func testCow() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("cow", .enabled(if: RUNALL))
func testCow() {
let expected =
"I know an old lady who swallowed a cow.\n" + "I don't know how she swallowed a cow!\n"
+ "She swallowed the cow to catch the goat.\n" + "She swallowed the goat to catch the dog.\n"
+ "She swallowed the dog to catch the cat.\n" + "She swallowed the cat to catch the bird.\n"
+ "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"
+ "She swallowed the spider to catch the fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die."
XCTAssertEqual(expected, FoodChain.song(start: 7, end: 7))
#expect(expected == FoodChain.song(start: 7, end: 7))
}

func testHorse() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("horse", .enabled(if: RUNALL))
func testHorse() {
let expected = "I know an old lady who swallowed a horse.\n" + "She's dead, of course!"
XCTAssertEqual(expected, FoodChain.song(start: 8, end: 8))
#expect(expected == FoodChain.song(start: 8, end: 8))
}

func testMultipleVerses() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("multiple verses", .enabled(if: RUNALL))
func testMultipleVerses() {
let expected =
"I know an old lady who swallowed a fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die.\n" + "\n"
Expand All @@ -98,11 +101,11 @@ class FoodChainTests: XCTestCase {
+ "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"
+ "She swallowed the spider to catch the fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die."
XCTAssertEqual(expected, FoodChain.song(start: 1, end: 3))
#expect(expected == FoodChain.song(start: 1, end: 3))
}

func testFullSong() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("full song", .enabled(if: RUNALL))
func testFullSong() {
let expected =
"I know an old lady who swallowed a fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die.\n" + "\n"
Expand Down Expand Up @@ -138,6 +141,6 @@ class FoodChainTests: XCTestCase {
+ "She swallowed the spider to catch the fly.\n"
+ "I don't know why she swallowed the fly. Perhaps she'll die.\n" + "\n"
+ "I know an old lady who swallowed a horse.\n" + "She's dead, of course!"
XCTAssertEqual(expected, FoodChain.song(start: 1, end: 8))
#expect(expected == FoodChain.song(start: 1, end: 8))
}
}
20 changes: 11 additions & 9 deletions exercises/practice/gigasecond/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -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 {
let dateFormatter = ISO8601DateFormatter()
let dateTimeFormatter = ISO8601DateFormatter()

override func setUp() {
init() {
dateFormatter.formatOptions = [.withFullDate]
dateTimeFormatter.formatOptions = [.withFullDate, .withFullTime]
}

{% 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.scenarios[0] == "date" -%}
XCTAssertEqual(gigasecond(from: dateFormatter.date(from: "{{case.input.moment}}")!), dateTimeFormatter.date(from: "{{case.expected}}Z")!)
#expect(gigasecond(from: dateFormatter.date(from: "{{case.input.moment}}")!) == dateTimeFormatter.date(from: "{{case.expected}}Z")!)
{%- elif case.scenarios[0] == "datetime" -%}
XCTAssertEqual(gigasecond(from: dateTimeFormatter.date(from: "{{case.input.moment}}Z")!), dateTimeFormatter.date(from: "{{case.expected}}Z")!)
#expect(gigasecond(from: dateTimeFormatter.date(from: "{{case.input.moment}}Z")!) == dateTimeFormatter.date(from: "{{case.expected}}Z")!)
{%- endif -%}
}
{% endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/gigasecond/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