1+ import XCTest
2+ @testable import AppRouter
3+
4+ final class AppRouterTests : XCTestCase {
5+ var router : AppRouter !
6+
7+ override func setUp( ) {
8+ super. setUp ( )
9+ router = AppRouter ( )
10+ }
11+
12+ override func tearDown( ) {
13+ router = nil
14+ super. tearDown ( )
15+ }
16+
17+ // MARK: - Initial State Tests
18+
19+ func testInitialState( ) {
20+ XCTAssertTrue ( router. path. isEmpty, " Navigation path should be empty initially " )
21+ XCTAssertTrue ( router. cartItems. isEmpty, " Cart items should be empty initially " )
22+ }
23+
24+ // MARK: - Navigation Tests
25+
26+ func testNavigationPath( ) {
27+ router. path. append ( . cart)
28+ XCTAssertEqual ( router. path. count, 1 , " Path should contain one route " )
29+ XCTAssertEqual ( router. path. first, . cart, " First route should be cart " )
30+
31+ router. path. append ( . summary)
32+ XCTAssertEqual ( router. path. count, 2 , " Path should contain two routes " )
33+ XCTAssertEqual ( router. path. last, . summary, " Last route should be summary " )
34+ }
35+
36+ func testClearNavigationPath( ) {
37+ router. path. append ( . cart)
38+ router. path. append ( . summary)
39+ XCTAssertEqual ( router. path. count, 2 , " Path should contain two routes " )
40+
41+ router. path = [ ]
42+ XCTAssertTrue ( router. path. isEmpty, " Path should be empty after clearing " )
43+ }
44+
45+ // MARK: - Cart Items Tests
46+
47+ func testAddCartItem( ) {
48+ router. cartItems. append ( " Pizza " )
49+ XCTAssertEqual ( router. cartItems. count, 1 , " Cart should contain one item " )
50+ XCTAssertEqual ( router. cartItems. first, " Pizza " , " First item should be Pizza " )
51+ }
52+
53+ func testAddMultipleCartItems( ) {
54+ router. cartItems. append ( " Pizza " )
55+ router. cartItems. append ( " Burger " )
56+ router. cartItems. append ( " Salad " )
57+
58+ XCTAssertEqual ( router. cartItems. count, 3 , " Cart should contain three items " )
59+ XCTAssertEqual ( router. cartItems, [ " Pizza " , " Burger " , " Salad " ] , " Items should match expected order " )
60+ }
61+
62+ func testRemoveCartItems( ) {
63+ router. cartItems. append ( " Pizza " )
64+ router. cartItems. append ( " Burger " )
65+ XCTAssertEqual ( router. cartItems. count, 2 , " Cart should contain two items " )
66+
67+ router. cartItems. removeAll ( )
68+ XCTAssertTrue ( router. cartItems. isEmpty, " Cart should be empty after removing all items " )
69+ }
70+
71+ func testRemoveSpecificCartItem( ) {
72+ router. cartItems. append ( " Pizza " )
73+ router. cartItems. append ( " Burger " )
74+ router. cartItems. append ( " Salad " )
75+
76+ router. cartItems. remove ( at: 1 )
77+ XCTAssertEqual ( router. cartItems. count, 2 , " Cart should contain two items after removal " )
78+ XCTAssertEqual ( router. cartItems, [ " Pizza " , " Salad " ] , " Items should match expected after removal " )
79+ }
80+
81+ // MARK: - Integration Tests
82+
83+ func testCompleteOrderFlow( ) {
84+ // Add items to cart
85+ router. cartItems. append ( " Pizza " )
86+ router. cartItems. append ( " Burger " )
87+ XCTAssertEqual ( router. cartItems. count, 2 , " Cart should contain two items " )
88+
89+ // Navigate to cart
90+ router. path. append ( . cart)
91+ XCTAssertEqual ( router. path. count, 1 , " Path should contain one route " )
92+ XCTAssertEqual ( router. path. first, . cart, " First route should be cart " )
93+
94+ // Navigate to summary
95+ router. path. append ( . summary)
96+ XCTAssertEqual ( router. path. count, 2 , " Path should contain two routes " )
97+ XCTAssertEqual ( router. path. last, . summary, " Last route should be summary " )
98+
99+ // Complete order (clear everything)
100+ router. path = [ ]
101+ router. cartItems. removeAll ( )
102+ XCTAssertTrue ( router. path. isEmpty, " Path should be empty after completing order " )
103+ XCTAssertTrue ( router. cartItems. isEmpty, " Cart should be empty after completing order " )
104+ }
105+
106+ func testNavigationBackToDashboard( ) {
107+ router. path. append ( . cart)
108+ router. path. append ( . summary)
109+ XCTAssertEqual ( router. path. count, 2 , " Path should contain two routes " )
110+
111+ // Navigate back to dashboard
112+ router. path. append ( . dashboard)
113+ XCTAssertEqual ( router. path. count, 3 , " Path should contain three routes " )
114+ XCTAssertEqual ( router. path. last, . dashboard, " Last route should be dashboard " )
115+ }
116+
117+ // MARK: - Edge Cases
118+
119+ func testEmptyCartNavigation( ) {
120+ // Should be able to navigate to cart even with empty cart
121+ router. path. append ( . cart)
122+ XCTAssertEqual ( router. path. first, . cart, " Should be able to navigate to cart " )
123+ }
124+
125+ func testDuplicateItems( ) {
126+ router. cartItems. append ( " Pizza " )
127+ router. cartItems. append ( " Pizza " )
128+ XCTAssertEqual ( router. cartItems. count, 2 , " Should allow duplicate items " )
129+ XCTAssertEqual ( router. cartItems, [ " Pizza " , " Pizza " ] , " Should contain duplicate items " )
130+ }
131+
132+ func testEmptyStringItem( ) {
133+ router. cartItems. append ( " " )
134+ XCTAssertEqual ( router. cartItems. count, 1 , " Should allow empty string items " )
135+ XCTAssertEqual ( router. cartItems. first, " " , " Should contain empty string " )
136+ }
137+
138+ // MARK: - Performance Tests
139+
140+ func testLargeCartPerformance( ) {
141+ let startTime = CFAbsoluteTimeGetCurrent ( )
142+
143+ // Add many items
144+ for i in 1 ... 1000 {
145+ router. cartItems. append ( " Item \( i) " )
146+ }
147+
148+ let endTime = CFAbsoluteTimeGetCurrent ( )
149+ let executionTime = endTime - startTime
150+
151+ XCTAssertEqual ( router. cartItems. count, 1000 , " Should contain 1000 items " )
152+ XCTAssertLessThan ( executionTime, 0.1 , " Adding 1000 items should take less than 0.1 seconds " )
153+ }
154+
155+ func testLargeNavigationPathPerformance( ) {
156+ let startTime = CFAbsoluteTimeGetCurrent ( )
157+
158+ // Add many navigation routes
159+ for _ in 1 ... 100 {
160+ router. path. append ( . cart)
161+ router. path. append ( . summary)
162+ }
163+
164+ let endTime = CFAbsoluteTimeGetCurrent ( )
165+ let executionTime = endTime - startTime
166+
167+ XCTAssertEqual ( router. path. count, 200 , " Should contain 200 routes " )
168+ XCTAssertLessThan ( executionTime, 0.1 , " Adding 200 routes should take less than 0.1 seconds " )
169+ }
170+ }
0 commit comments