Skip to content

Commit

Permalink
Add tests + fix #14
Browse files Browse the repository at this point in the history
  • Loading branch information
HookyQR committed May 7, 2016
1 parent 5365837 commit 6072e9f
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 24 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
*.vsix
.vscodeignore
typings
.vscode-test
.vscode
.jshintrc
.jshintrc
typings
25 changes: 21 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
language: node_js
node_js:
- "4.1"
- "stable"
os:
- osx
- linux

before_install:
- if [ $TRAVIS_OS_NAME == "linux" ]; then
export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
sh -e /etc/init.d/xvfb start;
sleep 3;
fi

env:
- CODE_VERSION="0.10.1"
- CODE_VERSION="0.10.9"
- CODE_VERSION="1.0.0"

install:
- npm install

script:
- npm test --silent
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ If you wish to include the files that are included by default, set `"beautify.on
Embedded version of js-beautify is v1.6.2.

## Changes:
### 0.1.3: 08 May 2016
* Fix [Issue #14: Full file beautify doubles text on version 1.1.0](https://github.com/HookyQR/VSCodeBeautify/issues/11)
* Add tests for supported formats and nested settings.

### 0.1.2: 20 Mar 2016
* Beautify with no .jsbeautifyrc file in path tree will use workspace settings for tabs/spaces indent. [Issue #11](https://github.com/HookyQR/VSCodeBeautify/issues/11)<br>Will use the editor setting if the file being beautified is visible, or workspace/user setting if it is not visible. (Beautify of a non-visible file can be envoked when beautify on save is enabled.)

Expand Down
34 changes: 17 additions & 17 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ const dumpError = e => {

const dropComments = inText => inText.replace(/(\/\*.*\*\/)|\/\/.*(?:[\r\n]|$)/g, "");

const mergeOpts = function(opts, type) {
const mergeOpts = function(opts, kind) {
const finOpts = {};
for (let a in opts) {
if (a !== 'js' && a !== 'html' && a !== 'css') {
finOpts[a] = opts[a];
}
}
//merge in the per type settings
if (type in opts) {
for (let b in opts[type]) {
if (kind in opts) {
for (let b in opts[kind]) {
if (b === 'allowed_file_extensions') continue;
finOpts[b] = opts[type][b];
finOpts[b] = opts[kind][b];
}
}
return finOpts;
Expand Down Expand Up @@ -64,9 +64,9 @@ const getBeautifyType = function(doc, dontAsk) {
});
if (vscode.languages.match(matcher, doc)) return "js";
}
if (cfg.HTMLfiles.indexOf(type) + 1 || cfg.HTMLfiles.indexOf(type.slice(1)) + 1) return 'html';
else if (cfg.CSSfiles.indexOf(type) + 1 || cfg.CSSfiles.indexOf(type.slice(1)) + 1) return 'css';
else if (cfg.JSfiles.indexOf(type) + 1 || cfg.JSfiles.indexOf(type.slice(1)) + 1) return 'js';
if (cfg.HTMLfiles.includes(type) || cfg.HTMLfiles.includes(type.slice(1))) return 'html';
else if (cfg.CSSfiles.includes(type) || cfg.CSSfiles.includes(type.slice(1))) return 'css';
else if (cfg.JSfiles.includes(type) || cfg.JSfiles.includes(type.slice(1))) return 'js';
if (dontAsk) return;

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -178,10 +178,9 @@ function beautifyOnSave(doc) {
refType = getBeautifyType(doc, true);
if (!refType) return;
}
if (cfg.onSave === true || (
Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0
)) {
const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
if (cfg.onSave === true || (Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0)) {
let range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
range = doc.validateRange(range);
//determine a default options
let defaultOptions = optionsFromFormat(vscode.workspace.getConfiguration('editor'));
//if this document is open, use the settings from that window
Expand All @@ -208,28 +207,29 @@ function activate(context) {
const active = vscode.window.activeTextEditor;
if (!active) return;
if (!active.document) return;
const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);

beautifyDoc(active.document, range, optionsFromFormat(active.options))
let range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);

range = active.document.validateRange(range);

return beautifyDoc(active.document, range, optionsFromFormat(active.options))
.then(newText => active.edit(editor => editor.replace(range, newText)), dumpError);
}));

//VS Code won't allow the formatters to run for json, or js. The inbuild
//js-beautify runs instead
context.subscriptions.push(
vscode.languages.registerDocumentRangeFormattingEditProvider('html', {
provideDocumentRangeFormattingEdits: rangeEditByType('html')
}));
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider('css', {
provideDocumentRangeFormattingEdits: rangeEditByType('css')
}));
//VS Code won't allow the formatters to run for json, or js. The inbuild
//js-beautify runs instead
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider('javascript', {
provideDocumentRangeFormattingEdits: rangeEditByType('js')
}));
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider('json', {
provideDocumentRangeFormattingEdits: rangeEditByType('js')
}));
vscode.workspace.onDidSaveTextDocument(beautifyOnSave);

}
exports.activate = activate;
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "beautify",
"displayName": "beautify",
"description": "Beautify code in place for VS Code",
"version": "0.1.2",
"version": "0.1.3",
"publisher": "HookyQR",
"engines": {
"vscode": "^0.10.1"
Expand Down Expand Up @@ -76,6 +76,14 @@
"js-beautify": "^1.6.2",
"minimatch": "^3.0.0"
},
"devDependencies": {
"vscode": "^0.11.x",
"mocha": "^2.4.5",
"expect.js": "~0.3.1"
},
"scripts": {
"test": "node ./node_modules/vscode/bin/test"
},
"repository": {
"type": "git",
"url": "https://github.com/HookyQR/VSCodeBeautify"
Expand Down
14 changes: 14 additions & 0 deletions test/data/.jsbeautifyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"indent_with_tabs": true,
"css": {
"selector_separator_newline": true
},
"js": {
"break_chained_methods": true,
"max_preserve_newlines": 2
},
"html": {
"brace_style": "none",
"preserve_newlines": false
}
}
1 change: 1 addition & 0 deletions test/data/in.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.a, #b{border: 1px solid green}
4 changes: 4 additions & 0 deletions test/data/in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><head><title>abc</title></head><body><a href='https://github.com/HookyQR/VSCodeBeautify' data-blank="blank">beautify for VS Code</a>


</body>
1 change: 1 addition & 0 deletions test/data/in.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a=1;function b(x){return x;}b(a).toString();
5 changes: 5 additions & 0 deletions test/data/in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"test": 1, "test2": "test3",


"test4":["test5",6]
}
4 changes: 4 additions & 0 deletions test/data/out.2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.a,
#b {
border: 1px solid green
}
7 changes: 7 additions & 0 deletions test/data/out.2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>

<head>
<title>abc</title>
</head>

<body><a href='https://github.com/HookyQR/VSCodeBeautify' data-blank="blank">beautify for VS Code</a> </body>
7 changes: 7 additions & 0 deletions test/data/out.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var a = 1;

function b(x) {
return x;
}
b(a)
.toString();
6 changes: 6 additions & 0 deletions test/data/out.2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"test": 1,
"test2": "test3",

"test4": ["test5", 6]
}
4 changes: 4 additions & 0 deletions test/data/out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.a,
#b {
border: 1px solid green
}
10 changes: 10 additions & 0 deletions test/data/out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>

<head>
<title>abc</title>
</head>

<body><a href='https://github.com/HookyQR/VSCodeBeautify' data-blank="blank">beautify for VS Code</a>


</body>
6 changes: 6 additions & 0 deletions test/data/out.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var a = 1;

function b(x) {
return x;
}
b(a).toString();
7 changes: 7 additions & 0 deletions test/data/out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"test": 1,
"test2": "test3",


"test4": ["test5", 6]
}
52 changes: 52 additions & 0 deletions test/extension.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
let vscode = require('vscode'),
expect = require('expect.js'),
path = require('path'),
fs = require('fs');

let root = path.join(path.dirname(__filename), 'data');

describe('with empty .jsbeautify', () => {
beforeEach(() => fs.writeFileSync(path.join(root, '.jsbeautifyrc'), "{}"));

['.js', '.html', '.json', '.css'].forEach(extension =>
it('beautify of "' + extension + "'", () => vscode.workspace.openTextDocument(path.join(root, 'in' + extension))
.then(doc => vscode.window.showTextDocument(doc)
.then(() => vscode.commands.executeCommand('HookyQR.beautify')
.then(() => expect(doc.getText())
.to.be(fs.readFileSync(path.join(root, 'out' + extension), 'utf8')))))));

['.html', '.css'].forEach(extension =>
it('format of "' + extension + "'", () => vscode.workspace.openTextDocument(path.join(root, 'in' + extension))
.then(doc => vscode.window.showTextDocument(doc)
.then(() => vscode.commands.executeCommand('editor.action.format')
.then(() => expect(doc.getText())
.to.be(fs.readFileSync(path.join(root, 'out' + extension), 'utf8')))))));
});

describe('with nested options in .jsbeautify', () => {
beforeEach(() => fs.writeFileSync(path.join(root, '.jsbeautifyrc'),
`{
"indent_with_tabs": true,
"css": {
"selector_separator_newline": true
},
"js": {
"break_chained_methods": true,
"max_preserve_newlines": 2
},
"html": {
"brace_style": "none",
"preserve_newlines": false
}
}`
));

['.js', '.html', '.json', '.css'].forEach(extension =>
it('beautify of "' + extension + "'", () => vscode.workspace.openTextDocument(path.join(root, 'in' + extension))
.then(doc => vscode.window.showTextDocument(doc)
.then(() => vscode.commands.executeCommand('HookyQR.beautify')
.then(() => expect(doc.getText())
.to.be(fs.readFileSync(path.join(root, 'out.2' + extension), 'utf8')))))));

});
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
let runner = require('vscode/lib/testrunner');

runner.configure({
useColors: true
});

module.exports = runner;

0 comments on commit 6072e9f

Please sign in to comment.