File: test_spellchecking.py

package info (click to toggle)
rpmlint 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,308 kB
  • sloc: python: 20,622; ansic: 2,511; xml: 1,272; makefile: 17; sh: 4
file content (87 lines) | stat: -rw-r--r-- 3,185 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import pytest
import rpmlint.spellcheck

from Testing import HAS_CZECH_DICTIONARY, HAS_ENGLISH_DICTIONARY


def get_suggestions(suggestion):
    suggestion = suggestion.split(' -> ')[-1]
    return sorted(suggestion.split(', '))


@pytest.mark.skipif(not rpmlint.spellcheck.ENCHANT, reason='Missing enchant bindings')
@pytest.mark.skipif(not HAS_ENGLISH_DICTIONARY, reason='Missing English dictionary')
def test_spelldict(capsys):
    """
    Check we can init dictionary spellchecker
    """
    spell = rpmlint.spellcheck.Spellcheck()
    spell._init_checker()
    out, err = capsys.readouterr()
    assert not out
    assert not err
    assert 'unable to load spellchecking dictionary' not in err

    spell._init_checker('not-existing-language')
    out, err = capsys.readouterr()
    assert not out
    assert 'unable to load spellchecking dictionary' in err

    assert 'en_US' in spell._enchant_checkers
    assert spell._enchant_checkers['en_US'] is not None
    assert 'not-existing-language' not in spell._enchant_checkers


@pytest.mark.skipif(not rpmlint.spellcheck.ENCHANT, reason='Missing enchant bindings')
@pytest.mark.skipif(not HAS_ENGLISH_DICTIONARY, reason='Missing English dictionary')
@pytest.mark.skipif(not HAS_CZECH_DICTIONARY, reason='Missing Czech dictionary')
def test_spellchecking():
    """
    Check if we can test the spelling
    """
    spell = rpmlint.spellcheck.Spellcheck()

    # correct text
    text = 'I swear this text is proper English'
    result = spell.spell_check(text, 'Description({}):')
    assert not result

    # english 2 typos
    text = "I don't think tihs tetx is correct English"
    result = spell.spell_check(text, 'Description({}):')
    assert len(result) == 2
    assert result['tihs'].startswith('Description(en_US): tihs -> ')
    assert 'this' in get_suggestions(result['tihs'])

    # different language, one typo
    text = 'Příčerně žluťoučký kůň'
    result = spell.spell_check(text, 'Summary({}):', 'cs_CZ')
    assert len(result) == 1
    assert result['Příčerně'].startswith('Summary(cs_CZ): Příčerně -> ')
    assert get_suggestions(result['Příčerně']) == ['Příčeně', 'Příčetně', 'Příšerně']

    # non-existing language, should return nothing:
    text = 'Weird english text'
    result = spell.spell_check(text, 'Summary({}):', 'de_CZ')
    assert not result


@pytest.mark.skipif(not rpmlint.spellcheck.ENCHANT, reason='Missing enchant bindings')
@pytest.mark.skipif(not HAS_ENGLISH_DICTIONARY, reason='Missing English dictionary')
def test_pkgname_spellchecking():
    spell = rpmlint.spellcheck.Spellcheck()

    pkgname = 'python-squeqe'
    text = "This package is squeqe's framework helper"
    result = spell.spell_check(text, 'Description({}):', 'en_US', pkgname)
    assert not result


@pytest.mark.skipif(not rpmlint.spellcheck.ENCHANT, reason='Missing enchant bindings')
def test_ignorelist_spellchecking():
    spell = rpmlint.spellcheck.Spellcheck()

    ignore = ['wrod', 'žížala']
    text = 'This package should not have any typos in wrod or žíŽala'
    result = spell.spell_check(text, 'Description({}):', ignored_words=ignore)
    assert not result