Skip to content

Commit

Permalink
Add tests for hover contributions. Fixes #53
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Nov 12, 2020
1 parent 489bec8 commit d8a8420
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 49 deletions.
57 changes: 41 additions & 16 deletions src/test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@
*--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import * as Parser from '../parser/jsonParser';
import * as SchemaService from '../services/jsonSchemaService';
import * as JsonSchema from '../jsonSchema';
import { JSONHover } from '../services/jsonHover';

import { Hover, Position, MarkedString, TextDocument } from '../jsonLanguageService';
import { Hover, Position, MarkedString, TextDocument, getLanguageService, JSONSchema, JSONWorkerContribution, LanguageServiceParams } from '../jsonLanguageService';

suite('JSON Hover', () => {

function testComputeInfo(value: string, schema: JsonSchema.JSONSchema, position: Position): PromiseLike<Hover> {
function testComputeInfo(value: string, schema: JSONSchema, position: Position, serviceParams?: LanguageServiceParams): PromiseLike<Hover> {
const uri = 'test://test.json';
const schemaUri = "http://myschemastore/test1";

if (!serviceParams) {
serviceParams = { schemaRequestService: requestService };
}
const service = getLanguageService(serviceParams);
service.configure({ schemas: [{ fileMatch: ["*.json"], uri: schemaUri, schema }] });

const schemaService = new SchemaService.JSONSchemaService(requestService);
const hoverProvider = new JSONHover(schemaService, [], Promise);
const id = "http://myschemastore/test1";
schemaService.registerExternalSchema(id, ["*.json"], schema);

const document = TextDocument.create(uri, 'json', 0, value);
const jsonDoc = Parser.parse(document);
return hoverProvider.doHover(document, position, jsonDoc);
const jsonDoc = service.parseJSONDocument(document);
return service.doHover(document, position, jsonDoc);
}

let requestService = function (uri: string): Promise<string> {
Expand All @@ -33,7 +32,7 @@ suite('JSON Hover', () => {
test('Simple schema', async function () {

const content = '{"a": 42, "b": "hello", "c": false}';
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
description: 'a very special object',
properties: {
Expand Down Expand Up @@ -68,7 +67,7 @@ suite('JSON Hover', () => {
test('Nested schema', async function () {

const content = '{"a": 42, "b": "hello"}';
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
oneOf: [{
type: 'object',
description: 'a very special object',
Expand Down Expand Up @@ -99,7 +98,7 @@ suite('JSON Hover', () => {
});

test('Enum description', async function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
'prop1': {
Expand Down Expand Up @@ -140,7 +139,7 @@ suite('JSON Hover', () => {
});

test('Multiline descriptions', async function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
'prop1': {
Expand All @@ -159,4 +158,30 @@ suite('JSON Hover', () => {
assert.deepEqual(result.contents, ['line1\n\nline2\r\n\r\nline3']);
});
});

test('Contribution descriptions', async function () {
const serviceParams: LanguageServiceParams = {
contributions: [{
async getInfoContribution(uri, location) {
return [`${uri}: ${location.join('/')}`];
},
collectDefaultCompletions(uri, results) {
return Promise.resolve();
},
collectPropertyCompletions(uri, location, currentWord, addValue, isLast, results) {
return Promise.resolve();
},
collectValueCompletions(uri, location, propertyKey, results) {
return Promise.resolve();
}
}]
};

let result = await testComputeInfo('{ "prop1": [ 1, false ] }', {}, { line: 0, character: 3 }, serviceParams);
assert.deepEqual(result.contents, ['test://test.json: prop1']);

result = await testComputeInfo('{ "prop1": [ 1, false ] }', {}, { line: 0, character: 13 }, serviceParams);
assert.deepEqual(result.contents, ['test://test.json: prop1/0']);

});
});
64 changes: 31 additions & 33 deletions src/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

import * as assert from 'assert';
import { getNodePath, getNodeValue, JSONDocument } from '../parser/jsonParser';
import * as JsonSchema from '../jsonSchema';
import { TextDocument, Range, ErrorCode, ASTNode, ObjectASTNode, getLanguageService } from '../jsonLanguageService';
import { DiagnosticSeverity } from '../jsonLanguageTypes';
import { TextDocument, Range, ErrorCode, ASTNode, ObjectASTNode, getLanguageService, JSONSchema} from '../jsonLanguageService';

suite('JSON Parser', () => {

Expand Down Expand Up @@ -40,7 +38,7 @@ suite('JSON Parser', () => {
return Range.create(textDoc.positionAt(offset), textDoc.positionAt(offset + length));
}

function validate(text: string, schema: JsonSchema.JSONSchema) {
function validate(text: string, schema: JSONSchema) {
const { textDoc, jsonDoc } = toDocument(text);
return jsonDoc.validate(textDoc, schema);
}
Expand Down Expand Up @@ -850,7 +848,7 @@ suite('JSON Parser', () => {
test('allOf', function () {


const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
id: 'test://schemas/main',
allOf: [
{
Expand Down Expand Up @@ -892,7 +890,7 @@ suite('JSON Parser', () => {
test('anyOf', function () {


const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
id: 'test://schemas/main',
anyOf: [
{
Expand Down Expand Up @@ -940,7 +938,7 @@ suite('JSON Parser', () => {



const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
id: 'test://schemas/main',
oneOf: [
{
Expand Down Expand Up @@ -984,7 +982,7 @@ suite('JSON Parser', () => {


test('not', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
id: 'test://schemas/main',
not: {
properties: {
Expand All @@ -1009,7 +1007,7 @@ suite('JSON Parser', () => {


test('if/then/else', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
id: 'test://schemas/main',
if: {
properties: {
Expand Down Expand Up @@ -1060,7 +1058,7 @@ suite('JSON Parser', () => {
});

test('nested if/then/else', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
id: 'test://schemas/main',
if: {
properties: {
Expand Down Expand Up @@ -1142,7 +1140,7 @@ suite('JSON Parser', () => {

const { textDoc, jsonDoc } = toDocument('{"prop1": 42, "prop2": true}');

const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
minProperties: 2
};

Expand All @@ -1164,7 +1162,7 @@ suite('JSON Parser', () => {

const { textDoc, jsonDoc } = toDocument('{"prop1": 42, "prop2": true}');

const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
maxProperties: 2
};

Expand All @@ -1183,7 +1181,7 @@ suite('JSON Parser', () => {
});

test('patternProperties', function () {
let schema: JsonSchema.JSONSchema = {
let schema: JSONSchema = {
id: 'test://schemas/main',
patternProperties: {
'^prop\\d$': {
Expand Down Expand Up @@ -1227,7 +1225,7 @@ suite('JSON Parser', () => {

test('additionalProperties', function () {

let schema: JsonSchema.JSONSchema = {
let schema: JSONSchema = {
additionalProperties: {
type: 'number'
}
Expand Down Expand Up @@ -1282,7 +1280,7 @@ suite('JSON Parser', () => {
});

test('enum', function () {
let schema: JsonSchema.JSONSchema = {
let schema: JSONSchema = {
properties: {
'prop': {
enum: ['violin', 'harmonica', 'banjo']
Expand Down Expand Up @@ -1336,7 +1334,7 @@ suite('JSON Parser', () => {
});

test('const', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
properties: {
'prop': {
const: 'violin'
Expand Down Expand Up @@ -1369,7 +1367,7 @@ suite('JSON Parser', () => {
});

test('oneOf const', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
properties: {
'prop': {
oneOf: [
Expand Down Expand Up @@ -1403,7 +1401,7 @@ suite('JSON Parser', () => {
});

test('propertyNames', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
propertyNames: {
type: 'string',
minLength: 2,
Expand All @@ -1427,7 +1425,7 @@ suite('JSON Parser', () => {

const { textDoc, jsonDoc } = toDocument('[1, 2, 3]');

const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'array',
uniqueItems: true
};
Expand All @@ -1451,7 +1449,7 @@ suite('JSON Parser', () => {

test('containsItem', function () {

const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'array',
contains: { type: "number", const: 3 }
};
Expand All @@ -1468,7 +1466,7 @@ suite('JSON Parser', () => {
});

test('items as array', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'array',
items: [
{
Expand Down Expand Up @@ -1503,7 +1501,7 @@ suite('JSON Parser', () => {
});

test('additionalItems', function () {
let schema: JsonSchema.JSONSchema = {
let schema: JSONSchema = {
type: 'array',
items: [
{
Expand Down Expand Up @@ -1568,7 +1566,7 @@ suite('JSON Parser', () => {
});

test('multipleOf', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'array',
items: {
type: 'integer',
Expand All @@ -1590,7 +1588,7 @@ suite('JSON Parser', () => {
});

test('multipleOf with floats', function () {
let schema: JsonSchema.JSONSchema = {
let schema: JSONSchema = {
type: 'array',
items: {
type: 'number',
Expand Down Expand Up @@ -1625,7 +1623,7 @@ suite('JSON Parser', () => {
});

test('dependencies with array', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
a: {
Expand Down Expand Up @@ -1657,7 +1655,7 @@ suite('JSON Parser', () => {
});

test('dependencies with schema', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
a: {
Expand Down Expand Up @@ -1697,7 +1695,7 @@ suite('JSON Parser', () => {
});

test('type as array', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
'prop': {
Expand Down Expand Up @@ -1727,7 +1725,7 @@ suite('JSON Parser', () => {

const { textDoc, jsonDoc } = toDocument('{"prop": 42}');

const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
'prop': {
Expand Down Expand Up @@ -1759,7 +1757,7 @@ suite('JSON Parser', () => {
const { jsonDoc } = toDocument('{"key":42}');
assert.strictEqual(jsonDoc.syntaxErrors.length, 0);

const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
'key': {
Expand Down Expand Up @@ -1820,7 +1818,7 @@ suite('JSON Parser', () => {
});

test('validate alternatives', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
'key': {
Expand Down Expand Up @@ -1870,7 +1868,7 @@ suite('JSON Parser', () => {
});

test('validate alternatives 2', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
'key': {
Expand Down Expand Up @@ -1920,7 +1918,7 @@ suite('JSON Parser', () => {
});

test('enum value merge', function () {
const schema: JsonSchema.JSONSchema = {
const schema: JSONSchema = {
type: 'object',
properties: {
'key': {
Expand Down Expand Up @@ -1956,7 +1954,7 @@ suite('JSON Parser', () => {
res = await ls.doValidation(textDoc, jsonDoc, { trailingCommas: 'ignore' });
assert.strictEqual(res.length, 0);

const schema: JsonSchema.JSONSchema = { type: 'object', required: ['foo'] };
const schema: JSONSchema = { type: 'object', required: ['foo'] };
res = await ls.doValidation(textDoc, jsonDoc, { trailingCommas: 'ignore' }, schema);
assert.strictEqual(res.length, 1);
assert.strictEqual(res[0].message, 'Missing property "foo".');
Expand Down

0 comments on commit d8a8420

Please sign in to comment.