test: some more fix to unit tests

This commit is contained in:
Peng Xiao 2018-12-21 17:23:40 +08:00
parent ed907a9096
commit 1fea33bd60
3 changed files with 44 additions and 162 deletions

View file

@ -24,116 +24,6 @@ export class SingleYAMLDocument extends JSONDocument {
return super.getNodeFromOffset(offset, true);
}
// getNodeFromOffsetEndInclusive(offset: number): ASTNode {
// if (!this.root) {
// return;
// }
// if (
// offset < this.root.offset ||
// offset > this.root.offset + this.root.length
// ) {
// // We somehow are completely outside the document
// // This is unexpected
// console.log('Attempting to resolve node outside of document');
// return null;
// }
// function* sliding2(nodes: ASTNode[]) {
// for (let i = 0; i < nodes.length; i ++) {
// yield [nodes[i], i === nodes.length ? null : nodes[i + 1]];
// }
// }
// const onLaterLine = (offset: number, node: ASTNode) => {
// const { line: actualLine } = getPosition(offset, this.lines);
// const { line: nodeEndLine } = getPosition(
// node.offset + node.length,
// this.lines
// );
// return actualLine > nodeEndLine;
// };
// let findNode = (nodes: ASTNode[]): ASTNode => {
// if (nodes.length === 0) {
// return null;
// }
// const gen = sliding2(nodes);
// for (let [first, second] of gen) {
// const end = second
// ? second.offset
// : first.parent.offset + first.parent.length;
// if (offset >= first.offset && offset < end) {
// const children = first.children;
// const foundChild = findNode(children);
// if (!foundChild && onLaterLine(offset, first)) {
// return this.getNodeByIndent(this.lines, offset, this.root);
// }
// return foundChild || first;
// }
// }
// return null;
// };
// return findNode(this.root.children) || this.root;
// }
// private getNodeByIndent = (
// lines: number[],
// offset: number,
// node: ASTNode
// ) => {
// const { line, column: indent } = getPosition(offset, this.lines);
// const children = node.children;
// function findNode(children: ASTNode[]) {
// if (children.length > 0) {
// }
// for (let idx = 0; idx < children.length; idx++) {
// const child = children[idx];
// const { line: childLine, column: childCol } = getPosition(
// child.offset,
// lines
// );
// if (childCol > indent) {
// return null;
// }
// const newChildren = child.children;
// const foundNode = findNode(newChildren);
// if (foundNode) {
// return foundNode;
// }
// // We have the right indentation, need to return based on line
// if (childLine == line) {
// return child;
// }
// if (childLine > line) {
// // Get previous
// idx - 1 >= 0 ? children[idx - 1] : child;
// }
// // Else continue loop to try next element
// }
// // Special case, we found the correct
// return children[children.length - 1];
// }
// return findNode(children) || node;
// };
public getNodeFromOffsetEndInclusive(offset: number): ASTNode {
const collector: ASTNode[] = [];
const findNode = (node: ASTNode): ASTNode => {
@ -167,6 +57,15 @@ export class SingleYAMLDocument extends JSONDocument {
}
return currMinNode || foundNode;
}
// Returns a node at offset which will be used as a hint for completion
//
public getCompletionNodeFromOffset(offset: number): ASTNode {
if (!this.root) {
return;
}
// TODO
}
}
export class YAMLDocument {

View file

@ -15,9 +15,28 @@ const languageService = getLanguageService(
[]
);
function traverse(
rootSymbols: DocumentSymbol[],
fn: (doc: DocumentSymbol) => void
) {
if (!rootSymbols || rootSymbols.length === 0) {
return;
}
rootSymbols.forEach(symbol => {
fn(symbol);
traverse(symbol.children, fn);
});
}
function allNodes(rootSymbols: DocumentSymbol[]): DocumentSymbol[] {
const res: DocumentSymbol[] = [];
traverse(rootSymbols, doc => res.push(doc));
return res;
}
// TODO: this suite is outdated and should be updated.
// https://github.com/Microsoft/vscode-json-languageservice/blob/master/src/test/documentSymbols.test.ts
xdescribe('Document Symbols Tests', () => {
describe('Document Symbols Tests', () => {
describe('Document Symbols Tests', function() {
function setup(content: string) {
return TextDocument.create(
@ -31,16 +50,18 @@ xdescribe('Document Symbols Tests', () => {
function parseSetup(content: string) {
const testTextDocument = setup(content);
const jsonDocument = parseYAML(testTextDocument.getText());
return languageService.findDocumentSymbols(
const rootSymbols = languageService.findDocumentSymbols(
testTextDocument,
jsonDocument
);
return allNodes(rootSymbols);
}
it('Document is empty', done => {
const content = '';
const symbols = parseSetup(content);
assert.equal(symbols, null);
assert.equal(symbols.length, 0);
done();
});
@ -82,14 +103,14 @@ xdescribe('Document Symbols Tests', () => {
it('Document Symbols with array of strings', done => {
const content = 'items:\n - test\n - test';
const symbols = parseSetup(content);
assert.equal(symbols.length, 1);
assert.equal(symbols.length, 3);
done();
});
it('Document Symbols with array', done => {
const content = 'authors:\n - name: Josh\n - email: jp';
const symbols = parseSetup(content);
assert.equal(symbols.length, 3);
assert.equal(symbols.length, 5);
done();
});
@ -97,7 +118,7 @@ xdescribe('Document Symbols Tests', () => {
const content =
'scripts:\n node1: test\n node2: test\nauthors:\n - name: Josh\n - email: jp';
const symbols = parseSetup(content);
assert.equal(symbols.length, 6);
assert.equal(symbols.length, 8);
done();
});

View file

@ -3,60 +3,22 @@ import { parse as parseYAML } from '../src/languageservice/parser/yamlParser';
describe('SingleYAMLDocument tests', () => {
function setup(content: string) {
return TextDocument.create(
'file://~/Desktop/vscode-k8s/test.yaml',
'yaml',
0,
content
);
}
describe('getNodeFromOffsetEndInclusive', () => {
const content = `a :
b:
`;
function parseSetup(offset: number) {
return (offset: number) => {
const yamlDocs = parseYAML(content);
// Should be one doc only
expect(yamlDocs.documents.length).toBe(1);
return yamlDocs.documents[0].getNodeFromOffsetEndInclusive(offset);
}
return yamlDocs.documents[0].getCompletionNodeFromOffset(offset);
};
}
it('0', () => {
const node = parseSetup(0);
expect(node.value).toBe('a');
expect(node.type).toBe('string');
});
it('1', () => {
describe('getCompletionNodeFromOffset for simple null value mapping', () => {
const content = 'a : \n ';
const parseSetup = setup(content);
it('within value of `a`', () => {
const node = parseSetup(1);
expect(node.value).toBe('a');
expect(node.type).toBe('string');
});
it('2', () => {
const node = parseSetup(2);
expect(node.type).toBe('property');
});
it('6', () => {
const node = parseSetup(6);
expect(node.value).toBe('b');
expect(node.type).toBe('string');
});
it('7', () => {
const node = parseSetup(7);
expect(node.value).toBe('b');
expect(node.type).toBe('string');
});
it('8', () => {
const node = parseSetup(8);
expect(node.value).toBe('b');
expect(node.type).toBe('string');
});
});
});