Skip to content
This repository was archived by the owner on May 20, 2026. It is now read-only.

Commit cb4a3e8

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

7 files changed

Lines changed: 119 additions & 89 deletions

File tree

Sources/YCoreUI/Protocols/Colorable.swift

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,51 @@
55
//
66
import UIKit
77

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

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

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

23-
/// Color to be displayed
31+
/// A color asset for this name value.
32+
///
33+
/// Default implementation calls `loadColor` and nil-coalesces to `fallbackColor`.
2434
var color: UIColor { get }
2535
}
2636

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

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

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

38-
/// Color to be displayed based on namespace
39-
/// - Returns: Color to be displayed
40-
func loadColor() -> UIColor? {
47+
/// Loads the named color.
48+
///
49+
/// Default implementation uses `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
50+
/// (prepended to `rawValue`) and `bundle`.
51+
/// - Returns: The named color or else `nil` if the named asset cannot be loaded
52+
public func loadColor() -> UIColor? {
4153
let name: String
4254
if let validNamespace = Self.namespace {
4355
name = "\(validNamespace)/\(rawValue)"
@@ -47,7 +59,8 @@ extension Colorable {
4759
return UIColor(named: name, in: Self.bundle, compatibleWith: nil)
4860
}
4961

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

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)