@@ -5,153 +5,137 @@ import RegexBuilder
55
66private let enablePrinting = false
77
8- @available ( SwiftStdlib 5 . 7 , * )
9- extension RegexDSLTests {
10-
11- func testContrivedAROExample( ) {
12- // Find and extract potential IDs. IDs are 8 bytes encoded as
13- // 16 hexadecimal numbers, with an optional `-` between every
14- // double-byte (i.e. 4 hex digits).
15- //
16- // AAAA-BBBB-CCCC-DDDD
17- // AAAABBBBCCCCDDDD
18- // AAAABBBBCCCC-DDDD
19- //
20- // IDs are converted to uppercase and hyphen separated
21- //
22- // The regex can have special capture names which affect replacement
23- // behavior
24- // - "salient": presented uppercase in square brackets after
25- // - "note": presented lowercase in parens
26- // - none: nothing
27- // - no captures: "<error>"
28- //
29- let input = """
30- Machine 1234-5678-90ab-CDEF connected
31- Session FEDCAB0987654321 ended
32- Artiface 0011deff-2231-abcd contrived
33- """
34- let noCapOutput = """
35- Machine <error> connected
36- Session <error> ended
37- Artiface <error> contrived
38- """
39- let unnamedOutput = """
40- Machine 1234-5678-90AB-CDEF connected
41- Session FEDC-AB09-8765-4321 ended
42- Artiface 0011-DEFF-2231-ABCD contrived
43- """
44- let salientOutput = """
45- Machine 1234-5678-90AB-CDEF [5678] connected
46- Session FEDC-AB09-8765-4321 [AB09] ended
47- Artiface 0011-DEFF-2231-ABCD [DEFF] contrived
48- """
49- let noteOutput = """
50- Machine 1234-5678-90AB-CDEF (5678) connected
51- Session FEDC-AB09-8765-4321 (ab09) ended
52- Artiface 0011-DEFF-2231-ABCD (deff) contrived
53- """
54-
55- enum Kind {
56- case none
57- case unnamed
58- case salient
59- case note
60-
61- func contains( captureNamed s: String ) -> Bool {
62- switch self {
63- case . none: return false
64- case . unnamed: return false
65- case . salient: return s == " salient "
66- case . note: return s == " note "
67- }
68- }
69-
70- var expected : String {
71- switch self {
72- case . none: return """
73- Machine <error> connected
74- Session <error> ended
75- Artiface <error> contrived
76- """
77- case . unnamed: return """
78- Machine 1234-5678-90AB-CDEF connected
79- Session FEDC-AB09-8765-4321 ended
80- Artiface 0011-DEFF-2231-ABCD contrived
81- """
82- case . salient: return """
83- Machine 1234-5678-90AB-CDEF [5678] connected
84- Session FEDC-AB09-8765-4321 [AB09] ended
85- Artiface 0011-DEFF-2231-ABCD [DEFF] contrived
86- """
87- case . note: return """
88- Machine 1234-5678-90AB-CDEF (5678) connected
89- Session FEDC-AB09-8765-4321 (ab09) ended
90- Artiface 0011-DEFF-2231-ABCD (deff) contrived
91- """
92- }
93- }
8+ // Find and extract potential IDs. IDs are 8 bytes encoded as
9+ // 16 hexadecimal numbers, with an optional `-` between every
10+ // double-byte (i.e. 4 hex digits).
11+ //
12+ // AAAA-BBBB-CCCC-DDDD
13+ // AAAABBBBCCCCDDDD
14+ // AAAABBBBCCCC-DDDD
15+ //
16+ // IDs are converted to uppercase and hyphen separated
17+ //
18+ // The regex can have special capture names which affect replacement
19+ // behavior
20+ // - "salient": presented uppercase in square brackets after
21+ // - "note": presented lowercase in parens
22+ // - none: nothing
23+ // - no captures: "<error>"
24+ fileprivate let input = """
25+ Machine 1234-5678-90ab-CDEF connected
26+ Session FEDCAB0987654321 ended
27+ Artiface 0011deff-2231-abcd contrived
28+ """
29+ fileprivate let noCapOutput = """
30+ Machine <error> connected
31+ Session <error> ended
32+ Artiface <error> contrived
33+ """
34+ fileprivate let unnamedOutput = """
35+ Machine 1234-5678-90AB-CDEF connected
36+ Session FEDC-AB09-8765-4321 ended
37+ Artiface 0011-DEFF-2231-ABCD contrived
38+ """
39+ fileprivate let salientOutput = """
40+ Machine 1234-5678-90AB-CDEF [5678] connected
41+ Session FEDC-AB09-8765-4321 [AB09] ended
42+ Artiface 0011-DEFF-2231-ABCD [DEFF] contrived
43+ """
44+ fileprivate let noteOutput = """
45+ Machine 1234-5678-90AB-CDEF (5678) connected
46+ Session FEDC-AB09-8765-4321 (ab09) ended
47+ Artiface 0011-DEFF-2231-ABCD (deff) contrived
48+ """
49+
50+ fileprivate enum Kind {
51+ case none
52+ case unnamed
53+ case salient
54+ case note
55+
56+ func contains( captureNamed s: String ) -> Bool {
57+ switch self {
58+ case . none: return false
59+ case . unnamed: return false
60+ case . salient: return s == " salient "
61+ case . note: return s == " note "
9462 }
63+ }
9564
96- func checkContains< Output> (
97- _ re: Regex < Output > , _ kind: Kind
98- ) {
99- for name in [ " " , " salient " , " note " , " other " ] {
100- XCTAssertEqual (
101- kind. contains ( captureNamed: name) , re. contains ( captureNamed: name) )
102- }
65+ var expected : String {
66+ switch self {
67+ case . none: return noCapOutput
68+ case . unnamed: return unnamedOutput
69+ case . salient: return salientOutput
70+ case . note: return noteOutput
10371 }
104- func checkAROReplacing< Output> (
105- _ re: Regex < Output > , _ kind: Kind
106- ) {
107- let aro = Regex < AnyRegexOutput > ( re)
108- let output = input. replacing ( aro) {
109- ( match: Regex < AnyRegexOutput > . Match ) -> String in
110-
111- if match. count < 5 { return " <error> " }
72+ }
73+ }
11274
113- let suffix : String
114- if re. contains ( captureNamed: " salient " ) {
115- let body = match [ " salient " ] !. substring? . uppercased ( ) ?? " <no capture> "
116- suffix = " [ \( body) ] "
117- } else if re. contains ( captureNamed: " note " ) {
118- let body = match [ " note " ] !. substring? . lowercased ( ) ?? " <no capture> "
119- suffix = " ( \( body) ) "
120- } else {
121- suffix = " "
122- }
75+ fileprivate func checkContains< Output> (
76+ _ re: Regex < Output > , _ kind: Kind
77+ ) {
78+ for name in [ " " , " salient " , " note " , " other " ] {
79+ XCTAssertEqual (
80+ kind. contains ( captureNamed: name) , re. contains ( captureNamed: name) )
81+ }
82+ }
12383
124- return match. output. dropFirst ( ) . lazy. map {
125- $0. substring!. uppercased ( )
126- } . joined ( separator: " - " ) + suffix
127- }
84+ fileprivate func checkAROReplacing< Output> (
85+ _ re: Regex < Output > , _ kind: Kind
86+ ) {
87+ let aro = Regex < AnyRegexOutput > ( re)
88+ let output = input. replacing ( aro) {
89+ ( match: Regex < AnyRegexOutput > . Match ) -> String in
90+
91+ if match. count < 5 { return " <error> " }
92+
93+ let suffix : String
94+ if re. contains ( captureNamed: " salient " ) {
95+ let body = match [ " salient " ] !. substring? . uppercased ( ) ?? " <no capture> "
96+ suffix = " [ \( body) ] "
97+ } else if re. contains ( captureNamed: " note " ) {
98+ let body = match [ " note " ] !. substring? . lowercased ( ) ?? " <no capture> "
99+ suffix = " ( \( body) ) "
100+ } else {
101+ suffix = " "
102+ }
128103
129- XCTAssertEqual ( output, kind. expected)
104+ return match. output. dropFirst ( ) . lazy. map {
105+ $0. substring!. uppercased ( )
106+ } . joined ( separator: " - " ) + suffix
107+ }
130108
131- if enablePrinting {
132- print ( " --- " )
133- print ( output)
134- print ( kind)
135- }
136- }
137- func check< Output> (
138- _ re: Regex < Output > , _ kind: Kind , _ expected: String
139- ) {
140- let aro = Regex < AnyRegexOutput > ( re)
109+ XCTAssertEqual ( output, kind. expected)
141110
142- let casted = try ! XCTUnwrap ( Regex ( aro, as: Output . self) )
111+ if enablePrinting {
112+ print ( " --- " )
113+ print ( output)
114+ print ( kind)
115+ }
116+ }
143117
144- // contains(captureNamed:)
145- checkContains ( re, kind)
146- checkContains ( aro, kind)
147- checkContains ( casted, kind)
118+ fileprivate func check< Output> (
119+ _ re: Regex < Output > , _ kind: Kind , _ expected: String
120+ ) {
121+ let aro = Regex < AnyRegexOutput > ( re)
122+ let casted = try ! XCTUnwrap ( Regex ( aro, as: Output . self) )
123+
124+ // contains(captureNamed:)
125+ checkContains ( re, kind)
126+ checkContains ( aro, kind)
127+ checkContains ( casted, kind)
128+
129+ // replacing
130+ checkAROReplacing ( re, kind)
131+ checkAROReplacing ( aro, kind)
132+ checkAROReplacing ( casted, kind)
133+ }
148134
149- // replacing
150- checkAROReplacing ( re, kind)
151- checkAROReplacing ( aro, kind)
152- checkAROReplacing ( casted, kind)
153- }
135+ @available ( SwiftStdlib 5 . 7 , * )
136+ extension RegexDSLTests {
154137
138+ func testContrivedAROExample( ) {
155139 // Literals (mocked up via explicit `as` types)
156140 check ( try ! Regex ( #"""
157141 (?x)
@@ -161,6 +145,7 @@ extension RegexDSLTests {
161145 . none,
162146 noCapOutput
163147 )
148+
164149 check ( try ! Regex ( #"""
165150 (?x)
166151 (\p{hexdigit}{4}) -? (\p{hexdigit}{4}) -?
0 commit comments