From 498eb2750bf0f86db71abdc000cccdd01a196bd7 Mon Sep 17 00:00:00 2001 From: lethosor Date: Mon, 6 Apr 2020 19:20:47 -0400 Subject: [PATCH] +Tests --- test/main.lua | 19 ++++++++++ test/structures/types_meta.lua | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 test/structures/types_meta.lua diff --git a/test/main.lua b/test/main.lua index c8e34cc70..46a91e1ca 100644 --- a/test/main.lua +++ b/test/main.lua @@ -22,6 +22,9 @@ end function expect.false_(value, comment) return not value, comment, 'expected false, got ' .. tostring(value) end +function expect.fail(comment) + return false, comment or 'check failed, no reason provided' +end function expect.nil_(value, comment) return value == nil, comment, 'expected nil, got ' .. tostring(value) end @@ -66,6 +69,22 @@ function expect.error(func, ...) return true end end +function expect.pairs_contains(table, key, comment) + for k, v in pairs(table) do + if k == key then + return true + end + end + return false, comment, ('could not find key "%s" in table'):format(key) +end +function expect.not_pairs_contains(table, key, comment) + for k, v in pairs(table) do + if k == key then + return false, comment, ('found key "%s" in table'):format(key) + end + end + return true +end function delay(frames) frames = frames or 1 diff --git a/test/structures/types_meta.lua b/test/structures/types_meta.lua new file mode 100644 index 000000000..212e9c7ed --- /dev/null +++ b/test/structures/types_meta.lua @@ -0,0 +1,65 @@ +function test.struct() + expect.eq(df.coord._kind, 'struct-type') + expect.eq(tostring(df.coord), '') + expect.eq(getmetatable(df.coord), 'coord') + expect.pairs_contains(df.coord, 'new') + expect.pairs_contains(df.coord, 'is_instance') + expect.pairs_contains(df.coord, 'sizeof') +end + +function test.class() + expect.eq(df.viewscreen._kind, 'class-type') + expect.eq(tostring(df.viewscreen), '') + expect.eq(getmetatable(df.viewscreen), 'viewscreen') + expect.pairs_contains(df.viewscreen, 'new') + expect.pairs_contains(df.viewscreen, 'is_instance') + expect.pairs_contains(df.viewscreen, 'sizeof') +end + +function test.enum() + expect.eq(df.interface_key._kind, 'enum-type') + expect.eq(tostring(df.interface_key), '') + expect.eq(getmetatable(df.interface_key), 'interface_key') + expect.pairs_contains(df.interface_key, 'new') + expect.pairs_contains(df.interface_key, 'is_instance') + expect.pairs_contains(df.interface_key, 'sizeof') +end + +function test.bitfield() + expect.eq(df.item_flags._kind, 'bitfield-type') + expect.eq(tostring(df.item_flags), '') + expect.eq(getmetatable(df.item_flags), 'item_flags') + expect.pairs_contains(df.item_flags, 'new') + expect.pairs_contains(df.item_flags, 'is_instance') + expect.pairs_contains(df.item_flags, 'sizeof') +end + +function test.global() + expect.eq(df.global._kind, 'global') + expect.eq(tostring(df.global), '') + expect.eq(getmetatable(df.global), 'global') + expect.not_pairs_contains(df.global, 'new') + expect.not_pairs_contains(df.global, 'is_instance') + expect.not_pairs_contains(df.global, 'sizeof') +end + +function test.unit() + expect.pairs_contains(df.unit, 'new') + expect.pairs_contains(df.unit, 'is_instance') + expect.pairs_contains(df.unit, 'sizeof') + expect.pairs_contains(df.unit, 'find') + expect.pairs_contains(df.unit, 'get_vector') + expect.pairs_contains(df.unit, '_kind') + + if df.unit.T_job then + expect.pairs_contains(df.unit, 'T_job') + expect.pairs_contains(df.unit.T_job, 'new') + expect.pairs_contains(df.unit.T_job, 'is_instance') + expect.pairs_contains(df.unit.T_job, 'sizeof') + expect.not_pairs_contains(df.unit.T_job, 'find') + expect.not_pairs_contains(df.unit.T_job, 'get_vector') + expect.pairs_contains(df.unit.T_job, '_kind') + else + expect.fail('unit.T_job not defined; unit has changed') + end +end