aboutsummaryrefslogtreecommitdiff
path: root/spec/test4_spec.lua
blob: 93c4e707057c30683cb5292c7011e74b5e32633a (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
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
local game_bin = nil
if package.config:sub(1,1) == "/" then -- linux or osx
	game_bin = "bin/client/bin/brokengine_client"
else
	game_bin = "bin\\client\\bin\\brokengine_client.exe"
end

function rungame()
	f = io.popen(game_bin .. " spec/headless","r")
	d = f:read("*all")
	f:close()
	return d
end

function writegame(...)
	f = assert(io.open("spec/headless/init.lua","w"))
	data = {"GAME.crashy()"}
	for _,v in pairs({...}) do
		data[#data + 1] = v
	end
	data[#data + 1] = "\nGAME.exit()\n"
	f:write(table.concat(data))
	f:close()
end

function assert_game_runs()
	local results = rungame()
	assert.truthy(results:find("\nGoodbye\n$"), results)
end

gui_api = {
	button = {
		func = "gui.newbutton",
		params = {
			{
				name = "dimensions",
				required = true,
				type = "rect4d",
			},
			{
				name = "default_text",
				required = true,
				type = "string",
			},
			{
				name = "parent",
				required = false,
				type = "guielement",
			},
		},
		callbacks = {
			"onClick", "onFocus", "onUnfocus", "onHover", "onLeave"
		}
	},
	fileopendialog = {
		func = "gui.newfileopendialog",
		params = {
			{
				name = "title",
				required = false,
				type = "string",
			},
			{
				name = "path",
				required = false,
				type = "filepath",
			},
			{
				name = "parent",
				required = false,
				type = "guielement",
			},
			{
				name = "modal",
				required = false,
				type = "boolean",
			}
		},
		callbacks = {
			"onDirectorySelect","onFileSelect","onCanceled"
		}
	},
	label = {
		func = "gui.newlabel",
		params = {
			{
				name = "pos",
				required = true,
				type = "rect4d",
			},
			{
				name = "text",
				required = true,
				type = "string",
			},
			{
				name = "parent",
				required = false,
				type = "guielement",
			}
		},
		callbacks = {

		}
	},
}

--[[For some reason these can't be local, _ENV dosn't find them.]]
local function mr()
	return string.format("%d",math.floor(math.random()*100))
end
function generate_rect4d()
	return string.format("{{%d,%d},{%d,%d}}",mr(),mr(),mr(),mr())
end
function generate_string()
	return "\"test!\""
end
function generate_guielement()
	return "gui.getroot()"
end
function generate_filepath()
	return "\".\""
end
function generate_boolean()
	return "true"
end
describe("Brok[en]gine",function()
	describe("gui system",function()
		for k,v in pairs(gui_api) do
			describe(k,function()
				local paramstring = {}
				local num_optional_args = 0
				local num_required_args = 0
				for i,j in pairs(v.params) do
					assert(_ENV["generate_" .. j.type], "Could not find a generator for type: " .. j.type)
					if j.required then
						num_required_args = num_required_args + 1
					else
						num_optional_args = num_optional_args + 1
					end
					paramstring[#paramstring + 1] = _ENV["generate_" .. j.type]()
				end
				for i = num_required_args, num_required_args + num_optional_args do
					it(string.format(" with %d params", i),function()
						local truncated_params = {}
						for j = 1, i do
							truncated_params[j] = paramstring[j]
						end
						local game_file = string.format("%s(%s)",v.func,table.concat(truncated_params, ","))
						--print("running:",game_file)
						writegame(game_file)
						assert_game_runs()
					end)
					it(string.format(" 100 elemants with %d params", i),function()
						local game_file = {}
						for j = 1,100 do
							local truncated_params = {}
							for k = 1, i do
								truncated_params[k] = paramstring[k]
							end
							game_file[#game_file + 1] = string.format("%s(%s)",v.func,table.concat(truncated_params, ","))
						end
						writegame(table.concat(game_file,"\n"))
						assert_game_runs()
					end)
				end
			end)
		end
	end)
end)