@@ -147,23 +147,50 @@ final class SVGParsingUtilitiesTests: XCTestCase {
147147 }
148148 }
149149
150- func testTransformParserMatchesFoundationRegex( ) throws {
151- let values = [
152- " translate(10, 20) " ,
153- " scale(2) rotate(-45) " ,
154- " matrix(1 0 0 1 10 -20) " ,
155- " translate(1e2, -2.5e-1) \n scale(0.5) " ,
156- " skewX(12.5) skewY(-8) " ,
157- ]
150+ func testTransformParserAcceptsLeadingPlus( ) {
151+ let operations = SVGTransformParser . operations ( in: " translate(0, +40) " )
158152
159- for value in values {
160- let expected = try foundationTransformOperations ( in: value)
161- let actual = SVGTransformParser . operations ( in: value)
153+ XCTAssertEqual ( operations. count, 1 )
154+ XCTAssertEqual ( operations [ 0 ] . name, " translate " )
155+ XCTAssertEqual ( operations [ 0 ] . values, [ 0 , 40 ] )
156+ }
162157
163- XCTAssertEqual ( actual. count, expected. count, value)
164- for (actualOperation, expectedOperation) in zip ( actual, expected) {
165- XCTAssertEqual ( actualOperation. name, expectedOperation. name, value)
166- XCTAssertEqual ( actualOperation. values, expectedOperation. values, value)
158+ func testTransformParserParsesSupportedSVGNumberSyntax( ) {
159+ let cases : [ ( value: String , expected: [ ( name: String , values: [ Double ] ) ] ) ] = [
160+ ( " translate(10, 20) " , [ ( " translate " , [ 10 , 20 ] ) ] ) ,
161+ ( " scale(2) rotate(-45) " , [ ( " scale " , [ 2 ] ) , ( " rotate " , [ - 45 ] ) ] ) ,
162+ (
163+ " matrix(1 0 0 1 10 -20) " ,
164+ [ ( " matrix " , [ 1 , 0 , 0 , 1 , 10 , - 20 ] ) ]
165+ ) ,
166+ (
167+ " translate(1e2, -2.5e-1) \n scale(0.5) " ,
168+ [ ( " translate " , [ 100 , - 0.25 ] ) , ( " scale " , [ 0.5 ] ) ]
169+ ) ,
170+ (
171+ " skewX(12.5) skewY(-8) " ,
172+ [ ( " skewX " , [ 12.5 ] ) , ( " skewY " , [ - 8 ] ) ]
173+ ) ,
174+ ]
175+
176+ for testCase in cases {
177+ let actual = SVGTransformParser . operations ( in: testCase. value)
178+
179+ XCTAssertEqual ( actual. count, testCase. expected. count, testCase. value)
180+ for (actualOperation, expectedOperation) in zip (
181+ actual,
182+ testCase. expected
183+ ) {
184+ XCTAssertEqual (
185+ actualOperation. name,
186+ expectedOperation. name,
187+ testCase. value
188+ )
189+ XCTAssertEqual (
190+ actualOperation. values,
191+ expectedOperation. values,
192+ testCase. value
193+ )
167194 }
168195 }
169196 }
@@ -198,39 +225,4 @@ final class SVGParsingUtilitiesTests: XCTestCase {
198225 return result
199226 }
200227
201- private func foundationTransformOperations(
202- in value: String
203- ) throws -> [ ( name: String , values: [ Double ] ) ] {
204- let attributeRegex = try NSRegularExpression (
205- pattern: " ([a-z]+) \\ ((( \\ -? \\ d+ \\ .? \\ d*e? \\ -? \\ d* \\ s*,? \\ s*)+) \\ ) " ,
206- options: . caseInsensitive
207- )
208- let numberRegex = try NSRegularExpression (
209- pattern: " \\ -? \\ d+ \\ .? \\ d*e? \\ -? \\ d* " ,
210- options: . caseInsensitive
211- )
212-
213- var remaining = value. replacingOccurrences ( of: " \n " , with: " " )
214- var result = [ ( name: String, values: [ Double] ) ] ( )
215-
216- while let match = attributeRegex. firstMatch (
217- in: remaining,
218- range: NSRange ( remaining. startIndex..< remaining. endIndex, in: remaining)
219- ) {
220- let name = ( remaining as NSString ) . substring ( with: match. range ( at: 1 ) )
221- let valuesString = ( remaining as NSString ) . substring ( with: match. range ( at: 2 ) )
222- let values = numberRegex. matches (
223- in: valuesString,
224- range: NSRange ( valuesString. startIndex..< valuesString. endIndex, in: valuesString)
225- ) . compactMap { match in
226- Double ( ( valuesString as NSString ) . substring ( with: match. range) )
227- }
228- result. append ( ( name, values) )
229-
230- let consumedLength = match. range. location + match. range. length
231- remaining = ( remaining as NSString ) . substring ( from: consumedLength)
232- }
233-
234- return result
235- }
236228}
0 commit comments