Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

markers: add additional problem-manager tests #8604

Merged
merged 1 commit into from
Oct 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
299 changes: 175 additions & 124 deletions packages/markers/src/browser/problem/problem-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,148 +18,199 @@ import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';

const disableJSDOM = enableJSDOM();

import { Container } from 'inversify';
import * as chai from 'chai';
import { ProblemManager } from './problem-manager';
import URI from '@theia/core/lib/common/uri';
import { LocalStorageService, StorageService } from '@theia/core/lib/browser/storage-service';

import { Container } from 'inversify';
import { ProblemManager } from './problem-manager';
import { Event } from '@theia/core/lib/common/event';
import { ILogger } from '@theia/core/lib/common/logger';
import { DiagnosticSeverity, Range } from 'vscode-languageserver-types';
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { LocalStorageService, StorageService } from '@theia/core/lib/browser/storage-service';

disableJSDOM();

const expect = chai.expect;

let manager: ProblemManager;
let testContainer: Container;

before(() => {
testContainer = new Container();
testContainer.bind(ILogger).to(MockLogger);
testContainer.bind(StorageService).to(LocalStorageService).inSingletonScope();
testContainer.bind(LocalStorageService).toSelf().inSingletonScope();
testContainer.bind(FileService).toConstantValue(<FileService>{
onDidFilesChange: Event.None
});
testContainer.bind(ProblemManager).toSelf();

manager = testContainer.get(ProblemManager);
manager.setMarkers(new URI('file:/foo/bar.txt'), 'me', [
{
range: {
start: {
line: 1,
character: 1
},
end: {
line: 1,
character: 1
}
},
message: 'Foo'
},
{
range: {
start: {
line: 1,
character: 1
},
end: {
line: 1,
character: 1
}
},
message: 'Bar'
}
]);

manager.setMarkers(new URI('file:/foo/foo.txt'), 'me', [
{
range: {
start: {
line: 1,
character: 1
},
end: {
line: 1,
character: 1
}
},
message: 'Foo'
},
{
range: {
start: {
line: 1,
character: 1
},
end: {
line: 1,
character: 2
}
},
message: 'Bar'
}
]);
});
let container: Container;

/**
* The default range for test purposes.
*/
const range: Range = { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } };

describe('problem-manager', () => {
it('replaces markers', () => {
let events = 0;
manager.onDidChangeMarkers(() => {
events++;

beforeEach(() => {
container = new Container();
container.bind(ILogger).to(MockLogger);
container.bind(StorageService).to(LocalStorageService).inSingletonScope();
container.bind(LocalStorageService).toSelf().inSingletonScope();
container.bind(FileService).toConstantValue(<FileService>{
onDidFilesChange: Event.None
});
container.bind(ProblemManager).toSelf();
manager = container.get(ProblemManager);
});

describe('#setMarkers', () => {

it('should successfully set new markers', () => {
expect(Array.from(manager.getUris()).length).to.equal(0);
manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range }]);
expect(Array.from(manager.getUris()).length).to.equal(1);
});
expect(events).equal(0);
const previous = manager.setMarkers(new URI('file:/foo/bar.txt'), 'me', [
{
range: {
start: {
line: 2,
character: 3
},
end: {
line: 2,
character: 1
}
},
message: 'Foo'
},
{
range: {
start: {
line: 1,
character: 1
},
end: {
line: 1,
character: 1
}
},
message: 'Bar'
}
]);
expect(previous.length).equal(2);
expect(events).equal(1);
expect(manager.findMarkers().length).equal(4);

it('should replace markers', () => {
const uri = new URI('a');

let events = 0;
manager.onDidChangeMarkers(() => events++);
expect(events).equal(0);

const initial = manager.setMarkers(uri, 'a', [{ message: 'a', range }]);
expect(initial.length).equal(0);
expect(events).equal(1);

const updated = manager.setMarkers(uri, 'a', [{ message: 'a', range }]);
expect(updated.length).equal(1);
expect(events).equal(2);

expect(manager.findMarkers({ uri }).length).equal(1);
});

});

it('should find markers with filter', () => {
expect(manager.findMarkers({
owner: 'me'
}).length).equal(4);
describe('#cleanAllMarkers', () => {

expect(manager.findMarkers({
owner: 'you'
}).length).equal(0);
it('should successfully clean all markers', () => {
// Create mock markers.
manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range }]);
manager.setMarkers(new URI('b'), 'b', [{ message: 'a', range }]);
manager.setMarkers(new URI('c'), 'c', [{ message: 'a', range }]);
expect(Array.from(manager.getUris()).length).to.equal(3);

expect(manager.findMarkers({
uri: new URI('file:/foo/foo.txt'),
owner: 'me'
}).length).equal(2);
// Clean the markers.
manager.cleanAllMarkers();
expect(Array.from(manager.getUris()).length).to.equal(0);
});

expect(manager.findMarkers({
dataFilter: data => data.range.end.character > 1
}).length).equal(1);
});

describe('#findMarkers', () => {

it('should find markers by `owner`', () => {
const owner: string = 'foo';
manager.setMarkers(new URI('a'), owner, [{ message: 'a', range }]);
manager.setMarkers(new URI('b'), owner, [{ message: 'a', range }]);

expect(manager.findMarkers({ owner }).length).equal(2);
expect(manager.findMarkers({ owner: 'unknown' }).length).equal(0);
});

it('should find markers by `owner` and `uri`', () => {
const owner: string = 'foo';
const uri = new URI('bar');

// Create a marker to match the filter.
manager.setMarkers(uri, owner, [{ message: 'a', range }]);

// Create 2 markers that do not match the filter.
manager.setMarkers(new URI('invalid'), 'invalid-owner', [{ message: 'a', range }]);
manager.setMarkers(new URI('invalid'), 'invalid-owner', [{ message: 'a', range }]);

// Expect to find the markers which satisfy the filter only.
expect(manager.findMarkers({ owner, uri }).length).equal(1);
});

describe('dataFilter', () => {

it('should find markers that satisfy filter for `severity`', () => {
manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);
expect(manager.findMarkers({ dataFilter: d => d.severity === DiagnosticSeverity.Error }).length).equal(1);
expect(manager.findMarkers({ dataFilter: d => d.severity !== DiagnosticSeverity.Error }).length).equal(0);
});

it('should find markers that satisfy filter for `code`', () => {
const code = 100;
manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range, code }]);
expect(manager.findMarkers({ dataFilter: d => d.code === code }).length).equal(1);
expect(manager.findMarkers({ dataFilter: d => d.code !== code }).length).equal(0);
});

it('should find markers that satisfy filter for `message`', () => {
const message = 'foo';
manager.setMarkers(new URI('a'), 'a', [{ message, range }]);
expect(manager.findMarkers({ dataFilter: d => d.message === message }).length).equal(1);
expect(manager.findMarkers({ dataFilter: d => d.message !== message }).length).equal(0);
});

it('should find markers that satisfy filter for `source`', () => {
const source = 'typescript';
manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range, source }]);
expect(manager.findMarkers({ dataFilter: d => d.source === source }).length).equal(1);
expect(manager.findMarkers({ dataFilter: d => d.source !== source }).length).equal(0);
});

it('should find markers that satisfy filter for `range`', () => {
manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range }]);
// The default `range` has a start line number of 0.
expect(manager.findMarkers({ dataFilter: d => d.range.start.line === 0 }).length).equal(1);
expect(manager.findMarkers({ dataFilter: d => d.range.start.line > 0 }).length).equal(0);
});

});

});

describe('#getUris', () => {

it('should return 0 uris when no markers are present', () => {
expect(Array.from(manager.getUris()).length).to.equal(0);
});

it('should return the list of uris', () => {
manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);
manager.setMarkers(new URI('b'), 'b', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);
expect(Array.from(manager.getUris()).length).to.equal(2);
});

});

describe('#getProblemStat', () => {

it('should return 0 stats when no markers are present', () => {
const { errors, warnings, infos } = manager.getProblemStat();
expect(errors).to.equal(0);
expect(warnings).to.equal(0);
expect(infos).to.equal(0);
});

it('should return the proper problem stats', () => {

// Create 3 error markers.
manager.setMarkers(new URI('error-1'), 'error-1', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);
manager.setMarkers(new URI('error-2'), 'error-2', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);
manager.setMarkers(new URI('error-3'), 'error-3', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);

// Create 2 warning markers.
manager.setMarkers(new URI('warning-1'), 'warning-1', [{ message: 'a', range, severity: DiagnosticSeverity.Warning }]);
manager.setMarkers(new URI('warning-2'), 'warning-2', [{ message: 'a', range, severity: DiagnosticSeverity.Warning }]);

// Create 1 info marker.
manager.setMarkers(new URI('info-1'), 'info-1', [{ message: 'a', range, severity: DiagnosticSeverity.Information }]);

// Collect the total problem stats for the application.
const { errors, warnings, infos } = manager.getProblemStat();

expect(errors).to.equal(3);
expect(warnings).to.equal(2);
expect(infos).to.equal(1);
});

});

});