From 4406619c84764dce53c72b1da038cc402fb6174d Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 31 Dec 2020 20:16:41 +0100 Subject: [PATCH] Simplify color parser tests --- Tests/MakeColorsTests/ColorParserTest.swift | 38 +++++++++------------ 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/Tests/MakeColorsTests/ColorParserTest.swift b/Tests/MakeColorsTests/ColorParserTest.swift index 3db8714..708c77d 100644 --- a/Tests/MakeColorsTests/ColorParserTest.swift +++ b/Tests/MakeColorsTests/ColorParserTest.swift @@ -3,68 +3,62 @@ import XCTest final class ColorParserTest: XCTestCase { func testScanningThreeDigitColor() throws { - let scanner = Scanner(string: "#abc") - let color = scanner.color() + let color = scanColor("#abc") XCTAssertEqual(Color(red: 0xAA, green: 0xBB, blue: 0xCC), color) } func testScanningThreeDigitColorUppercase() throws { - let scanner = Scanner(string: "#ABc") - let color = scanner.color() + let color = scanColor("#ABc") XCTAssertEqual(Color(red: 0xAA, green: 0xBB, blue: 0xCC), color) } func testScanningFourDigitColor() throws { - let scanner = Scanner(string: "#abcd") - let color = scanner.color() + let color = scanColor("#abcd") XCTAssertEqual(Color(red: 0xAA, green: 0xBB, blue: 0xCC, alpha: 0xDD), color) } func testScanningSixDigitColor() throws { - let scanner = Scanner(string: "#abcdef") - let color = scanner.color() + let color = scanColor("#abcdef") XCTAssertEqual(Color(red: 0xAB, green: 0xCD, blue: 0xEF), color) } func testScanningEightDigitColor() throws { - let scanner = Scanner(string: "#abcdef17") - let color = scanner.color() + let color = scanColor("#abcdef17") XCTAssertEqual(Color(red: 0xAB, green: 0xCD, blue: 0xEF, alpha: 0x17), color) } func testScanningRGBColor() throws { - let scanner = Scanner(string: "rgb(1,2,3)") - let color = scanner.color() + let color = scanColor("rgb(1,2,3)") XCTAssertEqual(Color(red: 1, green: 2, blue: 3), color) } func testScanningRGBAColor() throws { - let scanner = Scanner(string: "rgba(1,2,3,4)") - let color = scanner.color() + let color = scanColor("rgba(1,2,3,4)") XCTAssertEqual(Color(red: 1, green: 2, blue: 3, alpha: 4), color) } func testScanningWhite() throws { - let scanner = Scanner(string: "white(255)") - let color = scanner.color() + let color = scanColor("white(255)") XCTAssertEqual(Color(red: 255, green: 255, blue: 255, alpha: 255), color) } func testScanningWhiteWithAlpha() throws { - let scanner = Scanner(string: "white(255, 128)") - let color = scanner.color() + let color = scanColor("white(255, 128)") XCTAssertEqual(Color(red: 255, green: 255, blue: 255, alpha: 128), color) } func testWhiteFailsWithoutArguments() throws { - let scanner = Scanner(string: "white()") - let color = scanner.color() + let color = scanColor("white()") XCTAssertNil(color) } func testWhiteFailsWith3Arguments() throws { - let scanner = Scanner(string: "white(1,2,3)") - let color = scanner.color() + let color = scanColor("white(1,2,3)") XCTAssertNil(color) } + + private func scanColor(_ input: String) -> Color? { + let scanner = Scanner(string: input) + return scanner.color() + } }