From fe1c475e34dea13692266b651e7137e84f391af9 Mon Sep 17 00:00:00 2001 From: Adin Date: Mon, 27 Jul 2026 14:50:51 +0800 Subject: [PATCH] test: cover leading plus transform values --- .../SVGParsingUtilitiesTests.swift | 92 +++++++++---------- 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/Tests/SVGViewTests/SVGParsingUtilitiesTests.swift b/Tests/SVGViewTests/SVGParsingUtilitiesTests.swift index b61a01a..9450b9f 100644 --- a/Tests/SVGViewTests/SVGParsingUtilitiesTests.swift +++ b/Tests/SVGViewTests/SVGParsingUtilitiesTests.swift @@ -147,23 +147,50 @@ final class SVGParsingUtilitiesTests: XCTestCase { } } - func testTransformParserMatchesFoundationRegex() throws { - let values = [ - "translate(10, 20)", - "scale(2) rotate(-45)", - "matrix(1 0 0 1 10 -20)", - "translate(1e2, -2.5e-1)\nscale(0.5)", - "skewX(12.5) skewY(-8)", - ] + func testTransformParserAcceptsLeadingPlus() { + let operations = SVGTransformParser.operations(in: "translate(0, +40)") - for value in values { - let expected = try foundationTransformOperations(in: value) - let actual = SVGTransformParser.operations(in: value) + XCTAssertEqual(operations.count, 1) + XCTAssertEqual(operations[0].name, "translate") + XCTAssertEqual(operations[0].values, [0, 40]) + } - XCTAssertEqual(actual.count, expected.count, value) - for (actualOperation, expectedOperation) in zip(actual, expected) { - XCTAssertEqual(actualOperation.name, expectedOperation.name, value) - XCTAssertEqual(actualOperation.values, expectedOperation.values, value) + func testTransformParserParsesSupportedSVGNumberSyntax() { + let cases: [(value: String, expected: [(name: String, values: [Double])])] = [ + ("translate(10, 20)", [("translate", [10, 20])]), + ("scale(2) rotate(-45)", [("scale", [2]), ("rotate", [-45])]), + ( + "matrix(1 0 0 1 10 -20)", + [("matrix", [1, 0, 0, 1, 10, -20])] + ), + ( + "translate(1e2, -2.5e-1)\nscale(0.5)", + [("translate", [100, -0.25]), ("scale", [0.5])] + ), + ( + "skewX(12.5) skewY(-8)", + [("skewX", [12.5]), ("skewY", [-8])] + ), + ] + + for testCase in cases { + let actual = SVGTransformParser.operations(in: testCase.value) + + XCTAssertEqual(actual.count, testCase.expected.count, testCase.value) + for (actualOperation, expectedOperation) in zip( + actual, + testCase.expected + ) { + XCTAssertEqual( + actualOperation.name, + expectedOperation.name, + testCase.value + ) + XCTAssertEqual( + actualOperation.values, + expectedOperation.values, + testCase.value + ) } } } @@ -198,39 +225,4 @@ final class SVGParsingUtilitiesTests: XCTestCase { return result } - private func foundationTransformOperations( - in value: String - ) throws -> [(name: String, values: [Double])] { - let attributeRegex = try NSRegularExpression( - pattern: "([a-z]+)\\(((\\-?\\d+\\.?\\d*e?\\-?\\d*\\s*,?\\s*)+)\\)", - options: .caseInsensitive - ) - let numberRegex = try NSRegularExpression( - pattern: "\\-?\\d+\\.?\\d*e?\\-?\\d*", - options: .caseInsensitive - ) - - var remaining = value.replacingOccurrences(of: "\n", with: "") - var result = [(name: String, values: [Double])]() - - while let match = attributeRegex.firstMatch( - in: remaining, - range: NSRange(remaining.startIndex..