From 79f5986e7823b388c4562c7ccf8742a521685d2e Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Wed, 28 Jan 2009 18:41:19 +0000 Subject: [PATCH] Add tests for operator type checking. --- src/test/errors/operators.lua | 152 ++++++++++++++++++ .../java/org/luaj/vm/ErrorMessageTest.java | 4 + 2 files changed, 156 insertions(+) create mode 100644 src/test/errors/operators.lua diff --git a/src/test/errors/operators.lua b/src/test/errors/operators.lua new file mode 100644 index 00000000..0dc481fa --- /dev/null +++ b/src/test/errors/operators.lua @@ -0,0 +1,152 @@ +package.path = "?.lua;src/test/errors/?.lua" +require 'args' + +-- arg types for language operator + +-- ========= unary operators: - # not + +-- unary minus - +banner('unary -') +negative = function(a) return - a end +checkallpass('negative',{somenumber}) +checkallerrors('negative',{notanumber},'attempt to perform arithmetic on') + +-- length +banner('#') +lengthop = function(a) return #a end +checkallpass('lengthop',{sometable}) +checkallerrors('lengthop',{notatable},'attempt to get length of') + +-- length +banner('not') +notop = function(a) return not a end +checkallpass('notop',{somenumber}) +checkallerrors('notop',{notanumber},'attempt to perform arithmetic on') + +-- ========= binary ops: .. + - * / % ^ == ~= <= >= < > [] . and or +banner( '..' ) +concatop = function(a,b) return a..b end +checkallpass('concatop',{somestring,somestring}) +checkallerrors('concatop',{notastring,somestring},'attempt to concatenate') +checkallerrors('concatop',{somestring,notastring},'attempt to concatenate') + +banner( '+' ) +plusop = function(a,b) return a+b end +checkallpass('plusop',{somenumber,somenumber}) +checkallerrors('plusop',{notanumber,somenumber},'attempt to perform arithmetic on') +checkallerrors('plusop',{somenumber,notanumber},'attempt to perform arithmetic on') + +banner( '-' ) +minusop = function(a,b) return a-b end +checkallpass('minusop',{somenumber,somenumber}) +checkallerrors('minusop',{notanumber,somenumber},'attempt to perform arithmetic on') +checkallerrors('minusop',{somenumber,notanumber},'attempt to perform arithmetic on') + +banner( '*' ) +timesop = function(a,b) return a*b end +checkallpass('timesop',{somenumber,somenumber}) +checkallerrors('timesop',{notanumber,somenumber},'attempt to perform arithmetic on') +checkallerrors('timesop',{somenumber,notanumber},'attempt to perform arithmetic on') + +banner( '/' ) +divideop = function(a,b) return a/b end +checkallpass('divideop',{somenumber,somenumber}) +checkallerrors('divideop',{notanumber,somenumber},'attempt to perform arithmetic on') +checkallerrors('divideop',{somenumber,notanumber},'attempt to perform arithmetic on') + +banner( '%' ) +modop = function(a,b) return a%b end +checkallpass('modop',{somenumber,somenumber}) +checkallerrors('modop',{notanumber,somenumber},'attempt to perform arithmetic on') +checkallerrors('modop',{somenumber,notanumber},'attempt to perform arithmetic on') + +banner( '^' ) +powerop = function(a,b) return a^b end +checkallpass('powerop',{{2,'2.5'},{3,'3.5'}}) +checkallerrors('powerop',{notanumber,{3,'3.1'}},'attempt to perform arithmetic on') +checkallerrors('powerop',{{2,'2.1'},notanumber},'attempt to perform arithmetic on') + +banner( '==' ) +equalsop = function(a,b) return a==b end +checkallpass('equalsop',{anylua,anylua}) + +banner( '~=' ) +noteqop = function(a,b) return a~=b end +checkallpass('noteqop',{anylua,anylua}) + +banner( '<=' ) +leop = function(a,b) return a<=b end +checkallpass('leop',{{anumber},{anumber}}) +checkallpass('leop',{{astring,astrnum},{astring,astrnum}}) +checkallerrors('leop',{notanumber,{anumber}},'attempt to compare') +checkallerrors('leop',{{astrnum},{anumber}},'attempt to compare') +checkallerrors('leop',{notastring,{astring,astrnum}},'attempt to compare') +checkallerrors('leop',{{anumber},notanumber},'attempt to compare') +checkallerrors('leop',{{anumber},{astrnum}},'attempt to compare') +checkallerrors('leop',{{astring,astrnum},notastring},'attempt to compare') + +banner( '>=' ) +geop = function(a,b) return a>=b end +checkallpass('geop',{{anumber},{anumber}}) +checkallpass('geop',{{astring,astrnum},{astring,astrnum}}) +checkallerrors('geop',{notanumber,{anumber}},'attempt to compare') +checkallerrors('geop',{{astrnum},{anumber}},'attempt to compare') +checkallerrors('geop',{notastring,{astring,astrnum}},'attempt to compare') +checkallerrors('geop',{{anumber},notanumber},'attempt to compare') +checkallerrors('geop',{{anumber},{astrnum}},'attempt to compare') +checkallerrors('geop',{{astring,astrnum},notastring},'attempt to compare') + +banner( '<' ) +ltop = function(a,b) return a' ) +gtop = function(a,b) return a>b end +checkallpass('gtop',{{anumber},{anumber}}) +checkallpass('gtop',{{astring,astrnum},{astring,astrnum}}) +checkallerrors('gtop',{notanumber,{anumber}},'attempt to compare') +checkallerrors('gtop',{{astrnum},{anumber}},'attempt to compare') +checkallerrors('gtop',{notastring,{astring,astrnum}},'attempt to compare') +checkallerrors('gtop',{{anumber},notanumber},'attempt to compare') +checkallerrors('gtop',{{anumber},{astrnum}},'attempt to compare') +checkallerrors('gtop',{{astring,astrnum},notastring},'attempt to compare') + +banner( '[]' ) +bracketop = function(a,b) return a[b] end +checkallpass('bracketop',{sometable,notanil}) +checkallerrors('bracketop',{notatable,notanil},'attempt to index') +checkallerrors('bracketop',{sometable},'attempt to index') + +banner( '.' ) +dotop = function(a,b) return a.b end +checkallpass('dotop',{sometable,notanil}) +checkallerrors('dotop',{notatable,notanil},'attempt to index') +checkallerrors('dotop',{sometable},'attempt to index') + +banner( 'and' ) +types = {['table']='table',['function']='function',['thread']='thread'} +clean = function(x) return types[type(x)] or x end +andop = function(a,b) return clean(a and b) end +checkallpass('andop',{anylua,anylua}) + +banner( 'or' ) +orop = function(a,b) return clean(a or b) end +checkallpass('orop',{anylua,anylua}) + + +-- ========= for x in y +banner( 'for x=a,b,c' ) +forop = function(a,b,c) for x=a,b,c do end end +checkallpass('forop',{{1,'1.1'},{10,'10.1'},{2,'2.1'}}) +checkallerrors('forop',{notanumber,{10,'10.1'},{2,'2.1'}},"'for' initial value must be a number") +checkallerrors('forop',{{1,'1.1'},notanumber,{2,'2.1'}},"'for' limit must be a number") +checkallerrors('forop',{{1,'1.1'},{10,'10.1'},notanumber},"'for' step must be a number") + + diff --git a/src/test/java/org/luaj/vm/ErrorMessageTest.java b/src/test/java/org/luaj/vm/ErrorMessageTest.java index 743f99c0..13618776 100644 --- a/src/test/java/org/luaj/vm/ErrorMessageTest.java +++ b/src/test/java/org/luaj/vm/ErrorMessageTest.java @@ -25,6 +25,10 @@ public class ErrorMessageTest extends ScriptDrivenTest { runTest("modulelibargs"); } + public void testOperators() throws IOException, InterruptedException { + runTest("operators"); + } + public void testStringLibArgs() throws IOException, InterruptedException { runTest("stringlibargs"); }