Esempio n. 1
0
def test_contains_exactly_does_not_write_to_mismatch_output_if_it_matches():
    mismatch_output = []
    assert contains_exactly().matches([], mismatch_output)
    assert contains_exactly().matches((), mismatch_output)
    assert contains_exactly(equal_to(42), equal_to(9)).matches([42, 9], mismatch_output)
    assert contains_exactly(equal_to(42), equal_to(9)).matches([9, 42], mismatch_output)
    assert not mismatch_output
Esempio n. 2
0
def test_contains_exactly_matches_any_collection_with_only_those_elements_in_any_order():
    assert contains_exactly(equal_to(42), equal_to(9)).matches([42, 9], [])
    assert contains_exactly(equal_to(42), equal_to(9)).matches(set([42, 9]), [])
    assert contains_exactly(equal_to(42), equal_to(9)).matches([9, 42], [])
    assert not contains_exactly(equal_to(42), equal_to(9)).matches([42], [])
    assert not contains_exactly(equal_to(42), equal_to(9)).matches([9], [])
    assert not contains_exactly(equal_to(42), equal_to(9)).matches([], [])
    assert not contains_exactly(equal_to(42), equal_to(9)).matches([42, 9, 1], [])
Esempio n. 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)
    )))
Esempio n. 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)
    )))
Esempio n. 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)
    )))
Esempio n. 6
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)
    )))
Esempio n. 7
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)
    )))
Esempio n. 8
0
def test_contains_exactly_converts_non_matcher_elements_to_matchers_using_equal_to():
    assert contains_exactly(42, 9).matches([42, 9], [])
    assert not contains_exactly(42, 9).matches([42, 9, 1], [])
Esempio n. 9
0
def test_contains_exactly_writes_extra_elements_to_mismatch_output():
    mismatch_output = []
    contains_exactly(equal_to(42), equal_to(9)).matches([42, 1, 9, "eggs"], mismatch_output)
    assert_equals(["iterable contained extra elements: 1, 'eggs' (got: [42, 1, 9, 'eggs'])"], mismatch_output)
Esempio n. 10
0
def test_contains_exactly_writes_not_iterable_to_mismatch_output_if_not_passed_an_iterable():
    mismatch_output = []
    contains_exactly(equal_to(42), equal_to(9)).matches(None, mismatch_output)
    assert_equals(["was not iterable (got: None)"], mismatch_output)
Esempio n. 11
0
def test_contains_exactly_describes_missing_element_in_mismatch_output():
    mismatch_output = []
    contains_exactly(equal_to(42), equal_to(9)).matches([42], mismatch_output)
    assert_equals(["iterable did not contain element: 9 (got: [42])"], mismatch_output)
Esempio n. 12
0
def test_contains_exactly_describes_all_sub_matchers():
    assert_equals(str(contains_exactly(equal_to(42), equal_to(9))), "<iterable containing exactly: 42, 9>")
Esempio n. 13
0
def test_contains_exactly_empty_array_only_matches_empty_collections():
    assert contains_exactly().matches([], [])
    assert contains_exactly().matches((), [])
    assert not contains_exactly().matches([None], [])
    assert not contains_exactly().matches((None, ), [])
    assert not contains_exactly().matches(None, [])