aboutsummaryrefslogtreecommitdiff
path: root/go/game/attack_test.go
blob: 0a6113c6d50e7d372d2f598e6690dd6674e1125d (plain)
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
package game

import (
	"testing"
)

func TestParseIntAttack(t *testing.T) {
	a := parseAttack(uint64(1))
	if a.MaxRange() != 1 {
		t.Fatal("unexpected max range (1):", a.MaxRange())
	}
	if a.DamageInRange(1) != 1 {
		t.Fatal("unexpected damage in range 1 (1):", a.DamageInRange(1))
	}
}

func TestParseStringAttack(t *testing.T) {
	a := parseAttack("1")
	if a.MaxRange() != 1 {
		t.Fatal("unexpected max range (1):", a.MaxRange())
	}
	if a.DamageInRange(1) != 1 {
		t.Fatal("unexpected damage in range 1 (1):", a.DamageInRange(1))
	}
	if a.DamageInRange(2) != 0 {
		t.Fatal("unexpected damage in range 2 (0):", a.DamageInRange(2))
	}
}

func TestParseRangedAttack(t *testing.T) {
	a := parseAttack("1 range 2")
	if a.MaxRange() != 2 {
		t.Fatal("unexpected max range (2):", a.MaxRange())
	}
	if a.DamageInRange(1) != 1 {
		t.Fatal("unexpected damage in range 1 (1):", a.DamageInRange(1))
	}
	if a.DamageInRange(2) != 1 {
		t.Fatal("unexpected damage in range 2 (1):", a.DamageInRange(2))
	}
	if a.DamageInRange(3) != 0 {
		t.Fatal("unexpected damage in range 3 (0):", a.DamageInRange(3))
	}
}

func TestAttackString(t *testing.T) {
	aStr := "2 range 3"
	a := parseAttack(aStr)
	if a.String() != aStr {
		t.Fatal("unexpected attack string:", a.String())
	}
}

func TestAttackCopy(t *testing.T) {
	aStr := "2 range 3"
	a := parseAttack(aStr)
	c := a.Copy()
	c.Extend(1)
	if a.MaxRange() != 3 {
		t.Fatal("changed max range")
	}
	c.attacks[0] = 1
	if a.attacks[0] != 2 {
		t.Fatal("changed attack in range 0")
	}
}

func TestPikeAttack(t *testing.T) {
	a := parseAttack("1")
	a.flexAttack = pikeAttack()
	o := &Tile{Position: Position{0, 0}}
	u := NewUnit(NewCard("base/cavalry"), o, NewMockPlayer())
	enterTile(u, o)

	tile := &Tile{Position: Position{1, 2}}
	enterTile(NewUnitFromPath("base/cavalry", tile, NewMockPlayer()), tile)

	house := &Tile{Position: Position{2, 1}, Type: TileTypes.House, Raw: "house"}
	enterTile(NewUnitFromPath("base/cavalry", house, NewMockPlayer()), house)

	// This test is crooked because the attack of the attacking unit is checked in flexAttack and not a.
	d, ok := a.DamageForTile(u, tile)
	if !ok {
		t.Fatal("cavalry not in range for pike attack")
	}
	if d != u.Attack.DamageInRange(u.Attack.MaxRange()) {
		t.Fatal("unexpected damage: amount", d)
	}

	d, ok = a.DamageForTile(u, house)
	if ok || d != 0 {
		t.Fatal("protected cavalry in range for pike attack")
	}
}