Ejemplo n.º 1
0
def test_strip_tags_in_scripts(input, expected):
    r = Richtypo()
    r.text = input
    r.strip_tags()

    expected = re.sub(r'~(\d+)~', '~([^~]+)~', expected)  # it is a regex now
    expected = re.compile(expected)
    assert expected.match(r.text)
Ejemplo n.º 2
0
def test_unicode_with_ascii_only_characters():
    """
    If this test does not fail, and the above test does,
    then i've messed up with unicode
    """
    r = Richtypo(ruleset='en-lite')
    text = u'2 to 3'
    assert r.richtypo(text) == u'2%sto%s3' % (NBSP, NBSP)
Ejemplo n.º 3
0
def test_punctuation(input, expected):
    """
    Test the complex rule for spaces after punctuation marks:
    the 'generic:punctuation_spaces' which should also be followed
    by 'generic:spaces_eol'
    """
    r = Richtypo()
    assert r.richtypo(input) == expected
Ejemplo n.º 4
0
def test_build_rule_chain(rules):
    r = Richtypo()
    r.available_rules = {'b': Rule(pattern='b', replacement='d')}

    with patch('richtypo.Richtypo._get_ruleset_rules') as rules:
        rules.return_value = ['b', ABRule]

        r.build_rule_chain('generic')
        assert len(r.rules) == 2
Ejemplo n.º 5
0
def test_ruleset_input_param(get_ruleset):
    get_ruleset.return_value = [
        ABRule,
    ]
    r = Richtypo(ruleset='generic')  # this ruleset realy should exist
    assert len(r.rules) == 1
    assert ABRule in r.rules
Ejemplo n.º 6
0
def test_restore_tags(input):
    r = Richtypo()
    r.text = input

    r.strip_tags()
    r.restore_tags()
    assert r.text == input
Ejemplo n.º 7
0
def test_loading_generic_ruledef(rules):
    """
    If not specified, all rules should be taken from the generic ruledef (rules/generic.yaml)
    """
    rules.return_value = ['one', 'two']

    with patch('richtypo.Richtypo.build_rule_chain'
               ):  # dont actualy build rule chain
        with patch('richtypo.Richtypo._load_rules_from_file') as loader:
            Richtypo()
            assert loader.call_count == 1
            loader.assert_called_with('generic')
Ejemplo n.º 8
0
def test_loading_ruledef_only_once(get_ruleset):
    """
    Check if ruledefs are loaded only once
    """
    get_ruleset.return_value = [
        'ru:test', 'tst-ruledef:test', 'ru:test1', 'ru:test2'
    ]

    with patch('richtypo.Richtypo.build_rule_chain'
               ):  # dont actualy build rule chain
        with patch('richtypo.Richtypo._load_rules_from_file') as loader:
            Richtypo()
            assert loader.call_count == 2  #
            loader.assert_any_call('ru')
            loader.assert_any_call('tst-ruledef')
Ejemplo n.º 9
0
def test_rule_applying():
    r1 = Rule(pattern='b{2}', replacement='cc')
    r2 = Rule(pattern='c{2}', replacement='dd')

    r = Richtypo()
    r.rules = [r1, r2]
    r.text = 'abb'
    r.apply_rule_chain()

    assert r.text == 'add'
Ejemplo n.º 10
0
def test_loading_specified_ruledef(get_ruleset):
    """
    Check for loading specified ruledef

    ruledefs are specified when constructing a ruleset, like this:
        ruledef: rulename

    """
    get_ruleset.return_value = ['ru: test']

    with patch('richtypo.Richtypo.build_rule_chain'
               ):  # dont actualy build rule chain
        with patch('richtypo.Richtypo._load_rules_from_file') as loader:
            Richtypo()
            assert loader.call_count == 1
            loader.assert_called_with('ru')
Ejemplo n.º 11
0
def test_rule_order():
    """
    Check if rules are applyied in order they are supplied
    """
    r1 = Rule(pattern='b{2}', replacement='cc')
    r2 = Rule(pattern='c{2}', replacement='dd')
    r = Richtypo()
    r.rules = [r2, r1]  # r2 works only after r1
    r.text = 'abb'
    r.apply_rule_chain()

    assert r.text == 'acc'  # so the text should not be changed
Ejemplo n.º 12
0
def test_strip_tags_when_input_already_contains_richtypo_marks(input, expected):
    r = Richtypo()
    r.text = input
    r.strip_tags()
    r.restore_tags()
    assert r.text == expected
Ejemplo n.º 13
0
def test_get_rule_from_predefined_rules():
    r = Richtypo()
    rule = r._get_rule(ABRule)

    assert rule == ABRule
Ejemplo n.º 14
0
def test_get_rule_from_available():
    r = Richtypo()
    r.available_rules = {'b': Rule(pattern='b', replacement='d')}
    rule = r._get_rule('b')

    assert rule.pattern == 'b'
Ejemplo n.º 15
0
def test_rule_loading_by_default():
    r = Richtypo()
    assert len(r.available_rules.keys()) >= 1
Ejemplo n.º 16
0
def test_russian_simple():
    r = Richtypo(ruleset='ru-lite')
    text = u'Из-под топота копыт'
    assert r.richtypo(text) == u'Из-под%sтопота копыт' % NBSP