Skip to content

Commit 5a1986a

Browse files
author
Mark Pospesel
committed
[CM-929] Fix unit tests and expand documentation
1 parent 6cf844f commit 5a1986a

File tree

7 files changed

+123
-90
lines changed

7 files changed

+123
-90
lines changed
Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,58 @@
11
//
22
// Colorable.swift
3+
// YCoreUI
34
//
4-
// Created by Y Media Labs on 25/10/22.
5+
// Created by Panchami Shenoy on 26/10/22.
6+
// Copyright © 2022 Y Media Labs. All rights reserved.
57
//
8+
69
import UIKit
710

8-
/// Protocol for string based enum that has names of color assets
11+
/// Any named color asset that can be loaded from an asset catalog (primarily for use with string-based enums).
12+
///
13+
/// All properties and functions have default implementations. At a minimum just have your string-based enum conform
14+
/// to `Colorable` (and have an asset catalog with matching assets). If your enum and assets live inside a Swift
15+
/// package, override `bundle` to return `.module`. If your assets are categorized within their asset catalog by
16+
/// a namespace, then override `namespace` to return the proper string prefix.
917
public protocol Colorable: RawRepresentable where RawValue == String {
10-
/// The bundle containing the localized strings for this enum
18+
/// The bundle containing the color assets for this enum (default is `.main`)
1119
static var bundle: Bundle { get }
1220

13-
/// Namespace for color asset
21+
/// Optional namespace for the color assets (default is `nil`)
1422
static var namespace: String? { get }
1523

16-
/// Color to display error cases
24+
/// Fallback color to use in case a color asset cannot be loaded (default is `.systemPink`)
1725
static var fallbackColor: UIColor { get }
18-
19-
/// Color to be displayed based on namespace
20-
/// - Returns: Color to be displayed
26+
27+
/// Loads the named color.
28+
///
29+
/// Default implementation uses `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
30+
/// (prepended to `rawValue`) and `bundle`.
31+
/// - Returns: The named color or else `nil` if the named asset cannot be loaded
2132
func loadColor() -> UIColor?
2233

23-
/// Color to be displayed
34+
/// A color asset for this name value.
35+
///
36+
/// Default implementation calls `loadColor` and nil-coalesces to `fallbackColor`.
2437
var color: UIColor { get }
2538
}
2639

27-
/// Default implementation the `Colorable`
2840
extension Colorable {
29-
/// The bundle containing the localized strings
30-
static var bundle: Bundle { .main }
41+
/// The bundle containing the color assets for this enum (default is `.main`)
42+
public static var bundle: Bundle { .main }
3143

32-
/// Namespace for color asset
33-
static var namespace: String? { nil }
44+
/// Optional namespace for the color assets (default is `nil`)
45+
public static var namespace: String? { nil }
3446

35-
/// Color to display error cases
36-
static var fallbackColor: UIColor { .systemPink }
47+
/// Fallback color to use in case a color asset cannot be loaded (default is `.systemPink`)
48+
public static var fallbackColor: UIColor { .systemPink }
3749

38-
/// Color to be displayed based on namespace
39-
/// - Returns: Color to be displayed
40-
func loadColor() -> UIColor? {
50+
/// Loads the named color.
51+
///
52+
/// Default implementation uses `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
53+
/// (prepended to `rawValue`) and `bundle`.
54+
/// - Returns: The named color or else `nil` if the named asset cannot be loaded
55+
public func loadColor() -> UIColor? {
4156
let name: String
4257
if let validNamespace = Self.namespace {
4358
name = "\(validNamespace)/\(rawValue)"
@@ -47,7 +62,8 @@ extension Colorable {
4762
return UIColor(named: name, in: Self.bundle, compatibleWith: nil)
4863
}
4964

50-
/// Color to be displayed
51-
var color: UIColor { loadColor() ?? Self.fallbackColor }
52-
65+
/// A color asset for this name value.
66+
///
67+
/// Default implementation calls `loadColor` and nil-coalesces to `fallbackColor`.
68+
public var color: UIColor { loadColor() ?? Self.fallbackColor }
5369
}

Tests/YCoreUITests/Assets/Colors/Media.xcassets/Error/error100.colorset/Contents.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"color-space" : "srgb",
66
"components" : {
77
"alpha" : "1.000",
8-
"blue" : "0.824",
9-
"green" : "0.835",
10-
"red" : "1.000"
8+
"blue" : "0xD2",
9+
"green" : "0xD4",
10+
"red" : "0xFF"
1111
}
1212
},
1313
"idiom" : "universal"

Tests/YCoreUITests/Assets/Colors/Media.xcassets/Error/error50.colorset/Contents.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"color-space" : "srgb",
66
"components" : {
77
"alpha" : "1.000",
8-
"blue" : "0.945",
9-
"green" : "0.949",
10-
"red" : "1.000"
8+
"blue" : "0xF0",
9+
"green" : "0xF1",
10+
"red" : "0xFF"
1111
}
1212
},
1313
"idiom" : "universal"

Tests/YCoreUITests/Assets/Colors/Media.xcassets/warning100.colorset/Contents.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"color-space" : "srgb",
66
"components" : {
77
"alpha" : "1.000",
8-
"blue" : "0.690",
9-
"green" : "0.784",
10-
"red" : "0.984"
8+
"blue" : "0xAF",
9+
"green" : "0xC7",
10+
"red" : "0xFA"
1111
}
1212
},
1313
"idiom" : "universal"

Tests/YCoreUITests/Assets/Colors/Media.xcassets/warning50.colorset/Contents.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"color-space" : "srgb",
66
"components" : {
77
"alpha" : "1.000",
8-
"blue" : "0.780",
9-
"green" : "0.820",
10-
"red" : "0.949"
8+
"blue" : "0xC6",
9+
"green" : "0xD1",
10+
"red" : "0xF1"
1111
}
1212
},
1313
"idiom" : "universal"

Tests/YCoreUITests/Extensions/UIKit/ColorableProtocolTest.swift

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// ColorableTests.swift
3+
// YCoreUITests
4+
//
5+
// Created by Panchami Shenoy on 26/10/22.
6+
// Copyright © 2022 Y Media Labs. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import YCoreUI
11+
12+
final class ColorableTests: XCTestCase {
13+
func testBundle() {
14+
XCTAssertEqual(WarningColors.bundle, .module)
15+
XCTAssertEqual(ErrorColors.bundle, .module)
16+
XCTAssertEqual(PrimaryColors.bundle, .main)
17+
}
18+
19+
func testNamespace() {
20+
XCTAssertNil(WarningColors.namespace)
21+
XCTAssertEqual(ErrorColors.namespace, "Error")
22+
XCTAssertNil(PrimaryColors.namespace)
23+
}
24+
25+
func testFallbackColor() {
26+
XCTAssertEqual(WarningColors.fallbackColor, .systemPink)
27+
XCTAssertEqual(ErrorColors.fallbackColor, .systemPink)
28+
XCTAssertEqual(PrimaryColors.fallbackColor, .systemPurple)
29+
}
30+
31+
func testLoadColorWithoutNamespace() {
32+
WarningColors.allCases.forEach {
33+
XCTAssertNotNil($0.loadColor())
34+
}
35+
}
36+
37+
func testLoadColorWithNamespace() {
38+
ErrorColors.allCases.forEach {
39+
XCTAssertNotNil($0.loadColor())
40+
}
41+
}
42+
43+
func testMissingColor() {
44+
PrimaryColors.allCases.forEach {
45+
XCTAssertNil($0.loadColor())
46+
XCTAssertEqual($0.color, PrimaryColors.fallbackColor)
47+
}
48+
}
49+
}
50+
51+
private extension ColorableTests {
52+
enum ErrorColors: String, CaseIterable, Colorable {
53+
case error50
54+
case error100
55+
56+
static var bundle: Bundle { .module }
57+
static var namespace: String? { "Error" }
58+
}
59+
60+
enum WarningColors: String, CaseIterable, Colorable {
61+
case warning50
62+
case warning100
63+
64+
static var bundle: Bundle { .module }
65+
}
66+
67+
enum PrimaryColors: String, CaseIterable, Colorable {
68+
case primary50
69+
case primary100
70+
71+
static var fallbackColor: UIColor { .systemPurple }
72+
}
73+
}

0 commit comments

Comments
 (0)