-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathClassConformanceTests.swift
78 lines (65 loc) · 1.62 KB
/
ClassConformanceTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// ClassConformanceTests.swift
// Ladybug Tests
//
// Created by Jeff Hurray on 8/9/17.
// Copyright © 2017 jhurray. All rights reserved.
//
import XCTest
@testable import Ladybug
class Shirt1: JSONCodable {
static let jsonString: String = """
{
"cost": 19.99,
"size": "large"
}
"""
enum Size: String, Codable {
case small, medium, large
}
var cost: Double = 0
var size: Size = .small
}
class Shirt2: JSONCodable {
static let jsonString: String = """
{
"cost": 19.99,
"size": "large"
}
"""
enum Size: String, Codable {
case small, medium, large
}
let cost: Double
let size: Size
init() {
cost = 0
size = .small
}
}
class ClassConformanceTests: XCTestCase {
func testCreationWithVars() {
let data = Shirt1.jsonString.data(using: .utf8)!
do {
let json = try JSONSerialization.jsonObject(with: data)
let shirt = try Shirt1(json: json)
XCTAssertEqual(shirt.cost, 19.99)
XCTAssertEqual(shirt.size, .large)
}
catch let error {
XCTFail("Caught error: \(error)")
}
}
func testCreationWithInit() {
let data = Shirt2.jsonString.data(using: .utf8)!
do {
let json = try JSONSerialization.jsonObject(with: data)
let shirt = try Shirt2(json: json)
XCTAssertEqual(shirt.cost, 19.99)
XCTAssertEqual(shirt.size, .large)
}
catch let error {
XCTFail("Caught error: \(error)")
}
}
}