def test_respects_precedences(self): parse("1 + 2 * 3").should.contain( Add(Integer(1), Mul(Integer(2), Integer(3)))) parse("2 / 3 - 1").should.contain( Sub(Div(Integer(2), Integer(3)), Integer(1))) parse("5 * 2 - 3").should.contain( Sub(Mul(Integer(5), Integer(2)), Integer(3)))
def test_of_different_types_cant_be_compared(self): add = Add(Integer(18), Integer(120)) v1 = Integer(1) expected_message = f'You can\'t compare a BinaryOp and {v1.__class__.__name__}.' \ f'\nTokens being compared:\n{add.dump()}\n{v1.dump}' # noinspection PyUnresolvedReferences add.__eq__.when.called_with(v1).should.throw(LogicError, expected_message)
def test_of_different_types_cant_be_compared(self): v1 = Integer(1) add = Add(Integer(81), Integer(14)) expected_message = f'You can\'t compare a Value and {add.__class__.__name__}.' \ f'\nTokens being compared:\n{v1.dump()}\n{add}' # noinspection PyUnresolvedReferences v1.__eq__.when.called_with(add).should.throw(LogicError, expected_message)
def test_can_be_created_from_a_list_of_statements(self): s1 = Add(Integer(8), Integer(10)) s2 = Mul(Integer(18), Integer(1)) body = [ s1, s2, ] b1 = Block(body) b1.statements.should.contain(s1) b1.statements.should.contain(s2)
def test_supports_strings(self): parse("'alpha' + 'beta'").should.contain( Add(String('alpha'), String('beta')))
def test_addition(self): parse("1 + 1").should.contain(Add(Integer(1), Integer(1)))
def test_has_a_block_with_expression(self): prog = parse("1 + 1") prog.block.statements.should.contain(Add(Integer(1), Integer(1)))
def test_has_a_block(self): prog = parse("1 + 1") prog.block.should.equal(Block(Add(Integer(1), Integer(1))))
def test_can_be_created_from_single_statement(self): body = Add(Integer(8), Integer(10)) b1 = Block(body) b1.statements.should.contain(body)
def test_works_for_programs(self): p1 = Program(Block(Add(Integer(8), Integer(10)))) p2 = Program(Block(Add(Integer(8), Integer(10)))) # noinspection PyUnresolvedReferences p1.should.be.equal(p2)
def add(self, lhs, rhs): return Add(lhs, rhs)