Example #1
0
def test_all_of_str_is_conjunction_of_passed_matchers():
    class Rectangle(object):
        pass
    is_a_rectangle = is_a(Rectangle)
    is_20_wide = has_attr(width=20)
    matcher = all_of(is_a_rectangle, is_20_wide)
    assert_equals("(%s) and (%s)" % (is_a_rectangle, is_20_wide), str(matcher))
Example #2
0
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
Example #3
0
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)
    )))
Example #4
0
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)
    )))
Example #5
0
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)
    )))
Example #6
0
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)
Example #7
0
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)
    )))
Example #8
0
def test_all_of_passes_if_all_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 = all_of(is_a(Rectangle), has_attr(width=20))
    assert matcher.matches(Rectangle(20, 40), [])
    assert not matcher.matches(NotARectangle(20), [])
    assert not matcher.matches(Rectangle(30, 40), [])
    assert not matcher.matches("Rectangle", [])
Example #9
0
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)
    )))