From 1cefd179b5318d85a1fc84589d4dd3be0ee4e248 Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Tue, 14 May 2019 11:28:53 +0300 Subject: [PATCH 1/7] Add exit status --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index d9f6ca179..2f57d0502 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,13 +23,13 @@ jobs: install: skip env: TEST_DEVICE='platform=iOS Simulator,OS=12.2,name=iPhone SE' script: - - xcodebuild -scheme TokamakUIKit -sdk iphonesimulator | xcpretty - - xcodebuild -scheme TokamakAppKit -sdk macosx | xcpretty - - xcodebuild test -scheme TokamakCLI -sdk macosx | xcpretty + - set -o pipefail && xcodebuild -scheme TokamakUIKit -sdk iphonesimulator | xcpretty + - set -o pipefail && xcodebuild -scheme TokamakAppKit -sdk macosx | xcpretty + - set -o pipefail && xcodebuild test -scheme TokamakCLI -sdk macosx | xcpretty # this runs the build the second time, but it's the only way to make sure # that `Package.swift` is in a good state - swift build --target Tokamak - - xcodebuild test -enableCodeCoverage YES -scheme Tokamak | xcpretty + - set -o pipefail && xcodebuild test -enableCodeCoverage YES -scheme Tokamak | xcpretty after_success: - bash <(curl -s https://codecov.io/bash) # before_install: @@ -41,5 +41,5 @@ jobs: # - gem install cocoapods # Since Travis is not always on latest version # - pod install --project-directory=Example # - swift build -# - set -o pipefail && xcodebuild test -enableCodeCoverage YES -workspace Example/Tokamak.xcworkspace -scheme Tokamak-Example -sdk iphonesimulator11.0 ONLY_ACTIVE_ARCH=NO | xcpretty +# - set -o pipefail && set -o pipefail && xcodebuild test -enableCodeCoverage YES -workspace Example/Tokamak.xcworkspace -scheme Tokamak-Example -sdk iphonesimulator11.0 ONLY_ACTIVE_ARCH=NO | xcpretty # - pod lib lint From f69e55ee77ef4a11a5b51e0ef2ac74d6bb48875c Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Tue, 14 May 2019 13:48:26 +0300 Subject: [PATCH 2/7] Fix Color --- Sources/Tokamak/Components/Props/Color.swift | 42 ++++---------------- Tests/TokamakTests/ColorTests.swift | 27 +++++++++++++ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/Sources/Tokamak/Components/Props/Color.swift b/Sources/Tokamak/Components/Props/Color.swift index 61ae6d887..fb0ffc5e5 100644 --- a/Sources/Tokamak/Components/Props/Color.swift +++ b/Sources/Tokamak/Components/Props/Color.swift @@ -49,46 +49,18 @@ extension Color: ExpressibleByIntegerLiteral { extension Color { public init?(hex: String) { - let cString = hex.utf8CString - - // - 1 for the trailing null terminator - let hexSize = cString.count - 1 - - // If the first character is a '#', skip it - var offset = cString.first == 0x23 ? 1 : 0 - - // We only support 6 hexadecimal characters - if hexSize - offset != 6 { - return nil - } - - func nextByte() -> Int8? { - // Take the first byte as the high 4 bits - // Then the second byte as the low 4 bits - if - let high = cString[offset].hexDecoded(), - let low = cString[offset].hexDecoded() { - // In this case, unchecked is still safe as it's between 0 and 6 - offset = offset &+ 2 - - // Adds the two 4-bit pairs together to form a full byte - return (high << 4) & low - } - - return nil - } + let cArray = Array(hex.replacingOccurrences(of: "#", with: "")) guard - let red = nextByte(), - let green = nextByte(), - let blue = nextByte() + let red = Int(String(cArray[0...1]), radix: 16), + let green = Int(String(cArray[2...3]), radix: 16), + let blue = Int(String(cArray[4...5]), radix: 16) else { return nil } - - self.red = Double(UInt8(bitPattern: red)) / 255 - self.green = Double(UInt8(bitPattern: green)) / 255 - self.blue = Double(UInt8(bitPattern: blue)) / 255 + self.red = Double(red) / 255 + self.green = Double(green) / 255 + self.blue = Double(blue) / 255 alpha = 1 space = .sRGB } diff --git a/Tests/TokamakTests/ColorTests.swift b/Tests/TokamakTests/ColorTests.swift index fc691af09..aaac71fc2 100644 --- a/Tests/TokamakTests/ColorTests.swift +++ b/Tests/TokamakTests/ColorTests.swift @@ -17,5 +17,32 @@ final class ColorTests: XCTestCase { Color(hex: "FF00FF"), "The '#' before a hex code produced a different output than without it" ) + + guard let red = Color(hex: "#FF0000") else { + XCTFail("Hexadecimal decoding failed") + return + } + + XCTAssertEqual(red.red, 1) + XCTAssertEqual(red.green, 0) + XCTAssertEqual(red.blue, 0) + + guard let green = Color(hex: "#00FF00") else { + XCTFail("Hexadecimal decoding failed") + return + } + + XCTAssertEqual(green.red, 0) + XCTAssertEqual(green.green, 1) + XCTAssertEqual(green.blue, 0) + + guard let blue = Color(hex: "#0000FF") else { + XCTFail("Hexadecimal decoding failed") + return + } + + XCTAssertEqual(blue.red, 0) + XCTAssertEqual(blue.green, 0) + XCTAssertEqual(blue.blue, 1) } } From a904f653364c9a05088028afbb551e028e8f9b87 Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Tue, 14 May 2019 14:02:48 +0300 Subject: [PATCH 3/7] Update travis.yml script --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 2f57d0502..7d9073ec7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,6 +23,8 @@ jobs: install: skip env: TEST_DEVICE='platform=iOS Simulator,OS=12.2,name=iPhone SE' script: + - swift package generate-xcodeproj --xcconfig-overrides Package.xcconfig + - git checkout HEAD Tokamak.xcodeproj/xcshareddata/xcschemes/TokamakCLI.xcscheme - set -o pipefail && xcodebuild -scheme TokamakUIKit -sdk iphonesimulator | xcpretty - set -o pipefail && xcodebuild -scheme TokamakAppKit -sdk macosx | xcpretty - set -o pipefail && xcodebuild test -scheme TokamakCLI -sdk macosx | xcpretty From 97b2860224683edc92eace4fe02a3d4758510ebf Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Tue, 14 May 2019 14:32:59 +0300 Subject: [PATCH 4/7] Remove unused extension --- Sources/Tokamak/Components/Props/Color.swift | 30 -------------------- 1 file changed, 30 deletions(-) diff --git a/Sources/Tokamak/Components/Props/Color.swift b/Sources/Tokamak/Components/Props/Color.swift index fb0ffc5e5..a0b766cfe 100644 --- a/Sources/Tokamak/Components/Props/Color.swift +++ b/Sources/Tokamak/Components/Props/Color.swift @@ -65,33 +65,3 @@ extension Color { space = .sRGB } } - -private extension Int8 { - func hexDecoded() -> Int8? { - // If the character is between 0x30 and 0x39 it is a textual number - // 0x30 is equal to the ASCII `0` and 0x30 is equal to `0x39` - if self >= 0x30 && self <= 0x39 { - // The binary representation of this character can be found by subtracting `0` in ASCII - // This will then match `0` in binary. Which means `1` in ASCII matches `1` in binary - return self &- 0x30 - } else if self >= 0x41 && self <= 0x46 { - // This block executes if the integer is within the `a-z` lowercased ASCII range - // Then uses the algorithm described below to find the correct representation - return self &- Int8.lowercasedOffset - } else if self >= 0x61 && self <= 0x66 { - // This block executes if the integer is within the `A-Z` uppercased ASCII range - // Then uses the algorithm described below to find the correct representation - return self &- Int8.uppercasedOffset - } - - return nil - } - - // 'a' in hexadecimal is equal to `10` in decimal - // So by subtracting `a` we get the lowercased character narrowed down to base10 offset by 10 - // By adding 10 (or reducing the subtraction size by 10) we represent this character correctly as base10 - static let lowercasedOffset: Int8 = 0x41 &- 10 - - // The same as the lowercasedOffset, except for uppercased ASCII - static let uppercasedOffset: Int8 = 0x61 &- 10 -} From 0bd6a4c1b76281c278dcae07bbb26bdfcab8ec17 Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Tue, 14 May 2019 14:33:17 +0300 Subject: [PATCH 5/7] Add condition for safe --- Sources/Tokamak/Components/Props/Color.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Tokamak/Components/Props/Color.swift b/Sources/Tokamak/Components/Props/Color.swift index a0b766cfe..48e7d1be6 100644 --- a/Sources/Tokamak/Components/Props/Color.swift +++ b/Sources/Tokamak/Components/Props/Color.swift @@ -51,6 +51,8 @@ extension Color { public init?(hex: String) { let cArray = Array(hex.replacingOccurrences(of: "#", with: "")) + guard cArray.count == 6 else { return nil } + guard let red = Int(String(cArray[0...1]), radix: 16), let green = Int(String(cArray[2...3]), radix: 16), From 09108b553e09ea80f65e65b6a57eda14cfd1cda9 Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Tue, 14 May 2019 14:33:28 +0300 Subject: [PATCH 6/7] Add Color tests --- Tests/TokamakTests/ColorTests.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Tests/TokamakTests/ColorTests.swift b/Tests/TokamakTests/ColorTests.swift index aaac71fc2..65a0120fb 100644 --- a/Tests/TokamakTests/ColorTests.swift +++ b/Tests/TokamakTests/ColorTests.swift @@ -44,5 +44,11 @@ final class ColorTests: XCTestCase { XCTAssertEqual(blue.red, 0) XCTAssertEqual(blue.green, 0) XCTAssertEqual(blue.blue, 1) + + let broken = Color(hex: "#P000FF") + XCTAssertEqual(broken, nil) + + let short = Color(hex: "F01") + XCTAssertEqual(short, nil) } } From 4ccf41cab6c6e65c3c07119eabcedfe643b79edc Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Tue, 14 May 2019 14:46:07 +0300 Subject: [PATCH 7/7] Add codecov.yml --- codecov.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 codecov.yml diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 000000000..9c575e4ec --- /dev/null +++ b/codecov.yml @@ -0,0 +1,3 @@ +ignore: + - "Example" # ignore folders and all its contents + - "Tests"