From 843c1d9ed276b2994cd0e64191c06ed044c7d9ae Mon Sep 17 00:00:00 2001 From: Sheridan C Rawlins Date: Wed, 16 Nov 2022 13:32:04 -0800 Subject: [PATCH] Add Nil, NotNil, Greater, GreaterOrEqual, Less, LessOrEqual and `f` variants. --- tests/assert_const.go | 2 +- tests/assertions.lua | 138 +++++++++++++++++++- tests/assertions_const.go | 140 ++++++++++++++++++++- tests/lua_const.go | 2 +- tests/require_const.go | 2 +- tests/testdata/test_assertions_failing.lua | 42 ++++++- tests/testdata/test_assertions_passing.lua | 44 ++++++- 7 files changed, 356 insertions(+), 14 deletions(-) diff --git a/tests/assert_const.go b/tests/assert_const.go index 9e7ab2a..e787fd5 100644 --- a/tests/assert_const.go +++ b/tests/assert_const.go @@ -1,7 +1,7 @@ // GENERATED BY textFileToGoConst // GitHub: github.com/logrusorgru/textFileToGoConst // input file: assert.lua -// generated: Fri Nov 11 18:29:58 PST 2022 +// generated: Wed Nov 16 13:31:38 PST 2022 package tests diff --git a/tests/assertions.lua b/tests/assertions.lua index e6fa35b..e5880ac 100644 --- a/tests/assertions.lua +++ b/tests/assertions.lua @@ -15,6 +15,10 @@ function assertions:__call(...) end function assertions:cleanseString(s) + -- Protect against nil + if s == nil then + return s + end s = string.gsub(s, '\n', '\\n') s = string.gsub(s, '\t', '\\t') s = string.gsub(s, '\r', '\\r') @@ -155,25 +159,151 @@ Messages: %s ]], err, fmt), ...) end +function assertions:NotNil(t, object, ...) + if object ~= nil then + return true + end + return self:Fail(t, string.format([[ + +Error: Expected value not to be nil. +Messages: ]]), ...) +end + +function assertions:NotNilf(t, object, fmt, ...) + if object ~= nil then + return true + end + return self:Failf(t, string.format([[ + +Error: Expected value not to be nil. +Messages: %s +]], fmt), ...) +end + +function assertions:Nil(t, object, ...) + if object == nil then + return true + end + return self:Fail(t, string.format([[ + +Error: Expected nil but got "%s" +Messages: ]], object), ...) +end + +function assertions:Nilf(t, object, fmt, ...) + if object == nil then + return true + end + return self:Failf(t, string.format([[ + +Error: Expected nil but got "%s" +Messages: %s +]], object, fmt), ...) +end + function assertions:Error(t, err, ...) if err then return true end - return self:Fail(t, string.format([[ + return self:Failf(t, string.format([[ Error: An error is expected but got nil. -Messages: ]], err), ...) +Messages: ]]), ...) end function assertions:Errorf(t, err, fmt, ...) if err then return true end - return self:Fail(t, string.format([[ + return self:Failf(t, string.format([[ Error: An error is expected but got nil. Messages: %s -]], err, fmt), ...) +]], fmt), ...) +end + +function assertions:Greater(t, x, y, ...) + if x > y then + return true + end + return self:Fail(t, string.format([[ + +Error: "%s" is not greater than "%s" +Messages: ]], tostring(x), tostring(y)), ...) +end + +function assertions:Greaterf(t, x, y, fmt, ...) + if x > y then + return true + end + return self:Failf(t, string.format([[ + +Error: "%s" is not greater than "%s" +Messages: %s +]], tostring(x), tostring(y), fmt), ...) +end + +function assertions:GreaterOrEqual(t, x, y, ...) + if x >= y then + return true + end + return self:Fail(t, string.format([[ + +Error: "%s" is not greater or equal than "%s" +Messages: ]], tostring(x), tostring(y)), ...) +end + +function assertions:GreaterOrEqualf(t, x, y, fmt, ...) + if x >= y then + return true + end + return self:Failf(t, string.format([[ + +Error: "%s" is not greater or equal than "%s" +Messages: %s +]], tostring(x), tostring(y), fmt), ...) +end + +function assertions:Less(t, x, y, ...) + if x < y then + return true + end + return self:Fail(t, string.format([[ + +Error: "%s" is not less than "%s" +Messages: ]], tostring(x), tostring(y)), ...) +end + +function assertions:Lessf(t, x, y, fmt, ...) + if x < y then + return true + end + return self:Failf(t, string.format([[ + +Error: "%s" is not less than "%s" +Messages: %s +]], tostring(x), tostring(y), fmt), ...) +end + +function assertions:LessOrEqual(t, x, y, ...) + if x <= y then + return true + end + return self:Fail(t, string.format([[ + +Error: "%s" is not less or equal than "%s" +Messages: ]], tostring(x), tostring(y)), ...) +end + +function assertions:LessOrEqualf(t, x, y, fmt, ...) + if x <= y then + return true + end + return self:Failf(t, string.format([[ + +Error: "%s" is not less or equal than "%s" +Messages: %s +]], tostring(x), tostring(y), fmt), ...) end return assertions diff --git a/tests/assertions_const.go b/tests/assertions_const.go index 416ae77..5a79347 100644 --- a/tests/assertions_const.go +++ b/tests/assertions_const.go @@ -1,7 +1,7 @@ // GENERATED BY textFileToGoConst // GitHub: github.com/logrusorgru/textFileToGoConst // input file: assertions.lua -// generated: Fri Nov 11 18:29:57 PST 2022 +// generated: Wed Nov 16 13:31:38 PST 2022 package tests @@ -22,6 +22,10 @@ function assertions:__call(...) end function assertions:cleanseString(s) + -- Protect against nil + if s == nil then + return s + end s = string.gsub(s, '\n', '\\n') s = string.gsub(s, '\t', '\\t') s = string.gsub(s, '\r', '\\r') @@ -162,25 +166,151 @@ Messages: %s ]], err, fmt), ...) end +function assertions:NotNil(t, object, ...) + if object ~= nil then + return true + end + return self:Fail(t, string.format([[ + +Error: Expected value not to be nil. +Messages: ]]), ...) +end + +function assertions:NotNilf(t, object, fmt, ...) + if object ~= nil then + return true + end + return self:Failf(t, string.format([[ + +Error: Expected value not to be nil. +Messages: %s +]], fmt), ...) +end + +function assertions:Nil(t, object, ...) + if object == nil then + return true + end + return self:Fail(t, string.format([[ + +Error: Expected nil but got "%s" +Messages: ]], object), ...) +end + +function assertions:Nilf(t, object, fmt, ...) + if object == nil then + return true + end + return self:Failf(t, string.format([[ + +Error: Expected nil but got "%s" +Messages: %s +]], object, fmt), ...) +end + function assertions:Error(t, err, ...) if err then return true end - return self:Fail(t, string.format([[ + return self:Failf(t, string.format([[ Error: An error is expected but got nil. -Messages: ]], err), ...) +Messages: ]]), ...) end function assertions:Errorf(t, err, fmt, ...) if err then return true end - return self:Fail(t, string.format([[ + return self:Failf(t, string.format([[ Error: An error is expected but got nil. Messages: %s -]], err, fmt), ...) +]], fmt), ...) +end + +function assertions:Greater(t, x, y, ...) + if x > y then + return true + end + return self:Fail(t, string.format([[ + +Error: "%s" is not greater than "%s" +Messages: ]], tostring(x), tostring(y)), ...) +end + +function assertions:Greaterf(t, x, y, fmt, ...) + if x > y then + return true + end + return self:Failf(t, string.format([[ + +Error: "%s" is not greater than "%s" +Messages: %s +]], tostring(x), tostring(y), fmt), ...) +end + +function assertions:GreaterOrEqual(t, x, y, ...) + if x >= y then + return true + end + return self:Fail(t, string.format([[ + +Error: "%s" is not greater or equal than "%s" +Messages: ]], tostring(x), tostring(y)), ...) +end + +function assertions:GreaterOrEqualf(t, x, y, fmt, ...) + if x >= y then + return true + end + return self:Failf(t, string.format([[ + +Error: "%s" is not greater or equal than "%s" +Messages: %s +]], tostring(x), tostring(y), fmt), ...) +end + +function assertions:Less(t, x, y, ...) + if x < y then + return true + end + return self:Fail(t, string.format([[ + +Error: "%s" is not less than "%s" +Messages: ]], tostring(x), tostring(y)), ...) +end + +function assertions:Lessf(t, x, y, fmt, ...) + if x < y then + return true + end + return self:Failf(t, string.format([[ + +Error: "%s" is not less than "%s" +Messages: %s +]], tostring(x), tostring(y), fmt), ...) +end + +function assertions:LessOrEqual(t, x, y, ...) + if x <= y then + return true + end + return self:Fail(t, string.format([[ + +Error: "%s" is not less or equal than "%s" +Messages: ]], tostring(x), tostring(y)), ...) +end + +function assertions:LessOrEqualf(t, x, y, fmt, ...) + if x <= y then + return true + end + return self:Failf(t, string.format([[ + +Error: "%s" is not less or equal than "%s" +Messages: %s +]], tostring(x), tostring(y), fmt), ...) end return assertions diff --git a/tests/lua_const.go b/tests/lua_const.go index 9e8e35a..ab0a1c7 100644 --- a/tests/lua_const.go +++ b/tests/lua_const.go @@ -1,7 +1,7 @@ // GENERATED BY textFileToGoConst // GitHub: github.com/logrusorgru/textFileToGoConst // input file: suite.lua -// generated: Fri Nov 11 18:29:56 PST 2022 +// generated: Wed Nov 16 13:31:37 PST 2022 package tests diff --git a/tests/require_const.go b/tests/require_const.go index 9cfb89f..e72edfc 100644 --- a/tests/require_const.go +++ b/tests/require_const.go @@ -1,7 +1,7 @@ // GENERATED BY textFileToGoConst // GitHub: github.com/logrusorgru/textFileToGoConst // input file: require.lua -// generated: Fri Nov 11 18:29:59 PST 2022 +// generated: Wed Nov 16 13:31:39 PST 2022 package tests diff --git a/tests/testdata/test_assertions_failing.lua b/tests/testdata/test_assertions_failing.lua index 910521e..1f1aa16 100644 --- a/tests/testdata/test_assertions_failing.lua +++ b/tests/testdata/test_assertions_failing.lua @@ -47,4 +47,44 @@ end function TestNoError(t) assert:NoError(t, "foo bar") -end \ No newline at end of file +end + +function TestNil(t) + assert:Nil(t, "foo bar baz") +end + +function TestNotNil(t) + assert:NotNil(t, nil) +end + +function TestGreater(t) + assert:Greater(t, 4, 5) +end + +function TestGreaterf(t) + assert:Greaterf(t, 4, 5, "foo %s", "bar") +end + +function TestGreaterOrEqual(t) + assert:GreaterOrEqual(t, 4, 5) +end + +function TestGreaterOrEqualf(t) + assert:GreaterOrEqualf(t, 4, 5, "foo %s", "bar") +end + +function TestLess(t) + assert:Less(t, 2, 1) +end + +function TestLessf(t) + assert:Lessf(t, 2, 1, "abc %s", "def") +end + +function TestLessOrEqual(t) + assert:LessOrEqual(t, 2, 1) +end + +function TestLessOrEqualf(t) + assert:LessOrEqualf(t, 2, 1, "abc %s", "def") +end diff --git a/tests/testdata/test_assertions_passing.lua b/tests/testdata/test_assertions_passing.lua index 1d45e7f..5f2ea50 100644 --- a/tests/testdata/test_assertions_passing.lua +++ b/tests/testdata/test_assertions_passing.lua @@ -62,4 +62,46 @@ function TestObjectsInspectEqual(t) } } require:Equal(t, inspect(t1), inspect(t2)) -end \ No newline at end of file +end + +function TestNil(t) + assert:Nil(t, nil) +end + +function TestNotNil(t) + assert:NotNil(t, 123) + assert:NotNil(t, "") +end + + +function TestGreater(t) + assert:Greater(t, 4, 1) +end + +function TestGreaterf(t) + assert:Greaterf(t, 4, 1, "foo %s", "bar") +end + +function TestGreaterOrEqual(t) + assert:GreaterOrEqual(t, 4, 4) +end + +function TestGreaterOrEqualf(t) + assert:GreaterOrEqualf(t, 4, 4, "foo %s", "bar") +end + +function TestLess(t) + assert:Less(t, 2, 3) +end + +function TestLessf(t) + assert:Less(t, 2, 3, "abc %s", "def") +end + +function TestLessOrEqual(t) + assert:LessOrEqual(t, 2, 2) +end + +function TestLessOrEqualf(t) + assert:LessOrEqualf(t, 2, 2, "abc %s", "def") +end