Add First Test

This commit is contained in:
Josh Johnson 2018-01-30 16:45:10 -05:00
parent e1d41f6025
commit b9b6aec528
6 changed files with 88 additions and 29 deletions

View File

@ -87,7 +87,10 @@ export class V2XmlFormatter implements XmlFormatter {
// entering StartTag.Attribute
else if (location === Location.StartTag && [" ", "/", ">"].indexOf(cc) === -1) {
if (lastNonTextLocation === Location.AttributeValue && ((options.splitXmlnsOnFormat && xml.substr(i, 5).toLowerCase() === "xmlns") || options.splitAttributesOnFormat)) {
if (lastNonTextLocation === Location.AttributeValue
&& ((options.splitXmlnsOnFormat
&& xml.substr(i, 5).toLowerCase() === "xmlns")
|| options.splitAttributesOnFormat)) {
output += `${options.newLine}${this._getIndent(options, indentLevel)}`;
}
@ -118,6 +121,21 @@ export class V2XmlFormatter implements XmlFormatter {
output += `>${options.newLine}`;
}
// if this is an open tag followed by a line break, add an indent before the text (after the line break)
// TODO: there could be multiple lines of text here, so we'll need a less naive implementation at some point
else if (nc === "\r" || nc === "\n") {
output += `>${options.newLine}${this._getIndent(options, indentLevel)}`;
// fast-forward based on what type of line break was used
if (nc === "\r") {
i += 2;
}
else {
i++;
}
}
else {
output += ">";
}
@ -131,11 +149,16 @@ export class V2XmlFormatter implements XmlFormatter {
indentLevel--;
// if the end tag immediately follows another end tag, add a line break and indent
// if the end tag immediately follows a line break, just add an indentation
// otherwise, this should be treated as a same-line end tag(ex. <element>text</element>)
if (lastNonTextLocation === Location.EndTag) {
output += `${options.newLine}${this._getIndent(options, indentLevel)}<`;
}
else if (pc === "\n") {
output += `${this._getIndent(options, indentLevel)}<`;
}
else {
output += "<";
}

View File

@ -1,17 +1,42 @@
//
// Note: This example test is leveraging the Mocha test framework.
// Please refer to their documentation on https://mochajs.org/ for help.
//
// The module 'assert' provides assertion methods from node
import * as assert from "assert";
import { FormattingOptions } from "vscode";
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from "vscode";
import * as myExtension from "../extension";
import { TestDataLoader } from "./test-utils/test-data-loader";
import { XmlFormatter } from "../formatting/xml-formatter";
import { XmlFormattingOptions } from "../formatting/xml-formatting-options";
import { V2XmlFormatter } from "../formatting/formatters/v2-xml-formatter";
describe("V2XmlFormatter", () => {
const xmlFormatter = new V2XmlFormatter();
describe("#formatXml(xml, options)", () => {
const options = {
editorOptions: {
insertSpaces: true,
tabSize: 4
},
newLine: "\r\n",
removeCommentsOnMinify: false,
splitAttributesOnFormat: false,
splitXmlnsOnFormat: true
};
it("should handle basic XML", () => {
testFormatter(xmlFormatter, options, "basic");
});
});
// Defines a Mocha test suite to group tests of similar kind together
suite("Extension Tests", () => {
// TODO: implement tests
});
function testFormatter(xmlFormatter: XmlFormatter, options: XmlFormattingOptions, fileLabel: string): void {
const expectedFormattedXml = TestDataLoader.load(`${fileLabel}.formatted.xml`);
const unformattedXml = TestDataLoader.load(`${fileLabel}.unformatted.xml`);
const actualFormattedXml = xmlFormatter.formatXml(unformattedXml, options);
assert.equal(actualFormattedXml, expectedFormattedXml, "Actual formatted XML does not match expected formatted XML.");
}

View File

@ -15,7 +15,7 @@ import * as testRunner from "vscode/lib/testrunner";
// You can directly control Mocha options by uncommenting the following lines
// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
testRunner.configure({
ui: "tdd", // the TDD UI is being used in extension.test.ts (suite, test, etc.)
ui: "bdd",
useColors: true // colored output from test results
});

View File

@ -0,0 +1,3 @@
<root>
<element>text</element>
</root>

View File

@ -0,0 +1 @@
<root><element>text</element></root>

View File

@ -0,0 +1,7 @@
import * as fs from "fs";
export class TestDataLoader {
static load(fileName: string): string {
return fs.readFileSync(`${__dirname}/../../../src/test/test-data/${fileName}`, "UTF-8");
}
}