Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov committed Jun 28, 2024
1 parent b3c4b33 commit b65eae8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
31 changes: 24 additions & 7 deletions Calcium/MainReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import BigNumber
import ComposableArchitecture

enum MainReducerError: Error {
case invalidValues
}

@Reducer
struct MainReducer {
@ObservableState
Expand All @@ -30,6 +34,9 @@ struct MainReducer {
var enabledDivide = true
var enabledFactorial = true

var enabledClear = true
var enabledEquals = true

var leftValue: BInt?
var displayingText = ""
var latestOperationButton: CalculatorButton?
Expand All @@ -52,14 +59,14 @@ struct MainReducer {
switch calculatorButton {
case let .operation(operation):
if operation.isUnary {
set(enabled: false)
set(enabled: false, all: false)
}
default:
break
}
}

mutating func set(enabled: Bool) {
mutating func set(enabled: Bool, all: Bool) {
enabled0 = enabled
enabled1 = enabled
enabled2 = enabled
Expand All @@ -76,14 +83,22 @@ struct MainReducer {
enabledMultiply = enabled
enabledDivide = enabled
enabledFactorial = enabled

if all {
enabledClear = enabled
enabledEquals = enabled
}
}

mutating func onButtonPressed(calculatorButton: CalculatorButton) -> Operation? {
switch calculatorButton {
case let .digit(value):
onDigitButtonPressed(value: value)
case .clear:
leftValue = nil
displayingText = ""
latestOperationButton = nil
set(enabled: true, all: true)
case let .operation(value):
switch value {
case .plus, .minus, .multiply, .divide, .factorial:
Expand Down Expand Up @@ -115,11 +130,15 @@ struct MainReducer {
let lhs = state.leftValue
let rhs = BInt(state.displayingText)
if let operation = state.onButtonPressed(calculatorButton: button) {
state.set(enabled: false, all: true)
return .run { send in
guard let lhs, let rhs else {
throw MainReducerError.invalidValues
}
let value = await Task {
operation.calculateValue(
lhs: lhs!,
rhs: rhs!
lhs: lhs,
rhs: rhs
)
}.value
await send(.calculated(value), animation: .calciumDefault)
Expand All @@ -131,9 +150,7 @@ struct MainReducer {
case let .calculated(value):
state.displayingText = String(value)
state.latestOperationButton = .operation(.equals)

state.set(enabled: true)

state.set(enabled: true, all: true)
return .none
}
}
Expand Down
6 changes: 3 additions & 3 deletions Calcium/MainScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Animation {
struct MainScreen: View {
@Perception.Bindable var store: StoreOf<MainReducer>

private func view(for calculatorButton: CalculatorButton, enabled: Bool = true) -> some View {
private func view(for calculatorButton: CalculatorButton, enabled: Bool) -> some View {
Button {
store.send(.pressButton(calculatorButton), animation: .calciumDefault)
} label: {
Expand Down Expand Up @@ -54,15 +54,15 @@ struct MainScreen: View {
HStack {
view(for: .digit(.zero), enabled: store.state.enabled0)
view(for: .operation(.factorial), enabled: store.state.enabledFactorial)
view(for: .clear)
view(for: .clear, enabled: store.state.enabledClear)
}

HStack {
view(for: .operation(.plus), enabled: store.state.enabledPlus)
view(for: .operation(.minus), enabled: store.state.enabledMinus)
view(for: .operation(.multiply), enabled: store.state.enabledMultiply)
view(for: .operation(.divide), enabled: store.state.enabledDivide)
view(for: .operation(.equals))
view(for: .operation(.equals), enabled: store.state.enabledEquals)
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions CalciumTests/CalciumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ class CalciumTests: XCTestCase {
store.send(.pressButton(.digit(.one)))
XCTAssertEqual(screen.store.state.displayingText, CalculatorButton.digit(.one).displayingValue)
}

@MainActor
func testStateComplex() async throws {
let store = Store(initialState: MainReducer.State()) {
MainReducer()
}
let screen = MainScreen(
store: store
)
store.send(.pressButton(.digit(.one)))
store.send(.pressButton(.operation(.plus)))
store.send(.pressButton(.digit(.two)))
store.send(.pressButton(.operation(.equals)))

try await Task.sleep(nanoseconds: 1_000_000_000)

XCTAssertEqual(screen.store.state.displayingText, String(1 + 2))
}
}

0 comments on commit b65eae8

Please sign in to comment.