def _make_body(self, srcfile, first): return [ Number(1.0, locn=SourceLocation(srcfile, line=first + 1, column=1)), Number(2.0, locn=SourceLocation(srcfile, line=first + 1, column=9)), Number(3.0, locn=SourceLocation(srcfile, line=first + 2, column=3)), Number(4.0, locn=SourceLocation(srcfile, line=first + 3, column=1)), ]
def test_equal(self): self.assertEqual(Number(13), Number(13)) self.assertEqual( Number(17, locn=SourceLocation('path', line=2, column=3)), Number(17, locn=SourceLocation('path', line=2, column=3))) # Differ by value self.assertNotEqual(Number(13), Number(27)) # Differ only by source location self.assertNotEqual( Number(23, locn=SourceLocation('path', line=2, column=3)), Number(23, locn=SourceLocation('path', line=2, column=432)))
def test_procedure_round_trip(self) -> None: Procedure([ Number(value=42.0, locn=SourceLocation(srcfile="foo.toy", line=23, column=29)), Number(value=68.0, locn=SourceLocation(srcfile="foo.toy", line=24, column=31)) ], locn=SourceLocation(srcfile="foo.toy", line=22, column=1)).write(self.__sections) self.__reset() n2 = Instruction.read(self.__sections) self.assertIsInstance(n2, Procedure) expected = [Number(value=42.0), Number(value=68.0)] instructions = n2.instructions() self.assertSequenceEqual(expected, instructions) n2.read_debug(binary=self.__sections[SectionType.debug_line], line_base=0) self.assertEqual(SourceLocation(srcfile="foo.toy", line=23, column=29), instructions[0].locn()) self.assertEqual(SourceLocation(srcfile="foo.toy", line=24, column=31), instructions[1].locn())
def test_number_round_trip(self) -> None: Number(value=42.0, locn=SourceLocation(srcfile="foo.toy", line=23, column=29)).write(self.__sections) self.__reset() n2 = Instruction.read(self.__sections) self.assertIsInstance(n2, Number) self.assertEqual(42.0, n2.value()) self.assertIsNone(n2.locn()) # No debug line information yet. n2.read_debug(binary=self.__sections[SectionType.debug_line], line_base=0) l = n2.locn() self.assertEqual("foo.toy", l.srcfile) self.assertEqual(23, l.line) self.assertEqual(29, l.column)
def test_procedure_with_builtin_operator(self): name = list(Machine().systemdict().keys())[0] fixups = procedure_fixups(Procedure([Operator(name), Number(1.0)])) self.assertEqual(fixups, [])
def test_procedure_with_unnamed_instruction(self): fixups = procedure_fixups(Procedure([Operator('foo'), Number(1.0)])) self.assertEqual(fixups, ['foo'])
def _get_digest(self, *args, **kwargs): h = hashlib.new('md5') Number(*args, **kwargs).digest(h) return h.digest()
def test_execute(self): m = Mock(spec=Machine) Number(2.71).execute(m) m.operand_push.assert_called_once_with(2.71)