def test_arithmetic(self): """Test that arithmetic operations result in integers.""" for test in [ TypeTest(sir.BinOpCode(name = 'OP_ADD', left = sir.Int(5), right = sir.Int(6)), SymbolType.Integer), TypeTest(sir.BinOpCode(name = 'OP_ADD', left = sir.Bytes('05'), right = sir.Bytes('06')), SymbolType.Integer), ]: self._test(test)
def test_assignments(self): for node, expected in [ (sir.Declaration(name='foo', value=sir.Int(5), type_=SymbolType.Integer, mutable=True), 'let mutable foo = 5'), (sir.Declaration(name='foo', value=sir.Int(5), type_=SymbolType.Integer, mutable=False), 'let foo = 5'), (sir.Assignment(name='foo', value=sir.Int(5), type_=SymbolType.Integer), 'foo = 5'), (sir.Assignment(name='foo', value=sir.Bytes('05'), type_=SymbolType.Integer), 'foo = 05'), ]: self.assertEqual(expected, SInstructions.format_op(node))
def test_operations(self): for test in [ TypeTest(sir.UnaryOpCode(name = 'OP_SHA1', operand = sir.Int(5)), SymbolType.ByteArray), TypeTest(sir.UnaryOpCode(name = 'OP_SHA1', operand = sir.Symbol('foo')), SymbolType.Expr), TypeTest(sir.VariableArgsOpCode(name='OP_CHECKMULTISIG', operands=[sir.Int(1), sir.Bytes('111111'), sir.Bytes('222222'), sir.Int(2)]), SymbolType.ByteArray), ]: self._test(test)
def test_function_call(self): for node, expected in [ (sir.FunctionCall(name='foo', args=[]), 'foo()'), (sir.FunctionCall(name='foo', args=[sir.Int(5)]), 'foo(5)'), (sir.FunctionCall(name='foo', args=[sir.Int(5), sir.Int(6)]), 'foo(5, 6)'), (sir.FunctionCall(name='foo', args=[sir.Int(5), sir.Bytes('06')]), 'foo(5, 06)'), ]: self.assertEqual(expected, SInstructions.format_op(node))
def test_function(self): for node, expected in [ (sir.Function(name='foo', args=[sir.Symbol('a')], body=[sir.BinOpCode(name='OP_ADD', left=sir.Symbol('a'), right=sir.Int(2))]), 'func foo(a) {a + 2;}'), (sir.Function(name='foo', args=[sir.Symbol('a')], body=[sir.BinOpCode(name='OP_ADD', left=sir.Symbol('a'), right=sir.Int(2)), sir.Int(5)]), 'func foo(a) {a + 2; 5;}'), (sir.Function(name='foo', args=[sir.Symbol('a'), sir.Symbol('b')], body=[sir.BinOpCode(name='OP_ADD', left=sir.Symbol('a'), right=sir.Symbol('b')), sir.Int(5)]), 'func foo(a, b) {a + b; 5;}'), ]: self.assertEqual(expected, SInstructions.format_op(node))
def test_conditional(self): for node, expected in [ (sir.If(test=sir.Int(1), truebranch=[sir.Int(5)], falsebranch=[]), 'if 1 {5;}'), (sir.If(test=sir.Int(1), truebranch=[sir.Int(5)], falsebranch=[sir.Int(6)]), 'if 1 {5;} else {6;}'), (sir.If(test=sir.Int(1), truebranch=[sir.Int(5)], falsebranch=[sir.Int(6), sir.Int(7)]), 'if 1 {5;} else {6; 7;}'), ]: self.assertEqual(expected, SInstructions.format_op(node))
def test_basic_types(self): for test in [ TypeTest(sir.Int(5), SymbolType.Integer), TypeTest(sir.Bytes('05'), SymbolType.ByteArray), TypeTest(sir.Symbol('foo'), SymbolType.Symbol), ]: self._test(test)
def visit_Num(self, node): return types.Int(node.n)
def test_innerscript(self): for node, expected in [ (sir.InnerScript(statements=[sir.Int(5)]), '5;'), (sir.InnerScript(statements=[sir.Int(5), sir.Int(6)]), '5; 6;'), ]: self.assertEqual(expected, SInstructions.format_op(node))