File: SupportTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (243 lines) | stat: -rw-r--r-- 8,811 bytes parent folder | download
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SKSupport
import TSCBasic
import XCTest

final class SupportTests: XCTestCase {

  func checkLines(_ string: String, _ expected: [String], file: StaticString = #filePath, line: UInt = #line) {
    let table = LineTable(string)
    XCTAssertEqual(table.map { String($0) }, expected, file: file, line: line)
  }

  func checkOffsets(_ string: String, _ expected: [Int], file: StaticString = #filePath, line: UInt = #line) {
    let table = LineTable(string)
    XCTAssertEqual(
      table.map { string.utf8.distance(from: string.startIndex, to: $0.startIndex) },
      expected,
      file: file,
      line: line
    )
  }

  func testLineTable() {
    checkLines("", [""])
    checkLines("a", ["a"])
    checkLines("abc", ["abc"])
    checkLines("abc\n", ["abc\n", ""])
    checkLines("\n", ["\n", ""])
    checkLines("\n\n", ["\n", "\n", ""])
    checkLines("\r\n", ["\r\n", ""])
    checkLines("\r\r", ["\r", "\r", ""])
    checkLines("a\nb", ["a\n", "b"])
    checkLines("a\nb\n", ["a\n", "b\n", ""])
    checkLines("a\nb\nccccc", ["a\n", "b\n", "ccccc"])

    checkLines("\u{1}\nb", ["\u{1}\n", "b"])
    checkLines("\u{10000}\nb", ["\u{10000}\n", "b"])
    checkLines("\n\u{10000}", ["\n", "\u{10000}"])
    checkLines("\n\u{10000}b", ["\n", "\u{10000}b"])
    checkLines("\n\u{10000}\nc", ["\n", "\u{10000}\n", "c"])
    checkLines("\n\u{10000}b\nc", ["\n", "\u{10000}b\n", "c"])

    checkOffsets("a", [0])
    checkOffsets("abc", [0])
    checkOffsets("\n", [0, 1])
    checkOffsets("\n\n", [0, 1, 2])
    checkOffsets("\n\r\n\n", [0, 1, 3, 4])
    checkOffsets("a\nb", [0, 2])
    checkOffsets("a\nb\n", [0, 2, 4])
    checkOffsets("a\nbbb\nccccc", [0, 2, 6])
    checkOffsets("a\nbbb\nccccc", [0, 2, 6])

    checkOffsets("\u{1}\nb", [0, 2])
    checkOffsets("\u{100}\nb", [0, 3])
    checkOffsets("\u{1000}\nb", [0, 4])
    checkOffsets("\u{10000}\nb", [0, 5])
    checkOffsets("\n\u{10000}", [0, 1])
    checkOffsets("\n\u{10000}b", [0, 1])
    checkOffsets("\n\u{10000}b\nc", [0, 1, 7])

    XCTAssertEqual(LineTable("")[0], "")
    XCTAssertEqual(LineTable("\n")[1], "")
  }

  func checkLineAndColumns(
    _ table: LineTable,
    _ utf8Offset: Int,
    _ expected: (line: Int, utf16Column: Int),
    file: StaticString = #filePath,
    line: UInt = #line
  ) {
    let actual = table.lineAndUTF16ColumnOf(utf8Offset: utf8Offset)
    XCTAssert(actual == expected, "\(actual) != \(expected)", file: file, line: line)
  }

  func checkUTF8OffsetOf(
    _ table: LineTable,
    _ query: (line: Int, utf16Column: Int),
    _ expected: Int,
    file: StaticString = #filePath,
    line: UInt = #line
  ) {
    XCTAssertEqual(
      table.utf8OffsetOf(line: query.line, utf16Column: query.utf16Column),
      expected,
      file: file,
      line: line
    )
  }

  func checkUTF16ColumnAt(
    _ table: LineTable,
    _ query: (line: Int, utf8Column: Int),
    _ expected: Int,
    file: StaticString = #filePath,
    line: UInt = #line
  ) {
    XCTAssertEqual(
      table.utf16ColumnAt(line: query.line, utf8Column: query.utf8Column),
      expected,
      file: file,
      line: line
    )
  }

  func testLineTableLinePositionTranslation() {
    let t1 = LineTable(
      """
      0123
      5678
      abcd
      """
    )
    checkLineAndColumns(t1, 0, (line: 0, utf16Column: 0))
    checkLineAndColumns(t1, 2, (line: 0, utf16Column: 2))
    checkLineAndColumns(t1, 4, (line: 0, utf16Column: 4))
    checkLineAndColumns(t1, 5, (line: 1, utf16Column: 0))
    checkLineAndColumns(t1, 9, (line: 1, utf16Column: 4))
    checkLineAndColumns(t1, 10, (line: 2, utf16Column: 0))
    checkLineAndColumns(t1, 14, (line: 2, utf16Column: 4))
    checkLineAndColumns(t1, 15, (line: 2, utf16Column: 4))

    checkUTF8OffsetOf(t1, (line: 0, utf16Column: 0), 0)
    checkUTF8OffsetOf(t1, (line: 0, utf16Column: 2), 2)
    checkUTF8OffsetOf(t1, (line: 0, utf16Column: 4), 4)
    checkUTF8OffsetOf(t1, (line: 0, utf16Column: 5), 5)
    checkUTF8OffsetOf(t1, (line: 0, utf16Column: 6), 5)  // Recovers to end of line 0
    checkUTF8OffsetOf(t1, (line: 1, utf16Column: 0), 5)
    checkUTF8OffsetOf(t1, (line: 1, utf16Column: 4), 9)
    checkUTF8OffsetOf(t1, (line: 2, utf16Column: 0), 10)
    checkUTF8OffsetOf(t1, (line: 2, utf16Column: 4), 14)
    checkUTF8OffsetOf(t1, (line: 2, utf16Column: 5), 14)  // Recovers to end of line 2
    checkUTF8OffsetOf(t1, (line: 3, utf16Column: 0), 14)  // Recovers to end of source file

    checkUTF16ColumnAt(t1, (line: 0, utf8Column: 4), 4)
    checkUTF16ColumnAt(t1, (line: 0, utf8Column: 5), 5)
    checkUTF16ColumnAt(t1, (line: 0, utf8Column: 6), 5)  // Recovers to end of line 0
    checkUTF16ColumnAt(t1, (line: 2, utf8Column: 0), 0)
    checkUTF16ColumnAt(t1, (line: 3, utf8Column: 0), 0)  // Line out of bounds, so keeps column

    let t2 = LineTable(
      """
      こんにちは
      안녕하세요
      \u{1F600}\u{1F648}
      """
    )
    checkLineAndColumns(t2, 0, (line: 0, utf16Column: 0))
    checkLineAndColumns(t2, 15, (line: 0, utf16Column: 5))
    checkLineAndColumns(t2, 19, (line: 1, utf16Column: 1))
    checkLineAndColumns(t2, 32, (line: 2, utf16Column: 0))
    checkLineAndColumns(t2, 36, (line: 2, utf16Column: 2))
    checkLineAndColumns(t2, 40, (line: 2, utf16Column: 4))

    checkUTF8OffsetOf(t2, (line: 0, utf16Column: 0), 0)
    checkUTF8OffsetOf(t2, (line: 0, utf16Column: 5), 15)
    checkUTF8OffsetOf(t2, (line: 0, utf16Column: 6), 16)
    checkUTF8OffsetOf(t2, (line: 1, utf16Column: 1), 19)
    checkUTF8OffsetOf(t2, (line: 1, utf16Column: 6), 32)
    checkUTF8OffsetOf(t2, (line: 1, utf16Column: 7), 32)  // Recovers to end of line 1
    checkUTF8OffsetOf(t2, (line: 2, utf16Column: 0), 32)
    checkUTF8OffsetOf(t2, (line: 2, utf16Column: 2), 36)
    checkUTF8OffsetOf(t2, (line: 2, utf16Column: 4), 40)
    checkUTF8OffsetOf(t2, (line: 2, utf16Column: 5), 40)  // Recovers to end of line 2

    checkUTF16ColumnAt(t2, (line: 0, utf8Column: 3), 1)
    checkUTF16ColumnAt(t2, (line: 0, utf8Column: 15), 5)
    checkUTF16ColumnAt(t2, (line: 2, utf8Column: 4), 2)
  }

  func testLineTableEditing() {
    var t = LineTable("")
    t.replace(fromLine: 0, utf16Offset: 0, utf16Length: 0, with: "a")
    XCTAssertEqual(t, LineTable("a"))
    t.replace(fromLine: 0, utf16Offset: 0, utf16Length: 0, with: "cb")
    XCTAssertEqual(t, LineTable("cba"))
    t.replace(fromLine: 0, utf16Offset: 1, utf16Length: 1, with: "d")
    XCTAssertEqual(t, LineTable("cda"))
    t.replace(fromLine: 0, utf16Offset: 3, utf16Length: 0, with: "e")
    XCTAssertEqual(t, LineTable("cdae"))
    t.replace(fromLine: 0, utf16Offset: 3, utf16Length: 1, with: "")
    XCTAssertEqual(t, LineTable("cda"))

    t = LineTable("a")
    t.replace(fromLine: 0, utf16Offset: 0, utf16Length: 0, with: "\n")
    XCTAssertEqual(t, LineTable("\na"))
    t.replace(fromLine: 1, utf16Offset: 0, utf16Length: 0, with: "\n")
    XCTAssertEqual(t, LineTable("\n\na"))
    t.replace(fromLine: 2, utf16Offset: 1, utf16Length: 0, with: "\n")
    XCTAssertEqual(t, LineTable("\n\na\n"))

    t = LineTable(
      """
      abcd
      efgh
      """
    )

    t.replace(fromLine: 0, utf16Offset: 2, toLine: 1, utf16Offset: 1, with: "x")
    XCTAssertEqual(t, LineTable("abxfgh"))

    t.replace(fromLine: 0, utf16Offset: 2, toLine: 0, utf16Offset: 4, with: "p\nq\n")
    XCTAssertEqual(t, LineTable("abp\nq\ngh"))
  }

  func testByteStringWithUnsafeData() {
    ByteString(encodingAsUTF8: "").withUnsafeData { data in
      XCTAssertEqual(data.count, 0)
    }
    ByteString(encodingAsUTF8: "abc").withUnsafeData { data in
      XCTAssertEqual(data.count, 3)
    }
  }

  func testExpandingTilde() throws {
    XCTAssertEqual(try AbsolutePath(expandingTilde: "~/foo").basename, "foo")
    XCTAssertNotEqual(try AbsolutePath(expandingTilde: "~/foo").parentDirectory, .root)
    XCTAssertEqual(try AbsolutePath(expandingTilde: "/foo"), try AbsolutePath(validating: "/foo"))
  }

  func testResultProjection() {
    enum MyError: Error, Equatable {
      case err1, err2
    }
    typealias MyResult<T> = Swift.Result<T, MyError>

    XCTAssertEqual(MyResult.success(1).success, 1)
    XCTAssertNil(MyResult.failure(.err1).success)
    XCTAssertNil(MyResult.success(1).failure)
    XCTAssertEqual(MyResult<Int>.failure(.err1).failure, .err1)
  }
}