1
exercism fetch swift robot-name

Tests/LinuxMain.swift

1
2
3
4
5
6
import XCTest
@testable import RobotNameTests

XCTMain([
    testCase(RobotNameTests.allTests),
    ])

Tests/RobotNameTests/RobotNameTests.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import XCTest
import Foundation
@testable import RobotName

#if os(Linux)
#if swift(>=3.1)
    typealias Regex = NSRegularExpression
#else
    typealias Regex = RegularExpression
#endif
#else
    typealias Regex = NSRegularExpression
#endif

class RobotNameTests: XCTestCase {
    func robotNameIsCorrectlyFormatted(_ name: String) -> Bool {
        let robotNameRegex = try? Regex(pattern: "\\A\\w{2}\\d{3}\\z", options: Regex.Options.caseInsensitive)
        guard let matches = robotNameRegex?.matches(in: name, options: .withoutAnchoringBounds, range: NSRange(0..<name.utf16.count)) else { return false }

        return matches.count > 0
    }

    func testHasName() {
        let robot = Robot()
        XCTAssert(robotNameIsCorrectlyFormatted(robot.name))
    }

    func testNameSticks() {
        let robot = Robot()
        let name = robot.name
        XCTAssertEqual(name, robot.name)
    }

    func testDifferentRobotsHaveDifferentNames() {
        let firstRobot = Robot()
        let secondRobot = Robot()
        XCTAssertNotEqual(firstRobot.name, secondRobot.name)
    }

    func testResetName() {
        var robot = Robot()
        let firstName = robot.name
        robot.resetName()
        let secondName = robot.name
        XCTAssertNotEqual(firstName, secondName)
    }

    static var allTests: [(String, (RobotNameTests) -> () throws -> Void)] {
        return [
            ("testHasName", testHasName),
            ("testNameSticks", testNameSticks),
            ("testDifferentRobotsHaveDifferentNames", testDifferentRobotsHaveDifferentNames),
            ("testResetName", testResetName),
        ]
    }
}