Skip to content

Commit 447f9d5

Browse files
committed
add unit tests
1 parent ebf4feb commit 447f9d5

26 files changed

+6378
-0
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"Lua.workspace.library": ["./definitions"],
3+
"Lua.runtime.plugin": "./definitions/plugin.lua"
4+
}

tests/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Renoise API Tests
2+
3+
This is a set of basic tests for the renoise Lua API, which need to be invoked within Renoise's scripting terminal.
4+
5+
Use all.lua to run all of them in a batch.

tests/all.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local failed_tests = 0
2+
3+
-- run all tests except midi_actions which already executes a lot of sub tests
4+
local ignored_files = table.create{ "all.lua", "midi_actions.lua" }
5+
6+
-- run all in current directory
7+
for _, file in pairs(os.filenames(os.currentdir(), { "*.lua" })) do
8+
if ignored_files:find(file) == nil then
9+
local file_chunk = assert(loadfile(os.currentdir() .. file, "t"))
10+
print("Running '" .. file .. "' tests...")
11+
local result, error = pcall(file_chunk)
12+
if result == false then
13+
print("\tTest FAILED:\n" .. error)
14+
failed_tests = failed_tests + 1
15+
end
16+
end
17+
end
18+
19+
assert(failed_tests == 0,
20+
"One or more test failed. See output for details...")
21+

tests/app_window.lua

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--[[--------------------------------------------------------------------------
2+
TestAppWindow.lua
3+
--------------------------------------------------------------------------]]--
4+
5+
do
6+
7+
----------------------------------------------------------------------------
8+
-- tools
9+
10+
local function assert_error(statement)
11+
assert(pcall(statement) == false, "expected function error")
12+
end
13+
14+
15+
-- shortcuts
16+
17+
local window = renoise.app().window
18+
19+
20+
----------------------------------------------------------------------------
21+
-- upper frame
22+
23+
local notification_count = 0
24+
function disk_browser_is_visible_changed()
25+
notification_count = notification_count + 1
26+
end
27+
28+
window.disk_browser_is_visible = false
29+
30+
window.disk_browser_is_visible_observable:add_notifier(
31+
disk_browser_is_visible_changed)
32+
33+
window.disk_browser_is_visible = true
34+
assert(window.disk_browser_is_visible == true)
35+
assert(notification_count == 1)
36+
37+
window.disk_browser_is_visible = false
38+
assert(window.disk_browser_is_visible == false)
39+
assert(notification_count == 2)
40+
41+
window.disk_browser_is_visible_observable:remove_notifier(
42+
disk_browser_is_visible_changed)
43+
44+
end
45+
46+
47+
------------------------------------------------------------------------------
48+
-- test finalizers
49+
50+
collectgarbage()
51+
52+
53+
--[[--------------------------------------------------------------------------
54+
--------------------------------------------------------------------------]]--
55+

tests/class.lua

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
--[[--------------------------------------------------------------------------
2+
TestClass.lua
3+
--------------------------------------------------------------------------]]--
4+
5+
-- GlobalBaseClass
6+
7+
class "GlobalBaseClass"
8+
9+
function GlobalBaseClass:__init(str)
10+
self.__str = str
11+
end
12+
13+
function GlobalBaseClass:__tostring()
14+
return self.__str
15+
end
16+
17+
18+
-- GlobalMyClass
19+
20+
class "GlobalMyClass"(GlobalBaseClass)
21+
22+
function GlobalMyClass:__init(str)
23+
GlobalBaseClass.__init(self, str)
24+
end
25+
26+
27+
-- namespace
28+
29+
namespace = { }
30+
31+
32+
-- namespace.BaseClass
33+
34+
class (namespace, "BaseClass")
35+
36+
function namespace.BaseClass:__init(str)
37+
self.__str = str
38+
end
39+
40+
function namespace.BaseClass:__tostring()
41+
return self.__str
42+
end
43+
44+
45+
-- namespace.MyClass(BaseClass)
46+
47+
class (namespace, "MyClass")(namespace.BaseClass)
48+
49+
function namespace.MyClass:__init(str)
50+
namespace.BaseClass.__init(self, str)
51+
end
52+
53+
54+
------------------------------------------------------------------------------
55+
-- test
56+
57+
do
58+
59+
-- tools
60+
61+
local function assert_error(statement)
62+
assert(pcall(statement) == false, "expected function error")
63+
end
64+
65+
66+
-- test class namespaces
67+
68+
assert_error(function()
69+
assert(tostring(MyClass("Olla")) == "Olla")
70+
end)
71+
72+
assert(tostring(namespace.MyClass("Olla")) == "Olla")
73+
74+
assert(type(GlobalBaseClass) == "GlobalBaseClass class")
75+
assert(type(GlobalBaseClass()) == "GlobalBaseClass")
76+
77+
assert(type(GlobalMyClass) == "GlobalMyClass class")
78+
assert(type(GlobalMyClass()) == "GlobalMyClass")
79+
80+
assert(type(namespace.BaseClass) == "BaseClass class")
81+
assert(type(namespace.BaseClass()) == "BaseClass")
82+
83+
assert(type(namespace.MyClass) == "MyClass class")
84+
assert(type(namespace.MyClass()) == "MyClass")
85+
86+
assert(type(renoise.song()) == "Song")
87+
assert(type(renoise.Song) == "Song class")
88+
89+
-- rprint(namespace)
90+
-- oprint(GlobalMyClass())
91+
-- oprint(namespace.MyClass())
92+
93+
94+
-- test class object comparison
95+
96+
local obj1 = GlobalMyClass()
97+
local obj2 = GlobalMyClass()
98+
99+
assert(rawequal(obj1, obj1))
100+
assert(not rawequal(obj1, obj2))
101+
102+
assert(rawequal(renoise.song(), renoise.song()))
103+
assert(rawequal(renoise.song().tracks[1], renoise.song().tracks[1]))
104+
assert(not rawequal(renoise.song().tracks[1], renoise.song().tracks[2]))
105+
assert(not rawequal(renoise.song().tracks[1], renoise.song().instruments[1]))
106+
assert(not rawequal(renoise.song().tracks[1], obj1))
107+
108+
end
109+

0 commit comments

Comments
 (0)