File: test_helpers.nut

package info (click to toggle)
simutrans 124.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,880 kB
  • sloc: cpp: 160,224; ansic: 9,382; sh: 1,237; awk: 1,081; makefile: 932; javascript: 2
file content (189 lines) | stat: -rw-r--r-- 4,172 bytes parent folder | download
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
//
// This file is part of the Simutrans project under the Artistic License.
// (see LICENSE.txt)
//


//
// Test helpers
//
function make_assertion_str(val)
{
	if (typeof val == "string") {
		return "\"" + val + "\""
	}
	else {
		return "" + val
	}
}


function ASSERT_EQUAL(act, exp)
{
	if (!(act == exp)) {
		local err = ttext("Assertion failed, '{act} == {exp}' was not true")
		err.act = make_assertion_str(act)
		err.exp = make_assertion_str(exp)
		throw err.tostring()
	}
}

function ASSERT_LESS(lhs, rhs)
{
	if (!(lhs < rhs)) {
		local err = ttext("Assertion failed, '{lhs} &lt; {rhs}' was not true")
		err.lhs = make_assertion_str(lhs)
		err.rhs = make_assertion_str(rhs)
		throw err.tostring()
	}
}

function ASSERT_GREATER(lhs, rhs)
{
	if (!(lhs > rhs)) {
		local err = ttext("Assertion failed, '{lhs} &gt; {rhs}' was not true")
		err.lhs = make_assertion_str(lhs)
		err.rhs = make_assertion_str(rhs)
		throw err.tostring()
	}
}

function ASSERT_TRUE(a)
{
	if (!(a == true)) {
		local err = ttext("Assertion failed, '{a}' was not true")
		err.a = make_assertion_str(a)
		throw err.tostring()
	}
}


function ASSERT_FALSE(a)
{
	if (!(a == false)) {
		local err = ttext("Assertion failed, '{a}' was not false")
		err.a = make_assertion_str(a)
		throw err.tostring()
	}
}


// Set player funds (incl. non-cash assets) to some specific amount (in credit-cents)
function SET_PLAYER_FUNDS(pl, amount)
{
	pl.book_cash(amount - pl.get_current_net_wealth())
}


function RESET_ALL_PLAYER_FUNDS()
{
	local default_cash = 200000 * 100

	for (local i = 0; i < 8; ++i) {
		local pl = player_x(i)
		if (pl.is_valid()) {
			ASSERT_EQUAL(pl.get_current_maintenance(), 0)
			SET_PLAYER_FUNDS(pl, default_cash)
		}
	}
}


function char_to_dir(ch)
{
	switch (ch) {
	case '.': return dir.none // '.' == dontcare
	case '0': return dir.none
	case '1': return dir.north
	case '2': return dir.east
	case '3': return dir.northeast
	case '4': return dir.south
	case '5': return dir.northsouth
	case '6': return dir.southeast
	case '7': return dir.northsoutheast
	case '8': return dir.west
	case '9': return dir.northwest
	case 'A': return dir.eastwest
	case 'B': return dir.northeastwest
	case 'C': return dir.southwest
	case 'D': return dir.northsouthwest
	case 'E': return dir.southeastwest
	case 'F': return dir.all
	}

	ASSERT_TRUE(false) // should not reach here
}


function ASSERT_WAY_PATTERN(waytype, lefttop, pattern)
{
	local z = lefttop.z

	for (local y = 0; y < pattern.len(); ++y) {
		for (local x = 0; x < pattern[y].len(); ++x) {
			local tile = square_x(lefttop.x + x, lefttop.y + y).get_tile_at_height(z)
			local expected_dir = char_to_dir(pattern[y][x])

			if (tile != null) {
				local actual_dir = 0
				if (waytype != wt_power) {
					actual_dir = tile.get_way_dirs(waytype)
				}
				else {
					// powerlines connect to other powerlines automatically.
					local powerline = tile.find_object(mo_powerline)
					actual_dir = 0

					if (powerline) {
						for (local i = 0; i < 4; ++i) {
							local offset = dir.to_coord(1<<i)
							local nb = square_x(lefttop.x + x + offset.x, lefttop.y + y + offset.y)
							if (nb && nb.is_valid() && nb.get_tile_at_height(z) && nb.get_tile_at_height(z).find_object(mo_powerline)) {
								actual_dir = actual_dir | (1<<i)
							}
						}
					}
				}

				ASSERT_EQUAL(actual_dir, expected_dir)
			}
		}
	}
}


function ASSERT_WAY_PATTERN_MASKED(waytype, lefttop, pattern)
{
	if (waytype == wt_power) {
		ASSERT_WAY_PATTERN(waytype, lefttop, pattern)
		return
	}

	local z = lefttop.z

	for (local y = 0; y < pattern.len(); ++y) {
		for (local x = 0; x < pattern[y].len(); ++x) {
			local tile = square_x(lefttop.x + x, lefttop.y + y).get_tile_at_height(z)
			local expected_dir = char_to_dir(pattern[y][x])

			if (tile != null) {
				local actual_dir = tile.get_way_dirs_masked(waytype)
				ASSERT_EQUAL(actual_dir, expected_dir)
			}
		}
	}
}


function get_depot_by_wt(waytype)
{
	local list = building_desc_x.get_building_list(building_desc_x.depot)

	foreach (building in list) {
		if (building.get_type() == building_desc_x.depot && building.get_waytype() == waytype) {
			return building
		}
	}

	return null
}