aboutsummaryrefslogtreecommitdiff
path: root/go/game/action_test.go
blob: a4cf0752136de5f086dacbfc715de6cefb3b8684 (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
95
96
97
98
99
100
101
102
103
104
105
106
package game

import (
	"testing"

	"muhq.space/muhqs-game/go/utils"
)

const PASS_STRING = "muhq\u00A0pass"

var s *LocalState = NewLocalState()
var p *Player = s.AddNewPlayer("muhq", NewDeck())

func TestUpkeepAction(t *testing.T) {
	s := NewLocalState()
	p := s.AddNewPlayer("muhq", NewDeck())
	o := s.AddNewPlayer("opponent", NewDeck())
	m, _ := GetMap("2P-ring-street")
	s.SetMap(m)

	archer := s.addNewUnit(NewCard("base/archer"), Position{0, 0}, p)
	farmer := s.addNewUnit(NewCard("misc/farmer"), Position{0, 1}, p)
	// Change controller
	knight := s.addNewUnit(NewCard("base/knight"), Position{0, 2}, o)
	knight.controller = p

	king := s.addNewUnit(NewCard("misc/king"), Position{2, 2}, o)

	n := newUpkeepPrompt(p)
	ctx := n.Context.(TargetSelectionCtx)
	if ctx.Action.Source().(*Player) != p {
		t.Fatal("unexpected player ", p)
	}

	trgts := ctx.Action.Targets()
	if !trgts.AllowSelection() {
		t.Fatal("upkeep action does not allow target selection")
	}

	if trgts.RequireSelection() {
		t.Fatal("upkeep action does require selection")
	}

	opts := trgts.Options()
	if !utils.InterfaceSliceContains(opts, archer) || !utils.InterfaceSliceContains(opts, farmer) || !utils.InterfaceSliceContains(opts, farmer) {
		t.Fatal("Upkeep action targets does not contain the controlled units")
	}

	if utils.InterfaceSliceContains(opts, king) {
		t.Fatal("Upkeep action targets contains uncontrolled units")
	}
}

func TestMarshalPass(t *testing.T) {
	exp := PASS_STRING
	a := NewPassPriority(p)
	is := string(MarshalAction(a))
	if is != exp {
		t.Fatalf("expected string %s does not match %s\n", exp, is)
	}
}

func TestUnmarshalPass(t *testing.T) {
	in := PASS_STRING
	a, err := UnmarshalAction(s, []byte(in))
	if err != nil {
		t.Fatal(err)
	}
	if _, ok := a.(*PassPriority); !ok {
		t.Fatalf("expexted PassPriority not %s", a)
	}
}

const PICK_ACTION_STRING = "muhq\u00A0pick\u00A0base/archer:[base/archer, magic/more!]"

func TestMarshalPickAction(t *testing.T) {
	exp := PICK_ACTION_STRING
	poc := NewPileOfCards()
	c := NewCard("base/archer")
	poc.AddCard(c)
	poc.AddCard(NewCard("magic/more!"))
	a := NewDraftPick(p, poc, c)
	is := string(MarshalAction(a))
	if is != exp {
		t.Fatalf("expected string %s does not match %s\n", exp, is)
	}
}

func TestUnmarshalPickAction(t *testing.T) {
	in := PICK_ACTION_STRING
	a, err := UnmarshalAction(s, []byte(in))
	if err != nil {
		t.Fatal(err)
	}
	var p *DraftPick
	var ok bool
	if p, ok = a.(*DraftPick); !ok {
		t.Fatalf("expexted DraftPick not %v", a)
	}
	if len(p.pack.Cards()) != 2 {
		t.Fatalf("expexted two cards in pack not %d", len(p.pack.Cards()))
	}
	if p.pick.Name != "Archer" {
		t.Fatalf("expexted archer beeing picked not %s", p.pick.Path())
	}
}