1
exercism fetch swift ocr-numbers

Tests/LinuxMain.swift

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

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

Tests/OcrNumbersTests/OcrNumbersTests.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import XCTest
@testable import OcrNumbers

class OcrNumbersTests: XCTestCase {
    func testRecognizeZero() {
        let text =  " _ \n" +
            "| |\n" +
            "|_|\n" +
        "   "
        XCTAssertEqual("0", try? OCR(text).converted())
    }

    func testRecognizeOne() {
        let text =  "   \n" +
            "  |\n" +
            "  |\n" +
        "   "
        XCTAssertEqual("1", try? OCR(text).converted())
    }

    func testRecognizeTwo() {
        let text =  " _ \n" +
            " _|\n" +
            "|_ \n" +
        "   "
        XCTAssertEqual("2", try? OCR(text).converted())
    }

    func testRecognizeThree() {
        let text =  " _ \n" +
            " _|\n" +
            " _|\n" +
        "   "
        XCTAssertEqual("3", try? OCR(text).converted())
    }

    func testRecognizeFour() {
        let text =  "   \n" +
            "|_|\n" +
            "  |\n" +
        "   "
        XCTAssertEqual("4", try? OCR(text).converted())
    }

    func testRecognizeFive() {
        let text =  " _ \n" +
            "|_ \n" +
            " _|\n" +
        "   "
        XCTAssertEqual("5", try? OCR(text).converted())
    }

    func testRecognizeSix() {
        let text =  " _ \n" +
            "|_ \n" +
            "|_|\n" +
        "   "
        XCTAssertEqual("6", try? OCR(text).converted())
    }

    func testRecognizeSeven() {
        let text =  " _ \n" +
            "  |\n" +
            "  |\n" +
        "   "
        XCTAssertEqual("7", try? OCR(text).converted())
    }

    func testRecognizeEight() {
        let text =  " _ \n" +
            "|_|\n" +
            "|_|\n" +
        "   "
        XCTAssertEqual("8", try? OCR(text).converted())
    }

    func testRecognizeNine() {
        let text =  " _ \n" +
            "|_|\n" +
            " _|\n" +
        "   "
        XCTAssertEqual("9", try? OCR(text).converted())
    }

    func testIdentifyGarble() {
        let text =  "   \n" +
            "| |\n" +
            "| |\n" +
        "   "
        XCTAssertEqual("?", try? OCR(text).converted())
    }

    func testIdentify10() {
        let text =  "    _ \n" +
            "  || |\n" +
            "  ||_|\n" +
        "      "
        XCTAssertEqual("10", try? OCR(text).converted())
    }

    func testIdentify110101100() {
        let text =
            "       _     _        _  _ \n" +
                "  |  || |  || |  |  || || |\n" +
                "  |  ||_|  ||_|  |  ||_||_|\n" +
        "                           "
        XCTAssertEqual("110101100", try? OCR(text).converted())
    }

    func testIdentifyWithGarble() {
        let text =
            "       _     _           _ \n" +
                "  |  || |  || |     || || |\n" +
                "  |  | _|  ||_|  |  ||_||_|\n" +
        "                           "
        XCTAssertEqual("11?10?1?0", try? OCR(text).converted())
    }

    func testIdentify1234567890() {
        let text =
            "    _  _     _  _  _  _  _  _ \n" +
                "  | _| _||_||_ |_   ||_||_|| |\n" +
                "  ||_  _|  | _||_|  ||_| _||_|\n" +
        "                              "
        XCTAssertEqual("1234567890", try? OCR(text).converted())
    }

    func testIdentify123_456_789() {
        let text =
            "    _  _ \n" +
                "  | _| _|\n" +
                "  ||_  _|\n" +
                "         \n" +
                "    _  _ \n" +
                "|_||_ |_ \n" +
                "  | _||_|\n" +
                "         \n" +
                " _  _  _ \n" +
                "  ||_||_|\n" +
                "  ||_| _|\n" +
        "         "
        XCTAssertEqual("123,456,789", try? OCR(text).converted())
    }

    func testThrowsInvalidNumberOfLinesError() {
        let text =
            "    _  _ \n" +
        "  | _| _|"

        XCTAssertThrowsError(_ = try OCR(text)) { error in
            XCTAssertEqual(error as? OCR.OCRError, .invalidNumberOfLines)
        }
    }

    func testThrowsInvalidNumberOfColumnsError() {
        let text =
            "    _\n" +
                "  | _\n" +
                "  ||_\n" +
        "     "

        XCTAssertThrowsError(_ = try OCR(text)) { error in
            XCTAssertEqual(error as? OCR.OCRError, .invalidNumberOfColumns)
        }
    }

    static var allTests: [(String, (OcrNumbersTests) -> () throws -> Void)] {
        return [
            ("testRecognizeZero", testRecognizeZero),
            ("testRecognizeOne", testRecognizeOne),
            ("testRecognizeTwo", testRecognizeTwo),
            ("testRecognizeThree", testRecognizeThree),
            ("testRecognizeFour", testRecognizeFour),
            ("testRecognizeFive", testRecognizeFive),
            ("testRecognizeSix", testRecognizeSix),
            ("testRecognizeSeven", testRecognizeSeven),
            ("testRecognizeEight", testRecognizeEight),
            ("testRecognizeNine", testRecognizeNine),
            ("testIdentifyGarble", testIdentifyGarble),
            ("testIdentify10", testIdentify10),
            ("testIdentify110101100", testIdentify110101100),
            ("testIdentifyWithGarble", testIdentifyWithGarble),
            ("testIdentify1234567890", testIdentify1234567890),
            ("testIdentify123_456_789", testIdentify123_456_789),
            ("testThrowsInvalidNumberOfLinesError", testThrowsInvalidNumberOfLinesError),
            ("testThrowsInvalidNumberOfColumnsError", testThrowsInvalidNumberOfColumnsError),
        ]
    }
}