def test_all_of_describes_first_failing_match_if_match_fails(): class Rectangle(object): pass expected_output = [] is_a(Rectangle).matches("Rectangle", expected_output) mismatch_output = [] matcher = all_of(any_value(), is_a(Rectangle), has_attr(width=20)) matcher.matches("Rectangle", mismatch_output) assert_equals(expected_output, mismatch_output)
def test_any_of_str_is_disjunction_of_passed_matchers(): class Rectangle(object): pass is_a_rectangle = is_a(Rectangle) is_20_wide = has_attr(width=20) matcher = any_of(is_a_rectangle, is_20_wide) assert_equals("(%s) or (%s)" % (is_a_rectangle, is_20_wide), str(matcher))
def test_has_attr_can_use_matchers(): class Person(object): def __init__(self, name): self.name = name matcher = has_attr(name=is_a(str)) assert matcher.matches(Person("Bob"), []) assert not matcher.matches(Person(42), [])
def test_all_of_does_not_write_to_mismatch_output_if_it_matches(): class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height mismatch_description = [] matcher = all_of(is_a(Rectangle), has_attr(width=20)) assert matcher.matches(Rectangle(20, 40), mismatch_description) assert not mismatch_description
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_any_of_passes_if_any_matchers_satisfied(): class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height class NotARectangle(object): def __init__(self, width): self.width = width matcher = any_of(is_a(Rectangle), has_attr(width=20)) assert matcher.matches(Rectangle(20, 40), []) assert matcher.matches(NotARectangle(20), []) assert matcher.matches(Rectangle(30, 40), []) assert not matcher.matches("Rectangle", [])
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_is_a_does_not_show_module_if_type_is_builtin(): assert_equals("<value of type: int>", str(is_a(int)))
def test_is_a_writes_type_of_value_if_it_does_not_match(): class SomeClass(object): pass mismatch_output = [] is_a(str).matches(SomeClass(), mismatch_output) assert_equals(["got <value of type: test.funk.test_matchers.SomeClass>"], mismatch_output)
def test_is_a_does_not_write_to_mismatch_output_if_it_matches(): mismatch_output = [] assert is_a(type).matches(str, mismatch_output) assert is_a(str).matches("foo", mismatch_output) assert not mismatch_output
def test_is_a_str_shows_type(): class SomeClass(object): pass assert_equals(str(is_a(SomeClass)), "<value of type: test.funk.test_matchers.SomeClass>")
def test_is_a_matches_on_type(): assert is_a(type).matches(str, []) assert is_a(str).matches("foo", []) assert is_a(object).matches("foo", []) assert not is_a(str).matches(4, [])