mirror of
https://github.com/danbulant/monaco-yaml
synced 2026-07-08 12:30:39 +00:00
test: some more fix to unit tests
This commit is contained in:
parent
ed907a9096
commit
1fea33bd60
3 changed files with 44 additions and 162 deletions
|
|
@ -24,116 +24,6 @@ export class SingleYAMLDocument extends JSONDocument {
|
||||||
return super.getNodeFromOffset(offset, true);
|
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 {
|
public getNodeFromOffsetEndInclusive(offset: number): ASTNode {
|
||||||
const collector: ASTNode[] = [];
|
const collector: ASTNode[] = [];
|
||||||
const findNode = (node: ASTNode): ASTNode => {
|
const findNode = (node: ASTNode): ASTNode => {
|
||||||
|
|
@ -167,6 +57,15 @@ export class SingleYAMLDocument extends JSONDocument {
|
||||||
}
|
}
|
||||||
return currMinNode || foundNode;
|
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 {
|
export class YAMLDocument {
|
||||||
|
|
|
||||||
|
|
@ -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.
|
// TODO: this suite is outdated and should be updated.
|
||||||
// https://github.com/Microsoft/vscode-json-languageservice/blob/master/src/test/documentSymbols.test.ts
|
// 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() {
|
describe('Document Symbols Tests', function() {
|
||||||
function setup(content: string) {
|
function setup(content: string) {
|
||||||
return TextDocument.create(
|
return TextDocument.create(
|
||||||
|
|
@ -31,16 +50,18 @@ xdescribe('Document Symbols Tests', () => {
|
||||||
function parseSetup(content: string) {
|
function parseSetup(content: string) {
|
||||||
const testTextDocument = setup(content);
|
const testTextDocument = setup(content);
|
||||||
const jsonDocument = parseYAML(testTextDocument.getText());
|
const jsonDocument = parseYAML(testTextDocument.getText());
|
||||||
return languageService.findDocumentSymbols(
|
const rootSymbols = languageService.findDocumentSymbols(
|
||||||
testTextDocument,
|
testTextDocument,
|
||||||
jsonDocument
|
jsonDocument
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return allNodes(rootSymbols);
|
||||||
}
|
}
|
||||||
|
|
||||||
it('Document is empty', done => {
|
it('Document is empty', done => {
|
||||||
const content = '';
|
const content = '';
|
||||||
const symbols = parseSetup(content);
|
const symbols = parseSetup(content);
|
||||||
assert.equal(symbols, null);
|
assert.equal(symbols.length, 0);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -82,14 +103,14 @@ xdescribe('Document Symbols Tests', () => {
|
||||||
it('Document Symbols with array of strings', done => {
|
it('Document Symbols with array of strings', done => {
|
||||||
const content = 'items:\n - test\n - test';
|
const content = 'items:\n - test\n - test';
|
||||||
const symbols = parseSetup(content);
|
const symbols = parseSetup(content);
|
||||||
assert.equal(symbols.length, 1);
|
assert.equal(symbols.length, 3);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Document Symbols with array', done => {
|
it('Document Symbols with array', done => {
|
||||||
const content = 'authors:\n - name: Josh\n - email: jp';
|
const content = 'authors:\n - name: Josh\n - email: jp';
|
||||||
const symbols = parseSetup(content);
|
const symbols = parseSetup(content);
|
||||||
assert.equal(symbols.length, 3);
|
assert.equal(symbols.length, 5);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -97,7 +118,7 @@ xdescribe('Document Symbols Tests', () => {
|
||||||
const content =
|
const content =
|
||||||
'scripts:\n node1: test\n node2: test\nauthors:\n - name: Josh\n - email: jp';
|
'scripts:\n node1: test\n node2: test\nauthors:\n - name: Josh\n - email: jp';
|
||||||
const symbols = parseSetup(content);
|
const symbols = parseSetup(content);
|
||||||
assert.equal(symbols.length, 6);
|
assert.equal(symbols.length, 8);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,60 +3,22 @@ import { parse as parseYAML } from '../src/languageservice/parser/yamlParser';
|
||||||
|
|
||||||
describe('SingleYAMLDocument tests', () => {
|
describe('SingleYAMLDocument tests', () => {
|
||||||
function setup(content: string) {
|
function setup(content: string) {
|
||||||
return TextDocument.create(
|
return (offset: number) => {
|
||||||
'file://~/Desktop/vscode-k8s/test.yaml',
|
|
||||||
'yaml',
|
|
||||||
0,
|
|
||||||
content
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('getNodeFromOffsetEndInclusive', () => {
|
|
||||||
const content = `a :
|
|
||||||
b:
|
|
||||||
|
|
||||||
`;
|
|
||||||
function parseSetup(offset: number) {
|
|
||||||
const yamlDocs = parseYAML(content);
|
const yamlDocs = parseYAML(content);
|
||||||
|
|
||||||
// Should be one doc only
|
// Should be one doc only
|
||||||
expect(yamlDocs.documents.length).toBe(1);
|
expect(yamlDocs.documents.length).toBe(1);
|
||||||
return yamlDocs.documents[0].getNodeFromOffsetEndInclusive(offset);
|
return yamlDocs.documents[0].getCompletionNodeFromOffset(offset);
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
it('0', () => {
|
describe('getCompletionNodeFromOffset for simple null value mapping', () => {
|
||||||
const node = parseSetup(0);
|
const content = 'a : \n ';
|
||||||
expect(node.value).toBe('a');
|
const parseSetup = setup(content);
|
||||||
expect(node.type).toBe('string');
|
it('within value of `a`', () => {
|
||||||
});
|
|
||||||
|
|
||||||
it('1', () => {
|
|
||||||
const node = parseSetup(1);
|
const node = parseSetup(1);
|
||||||
expect(node.value).toBe('a');
|
expect(node.value).toBe('a');
|
||||||
expect(node.type).toBe('string');
|
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');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue