aboutsummaryrefslogtreecommitdiff
path: root/internal/model/model_test.go
blob: 8e4ee63060cdf4ca4a7eae27298733cc9f6e5dcd (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
package model

import "testing"

func TestNewMap(t *testing.T) {
	m := NewMap("test", 10, 5, nil)
	if m.Width != 10 || m.Height != 5 {
		t.Fatalf("NewMap size: got %dx%d", m.Width, m.Height)
	}
	if m.Grid[0][0].Terrain != -1 {
		t.Fatal("NewMap: grid not initialized to -1")
	}
	if len(m.Grid) != 5 || len(m.Grid[0]) != 10 {
		t.Fatal("NewMap: grid dimensions wrong")
	}
}

func TestClone(t *testing.T) {
	m := NewMap("test", 10, 5, nil)
	m2 := m.Clone()
	m2.Grid[0][0].Terrain = 0
	if m.Grid[0][0].Terrain != -1 {
		t.Fatal("Clone shares data")
	}
}

func TestInBounds(t *testing.T) {
	m := NewMap("test", 10, 5, nil)
	if !m.InBounds(Point{X: 0, Y: 0}) {
		t.Fatal("InBounds(0,0) should be true")
	}
	if m.InBounds(Point{X: -1, Y: 0}) {
		t.Fatal("InBounds(-1,0) should be false")
	}
	if m.InBounds(Point{X: 0, Y: 5}) {
		t.Fatal("InBounds(0,5) should be false")
	}
	if m.InBounds(Point{X: 10, Y: 0}) {
		t.Fatal("InBounds(10,0) should be false")
	}
}

func TestSetGetCell(t *testing.T) {
	m := NewMap("test", 10, 5, nil)
	m.SetCell(Point{X: 1, Y: 2}, 5, "red")
	c := m.CellAt(Point{X: 1, Y: 2})
	if c.Terrain != 5 || c.Color != "red" {
		t.Fatalf("SetCell/GetCell: got %d,%s", c.Terrain, c.Color)
	}
}

func TestUndoStack(t *testing.T) {
	u := &UndoStack{}
	m := NewMap("test", 3, 3, nil)
	u.Push(m)
	m.SetCell(Point{X: 1, Y: 1}, 2, "green")
	u.Push(m)
	m.SetCell(Point{X: 2, Y: 2}, 3, "blue")

	if entry := u.Undo(); entry != nil {
		*entry.Target = *entry.State
	}
	if m.CellAt(Point{X: 2, Y: 2}).Terrain != -1 {
		t.Fatal("Undo failed")
	}
}

func TestClamp(t *testing.T) {
	if Clamp(5, 0, 10) != 5 {
		t.Fatal("Clamp middle")
	}
	if Clamp(-1, 0, 10) != 0 {
		t.Fatal("Clamp low")
	}
	if Clamp(11, 0, 10) != 10 {
		t.Fatal("Clamp high")
	}
}

func TestPickColor(t *testing.T) {
	tc := Terrain{Colors: []TerrainColor{{Color: "22", Weight: 100}}}
	if tc.PickColor() != "22" {
		t.Fatal("PickColor single failed")
	}
	tc2 := Terrain{}
	if tc2.PickColor() != "0" {
		t.Fatal("PickColor empty failed")
	}
}

func TestTerrainSymbol(t *testing.T) {
	tr := Terrain{Symbol: "X", ASCII: "x"}
	if tr.GetSymbol(true) != "X" {
		t.Fatal("Unicode symbol")
	}
	if tr.GetSymbol(false) != "x" {
		t.Fatal("ASCII symbol")
	}
}