aboutsummaryrefslogtreecommitdiff
path: root/spec/test2_spec.lua
blob: 82ca33eb2ebe6749f69049d8af3cb59bba428180 (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
--do return end
--local test = require("u-test")
if not package.path:find("spec/%?%.lua;") then
	print("including spec")
	package.path = "./spec/?.lua;" .. package.path
end
local common = require("common")
_G.assert = assert

describe("gui library", function()
	for k,v in pairs({
		buttons = "newbutton",
		checkboxes = "newcheckbox",
		colorselectors = "newcolorselector",
		editboxes = "neweditbox",
		openfiledialogs = "newfileopendialog",
		images = "newiguiimage",
		labels = "newlabel",
		spinboxes = "newspinbox",
		treeviews = "newtreeview",
		windows = "newwindow",
	}) do
		it("should have a function to create " .. k,function()
			common.writegame("assert(gui." .. v .. ")")
			common.assert_game_runs()
		end)
	end
	it("should have a function to check the screen's width",function()
		common.writegame("assert(scrw)")
		common.assert_game_runs()
	end)
	it("should have a function to check the screen's height", function()
		common.writegame("assert(scrh)")
		common.assert_game_runs()
	end)
end)


local protocols = {
	"PAIR", "BUS", "PUB", "SUB", "PULL", "PUSH", "REQ", "REP", "RESPOND", "SURVEY"
}
describe("network library",function()
	for _,v in pairs(protocols) do
		it("should have a constant for the " .. v .. " protocol",function()
			common.writegame(string.format("assert(net.%s)",v))
			common.assert_game_runs()
		end)
	end
	it("should have a function to create a new socket",function()
		common.writegame("assert(net.newsocket)")
		common.assert_game_runs()
	end)
	for _,v in pairs(protocols) do
		for _,j in pairs{"bind","connect","send"} do
			it(string.format("should be able to create a %s socket with a %s method",v,j))
			common.writegame(string.format([[
				local socket = net.newsocket(net.%s)
				assert(socket.%s)
				assert(type(socket.%s) == "function")
			]],v,j,j))
			common.assert_game_runs()
		end
	end
	it("should be able to set up a PAIR connection and send data through #smoke",function()
		common.writeserver([[
			local has_ponged = false
			local socket = net.newsocket(net.PAIR)
			socket:bind("tcp://127.0.0.1:5555")
			function socket:receive(stream)
				print("socket receive triggered")
				has_ponged = true
				local message = stream:readstring()
				assert(message == "ping")
				socket:send(function(stream2)
					stream2:writestring("pong")
				end)
			end
			local i = os.time()
			function GAME.tick()
				if has_ponged then
					GAME.exit()
				end
				if i - os.time() > 5 then
					error("Failed")
				end
			end
		]])
		common.writegame([[
			local socket = net.newsocket(net.PAIR)
			socket:connect("tcp://127.0.0.1:5555")
			function socket:receive(stream)
				local message = stream:readstring()
				assert(message == "pong")
			end
			socket:send(function(stream)
				stream:writestring("ping")
			end)
		]])
		common.assert_both_run()
	end)
end)