summaryrefslogtreecommitdiffstats
path: root/tests/unit/highlighter.js
blob: d582d38e3422201c26475269ee043fa661bd8747 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-

// Test cases for SearchResult description match highlighter

const JsUnit = imports.jsUnit;
const Pango = imports.gi.Pango;

const Environment = imports.ui.environment;
Environment.init();

const Util = imports.misc.util;

const tests = [
    { input: 'abc cba',
      terms: null,
      output: 'abc cba' },
    { input: 'abc cba',
      terms: [],
      output: 'abc cba' },
    { input: 'abc cba',
      terms: [''],
      output: 'abc cba' },
    { input: 'abc cba',
      terms: ['a'],
      output: '<b>a</b>bc cb<b>a</b>' },
    { input: 'abc cba',
      terms: ['a', 'a'],
      output: '<b>a</b>bc cb<b>a</b>' },
    { input: 'CaSe InSenSiTiVe',
      terms: ['cas', 'sens'],
      output: '<b>CaS</b>e In<b>SenS</b>iTiVe' },
    { input: 'This contains the < character',
      terms: null,
      output: 'This contains the &lt; character' },
    { input: 'Don\'t',
      terms: ['t'],
      output: 'Don&apos;<b>t</b>' },
    { input: 'Don\'t',
      terms: ['n\'t'],
      output: 'Do<b>n&apos;t</b>' },
    { input: 'Don\'t',
      terms: ['o', 't'],
      output: 'D<b>o</b>n&apos;<b>t</b>' },
    { input: 'salt&pepper',
      terms: ['salt'],
      output: '<b>salt</b>&amp;pepper' },
    { input: 'salt&pepper',
      terms: ['salt', 'alt'],
      output: '<b>salt</b>&amp;pepper' },
    { input: 'salt&pepper',
      terms: ['pepper'],
      output: 'salt&amp;<b>pepper</b>' },
    { input: 'salt&pepper',
      terms: ['salt', 'pepper'],
      output: '<b>salt</b>&amp;<b>pepper</b>' },
    { input: 'salt&pepper',
      terms: ['t', 'p'],
      output: 'sal<b>t</b>&amp;<b>p</b>e<b>p</b><b>p</b>er' },
    { input: 'salt&pepper',
      terms: ['t', '&', 'p'],
      output: 'sal<b>t</b><b>&amp;</b><b>p</b>e<b>p</b><b>p</b>er' },
    { input: 'salt&pepper',
      terms: ['e'],
      output: 'salt&amp;p<b>e</b>pp<b>e</b>r' },
    { input: 'salt&pepper',
      terms: ['&a', '&am', '&amp', '&amp;'],
      output: 'salt&amp;pepper' },
    { input: '&&&&&',
      terms: ['a'],
      output: '&amp;&amp;&amp;&amp;&amp;' },
    { input: '&;&;&;&;&;',
      terms: ['a'],
      output: '&amp;;&amp;;&amp;;&amp;;&amp;;' },
    { input: '&;&;&;&;&;',
      terms: [';'],
      output: '&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>' },
    { input: '&amp;',
      terms: ['a'],
      output: '&amp;<b>a</b>mp;' }
];

try {
    for (let i = 0; i < tests.length; i++) {
        let highlighter = new Util.Highlighter(tests[i].terms);
        let output = highlighter.highlight(tests[i].input);

        JsUnit.assertEquals(`Test ${i + 1} highlight ` +
            `"${tests[i].terms}" in "${tests[i].input}"`,
            output, tests[i].output);

        let parsed = false;
        try {
            Pango.parse_markup(output, -1, '');
            parsed = true;
        } catch (e) {}
        JsUnit.assertEquals(`Test ${i + 1} is valid markup`, true, parsed);
    }
} catch (e) {
    if (typeof(e.isJsUnitException) != 'undefined'
        && e.isJsUnitException)
    {
        if (e.comment)
            log(`Error in: ${e.comment}`);
    }
    throw e;
}