Skip to content

Commit 703f540

Browse files
bverbekenCopilot
andauthored
Add unit tests (#49)
* simple unit test * use actions/checkout@v4 instead of v2 * enhance CI workflow to include unit tests and enable test discovery * failing test * fixed test * added tests for SeatsioObjectPricing.init() * more unit tests * Update Package.swift Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove unnecessary --enable-test-discovery --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent ac81bbf commit 703f540

3 files changed

Lines changed: 105 additions & 3 deletions

File tree

.github/workflows/build.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ jobs:
1212
runs-on: macos-14
1313

1414
steps:
15-
- uses: actions/checkout@v2
16-
- run: swift build
17-
- run: pod lib lint
15+
- uses: actions/checkout@v4
16+
- name: Build
17+
run: swift build
18+
- name: Run tests
19+
run: swift test --parallel
20+
- name: Lint podspec
21+
run: pod lib lint

Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ let package = Package(
2222
.product(name: "SwiftyJSON", package: "SwiftyJSON")
2323
],
2424
path: "Seatsio/Sources"
25+
),
26+
.testTarget(
27+
name: "SeatsioTests",
28+
dependencies: ["Seatsio"],
29+
path: "Seatsio/Tests"
2530
)
2631
]
2732
)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import XCTest
2+
@testable import Seatsio
3+
4+
final class SeatsioObjectPricingTests: XCTestCase {
5+
6+
func testInitFromJSON_withStringFormattedPrice() throws {
7+
let json = """
8+
{
9+
"ticketType": "VIP",
10+
"price": 120.5,
11+
"formattedPrice": "$120.50",
12+
"originalPrice": 150.0,
13+
"formattedOriginalPrice": "$150.00",
14+
"label": "Early Bird",
15+
"fee": 5.0,
16+
"description": "Front row"
17+
}
18+
""".data(using: .utf8)!
19+
20+
let decoded = try JSONDecoder().decode(SeatsioObjectPricing.self, from: json)
21+
22+
XCTAssertEqual(decoded.ticketType, "VIP")
23+
XCTAssertEqual(decoded.price, 120.5)
24+
XCTAssertEqual(decoded.formattedPrice, "$120.50")
25+
XCTAssertEqual(decoded.originalPrice, 150.0)
26+
XCTAssertEqual(decoded.formattedOriginalPrice, "$150.00")
27+
XCTAssertEqual(decoded.label, "Early Bird")
28+
XCTAssertEqual(decoded.fee, 5.0)
29+
XCTAssertEqual(decoded.description, "Front row")
30+
}
31+
32+
func testInitFromJSON_withNumericFormattedPrice() throws {
33+
let json = """
34+
{
35+
"formattedPrice": 120.5,
36+
"formattedOriginalPrice": 150.0
37+
}
38+
""".data(using: .utf8)!
39+
40+
let decoded = try JSONDecoder().decode(SeatsioObjectPricing.self, from: json)
41+
42+
XCTAssertEqual(decoded.formattedPrice, "120.5")
43+
XCTAssertEqual(decoded.formattedOriginalPrice, "150.0")
44+
}
45+
46+
func testInitFromJSON_withoutFormattedPriceFields() throws {
47+
let json = """
48+
{
49+
"ticketType": "Standard",
50+
"price": 90.0,
51+
"originalPrice": 100.0
52+
}
53+
""".data(using: .utf8)!
54+
55+
let decoded = try JSONDecoder().decode(SeatsioObjectPricing.self, from: json)
56+
57+
XCTAssertEqual(decoded.ticketType, "Standard")
58+
XCTAssertEqual(decoded.price, 90.0)
59+
XCTAssertEqual(decoded.originalPrice, 100.0)
60+
XCTAssertNil(decoded.formattedPrice)
61+
XCTAssertNil(decoded.formattedOriginalPrice)
62+
}
63+
64+
func testInitFromJSON_onlyFormattedPricePresent() throws {
65+
let json = """
66+
{
67+
"formattedPrice": "$45.00"
68+
}
69+
""".data(using: .utf8)!
70+
71+
let decoded = try JSONDecoder().decode(SeatsioObjectPricing.self, from: json)
72+
73+
XCTAssertEqual(decoded.formattedPrice, "$45.00")
74+
XCTAssertNil(decoded.formattedOriginalPrice)
75+
XCTAssertNil(decoded.price)
76+
XCTAssertNil(decoded.originalPrice)
77+
}
78+
79+
func testInitFromJSON_onlyFormattedOriginalPricePresent() throws {
80+
let json = """
81+
{
82+
"formattedOriginalPrice": "$150.00"
83+
}
84+
""".data(using: .utf8)!
85+
86+
let decoded = try JSONDecoder().decode(SeatsioObjectPricing.self, from: json)
87+
88+
XCTAssertNil(decoded.formattedPrice)
89+
XCTAssertEqual(decoded.formattedOriginalPrice, "$150.00")
90+
XCTAssertNil(decoded.price)
91+
XCTAssertNil(decoded.originalPrice)
92+
}
93+
}

0 commit comments

Comments
 (0)