def test_adds_error_if_production_rule_is_never_used(context): errors = [] parse("$SENTENCE -> \n$RUDE_ADJ -> ugly", RuleSet(), errors); assert_that(errors, m.contains_exactly(m.all_of( m.has_attr(message="Production rule with start symbol $RUDE_ADJ is never used (line 2)", line_number=2, start="RUDE_ADJ"), m.is_a(RuleNeverUsed) )))
def test_adds_error_if_sentence_has_no_production_rule(context): errors = [] parse("", RuleSet(), errors); assert_that(errors, m.contains_exactly(m.all_of( m.has_attr(message="No production rule for non-terminal $SENTENCE", non_terminal="SENTENCE"), m.is_a(NoProductionRule) )))
def test_adds_error_if_non_terminal_is_used_with_no_matching_production_rule(context): errors = [] parse("\n\n$SENTENCE -> $INSULT\n\n", RuleSet(), errors); assert_that(errors, m.contains_exactly(m.all_of( m.has_attr(message="No production rule for non-terminal $INSULT (line 3, character 14)", line_number=3, character_number=14, non_terminal="INSULT"), m.is_a(NoProductionRule) )))
def test_adds_error_with_line_number_if_closing_brace_for_second_variable_is_missing(context): errors = [] parse("\n\n$SENTENCE -> You're ${RUDE_ADJ}er than ${OBJ\n" + "$SENTENCE ->\n\n", RuleSet(), errors) assert_that(errors, m.contains_exactly(m.all_of( m.has_attr(message="Missing closing brace on line 3 (opening brace at character 41)", line_number=3, opening_brace_character_number=41), m.is_a(MissingClosingBrace) )))
def test_adds_error_with_line_number_if_arrow_is_missing(context): rule_set = context.mock(RuleSet) allows(rule_set).add errors = [] parse("\n\n$SENTENCE - You're ${RUDE_ADJ}er than I thought\n" + "$RUDE_ADJ ->\n" + "$SENTENCE -> $RUDE_ADJ", rule_set, errors) assert_that(errors, m.contains_exactly(m.all_of( m.has_attr(message="Missing symbol on line 3: ->", line_number=3), m.is_a(MissingArrow) )))
def test_assert_that_raises_assertion_error_describing_expected_and_actual_results(): class HasZeroLength(Matcher): def matches(self, value, failure_out): if len(value): failure_out.append("got <value of length %s>" % len(value)) return False return True def __str__(self): return "<value of length zero>" assert_that([], HasZeroLength()) assert_raises_str(AssertionError, "Expected: <value of length zero>\nbut: got <value of length 8>", lambda: assert_that("Anything", HasZeroLength()))
def test_assert_that_raises_assertion_error_if_matcher_returns_false(): class FalseMatcher(Matcher): def matches(self, value, failure_out): return False assert_raises(AssertionError, lambda: assert_that("Anything", FalseMatcher()))
def test_assert_that_passes_if_matcher_returns_true(): class TrueMatcher(Matcher): def matches(self, value, failure_out): return True assert_that("Anything", TrueMatcher())