babel-code-frame: Add tests
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
"chalk": "^1.1.0",
|
||||
"esutils": "^2.0.2",
|
||||
"js-tokens": "^1.0.1",
|
||||
"line-numbers": "^0.2.0",
|
||||
"repeating": "^1.1.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
/* eslint indent: 0 */
|
||||
/* eslint max-len: 0 */
|
||||
|
||||
//import lineNumbers from "line-numbers";
|
||||
import lineNumbers from "line-numbers";
|
||||
import repeating from "repeating";
|
||||
import jsTokens from "js-tokens";
|
||||
import esutils from "esutils";
|
||||
import chalk from "chalk";
|
||||
|
||||
function lineNumbers(lines) {
|
||||
return lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chalk styles for token types.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,106 @@
|
||||
var buildCodeFrame = require("..");
|
||||
var assert = require("assert");
|
||||
var chalk = require("chalk");
|
||||
var codeFrame = require("..");
|
||||
|
||||
suite("babel-code-frame", function () {
|
||||
test("basic usage", function () {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor()",
|
||||
"};",
|
||||
].join('\n');
|
||||
assert.equal(codeFrame(rawLines, 2, 16), [
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" | ^",
|
||||
" 3 | };",
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
test("optional column number", function () {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor()",
|
||||
"};",
|
||||
].join('\n');
|
||||
assert.equal(codeFrame(rawLines, 2, null), [
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" 3 | };",
|
||||
].join("\n"));
|
||||
});
|
||||
|
||||
test("optional column number", function () {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor()",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 2, null), [
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" 3 | };",
|
||||
].join("\n"));
|
||||
});
|
||||
|
||||
test("maximum context lines and padding", function () {
|
||||
const rawLines = [
|
||||
"/**",
|
||||
" * Sums two numbers.",
|
||||
" *",
|
||||
" * @param a Number",
|
||||
" * @param b Number",
|
||||
" * @returns Number",
|
||||
" */",
|
||||
"",
|
||||
"function sum(a, b) {",
|
||||
" return a + b",
|
||||
"}"
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 7, 2), [
|
||||
" 5 | * @param b Number",
|
||||
" 6 | * @returns Number",
|
||||
"> 7 | */",
|
||||
" | ^",
|
||||
" 8 | ",
|
||||
" 9 | function sum(a, b) {",
|
||||
" 10 | return a + b",
|
||||
].join("\n"));
|
||||
});
|
||||
|
||||
test("no unnecessary padding due to one-off errors", function () {
|
||||
const rawLines = [
|
||||
"/**",
|
||||
" * Sums two numbers.",
|
||||
" *",
|
||||
" * @param a Number",
|
||||
" * @param b Number",
|
||||
" * @returns Number",
|
||||
" */",
|
||||
"",
|
||||
"function sum(a, b) {",
|
||||
" return a + b",
|
||||
"}"
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 6, 2), [
|
||||
" 4 | * @param a Number",
|
||||
" 5 | * @param b Number",
|
||||
"> 6 | * @returns Number",
|
||||
" | ^",
|
||||
" 7 | */",
|
||||
" 8 | ",
|
||||
" 9 | function sum(a, b) {",
|
||||
].join("\n"));
|
||||
});
|
||||
|
||||
test("opts.highlightCode", function () {
|
||||
const rawLines = "console.log('babel')";
|
||||
const result = codeFrame(rawLines, 1, 9, {highlightCode: true})
|
||||
const stripped = chalk.stripColor(result);
|
||||
assert.ok(result.length > stripped.length);
|
||||
assert.equal(stripped, [
|
||||
"> 1 | console.log('babel')",
|
||||
" | ^",
|
||||
].join("\n"))
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user