From dae9a662c23724749d78cda42f9f14bd4b450b66 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Wed, 26 Mar 2014 16:59:47 +1000 Subject: Write, use, and test isValid(). --- ClientHamr/GuiLua/skang.lua | 24 ++++++++++++++++++------ ClientHamr/GuiLua/test.lua | 13 ++++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) (limited to 'ClientHamr/GuiLua') diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index f88ff1c..cb97a76 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua @@ -217,10 +217,17 @@ Thing.append = function (self,data) -- Append to the value of this Thing. end Thing.isValid = function (self) -- Check if this Thing is valid, return resulting error messages in errors. - self.errors = {} - -- TODO - Should check for required, matching mask, matching type, etc. - return true - end + -- Anything that overrides this method, should call this super method first. + local value = self.module[self.names[1] ] + self.errors = {} + if 'nil' == type(value) then + if self.required then table.insert(self.errors, self.names[1] .. ' is required!') end + else + if self.types[1] ~= type(value) then table.insert(self.errors, self.names[1] .. ' should be a ' .. self.types[1] .. ', but it is a ' .. type(value) .. '!') end + end + -- TODO - Should check for matching mask, and anything else. + return #(self.errors) == 0 +end Thing.remove = function (self) -- Delete this Thing. end @@ -264,8 +271,13 @@ Thing.__newindex = function (table, key, value) end print(thing.module._NAME .. '.' .. name .. '(' .. types .. ') -> ' .. thing.help) else - thing:isValid() - print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. name .. ' = ' .. (value or 'nil') .. ' -> ' .. thing.help) + -- NOTE - invalid values are still stored, this is by design. + if not thing:isValid() then + for i, v in ipairs(thing.errors) do + print('ERROR - ' .. v) + end + end +-- print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. name .. ' = ' .. (value or 'nil') .. ' -> ' .. thing.help) -- TODO - Go through it's linked things and set them to. end else diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index 97adf9d..bc595fe 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua @@ -30,7 +30,7 @@ print('code') -- A variable that is private to this module. local foo -skang.thing(_M, 'fooble,f', 'Help text goes here', 1, nil, '"edit", "The fooble:", 1, 1, 10, 50') +skang.thing(_M, 'fooble,f', 'Help text goes here', 1, 'number', '"edit", "The fooble:", 1, 1, 10, 50', true) skang.thing(_M, 'bar', 'Help text', "Default") -- We can use inline functions if we don't need the function internally. @@ -58,3 +58,14 @@ test.fooble = 42 print('f is now ' .. test.fooble .. ' ' .. test.f) test.fooble = nil print('f is now ' .. test.fooble .. ' ' .. test.f) +-- First, disable the default value, so we see "is required" errors. +skang.things.f.default = nil +test.fooble = 42 +test.fooble = 'Should fail.' +test.fooble = 42 +test.fooble = nil +test.fooble = nil +test.fooble = 42 +test.fooble = true +test.f = 42 +test.f = nil -- cgit v1.1