-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor_test.go
More file actions
193 lines (155 loc) · 5.51 KB
/
processor_test.go
File metadata and controls
193 lines (155 loc) · 5.51 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package processor_test
import (
"log"
"os"
"testing"
"tactical-turn-processor/processor"
)
// Helper to suppress logs during tests for cleaner output
func TestMain(m *testing.M) {
log.SetOutput(os.Stdout) // Change to os.Stderr or ioutil.Discard for silence
exitCode := m.Run()
os.Exit(exitCode)
}
func TestNewProcessor(t *testing.T) {
p := processor.NewProcessor()
if p == nil {
t.Errorf("NewProcessor returned nil")
}
if len(p.Actors) != 0 {
t.Errorf("Expected 0 actors, got %d", len(p.Actors))
}
}
func TestAddActor(t *testing.T) {
p := processor.NewProcessor()
actor1 := processor.NewBasicCombatant("Hero", 100)
actor2 := processor.NewBasicCombatant("Goblin", 50)
p.AddActor(actor1)
p.AddActor(actor2)
if len(p.Actors) != 2 {
t.Errorf("Expected 2 actors, got %d", len(p.Actors))
}
if p.Actors[0].ID() != "Hero" {
t.Errorf("Expected first actor ID 'Hero', got '%s'", p.Actors[0].ID())
}
foundActor := p.FindActorByID("Goblin")
if foundActor == nil || foundActor.ID() != "Goblin" {
t.Errorf("FindActorByID failed to find Goblin")
}
if p.FindActorByID("NonExistent") != nil {
t.Errorf("FindActorByID found a non-existent actor")
}
}
func TestActorDamageAndHeal(t *testing.T) {
combatant := processor.NewBasicCombatant("TestDummy", 100)
if combatant.CurrentHP() != 100 {
t.Errorf("Expected initial HP 100, got %d", combatant.CurrentHP())
}
combatant.TakeDamage(30)
if combatant.CurrentHP() != 70 {
t.Errorf("Expected HP 70 after damage, got %d", combatant.CurrentHP())
}
combatant.Heal(20)
if combatant.CurrentHP() != 90 {
t.Errorf("Expected HP 90 after heal, got %d", combatant.CurrentHP())
}
combatant.TakeDamage(100) // Overkill
if combatant.CurrentHP() != 0 || combatant.IsAlive() {
t.Errorf("Expected HP 0 and not alive after overkill, got %d", combatant.CurrentHP())
}
combatant.Heal(50)
if combatant.CurrentHP() != 50 || !combatant.IsAlive() {
t.Errorf("Expected HP 50 and alive after healing from dead, got %d", combatant.CurrentHP())
}
combatant.Heal(200) // Overheal
if combatant.CurrentHP() != combatant.MaxHP() {
t.Errorf("Expected HP to cap at MaxHP after overheal, got %d", combatant.CurrentHP())
}
}
func TestProcessAction(t *testing.T) {
p := processor.NewProcessor()
hero := processor.NewBasicCombatant("Hero", 100)
goblin := processor.NewBasicCombatant("Goblin", 50)
p.AddActor(hero)
p.AddActor(goblin)
attack := &processor.BasicAttackAction{DamageAmount: 20}
err := p.ProcessAction(attack, hero, []processor.Actor{goblin})
if err != nil {
t.Errorf("Error processing action: %v", err)
}
if goblin.CurrentHP() != 30 {
t.Errorf("Expected Goblin HP 30 after attack, got %d", goblin.CurrentHP())
}
// Test attacking a dead combatant
goblin.TakeDamage(30) // Goblin is now dead
if goblin.IsAlive() {
t.Errorf("Expected Goblin to be dead")
}
err = p.ProcessAction(attack, hero, []processor.Actor{goblin})
if err != nil {
t.Logf("Expected error when attacking dead target: %v", err) // This test case is currently designed to let the action implement specific target validation, or allow processor to validate it. BasicAttackAction doesn't stop, just deals damage.
// A real game might have rules about not targeting dead entities. For now, the damage will just reduce HP to 0 again.
}
if goblin.CurrentHP() != 0 {
t.Errorf("Expected Goblin HP to remain 0 after attacking dead target, got %d", goblin.CurrentHP())
}
// Test performer being dead
hero.TakeDamage(100) // Hero is now dead
err = p.ProcessAction(attack, hero, []processor.Actor{goblin}) // Hero tries to attack after dying
if err == nil {
t.Errorf("Expected an error when dead performer tries to act, got nil")
}
if err != nil && err.Error() != "performer Hero is not alive and cannot perform action" {
t.Errorf("Expected specific error for dead performer, got: %v", err)
}
}
func TestGetAliveActors(t *testing.T) {
p := processor.NewProcessor()
hero := processor.NewBasicCombatant("Hero", 100)
goblin1 := processor.NewBasicCombatant("Goblin1", 50)
goblin2 := processor.NewBasicCombatant("Goblin2", 1)
p.AddActor(hero)
p.AddActor(goblin1)
p.AddActor(goblin2)
alive := p.GetAliveActors()
if len(alive) != 3 {
t.Errorf("Expected 3 alive actors, got %d", len(alive))
}
goblin2.TakeDamage(1) // Goblin2 dies
alive = p.GetAliveActors()
if len(alive) != 2 {
t.Errorf("Expected 2 alive actors, got %d", len(alive))
}
for _, a := range alive {
if a.ID() == "Goblin2" {
t.Errorf("Goblin2 should not be in the alive actors list")
}
}
// All die
hero.TakeDamage(100)
goblin1.TakeDamage(50)
alive = p.GetAliveActors()
if len(alive) != 0 {
t.Errorf("Expected 0 alive actors, got %d", len(alive))
}
}
func TestGetActorStatus(t *testing.T) {
p := processor.NewProcessor()
hero := processor.NewBasicCombatant("Hero", 100)
goblin := processor.NewBasicCombatant("Goblin", 50)
p.AddActor(hero)
p.AddActor(goblin)
statusHero := p.GetActorStatus(hero)
if statusHero != "Hero (HP: 100/100)" {
t.Errorf("Expected status 'Hero (HP: 100/100)', got '%s'", statusHero)
}
goblin.TakeDamage(50)
statusGoblin := p.GetActorStatus(goblin)
if statusGoblin != "[DEAD] Goblin (HP: 0/50)" {
t.Errorf("Expected status '[DEAD] Goblin (HP: 0/50)', got '%s'", statusGoblin)
}
statusNil := p.GetActorStatus(nil)
if statusNil != "Actor is nil" {
t.Errorf("Expected status 'Actor is nil', got '%s'", statusNil)
}
}