This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+25
View File
@@ -0,0 +1,25 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, fs = require('fs')
, customTheme = require('./fixtures/custom')
, cardinal = require('..')
function inspect(obj, depth) {
console.log(require('util').inspect(obj, false, depth || 5, true));
}
test('\nhighlighting a block comment without line numbers', function (t) {
var code = fs.readFileSync(__dirname + '/fixtures/block-comment.js', 'utf8');
var highlighted = cardinal.highlight(code, { theme: customTheme });
t.equal(highlighted, '\n\u001b[90m/**\n * This is a meaningless block jsdoc for a meaningless function.\n * Joins two strings, separating them to appear on two lines.\n * \n * @name foo\n * @function\n * @param uno {String} first string\n * @param dos {String} second string\n * @return {String} result of the join\n */\u001b[39m\n\u001b[96mmodule\u001b[39m\u001b[32m.\u001b[39m\u001b[96mexports\u001b[39m \u001b[93m=\u001b[39m \u001b[94mfunction\u001b[39m \u001b[96mfoo\u001b[39m \u001b[90m(\u001b[39m\u001b[96muno\u001b[39m\u001b[32m,\u001b[39m \u001b[96mdos\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m\n \u001b[31mreturn\u001b[39m \u001b[96muno\u001b[39m \u001b[93m+\u001b[39m \u001b[92m\'\\n\'\u001b[39m \u001b[93m+\u001b[39m \u001b[96mdos\u001b[39m\u001b[90m;\u001b[39m\n\u001b[33m}\u001b[39m\n')
t.end()
})
test('\nhighlighting a block comment with line numbers', function (t) {
var code = fs.readFileSync(__dirname + '/fixtures/block-comment.js', 'utf8');
var highlighted = cardinal.highlight(code, { theme: customTheme, linenos: true });
t.equal(highlighted, '\u001b[90m 1: \n\u001b[90m 2: \u001b[90m/**\n\u001b[90m 3: * This is a meaningless block jsdoc for a meaningless function.\n\u001b[90m 4: * Joins two strings, separating them to appear on two lines.\n\u001b[90m 5: * \n\u001b[90m 6: * @name foo\n\u001b[90m 7: * @function\n\u001b[90m 8: * @param uno {String} first string\n\u001b[90m 9: * @param dos {String} second string\n\u001b[90m10: * @return {String} result of the join\n\u001b[90m11: */\u001b[39m\n\u001b[90m12: \u001b[96mmodule\u001b[39m\u001b[32m.\u001b[39m\u001b[96mexports\u001b[39m \u001b[93m=\u001b[39m \u001b[94mfunction\u001b[39m \u001b[96mfoo\u001b[39m \u001b[90m(\u001b[39m\u001b[96muno\u001b[39m\u001b[32m,\u001b[39m \u001b[96mdos\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m\n\u001b[90m13: \u001b[31mreturn\u001b[39m \u001b[96muno\u001b[39m \u001b[93m+\u001b[39m \u001b[92m\'\\n\'\u001b[39m \u001b[93m+\u001b[39m \u001b[96mdos\u001b[39m\u001b[90m;\u001b[39m\n\u001b[90m14: \u001b[33m}\u001b[39m')
t.end()
})
+95
View File
@@ -0,0 +1,95 @@
/*jshint asi:true */
'use strict';
var fs = require('fs')
, path = require('path')
, utl = require('../utl')
, highlighter = require('..')
, colors = require('ansicolors')
, diffFile = path.join(__dirname, 'fixtures', 'git-diff.txt')
, diff = fs.readFileSync(diffFile, 'utf-8')
// @@ is not a valid js token, so when we see it, we can be sure that we are dealing with a git or svn diff
var diffRegex = /^@@[^@]+@@$/m;
var diffIndRegex = /^(@@[^@]+@@)(.*)$/;
var addRemRegex = /^[+\-]/;
var lines = diff.split('\n');
function isDiff(lines) {
return !!lines
.filter(function (line) {
return diffRegex.test(line);
})
.length;
}
var diff = isDiff(lines);
function tryHighlight(code) {
// TODO: need to remove symbols added to get valid code
// this should be done by getting the splits instead of the actual code from the highlighter
// now we can remove first / last one after highlighting completed
function tryAppending(appended, tryNext) {
try {
return highlighter.highlight(code + appended);
} catch (e) {
return tryNext(code);
}
}
function tryRemoveLeadingComma(tryNext) {
var success;
try {
success = highlighter.highlight(code.replace(/^( +),(.+)$/, '$1 $2'));
return success;
} catch (e) {
return tryNext(code);
}
}
function tryPlain() {
try {
return highlighter.highlight(code);
} catch (e) {
return tryCloseMustache();
}
}
function tryCloseMustache() { return tryAppending('}', tryCloseParen); }
function tryCloseParen() { return tryAppending('\\)', tryCloseMustacheParen); }
function tryCloseMustacheParen() { return tryAppending('})', tryRemovingCommas);}
function tryRemovingCommas() { return tryRemoveLeadingComma(giveUp); }
function giveUp() { return code; }
return tryPlain();
}
function highlightDiffInd(line, matches) {
var highlighted = colors.brightBlue(matches[1])
, code = matches[2];
return code ? highlighted + tryHighlight(code) : highlighted;
}
function colorsAddRemove(c) {
return addRemRegex.test(c) ? colors.yellow(c) : c;
}
function highlightDiff(line) {
var diffIndMatches = diffIndRegex.exec(line);
return diffIndMatches
? highlightDiffInd(line, diffIndMatches)
: colorsAddRemove(line[0]) + tryHighlight(line.slice(1));
}
var highlightFn = diff ? highlightDiff : tryHighlight;
var highlightedLines = lines.map(highlightFn);
console.log(highlightedLines.join('\n'));
+50
View File
@@ -0,0 +1,50 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, fs = require('fs')
, path = require('path')
, customTheme = require('./fixtures/custom')
, cardinal = require('..')
function inspect (obj) {
return console.log(util.inspect(obj, false, 5, false))
}
var file = path.join(__dirname, 'fixtures/foo.js')
, fileWithErrors = path.join(__dirname, 'fixtures/foo-with-errors.js')
test('supplying custom theme', function (t) {
cardinal.highlightFile(file, { theme: customTheme }, function (err, highlighted) {
t.equals(null, err, 'no error')
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[96mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[96ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[96ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[31mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\n')
t.end()
})
})
test('not supplying custom theme', function (t) {
cardinal.highlightFile(file, function (err, highlighted) {
t.equals(null, err, 'no error')
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[37mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\n')
t.end()
})
})
test('syntactically invalid code', function (t) {
cardinal.highlightFile(fileWithErrors, function (err, highlighted) {
t.equals(null, err, 'no error')
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\u001b[90m;\u001b[39m\n')
t.end()
})
})
test('non existing file', function (t) {
cardinal.highlightFile('./not/existing', function (err, highlighted) {
t.similar(err.message, /ENOENT. .*not.existing/)
t.end()
})
})
+45
View File
@@ -0,0 +1,45 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, fs = require('fs')
, path = require('path')
, customTheme = require('./fixtures/custom')
, cardinal = require('..')
function inspect (obj) {
return console.log(util.inspect(obj, false, 5, false))
}
var file = path.join(__dirname, 'fixtures/foo.js')
, fileWithErrors = path.join(__dirname, 'fixtures/foo-with-errors.js')
test('supplying custom theme', function (t) {
var highlighted = cardinal.highlightFileSync(file, { theme: customTheme });
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[96mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[96ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[96ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[31mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\n')
t.end()
})
test('not supplying custom theme', function (t) {
var highlighted = cardinal.highlightFileSync(file);
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[37mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\n')
t.end()
})
test('syntactically invalid code', function (t) {
var highlighted = cardinal.highlightFileSync(fileWithErrors);
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\u001b[90m;\u001b[39m\n')
t.end()
})
test('non existing file', function (t) {
try {
cardinal.highlightFileSync('./not/existing');
} catch (e) {
t.similar(e.message, /ENOENT. .*not.existing/)
t.end()
}
})
+3
View File
@@ -0,0 +1,3 @@
// Test Cases
// highlightDiff('@@ -25,22 +31,47 @@ function resolveTheme (config) { }')
+22
View File
@@ -0,0 +1,22 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, path = require('path')
, cardinal = require('..')
function inspect (obj) {
return console.log(util.inspect(obj, false, 5, false))
}
var file = path.join(__dirname, 'fixtures/json.json');
test('without custom theme', function (t) {
cardinal.highlightFile(file, function (err, highlighted) {
t.equals(null, err, 'no error');
t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[32m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m');
t.end();
});
});
+20
View File
@@ -0,0 +1,20 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, path = require('path')
, cardinal = require('..')
function inspect (obj) {
return console.log(util.inspect(obj, false, 5, false))
}
var file = path.join(__dirname, 'fixtures/json.json');
test('without custom theme', function (t) {
var highlighted = cardinal.highlightFileSync(file);
t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[32m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m');
t.end();
});
+39
View File
@@ -0,0 +1,39 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, customTheme = require('./fixtures/custom')
, cardinal = require('..')
function inspect (obj) {
return console.log(util.inspect(obj, false, 5, false))
}
var json = JSON.stringify({
foo: 'bar',
baz: 'quux',
bam: null
});
test('supplying custom theme', function (t) {
var highlighted = cardinal.highlight(json, { theme: customTheme });
t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[92m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[92m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[92m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m')
t.end();
});
test('not supplying custom theme', function (t) {
var highlighted = cardinal.highlight(json);
t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[32m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m')
t.end();
});
test('with the obsoleted json option (ignored)', function (t) {
var highlighted = cardinal.highlight(json, { json: true });
t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[32m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m')
t.end();
});
+68
View File
@@ -0,0 +1,68 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, fs = require('fs')
, customTheme = require('./fixtures/custom')
, cardinal = require('..')
function inspect (obj) {
return console.log(util.inspect(obj, false, 5, false))
}
var code = 'function foo() { var a = 3; return a > 2 ? true : false; }'
, codeWithErrors = 'function () { var a = 3; return a > 2 ? true : false; }';
test('supplying custom theme', function (t) {
var highlighted = cardinal.highlight(code, { theme: customTheme });
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[96mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \u001b[32mvar\u001b[39m \u001b[96ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[96ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[31mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \u001b[33m}\u001b[39m')
t.end()
})
test('not supplying custom theme', function (t) {
var highlighted = cardinal.highlight(code);
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[37mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \u001b[33m}\u001b[39m')
t.end()
})
test('syntactically invalid code', function (t) {
var highlighted = cardinal.highlight(codeWithErrors);
t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \u001b[33m}\u001b[39m')
t.end()
})
test('line numbers no firstline given', function (t) {
var highlighted = cardinal.highlight(code, { linenos: true });
t.equals(highlighted, '\u001b[90m1: \u001b[94mfunction\u001b[39m \u001b[37mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \u001b[33m}\u001b[39m')
t.end()
})
test('line numbers firstline 99', function (t) {
var highlighted = cardinal.highlight(code, { linenos: true, firstline: 99 });
t.equals(highlighted, '\u001b[90m99: \u001b[94mfunction\u001b[39m \u001b[37mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \u001b[33m}\u001b[39m')
t.end()
})
test('line numbers multi line no first line given', function (t) {
var multilineCode = '' +
function foo () {
return 1;
};
var highlighted = cardinal.highlight(multilineCode, { linenos: true });
t.equals(highlighted,'\u001b[90m1: \u001b[94mfunction\u001b[39m \u001b[37mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m\n\u001b[90m2: \u001b[31mreturn\u001b[39m \u001b[34m1\u001b[39m\u001b[90m;\u001b[39m\n\u001b[90m3: \u001b[33m}\u001b[39m')
t.end()
})
test('line numbers multi line first line 99', function (t) {
var multilineCode = '' +
function foo () {
return 1;
};
var highlighted = cardinal.highlight(multilineCode, { linenos: true, firstline: 99 });
t.equals(highlighted,'\u001b[90m 99: \u001b[94mfunction\u001b[39m \u001b[37mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m\n\u001b[90m100: \u001b[31mreturn\u001b[39m \u001b[34m1\u001b[39m\u001b[90m;\u001b[39m\n\u001b[90m101: \u001b[33m}\u001b[39m')
t.end()
})
+44
View File
@@ -0,0 +1,44 @@
'use strict';
/*jshint asi: true*/
// applying esprima to a bunch of files of contained libraries as a smoke test
var test = require('tap').test
, path = require('path')
, fs = require('fs')
, readdirp = require('readdirp')
, cardinal = require('..')
, node_modules = path.join(__dirname, '..', 'node_modules')
, tapdir = path.join(node_modules, 'tap')
, redeyeddir = path.join(node_modules, 'redeyed')
test('tap', function (t) {
var invalidTapFiles = [
'async-map-ordered.js'
, 'prof.js'
]
readdirp({ root: tapdir, fileFilter: '*.js' })
.on('data', function (entry) {
if (~invalidTapFiles.indexOf(entry.name)) return
var code = fs.readFileSync(entry.fullPath, 'utf-8')
, result = cardinal.highlight(code);
t.assert(~result.indexOf('[32mvar\u001b[39m') || !(~result.indexOf('var ')), 'highlighted ' + entry.path)
})
.on('end', t.end.bind(t))
})
test('redeyed', function (t) {
readdirp({ root: redeyeddir, fileFilter: 'redeyed.js' })
.on('data', function (entry) {
var code = fs.readFileSync(entry.fullPath, 'utf-8')
, result = cardinal.highlight(code);
t.assert(~result.indexOf('[32mvar\u001b[39m') || !(~result.indexOf('var ')), 'highlighted ' + entry.path)
})
.on('end', t.end.bind(t))
})
+14
View File
@@ -0,0 +1,14 @@
/**
* This is a meaningless block jsdoc for a meaningless function.
* Joins two strings, separating them to appear on two lines.
*
* @name foo
* @function
* @param uno {String} first string
* @param dos {String} second string
* @return {String} result of the join
*/
module.exports = function foo (uno, dos) {
return uno + '\n' + dos;
}
+144
View File
@@ -0,0 +1,144 @@
var colors = require('ansicolors');
// Change the below definitions in order to tweak the color theme.
module.exports = {
'Boolean': {
// changed from default
'true' : colors.red
, 'false' : undefined
, _default : colors.brightRed
}
, 'Identifier': {
'undefined' : colors.brightBlack
, 'self' : colors.brightRed
, 'console' : colors.blue
, 'log' : colors.blue
, 'warn' : colors.red
, 'error' : colors.brightRed
//
// changed from default
, _default : colors.brightCyan
}
, 'Null': {
_default: colors.brightBlack
}
, 'Numeric': {
_default: colors.blue
}
, 'String': {
_default: colors.brightGreen
}
, 'Keyword': {
'break' : undefined
, 'case' : undefined
, 'catch' : colors.cyan
, 'continue' : undefined
, 'debugger' : undefined
, 'default' : undefined
, 'delete' : colors.red
, 'do' : undefined
, 'else' : undefined
, 'finally' : colors.cyan
, 'for' : undefined
, 'function' : undefined
, 'if' : undefined
, 'in' : undefined
, 'instanceof' : undefined
, 'new' : colors.red
, 'return' : colors.red
, 'switch' : undefined
, 'this' : colors.brightRed
, 'throw' : undefined
, 'try' : colors.cyan
, 'typeof' : undefined
, 'var' : colors.green
, 'void' : undefined
, 'while' : undefined
, 'with' : undefined
, _default : colors.brightBlue
}
, 'Punctuator': {
';': colors.brightBlack
, '.': colors.green
, ',': colors.green
, '{': colors.yellow
, '}': colors.yellow
, '(': colors.brightBlack
, ')': colors.brightBlack
, '[': colors.yellow
, ']': colors.yellow
, '<': undefined
, '>': undefined
, '+': undefined
, '-': undefined
, '*': undefined
, '%': undefined
, '&': undefined
, '|': undefined
, '^': undefined
, '!': undefined
, '~': undefined
, '?': undefined
, ':': undefined
, '=': undefined
, '<=': undefined
, '>=': undefined
, '==': undefined
, '!=': undefined
, '++': undefined
, '--': undefined
, '<<': undefined
, '>>': undefined
, '&&': undefined
, '||': undefined
, '+=': undefined
, '-=': undefined
, '*=': undefined
, '%=': undefined
, '&=': undefined
, '|=': undefined
, '^=': undefined
, '/=': undefined
, '===': undefined
, '!==': undefined
, '>>>': undefined
, '<<=': undefined
, '>>=': undefined
, '>>>=': undefined
, _default: colors.brightYellow
}
// line comment
, Line: {
_default: colors.brightBlack
}
/* block comment */
, Block: {
_default: colors.brightBlack
}
, _default: undefined
};
+3
View File
@@ -0,0 +1,3 @@
function () {
var a = 3; return a > 2 ? true : false;
};
+3
View File
@@ -0,0 +1,3 @@
function foo() {
var a = 3; return a > 2 ? true : false;
}
+78
View File
@@ -0,0 +1,78 @@
diff --git a/test/settings.js b/test/settings.js
index 7b28d66..642688f 100644
--- a/test/settings.js
+++ b/test/settings.js
@@ -1,14 +1,20 @@
'use strict';
/*jshint asi: true*/
-var test = require('tap').test
- , path = require('path')
- , fs = require('fs')
- , settings = require('../settings')
- , existsSync = fs.existsSync || path.existsSync
+var test = require('tap').test
+ , path = require('path')
+ , fs = require('fs')
, hideSemicolonsTheme = require('../themes/hide-semicolons')
, home = path.join(__dirname, 'fixtures', 'home')
, rcpath = path.join(home, '.cardinalrc')
+ , existsSync = fs.existsSync || path.existsSync
+ , settingsResolve = require.resolve('../settings')
+ , settings
+
+function setup () {
+ delete require.cache[settingsResolve]
+ settings = require(settingsResolve)
+}
function writerc(config) {
fs.writeFileSync(rcpath, JSON.stringify(config), 'utf-8')
@@ -25,22 +31,47 @@ function resolveTheme (config) {
return result;
}
+function getSettings (config) {
+ writerc(config)
+ var result = settings.getSettings(home)
+ removerc()
+ return result;
+}
+
if (!existsSync(home)) fs.mkdirSync(home);
test('no .cardinalrc in home', function (t) {
+ setup()
var theme = settings.resolveTheme(home)
t.equals(theme, undefined, 'resolves no theme')
t.end()
})
test('.cardinalrc with theme "hide-semicolons" in home', function (t) {
+ setup()
var theme = resolveTheme({ theme: "hide-semicolons" })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end()
})
test('.cardinalrc with full path to "hide-semicolons.js" in home', function (t) {
+ setup()
var theme = resolveTheme({ theme: path.join(__dirname, '..', 'themes', 'hide-semicolons.js') })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end()
})
+
+test('.cardinalrc with linenos: true', function (t) {
+ setup()
+ var opts = { linenos: true }
+ t.deepEquals(getSettings(opts), opts)
+ t.end()
+})
+
+test('.cardinalrc with linenos: true and theme', function (t) {
+ setup()
+ var opts = { linenos: true, theme: 'some theme' }
+ t.deepEquals(getSettings(opts), opts)
+ t.end()
+})
+
+1
View File
@@ -0,0 +1 @@
{"foo":"bar","baz":"quux","bam":null}
+23
View File
@@ -0,0 +1,23 @@
Index: grunt.js
===================================================================
--- grunt.js (revision 31200)
+++ grunt.js (working copy)
@@ -12,6 +12,7 @@
module.exports = function (grunt) {
+ console.log('hello world');
// Project configuration.
grunt.initConfig({
lint: {
@@ -19,10 +20,6 @@
'packages/services.web/{!(test)/**/,}*.js',
'packages/error/**/*.js'
],
- scripts: [
- 'grunt.js',
- 'db/**/*.js'
- ],
browser: [
'packages/web/server.js',
'packages/web/server/**/*.js',
+77
View File
@@ -0,0 +1,77 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, path = require('path')
, fs = require('fs')
, hideSemicolonsTheme = require('../themes/hide-semicolons')
, home = path.join(__dirname, 'fixtures', 'home')
, rcpath = path.join(home, '.cardinalrc')
, existsSync = fs.existsSync || path.existsSync
, settingsResolve = require.resolve('../settings')
, settings
function setup () {
delete require.cache[settingsResolve]
settings = require(settingsResolve)
}
function writerc(config) {
fs.writeFileSync(rcpath, JSON.stringify(config), 'utf-8')
}
function removerc () {
fs.unlinkSync(rcpath)
}
function resolveTheme (config) {
writerc(config)
var result = settings.resolveTheme(home)
removerc()
return result;
}
function getSettings (config) {
writerc(config)
var result = settings.getSettings(home)
removerc()
return result;
}
if (!existsSync(home)) fs.mkdirSync(home);
test('no .cardinalrc in home', function (t) {
setup()
var theme = settings.resolveTheme(home)
t.equals(theme, undefined, 'resolves no theme')
t.end()
})
test('.cardinalrc with theme "hide-semicolons" in home', function (t) {
setup()
var theme = resolveTheme({ theme: "hide-semicolons" })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end()
})
test('.cardinalrc with full path to "hide-semicolons.js" in home', function (t) {
setup()
var theme = resolveTheme({ theme: path.join(__dirname, '..', 'themes', 'hide-semicolons.js') })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end()
})
test('.cardinalrc with linenos: true', function (t) {
setup()
var opts = { linenos: true }
t.deepEquals(getSettings(opts), opts)
t.end()
})
test('.cardinalrc with linenos: true and theme', function (t) {
setup()
var opts = { linenos: true, theme: 'some theme' }
t.deepEquals(getSettings(opts), opts)
t.end()
})
+22
View File
@@ -0,0 +1,22 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, path = require('path')
, fs = require('fs')
, themesdir = path.join(__dirname, '..', 'themes')
, allFiles = fs.readdirSync(themesdir)
test('validate themes by requiring all of them', function (t) {
allFiles
.filter(function (file) { return path.extname(file) === '.js'; })
.forEach(function (theme) {
try {
t.ok(require(path.join(themesdir, theme)), theme + ' is valid')
} catch (e) {
t.fail('theme: ' + theme + ' is invalid! ' + e.message)
}
})
t.end()
})