This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.js
104 lines (89 loc) · 3.54 KB
/
test.js
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
const test = require('tape')
, pygments = require('./')
, fs = require('fs')
, path = require('path')
, streamEqual = require('stream-equal')
function createTests (test, pygments) {
function simpleStringConversionTest (python) {
return function (t) {
var cases = [
{
lang: 'js'
, format: 'html'
, input: 'var a = "b";'
, output: '<div class="highlight"><pre><span class="kd">var</span> <span class="nx">a</span> '
+ '<span class="o">=</span> <span class="s2">"b"</span><span class="p">;</span></pre></div>'
}
, {
lang: 'c++'
, format: 'console'
, input: 'bool a = true;'
, output: '\u001b[36mbool\u001b[39;49;00m \u001b[39;49;00ma\u001b[39;49;00m '
+ '\u001b[39;49;00m=\u001b[39;49;00m \u001b[39;49;00m\u001b[36mtrue\u001b[39;49;00m;\u001b[39;49;00m'
}
, {
lang: 'php'
, format: 'html'
, input: 'var a = true;'
, output: '<div class="highlight"><pre><span class="x">var a = true;</span></pre></div>'
}
, {
lang: 'php'
, format: 'html'
, options: { startinline: 1 }
, input: 'var a = true;'
, output: '<div class="highlight"><pre><span class="k">var</span> <span class="nx">a</span> '
+ '<span class="o">=</span> <span class="k">true</span><span class="p">;</span></pre></div>'
}
]
t.plan(cases.length * 3)
cases.forEach(function (c) {
pygments(
{
lang : c.lang
, format : c.format
, options : c.options || {}
, python : python
}
, c.input
, function (err, result) {
t.equal(err, null)
t.ok(Buffer.isBuffer(result), 'isBuffer')
result = result.toString().replace(/\n/g, '')
t.equal(result, c.output)
}
)
})
}
}
function fileConversionTest (python) {
return function (t) {
t.plan(2)
var fileIn = fs.createReadStream(__dirname + '/test-fixtures/active_model.rb')
, fileOut = fs.createWriteStream(
__dirname + '/test-fixtures/active_model.tmp'
, { flags: 'w+', encoding: null, mode: 0666 }
)
fileIn.pipe(pygments({ lang: 'rb', format: 'html', python: python })).pipe(fileOut)
fileOut.on('close', function() {
var expectedResult = fs.createReadStream(path.join(__dirname, '/test-fixtures/active_model.html'))
, result = fs.createReadStream(path.join(__dirname, '/test-fixtures/active_model.tmp'))
streamEqual(expectedResult, result, function (err, equal) {
t.notOk(err, 'no error')
t.ok(equal, 'stream equal')
})
})
}
}
test('simple string conversions', simpleStringConversionTest())
test('file conversions', fileConversionTest())
if (fs.existsSync('/usr/bin/python3')) {
test('simple string conversions (python3)', simpleStringConversionTest('/usr/bin/python3'))
test('file conversions (python3)', fileConversionTest('/usr/bin/python3'))
} else {
console.error('NO /usr/bin/python3, not testing Python3')
}
}
module.exports = createTests
if (require.main === module)
createTests(test, pygments)