Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
q-rault committed Jun 3, 2023
1 parent b4830f0 commit a37b053
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { Hover, MarkupContent, MarkupKind, Position, Range } from 'vscode-languageserver-types';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { LanguageSettings } from '../yamlLanguageService';
import { LanguageSettings, HoverSettings } from '../yamlLanguageService';
import { YAMLSchemaService } from './yamlSchemaService';
import { setKubernetesParserOption } from '../parser/isKubernetes';
import { TextDocument } from 'vscode-languageserver-textdocument';
Expand All @@ -24,7 +24,7 @@ import { ASTNode } from 'vscode-json-languageservice';
export class YAMLHover {
private shouldHover: boolean;
private indentation: string;
private hoverSettings: LanguageSettings['hoverSettings'];
private hoverSettings: HoverSettings;
private schemaService: YAMLSchemaService;

constructor(schemaService: YAMLSchemaService, private readonly telemetry?: Telemetry) {
Expand All @@ -36,7 +36,7 @@ export class YAMLHover {
if (languageSettings) {
this.shouldHover = languageSettings.hover;
this.indentation = languageSettings.indentation;
this.hoverSettings = languageSettings.hoverSettings;
this.hoverSettings = languageSettings.hoverSettings ?? {};
}
}

Expand Down Expand Up @@ -110,9 +110,8 @@ export class YAMLHover {
return value.replace(/\|\|\s*$/, '');
};

const hoverSettings = this.hoverSettings || {};
const showSource = !!hoverSettings?.showSource;
const showTitle = !!hoverSettings?.showTitle;
const showSource = this.hoverSettings?.showSource ?? true; // showSource enabled by default
const showTitle = this.hoverSettings?.showTitle ?? true; // showTitle enabled by default

return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => {
if (schema && node && !schema.errors.length) {
Expand Down
7 changes: 6 additions & 1 deletion src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ export interface SchemasSettings {
versions?: SchemaVersions;
}

export interface HoverSettings {
showSource?: boolean;
showTitle?: boolean;
}

export interface LanguageSettings {
validate?: boolean; //Setting for whether we want to validate the schema
hover?: boolean; //Setting for whether we want to have hover results
hoverSettings?: { showSource?: boolean; showTitle?: boolean }; // Settings for hover parts
hoverSettings?: HoverSettings; // Settings for hover parts
completion?: boolean; //Setting for whether we want to have completion results
format?: boolean; //Setting for whether we want to have the formatter or not
isKubernetes?: boolean; //If true then its validating against kubernetes
Expand Down
108 changes: 108 additions & 0 deletions test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,114 @@ users:
);
});

it('Hover hides title and source when disabled', async () => {
(() => {
languageSettingsSetup = new ServiceSetup()
.withHover()
.withSchemaFileMatch({
uri: 'http://google.com',
fileMatch: ['bad-schema.yaml'],
})
.withHoverSettings({
showTitle: false,
showSource: false,
});
const {
languageService: langService,
languageHandler: langHandler,
yamlSettings: settings,
telemetry: testTelemetry,
} = setupLanguageService(languageSettingsSetup.languageSettings);
languageService = langService;
languageHandler = langHandler;
yamlSettings = settings;
telemetry = testTelemetry;
})();
languageService.addSchema(SCHEMA_ID, {
type: 'object',
title: 'Living being',
properties: {
animal: {
type: 'string',
description: 'should return this description',
enum: ['cat', 'dog'],
examples: ['cat', 'dog'],
},
},
});
const content = 'animal:\n ca|t|'; // len: 13, pos: 12
const result = await parseSetup(content);

assert.strictEqual(MarkupContent.is(result.contents), true);
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
assert.strictEqual(
(result.contents as MarkupContent).value,
`should return this description
Examples:
\`\`\`"cat"\`\`\`
\`\`\`"dog"\`\`\``
);
});

it('Hover works when title and source explicitely enabled', async () => {
(() => {
languageSettingsSetup = new ServiceSetup()
.withHover()
.withSchemaFileMatch({
uri: 'http://google.com',
fileMatch: ['bad-schema.yaml'],
})
.withHoverSettings({
showTitle: true,
showSource: true,
});
const {
languageService: langService,
languageHandler: langHandler,
yamlSettings: settings,
telemetry: testTelemetry,
} = setupLanguageService(languageSettingsSetup.languageSettings);
languageService = langService;
languageHandler = langHandler;
yamlSettings = settings;
telemetry = testTelemetry;
})();
languageService.addSchema(SCHEMA_ID, {
type: 'object',
title: 'Living being',
properties: {
animal: {
type: 'string',
description: 'should return this description',
enum: ['cat', 'dog'],
examples: ['cat', 'dog'],
},
},
});
const content = 'animal:\n ca|t|'; // len: 13, pos: 12
const result = await parseSetup(content);

assert.strictEqual(MarkupContent.is(result.contents), true);
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
assert.strictEqual(
(result.contents as MarkupContent).value,
`#### Living being
should return this description
Examples:
\`\`\`"cat"\`\`\`
\`\`\`"dog"\`\`\`
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
);
});

it('Hover works on examples', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
Expand Down
8 changes: 7 additions & 1 deletion test/utils/serviceSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { LanguageSettings, SchemasSettings } from '../../src/languageservice/yamlLanguageService';
import { HoverSettings, LanguageSettings, SchemasSettings } from '../../src/languageservice/yamlLanguageService';

export class ServiceSetup {
/*
Expand All @@ -12,6 +12,7 @@ export class ServiceSetup {
languageSettings: LanguageSettings = {
validate: false,
hover: false,
hoverSettings: {},
completion: false,
format: false,
isKubernetes: false,
Expand All @@ -33,6 +34,11 @@ export class ServiceSetup {
return this;
}

withHoverSettings(hoverSettings: HoverSettings): ServiceSetup {
this.languageSettings.hoverSettings = hoverSettings;
return this;
}

withCompletion(): ServiceSetup {
this.languageSettings.completion = true;
return this;
Expand Down

0 comments on commit a37b053

Please sign in to comment.