Skip to content

Commit

Permalink
Write unit tests for Color+Extension.swift (#101)
Browse files Browse the repository at this point in the history
- Wrote tests
- Possible minor bug where there's an alpha missing on 32-bit colors?
  • Loading branch information
AdrianBinDC authored May 26, 2020
1 parent 99b952f commit 4963ec5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/SwiftUICharts/Base/Extensions/Color+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extension Color {
case 6: // RGB (24-bit)
(red, green, blue) = (int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
// FIXME: I think we need an an alpha value on this one. See link below.
// https://stackoverflow.com/a/56874327/4475605
(red, green, blue) = (int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(red, green, blue) = (0, 0, 0)
Expand Down
56 changes: 56 additions & 0 deletions Tests/SwiftUIChartsTests/ColorExtensionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// ColorExtensionTests.swift
// SwiftUIChartsTests
//
// Created by Adrian Bolinger on 5/24/20.
//

@testable import SwiftUICharts
import SwiftUI
import XCTest

class ColorExtensionTests: XCTestCase {
func testTwentyFourBitRGBColors() {
let actualWhite = Color(hexString: "FFFFFF")
let expectedWhite = Color(red: 1, green: 1, blue: 1)
XCTAssertEqual(actualWhite, expectedWhite)

let actualBlack = Color(hexString: "000000")
let expectedBlack = Color(red: 0, green: 0, blue: 0)
XCTAssertEqual(actualBlack, expectedBlack)

let actualRed = Color(hexString: "FF0000")
let expectedRed = Color(red: 255/255, green: 0, blue: 0)
XCTAssertEqual(actualRed, expectedRed)

let actualGreen = Color(hexString: "00FF00")
let expectedGreen = Color(red: 0, green: 1, blue: 0)
XCTAssertEqual(actualGreen, expectedGreen)

let actualBlue = Color(hexString: "0000FF")
let expectedBlue = Color(red: 0, green: 0, blue: 1)
XCTAssertEqual(actualBlue, expectedBlue)
}

func testTwelveBitRGBColors() {
let actualWhite = Color(hexString: "FFF")
let expectedWhite = Color(red: 1, green: 1, blue: 1)
XCTAssertEqual(actualWhite, expectedWhite)

let actualBlack = Color(hexString: "000")
let expectedBlack = Color(red: 0, green: 0, blue: 0)
XCTAssertEqual(actualBlack, expectedBlack)

let actualRed = Color(hexString: "F00")
let expectedRed = Color(red: 255/255, green: 0, blue: 0)
XCTAssertEqual(actualRed, expectedRed)

let actualGreen = Color(hexString: "0F0")
let expectedGreen = Color(red: 0, green: 1, blue: 0)
XCTAssertEqual(actualGreen, expectedGreen)

let actualBlue = Color(hexString: "00F")
let expectedBlue = Color(red: 0, green: 0, blue: 1)
XCTAssertEqual(actualBlue, expectedBlue)
}
}

0 comments on commit 4963ec5

Please sign in to comment.