File: CustomTests.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 (726 lines) | stat: -rw-r--r-- 23,026 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 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
//
//===----------------------------------------------------------------------===//

import XCTest
import _StringProcessing
@testable import RegexBuilder

// A nibbler processes a single character from a string
@available(SwiftStdlib 5.7, *)
private protocol Nibbler: CustomConsumingRegexComponent {
  func nibble(_: Character) -> RegexOutput?
}

@available(SwiftStdlib 5.7, *)
extension Nibbler {
  // Default implementation, just feed the character in
  func consuming(
    _ input: String,
    startingAt index: String.Index,
    in bounds: Range<String.Index>
  ) throws -> (upperBound: String.Index, output: RegexOutput)? {
    guard index != bounds.upperBound, let res = nibble(input[index]) else {
      return nil
    }
    return (input.index(after: index), res)
  }
}


// A number nibbler
@available(SwiftStdlib 5.7, *)
private struct Numbler: Nibbler {
  typealias RegexOutput = Int
  func nibble(_ c: Character) -> Int? {
    c.wholeNumberValue
  }
}

// An ASCII value nibbler
@available(SwiftStdlib 5.7, *)
private struct Asciibbler: Nibbler {
  typealias RegexOutput = UInt8
  func nibble(_ c: Character) -> UInt8? {
    c.asciiValue
  }
}

@available(SwiftStdlib 5.7, *)
private struct IntParser: CustomConsumingRegexComponent {
  struct ParseError: Error, Hashable {}
  typealias RegexOutput = Int
  func consuming(_ input: String,
    startingAt index: String.Index,
    in bounds: Range<String.Index>
  ) throws -> (upperBound: String.Index, output: Int)? {
    guard index != bounds.upperBound else { return nil }

    let r = Regex {
      Capture<(Substring, Int?)>(OneOrMore(.digit)) { Int($0) }
    }

    guard let match = input[index..<bounds.upperBound].prefixMatch(of: r),
            let output = match.1 else {
      throw ParseError()
    }

    return (match.range.upperBound, output)
  }
}

@available(SwiftStdlib 5.7, *)
private struct CurrencyParser: CustomConsumingRegexComponent {
  enum Currency: String, Hashable {
    case usd = "USD"
    case ntd = "NTD"
    case dem = "DEM"
  }

  enum ParseError: Error, Hashable {
    case unrecognized
    case deprecated
  }

  typealias RegexOutput = Currency
  func consuming(_ input: String,
             startingAt index: String.Index,
             in bounds: Range<String.Index>
  ) throws -> (upperBound: String.Index, output: Currency)? {

    guard index != bounds.upperBound else { return nil }

    let substr = input[index..<bounds.upperBound]
    guard !substr.isEmpty else { return nil }

    let currencies: [Currency] = [ .usd, .ntd ]
    let deprecated: [Currency] = [ .dem ]

    for currency in currencies {
      if substr.hasPrefix(currency.rawValue) {
        return (input.range(of: currency.rawValue)!.upperBound, currency)
      }
    }

    for dep in deprecated {
      if substr.hasPrefix(dep.rawValue) {
        throw ParseError.deprecated
      }
    }
    throw ParseError.unrecognized
  }
}

enum MatchCall {
  case match
  case firstMatch
}

@available(SwiftStdlib 5.7, *)
fileprivate func customTest<Match: Equatable>(
  _ regex: Regex<Match>,
  _ tests: (input: String, call: MatchCall, match: Match?)...,
  file: StaticString = #file,
  line: UInt = #line
) {
  for (input, call, match) in tests {
    let result: Match?
    switch call {
    case .match:
      result = input.wholeMatch(of: regex)?.output
    case .firstMatch:
      result = input.firstMatch(of: regex)?.output
    }
    XCTAssertEqual(result, match, file: file, line: line)
  }
}

@available(SwiftStdlib 5.7, *)
fileprivate func customTest<Match>(
  _ regex: some RegexComponent<Match>,
  _ isEquivalent: (Match, Match) -> Bool,
  _ tests: (input: String, call: MatchCall, match: Match?)...,
  file: StaticString = #file,
  line: UInt = #line
) {
  for (input, call, match) in tests {
    let result: Match?
    switch call {
    case .match:
      result = input.wholeMatch(of: regex)?.output
    case .firstMatch:
      result = input.firstMatch(of: regex)?.output
    }
    switch (result, match) {
    case let (result?, match?):
      XCTAssert(
        isEquivalent(result, match),
        "'\(result)' isn't equal to '\(match)'.",
        file: file, line: line)
    case (nil, nil):
      // Success
      break
    case (nil, _):
      XCTFail("No match when expected", file: file, line: line)
    case (_, nil):
      XCTFail("Unexpected match", file: file, line: line)
    }
  }
}

// Test support
struct Concat : Equatable {
  var wrapped: String
  init(_ name: String, _ suffix: Int?) {
    if let suffix = suffix {
      wrapped = name + String(suffix)
    } else {
      wrapped = name
    }
  }
}

extension Concat : Collection {
  typealias Index = String.Index
  typealias Element = String.Element

  var startIndex: Index { return wrapped.startIndex }
  var endIndex: Index { return wrapped.endIndex }

  subscript(position: Index) -> Element {
    return wrapped[position]
  }

  func index(after i: Index) -> Index {
    return wrapped.index(after: i)
  }
}

extension Concat: BidirectionalCollection {
  typealias Indices = String.Indices
  typealias SubSequence = String.SubSequence

  func index(before i: Index) -> Index {
    return wrapped.index(before: i)
  }

  var indices: Indices {
    wrapped.indices
  }

  subscript(bounds: Range<Index>) -> Substring {
    Substring(wrapped[bounds])
  }
}

@available(SwiftStdlib 5.7, *)
class CustomRegexComponentTests: XCTestCase {
  // TODO: Refactor below into more exhaustive, declarative
  // tests.
  func testCustomRegexComponents() throws {
    customTest(
      Regex {
        Numbler()
        Asciibbler()
      },
      ("4t", .match, "4t"),
      ("4", .match, nil),
      ("t", .match, nil),
      ("t x1y z", .firstMatch, "1y"),
      ("t4", .match, nil))

    customTest(
      Regex {
        OneOrMore { Numbler() }
      },
      ("ab123c", .firstMatch, "123"),
      ("abc", .firstMatch, nil),
      ("55z", .match, nil),
      ("55z", .firstMatch, "55"))

    customTest(
      Regex {
        Numbler()
      },
      ("ab123c", .firstMatch, 1),
      ("abc", .firstMatch, nil),
      ("55z", .match, nil),
      ("55z", .firstMatch, 5))

//    customTest(
//      Regex<Substring> {
//        #/(?<prefix>\D+)/#
//        Optionally("~")
//      },
//      ("ab123c", .firstMatch, "ab"),
//      ("abc", .firstMatch, "abc"),
//      ("123", .firstMatch, nil),
//      ("a55z", .match, nil),
//      ("a55z", .firstMatch, "a"))

    customTest(
      Regex<(Substring, Substring, Int)> {
        #/(\D+)/#
        Capture(Numbler())
      },
      ==,
      ("ab123c", .firstMatch, ("ab1", "ab", 1)),
      ("abc", .firstMatch, nil),
      ("123", .firstMatch, nil),
      ("a55z", .match, nil),
      ("a55z", .firstMatch, ("a5", "a", 5)))

    customTest(
      Regex<(Substring, prefix: Substring)> {
        #/(?<prefix>\D+)/#
      },
      ==,
      ("ab123c", .firstMatch, ("ab", "ab")),
      ("abc", .firstMatch, ("abc", "abc")),
      ("123", .firstMatch, nil),
      ("a55z", .match, nil),
      ("a55z", .firstMatch, ("a", "a")))

//    customTest(
//      Regex<(Substring, Int)> {
//        #/(?<prefix>\D+)/#
//        Capture(Numbler())
//      },
//      ==,
//      ("ab123c", .firstMatch, ("ab1", 1)),
//      ("abc", .firstMatch, nil),
//      ("123", .firstMatch, nil),
//      ("a55z", .match, nil),
//      ("a55z", .firstMatch, ("a5", 5)))
    
//    customTest(
//      Regex<(Substring, Int, Substring)> {
//        #/(?<prefix>\D+)/#
//        Regex {
//          Capture(Numbler())
//          Capture(OneOrMore(.word))
//        }
//      },
//      ==,
//      ("ab123c", .firstMatch, ("ab123c", 1, "23c")),
//      ("abc", .firstMatch, nil),
//      ("123", .firstMatch, nil),
//      ("a55z", .match, ("a55z", 5, "5z")),
//      ("a55z", .firstMatch, ("a55z", 5, "5z")))
    
    customTest(
      Regex<(Substring, Substring)> {
        Capture {
          OneOrMore {
            Numbler()
          }
        }
      },
      ==,
      ("abc123", .firstMatch, ("123", "123")),
      ("abc123", .match, nil),
      ("abc", .firstMatch, nil))
    
    customTest(
      Regex<(Substring, Int)> {
        OneOrMore {
          Capture { Numbler() }
        }
      },
      ==,
      ("ab123c", .firstMatch, ("123", 3)),
      ("abc", .firstMatch, nil),
      ("55z", .match, nil),
      ("55z", .firstMatch, ("55", 5)))
  }

  func testRegexAbort() {

    enum Radix: Hashable {
      case dot
      case comma
    }
    struct Abort: Error, Hashable {}

    let hexRegex = Regex {
      Capture { OneOrMore(.hexDigit) }
      TryCapture { CharacterClass.any } transform: { c -> Radix? in
        switch c {
        case ".": return Radix.dot
        case ",": return Radix.comma
        case "❗️":
          // Malicious! Toxic levels of emphasis detected.
          throw Abort()
        default:
          // Not a radix
          return nil
        }
      }
      Capture { OneOrMore(.hexDigit) }
    }
    // hexRegex: Regex<(Substring, Substring, Radix?, Substring)>
    // TODO: Why is Radix optional?

    do {
      guard let m = try hexRegex.wholeMatch(in: "123aef.345") else {
        XCTFail()
        return
      }
      XCTAssertEqual(m.0, "123aef.345")
      XCTAssertEqual(m.1, "123aef")
      XCTAssertEqual(m.2, .dot)
      XCTAssertEqual(m.3, "345")
    } catch {
      XCTFail()
    }

    do {
      _ = try hexRegex.wholeMatch(in: "123aef❗️345")
      XCTFail()
    } catch let e as Abort {
      XCTAssertEqual(e, Abort())
    } catch {
      XCTFail()
    }

    struct Poison: Error, Hashable {}

    let addressRegex = Regex {
      "0x"
      Capture(Repeat(.hexDigit, count: 8)) { hex -> Int in
        let i = Int(hex, radix: 16)!
        if i == 0xdeadbeef {
          throw Poison()
        }
        return i
      }
    }

    do {
      guard let m = try addressRegex.wholeMatch(in: "0x1234567f") else {
        XCTFail()
        return
      }
      XCTAssertEqual(m.0, "0x1234567f")
      XCTAssertEqual(m.1, 0x1234567f)
    } catch {
      XCTFail()
    }

    do {
      _ = try addressRegex.wholeMatch(in: "0xdeadbeef")
      XCTFail()
    } catch let e as Poison {
      XCTAssertEqual(e, Poison())
    } catch {
      XCTFail()
    }


  }

  func testCustomRegexThrows() {

    func customTest<Match: Equatable, E: Error & Equatable>(
      _ regex: Regex<Match>,
      _ tests: (input: String, match: Match?, expectError: E?)...,
      file: StaticString = #file,
      line: UInt = #line
    ) {
      for (input, match, expectError) in tests {
        do {
          let result = try regex.wholeMatch(in: input)?.output
          XCTAssertEqual(result, match)
        } catch let e as E {
          XCTAssertEqual(e, expectError)
        } catch {
          XCTFail()
        }
      }
    }

    func customTest<Match: Equatable, Error1: Error & Equatable, Error2: Error & Equatable>(
      _ regex: Regex<Match>,
      _ tests: (input: String, match: Match?, expectError1: Error1?, expectError2: Error2?)...,
      file: StaticString = #file,
      line: UInt = #line
    ) {
      for (input, match, expectError1, expectError2) in tests {
        do {
          let result = try regex.wholeMatch(in: input)?.output
          XCTAssertEqual(result, match)
        } catch let e as Error1 {
          XCTAssertEqual(e, expectError1, input, file: file, line: line)
        } catch let e as Error2 {
          XCTAssertEqual(e, expectError2, input, file: file, line: line)
        } catch {
          XCTFail("caught error: \(error.localizedDescription)")
        }
      }
    }

    func customTest<Capture: Equatable, Error1: Error & Equatable, Error2: Error & Equatable>(
      _ regex: Regex<(Substring, Capture)>,
      _ tests: (input: String, match: (Substring, Capture)?, expectError1: Error1?, expectError2: Error2?)...,
      file: StaticString = #file,
      line: UInt = #line
    ) {
      for (input, match, expectError1, expectError2) in tests {
        do {
          let result = try regex.wholeMatch(in: input)?.output
          XCTAssertEqual(result?.0, match?.0, file: file, line: line)
          XCTAssertEqual(result?.1, match?.1, file: file, line: line)
        } catch let e as Error1 {
          XCTAssertEqual(e, expectError1, input, file: file, line: line)
        } catch let e as Error2 {
          XCTAssertEqual(e, expectError2, input, file: file, line: line)
        } catch {
          XCTFail("caught error: \(error.localizedDescription)")
        }
      }
    }

    func customTest<Capture1: Equatable, Capture2: Equatable, Error1: Error & Equatable, Error2: Error & Equatable>(
      _ regex: Regex<(Substring, Capture1, Capture2)>,
      _ tests: (input: String, match: (Substring, Capture1, Capture2)?, expectError1: Error1?, expectError2: Error2?)...,
      file: StaticString = #file,
      line: UInt = #line
    ) {
      for (input, match, expectError1, expectError2) in tests {
        do {
          let result = try regex.wholeMatch(in: input)?.output
          XCTAssertEqual(result?.0, match?.0, file: file, line: line)
          XCTAssertEqual(result?.1, match?.1, file: file, line: line)
          XCTAssertEqual(result?.2, match?.2, file: file, line: line)
        } catch let e as Error1 {
          XCTAssertEqual(e, expectError1, input,  file: file, line: line)
        } catch let e as Error2 {
          XCTAssertEqual(e, expectError2, input, file: file, line: line)
        } catch {
          XCTFail("caught error: \(error.localizedDescription)")
        }
      }
    }

    // No capture, one error
    customTest(
      Regex {
        IntParser()
      },
      ("zzz", nil, IntParser.ParseError()),
      ("x10x", nil, IntParser.ParseError()),
      ("30", 30, nil)
    )

    customTest(
      Regex {
        CurrencyParser()
      },
      ("USD", .usd, nil),
      ("NTD", .ntd, nil),
      ("NTD USD", nil, nil),
      ("DEM", nil, CurrencyParser.ParseError.deprecated),
      ("XXX", nil, CurrencyParser.ParseError.unrecognized)
    )

    // No capture, two errors
    customTest(
      Regex {
        IntParser()
        " "
        IntParser()
      },
      ("20304 100", "20304 100", nil, nil),
      ("20304.445 200", nil, IntParser.ParseError(), nil),
      ("20304 200.123", nil, nil, IntParser.ParseError()),
      ("20304.445 200.123", nil, IntParser.ParseError(), IntParser.ParseError())
    )

    customTest(
      Regex {
        CurrencyParser()
        IntParser()
      },
      ("USD100", "USD100", nil, nil),
      ("XXX100", nil, CurrencyParser.ParseError.unrecognized, nil),
      ("USD100.000", nil, nil, IntParser.ParseError()),
      ("XXX100.0000", nil, CurrencyParser.ParseError.unrecognized, IntParser.ParseError())
    )

    // One capture, two errors: One error is thrown from inside a capture,
    // while the other one is thrown from outside
    customTest(
      Regex {
        Capture { CurrencyParser() }
        IntParser()
      },
      ("USD100", ("USD100", .usd), nil, nil),
      ("NTD305.5", nil, nil, IntParser.ParseError()),
      ("DEM200", ("DEM200", .dem), CurrencyParser.ParseError.deprecated, nil),
      ("XXX", nil, CurrencyParser.ParseError.unrecognized, IntParser.ParseError())
    )

    customTest(
      Regex {
        CurrencyParser()
        Capture { IntParser() }
      },
      ("USD100", ("USD100", 100), nil, nil),
      ("NTD305.5", nil, nil, IntParser.ParseError()),
      ("DEM200", ("DEM200", 200), CurrencyParser.ParseError.deprecated, nil),
      ("XXX", nil, CurrencyParser.ParseError.unrecognized, IntParser.ParseError())
    )

    // One capture, two errors: Both errors are thrown from inside the capture
    customTest(
      Regex {
        Capture {
          CurrencyParser()
          IntParser()
        }
      },
      ("USD100", ("USD100", "USD100"), nil, nil),
      ("NTD305.5", nil, nil, IntParser.ParseError()),
      ("DEM200", ("DEM200", "DEM200"), CurrencyParser.ParseError.deprecated, nil),
      ("XXX", nil, CurrencyParser.ParseError.unrecognized, IntParser.ParseError())
    )

    // Two captures, two errors: Different erros are thrown from inside captures
    customTest(
      Regex {
        Capture(CurrencyParser())
        Capture(IntParser())
      },
      ("USD100", ("USD100", .usd, 100), nil, nil),
      ("NTD500", ("NTD500", .ntd, 500), nil, nil),
      ("XXX20", nil, CurrencyParser.ParseError.unrecognized, IntParser.ParseError()),
      ("DEM500", nil, CurrencyParser.ParseError.deprecated, nil),
      ("DEM500.345", nil, CurrencyParser.ParseError.deprecated, IntParser.ParseError()),
      ("NTD100.345", nil, nil, IntParser.ParseError())
    )

  }


  func testMatchVarients() {
    func customTest<Match: Equatable>(
      _ regex: Regex<Match>,
      _ input: Concat,
      expected: (wholeMatch: Match?, firstMatch: Match?, prefixMatch: Match?),
      file: StaticString = #file, line: UInt = #line
    ) {
      let wholeResult = input.wholeMatch(of: regex)?.output
      let firstResult = input.firstMatch(of: regex)?.output
      let prefixResult = input.prefixMatch(of: regex)?.output
      XCTAssertEqual(wholeResult, expected.wholeMatch, file: file, line: line)
      XCTAssertEqual(firstResult, expected.firstMatch, file: file, line: line)
      XCTAssertEqual(prefixResult, expected.prefixMatch, file: file, line: line)
    }

    typealias CaptureMatch1 = (Substring, Int?)
    func customTest(
      _ regex: Regex<CaptureMatch1>,
      _ input: Concat,
      expected: (wholeMatch: CaptureMatch1?, firstMatch: CaptureMatch1?, prefixMatch: CaptureMatch1?),
      file: StaticString = #file, line: UInt = #line
    ) {
      let wholeResult = input.wholeMatch(of: regex)?.output
      let firstResult = input.firstMatch(of: regex)?.output
      let prefixResult = input.prefixMatch(of: regex)?.output
      XCTAssertEqual(wholeResult?.0, expected.wholeMatch?.0, file: file, line: line)
      XCTAssertEqual(wholeResult?.1, expected.wholeMatch?.1, file: file, line: line)

      XCTAssertEqual(firstResult?.0, expected.firstMatch?.0, file: file, line: line)
      XCTAssertEqual(firstResult?.1, expected.firstMatch?.1, file: file, line: line)

      XCTAssertEqual(prefixResult?.0, expected.prefixMatch?.0, file: file, line: line)
      XCTAssertEqual(prefixResult?.1, expected.prefixMatch?.1, file: file, line: line)
    }

    var regex = Regex {
      OneOrMore(.digit)
    }

    customTest(regex, Concat("amy", 2023), expected:(nil, "2023", nil)) // amy2023
    customTest(regex, Concat("amy2023", nil), expected:(nil, "2023", nil))
    customTest(regex, Concat("amy", nil), expected:(nil, nil, nil))
    customTest(regex, Concat("", 2023), expected:("2023", "2023", "2023")) // 2023
    customTest(regex, Concat("bob012b", 2023), expected:(nil, "012", nil)) // b012b2023
    customTest(regex, Concat("bob012b", nil), expected:(nil, "012", nil))
    customTest(regex, Concat("007bob", 2023), expected:(nil, "007", "007"))
    customTest(regex, Concat("", nil), expected:(nil, nil, nil))

    regex = Regex {
      OneOrMore(CharacterClass("a"..."z"))
    }

    customTest(regex, Concat("amy", 2023), expected:(nil, "amy", "amy")) // amy2023
    customTest(regex, Concat("amy", nil), expected:("amy", "amy", "amy"))
    customTest(regex, Concat("amy2022-bob", 2023), expected:(nil, "amy", "amy")) // amy2023
    customTest(regex, Concat("", 2023), expected:(nil, nil, nil)) // 2023
    customTest(regex, Concat("bob012b", 2023), expected:(nil, "bob", "bob")) // b012b2023
    customTest(regex, Concat("bob012b", nil), expected:(nil, "bob", "bob"))
    customTest(regex, Concat("007bob", 2023), expected:(nil, "bob", nil))
    customTest(regex, Concat("", nil), expected:(nil, nil, nil))

    regex = Regex {
      OneOrMore {
        CharacterClass("A"..."Z")
        OneOrMore(CharacterClass("a"..."z"))
        Repeat(.digit, count: 2)
      }
    }

    customTest(regex, Concat("Amy12345", nil), expected:(nil, "Amy12", "Amy12"))
    customTest(regex, Concat("Amy", 2023), expected:(nil, "Amy20", "Amy20"))
    customTest(regex, Concat("Amy", 23), expected:("Amy23", "Amy23", "Amy23"))
    customTest(regex, Concat("", 2023), expected:(nil, nil, nil)) // 2023
    customTest(regex, Concat("Amy23 Boba17", nil), expected:(nil, "Amy23", "Amy23"))
    customTest(regex, Concat("amy23 Boba17", nil), expected:(nil, "Boba17", nil))
    customTest(regex, Concat("Amy23 boba17", nil), expected:(nil, "Amy23", "Amy23"))
    customTest(regex, Concat("amy23 Boba", 17), expected:(nil, "Boba17", nil))
    customTest(regex, Concat("Amy23Boba17", nil), expected:("Amy23Boba17", "Amy23Boba17", "Amy23Boba17"))
    customTest(regex, Concat("Amy23Boba", 17), expected:("Amy23Boba17", "Amy23Boba17", "Amy23Boba17"))
    customTest(regex, Concat("23 Boba", 17), expected:(nil, "Boba17", nil))

    let twoDigitRegex = Regex {
      OneOrMore {
        CharacterClass("A"..."Z")
        OneOrMore(CharacterClass("a"..."z"))
        Capture<(Substring, Int?)>(Repeat(.digit, count: 2)) { Int($0) }
      }
    }

    customTest(twoDigitRegex, Concat("Amy12345", nil), expected: (nil, ("Amy12", 12), ("Amy12", 12)))
    customTest(twoDigitRegex, Concat("Amy", 12345), expected: (nil, ("Amy12", 12), ("Amy12", 12)))
    customTest(twoDigitRegex, Concat("Amy", 12), expected: (("Amy12", 12), ("Amy12", 12), ("Amy12", 12)))
    customTest(twoDigitRegex, Concat("Amy23 Boba", 17), expected: (nil, firstMatch: ("Amy23", 23), prefixMatch: ("Amy23", 23)))
    customTest(twoDigitRegex, Concat("amy23 Boba20", 23), expected:(nil, ("Boba20", 20), nil))
    customTest(twoDigitRegex, Concat("Amy23Boba17", nil), expected:(("Amy23Boba17", 17), ("Amy23Boba17", 17), ("Amy23Boba17", 17)))
    customTest(twoDigitRegex, Concat("Amy23Boba", 17), expected:(("Amy23Boba17", 17), ("Amy23Boba17", 17), ("Amy23Boba17", 17)))

    let millennium = Regex {
      CharacterClass("A"..."Z")
      OneOrMore(CharacterClass("a"..."z"))
      Capture { Repeat(.digit, count: 4) } transform: { v -> Int? in
        guard let year = Int(v) else { return nil }
        return year > 2000 ? year : nil
      }
    }

    customTest(millennium, Concat("Amy2025", nil), expected: (("Amy2025", 2025), ("Amy2025", 2025), ("Amy2025", 2025)))
    customTest(millennium, Concat("Amy", 2025), expected: (("Amy2025", 2025), ("Amy2025", 2025), ("Amy2025", 2025)))
    customTest(millennium, Concat("Amy1995", nil), expected: (("Amy1995", nil), ("Amy1995", nil), ("Amy1995", nil)))
    customTest(millennium, Concat("Amy", 1995), expected: (("Amy1995", nil), ("Amy1995", nil), ("Amy1995", nil)))
    customTest(millennium, Concat("amy2025", nil), expected: (nil, nil, nil))
    customTest(millennium, Concat("amy", 2025), expected: (nil, nil, nil))
  }
}