aboutsummaryrefslogtreecommitdiff
path: root/spec/common.lua
blob: f1eee0486127ee18fd384af8623165290ddc3fae (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
local common = {}
local game_bin = nil

if package.config:sub(1,1) == "/" then -- linux or osx
	game_bin = "bin/client/bin/brokengine_client"
	server_bin = "bin/server/bin/brokengine_server"
else -- windows
	game_bin = "bin\\client\\bin\\brokengine_client.exe"
	server_bin = "bin\\server\\bin\\brokengine_server.exe"
end

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

function common.runboth()
	--print("Running both")
	--Do we have a race condition here? (Can client start and send it's message
	--before the server is ready to accept?
	f1 = assert(io.popen(server_bin .. " spec/server","r"))
	f2 = assert(io.popen(game_bin .. " spec/headless","r"))
	--print("Both ran...")
	d1 = f1:read("*all")
	d2 = f2:read("*all")
	--print("Both read all")
	f1:close()
	f2:close()
	--print("returning")
	return d1, d2
end

function common.writegame(...)
	f = 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,"\n"))
	f:close()
end

function common.writeserver(...)
	f = io.open("spec/server/init.lua","w")
	data = {...}
	data[#data + 1] = "\nGAME.exit()\n"
	f:write(table.concat(data,"\n"))
	f:close()
end

function common.assert_game_runs()
	assert.is_not_nil(common.rungame():find("\nGoodbye\n$"))
end
function common.assert_both_run()
	local a,b = common.runboth()
	assert.is_not_nil(a:find("\nGoodbye\n$"))
	assert.is_not_nil(b:find("\nGoodbye\n$"))
end
return common