forked from square/StringTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTemplateTests.swift
More file actions
90 lines (74 loc) · 4.48 KB
/
StringTemplateTests.swift
File metadata and controls
90 lines (74 loc) · 4.48 KB
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
//
// Copyright © 2018 Square, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import StringTemplate
import XCTest
final class StringTemplateTests: XCTestCase {
func testSuccessfulTemplateApplication() {
let phone = String.Template(placeholderToken: "&", template: "(&&&) &&&-&&&&")
let template = String.Template(placeholderToken: "%", template: "%% %%% (%%)*(%%)")
XCTAssertEqual(try? "5555555555".applying(template: phone), "(555) 555-5555")
XCTAssertEqual(try? "1234567890".applying(template: phone), "(123) 456-7890")
XCTAssertEqual(try? "123456789".applying(template: template), "12 345 (67)*(89)")
XCTAssertEqual(try? "000000000".applying(template: template), "00 000 (00)*(00)")
}
func testSuccessfulPartialTemplateApplication() {
let phone = String.Template(placeholderToken: "&", template: "(&&&) &&&-&&&&")
let template = String.Template(placeholderToken: "%", template: "%% %%% (%%)*(%%)")
XCTAssertEqual(try? "5555555555".applying(template: phone, options: [.allowPartial]), "(555) 555-5555")
XCTAssertEqual(try? "5555555".applying(template: phone, options: [.allowPartial]), "(555) 555-5")
XCTAssertEqual(try? "123456".applying(template: phone, options: [.allowPartial]), "(123) 456-")
XCTAssertEqual(try? "12345".applying(template: template, options: [.allowPartial]), "12 345 (")
XCTAssertEqual(try? "0000000".applying(template: template, options: [.allowPartial]), "00 000 (00)*(")
}
func testSuccessfulOverflowTemplateApplication() {
let phone = String.Template(placeholderToken: "&", template: "(&&&) &&&-&&&&")
let template = String.Template(placeholderToken: "%", template: "%% %%% (%%)*(%%)")
XCTAssertEqual(try? "5555555555555".applying(template: phone, options: [.allowOverflow]), "(555) 555-5555")
XCTAssertEqual(try? "123456789000".applying(template: phone, options: [.allowOverflow]), "(123) 456-7890")
XCTAssertEqual(try? "1234567890000".applying(template: template, options: [.allowOverflow]), "12 345 (67)*(89)")
XCTAssertEqual(try? "00000000000000".applying(template: template, options: [.allowOverflow]), "00 000 (00)*(00)")
}
func testUnsuccessfulTemplateApplication() {
let template = String.Template(placeholderToken: "X", template: "XXXX")
assert(try "123".applying(template: template), throws: String.Template.Error.tooShort)
assert(try "12345".applying(template: template), throws: String.Template.Error.tooLong)
}
func testUnsuccessfulPartialTemplateApplication() {
let template = String.Template(placeholderToken: "X", template: "XXXX")
XCTAssertEqual(try? "123".applying(template: template, options: [.allowPartial]), "123")
assert(try "12345".applying(template: template, options: [.allowPartial]), throws: String.Template.Error.tooLong)
}
func testUnsuccessfulOverflowTemplateApplication() {
let template = String.Template(placeholderToken: "X", template: "XXXX")
XCTAssertEqual(try? "12345".applying(template: template, options: [.allowOverflow]), "1234")
assert(try "123".applying(template: template, options: [.allowOverflow]), throws: String.Template.Error.tooShort)
}
func testPreservedCharacters() {
let template = String.Template(placeholderToken: "X", template: "0XXX0", preservedCharacters: .decimalDigits)
XCTAssertEqual(try? "103".applying(template: template), "01030")
}
}
// Assert that a throwing function throws a specific error
func assert<T, E>(_ function: @autoclosure () throws -> T, throws expected: E, file: StaticString = #file, line: UInt = #line) where E: Error & Equatable {
do {
_ = try function()
XCTFail("Expected an error to be thrown", file: file, line: line)
} catch let error as E {
XCTAssertEqual(error, expected, file: file, line: line)
} catch {
XCTFail("Incorrect error type was thrown: \(error)", file: file, line: line)
}
}