Create unit test for color JSON

This commit is contained in:
Sven Weidauer 2021-01-02 22:03:41 +01:00
parent be2a655c40
commit 1c31c11f49
4 changed files with 41 additions and 2 deletions

View file

@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "RBBJSON",
"repositoryURL": "https://github.com/robb/RBBJSON",
"state": {
"branch": "main",
"revision": "102c970283e105d7c5be2e29630db29c808c20eb",
"version": null
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",

View file

@ -10,6 +10,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.3.1")),
.package(url: "https://github.com/robb/RBBJSON", .branch("main")),
],
targets: [
.target(
@ -26,7 +27,10 @@ let package = Package(
),
.testTarget(
name: "MakeColorsTests",
dependencies: ["LibMakeColors"]
dependencies: [
"LibMakeColors",
.product(name: "RBBJSON", package: "RBBJSON"),
]
),
]
)

View file

@ -58,7 +58,7 @@ private let infoTag = """
}
"""
private extension Color {
extension Color {
func json() -> String {
"""
{

View file

@ -0,0 +1,26 @@
@testable import LibMakeColors
import RBBJSON
import XCTest
class AssetCatalogFormattingTest: XCTestCase {
func testColorProducedValidJSON() throws {
let color = Color(red: 0xFF, green: 0xF0, blue: 0x0F)
let data = Data(color.json().utf8)
let json = try JSONDecoder().decode(RBBJSON.self, from: data)
XCTAssertEqual(json.info.author, .string("de.5sw.MakeColors"))
XCTAssertEqual(json.info.version, .number(1))
XCTAssertEqual(Array(json.colors[any: .child]).count, 1)
XCTAssertEqual(json.colors[0].idiom, .string("universal"))
let jsonColor = json.colors[0].color
XCTAssertEqual(jsonColor["color-space"], .string("srgb"))
XCTAssertEqual(jsonColor.components.alpha, .string("1.0"))
XCTAssertEqual(jsonColor.components.red, .string("0xFF"))
XCTAssertEqual(jsonColor.components.green, .string("0xF0"))
XCTAssertEqual(jsonColor.components.blue, .string("0x0F"))
}
}