Skip to content

Commit

Permalink
markers: update problem-manager tests
Browse files Browse the repository at this point in the history
- updates the problem-manager tests for readability, and
  maintainability.
- includes additional test cases.

Signed-off-by: vince-fugnitto <[email protected]>
  • Loading branch information
vince-fugnitto committed Oct 9, 2020
1 parent 3db2314 commit c1b53f1
Showing 1 changed file with 119 additions and 180 deletions.
299 changes: 119 additions & 180 deletions packages/markers/src/browser/problem/problem-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,173 +18,144 @@ 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 } from 'vscode-languageserver-types';
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { Range } from 'vscode-languageserver-types';
import { LocalStorageService, StorageService } from '@theia/core/lib/browser/storage-service';

disableJSDOM();

const expect = chai.expect;

let manager: ProblemManager;
let testContainer: Container;

beforeEach(() => {
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.cleanAllMarkers();
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;

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
});
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);
container.bind(ProblemManager).toSelf();
manager = container.get(ProblemManager);
});

it('should find markers with filter', () => {
expect(manager.findMarkers({
owner: 'me'
}).length).equal(4);
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: { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } } }]);
expect(Array.from(manager.getUris()).length).to.equal(1);
});

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

expect(manager.findMarkers({
owner: 'you'
}).length).equal(0);
let events = 0;
manager.onDidChangeMarkers(() => events++);
expect(events).equal(0);

expect(manager.findMarkers({
uri: new URI('file:/foo/foo.txt'),
owner: 'me'
}).length).equal(2);
const initial = manager.setMarkers(uri, 'a', [{ message: 'a', range: { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } } }]);
expect(initial.length).equal(0);
expect(events).equal(1);

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

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

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

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

it('should successfully clean all markers', () => {

// Create mock markers.
manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range: { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } } }]);
manager.setMarkers(new URI('b'), 'b', [{ message: 'a', range: { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } } }]);
manager.setMarkers(new URI('c'), 'c', [{ message: 'a', range: { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } } }]);
expect(Array.from(manager.getUris()).length).to.equal(3);

// Clean the markers.
manager.cleanAllMarkers();
expect(Array.from(manager.getUris()).length).to.equal(0);

});

});

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

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

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: { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } } }]);

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

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

});

it('should find markers that satisfy the dataFilter', () => {
const range = { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } };

manager.setMarkers(new URI('a'), 'a', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);
manager.setMarkers(new URI('b'), 'a', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);
manager.setMarkers(new URI('c'), 'a', [{ message: 'a', range, severity: DiagnosticSeverity.Error }]);

expect(manager.findMarkers({ dataFilter: d => d.severity === DiagnosticSeverity.Error }).length).equal(3);
expect(manager.findMarkers({ dataFilter: d => d.severity !== DiagnosticSeverity.Error }).length).equal(0);

it('should return the list of uris', () => {
const uris: string[] = Array.from(manager.getUris());
expect(uris.length).to.equal(2);
});

});

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

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

it('should return the list of uris', () => {
const range = { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } };
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', () => {
manager.cleanAllMarkers();
const { errors, warnings, infos } = manager.getProblemStat();
expect(errors).to.equal(0);
expect(warnings).to.equal(0);
Expand All @@ -193,53 +164,21 @@ describe('problem-manager', () => {

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

// Clean the markers for test data.
manager.cleanAllMarkers();
const range = { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } };

// 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 }]);

const range: Range = {
start: {
line: 1, character: 1
},
end: {
line: 1, character: 1
}
};

// Create 3 error, 2 warning and 1 info markers.
manager.setMarkers(new URI('foo'), 'bar', [
{
message: 'error-1',
range,
severity: 1,
},
{
message: 'error-2',
range,
severity: 1,
},
{
message: 'error-3',
range,
severity: 1,
},
{
message: 'warning-1',
range,
severity: 2,
},
{
message: 'warning-2',
range,
severity: 2,
},
{
message: 'info-1',
range,
severity: 3,
}
]);

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

expect(errors).to.equal(3);
Expand Down

0 comments on commit c1b53f1

Please sign in to comment.