Skip to content

Commit 420512e

Browse files
Added unit tests
1 parent 786fbca commit 420512e

5 files changed

Lines changed: 610 additions & 4 deletions

File tree

0 Bytes
Binary file not shown.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Xcode
2+
#
3+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4+
5+
## User settings
6+
xcuserdata/
7+
8+
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
9+
*.xcscmblueprint
10+
*.xccheckout
11+
12+
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
13+
build/
14+
DerivedData/
15+
*.moved-aside
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
25+
## Obj-C/Swift specific
26+
*.hmap
27+
28+
## App packaging
29+
*.ipa
30+
*.dSYM.zip
31+
*.dSYM
32+
33+
## Playgrounds
34+
timeline.xctimeline
35+
playground.xcworkspace
36+
37+
# Swift Package Manager
38+
#
39+
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
40+
# Packages/
41+
# Package.pins
42+
# Package.resolved
43+
# *.xcodeproj
44+
#
45+
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
46+
# hence it is not needed unless you have added a package configuration file to your project
47+
# .swiftpm
48+
49+
.build/
50+
51+
# CocoaPods
52+
#
53+
# We recommend against adding the Pods directory to your .gitignore. However
54+
# you should judge for yourself, the pros and cons are mentioned at:
55+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
56+
#
57+
# Pods/
58+
#
59+
# Add this line if you want to avoid checking in source code from the Xcode workspace
60+
# *.xcworkspace
61+
62+
# Carthage
63+
#
64+
# Add this line if you want to avoid checking in source code from Carthage dependencies.
65+
# Carthage/Checkouts
66+
67+
Carthage/Build/
68+
69+
# Accio dependency management
70+
Dependencies/
71+
.accio/
72+
73+
# fastlane
74+
#
75+
# It is recommended to not store the screenshots in the git repo.
76+
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
77+
# For more information about the recommended setup visit:
78+
# https://docs.fastlane.tools/best-practices/source-control/
79+
80+
fastlane/report.xml
81+
fastlane/Preview.html
82+
fastlane/screenshots/**/*.png
83+
fastlane/test_output
84+
85+
# Code Injection
86+
#
87+
# After new code Injection tools there's a generated folder /iOSInjectionProject
88+
# https://github.com/johnno1962/injectionforxcode
89+
90+
iOSInjectionProject/
91+
92+
# macOS
93+
.DS_Store
94+
.AppleDouble
95+
.LSOverride
96+
97+
# Icon must end with two \r
98+
Icon
99+
100+
# Thumbnails
101+
._*
102+
103+
# Files that might appear in the root of a volume
104+
.DocumentRevisions-V100
105+
.fseventsd
106+
.Spotlight-V100
107+
.TemporaryItems
108+
.Trashes
109+
.VolumeIcon.icns
110+
.com.apple.timemachine.donotpresent
111+
112+
# Directories potentially created on remote AFP share
113+
.AppleDB
114+
.AppleDesktop
115+
Network Trash Folder
116+
Temporary Items
117+
.apdisk
118+
119+
# IDE
120+
.vscode/
121+
.idea/
122+
*.swp
123+
*.swo
124+
*~
125+
126+
# Logs
127+
*.log
128+
logs/
129+
130+
# Environment variables
131+
.env
132+
.env.local
133+
.env.development.local
134+
.env.test.local
135+
.env.production.local
136+
137+
# Temporary files
138+
*.tmp
139+
*.temp
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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

Comments
 (0)