File: rainbow-test.js

package info (click to toggle)
rainbow.js 2.1.7%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 1,148 kB
  • sloc: javascript: 4,771; makefile: 13
file content (130 lines) | stat: -rw-r--r-- 3,883 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* global Rainbow */

/////////////////////////
// Helpers and globals //
/////////////////////////

const expect = chai.expect;

const genericPatterns = [{
    name: 'test',
    pattern: /test/gi
}];

const patternA = [{
    name: 'a',
    pattern: /here/gi
}];

const patternB = [{
    name: 'b',
    pattern: /is/gi
}];

const patternDollar = [{
    name: 'dollar',
    pattern: /'\$'/g
}];

////////////////
// Test suite //
////////////////

describe('Rainbow', () => {
    it('Basic things are defined', () => {
        expect(Rainbow).to.exist;
        expect(Rainbow.color).to.be.a('function');
        expect(Rainbow.extend).to.be.a('function');
        expect(Rainbow.onHighlight).to.be.a('function');
        expect(Rainbow.addAlias).to.be.a('function');
    });

    it('Should apply global class', (done) => {
        Rainbow.extend('generic', [{
            name: 'name',
            pattern: /Craig/gm
        }]);

        Rainbow.color('My name is Craig', { language: 'generic', globalClass: 'global' }, (result) => {
            expect(result).to.equal('My name is <span class="name global">Craig</span>');
            done();
        });
    });

    it('Should properly use patterns', (done) => {
        Rainbow.extend('generic', genericPatterns);

        Rainbow.color('here is a test', 'generic', (result) => {
            expect(result).to.equal('here is a <span class="test">test</span>');
            done();
        });
    });

    it('Should properly extend generic patterns', (done) => {
        Rainbow.extend('newLanguage', patternA, 'generic');

        Rainbow.color('here is a test', 'newLanguage', (result) => {
            expect(result).to.equal('<span class="a">here</span> is a <span class="test">test</span>');
            done();
        });
    });

    it('Should properly extend other patterns that extend generic patterns', (done) => {
        Rainbow.extend('newLanguage', patternB);

        Rainbow.color('here is a test', 'newLanguage', (result) => {
            expect(result).to.equal('<span class="a">here</span> <span class="b">is</span> a <span class="test">test</span>');
            done();
        });
    });

    it('Should properly apply aliases', (done) => {
        Rainbow.addAlias('new', 'newLanguage');

        Rainbow.color('here is a test', 'new', (result) => {
            expect(result).to.equal('<span class="a">here</span> <span class="b">is</span> a <span class="test">test</span>');
            done();
        });
    });

    it('Should properly remove language', (done) => {
        Rainbow.extend('foo', genericPatterns);

        Rainbow.color('just a test', 'foo', (result) => {
            expect(result).to.equal('just a <span class="test">test</span>');

            Rainbow.remove('foo');

            Rainbow.color('just a test', 'foo', (result2) => {
                expect(result2).to.equal('just a test');
                done();
            });
        });
    });

    // Not sure why anyone would want this behavior, but since we are faking
    // global regex matches we should make sure this works too.
    it('Should work with non global regex matches', (done) => {
        Rainbow.remove('foo');
        Rainbow.extend('foo', [
            {
                name: 'number',
                pattern: /\b\d+\b/
            }
        ]);

        Rainbow.color('123 456 789', 'foo', (result) => {
            expect(result).to.equal('<span class="number">123</span> 456 789');
            done();
        });
    });

    it('Should support dollar signs in replacements', (done) => {
        Rainbow.extend('dollarLanguage', patternDollar);

        Rainbow.color('here is a test with a \'$\' sign in it', 'dollarLanguage', (result) => {
            expect(result).to.equal('here is a test with a <span class="dollar">\'$\'</span> sign in it');
            done();
        });
    });
});