From b033db7473202b9666b53c8b50899c5553d444b7 Mon Sep 17 00:00:00 2001 From: detachhead Date: Wed, 24 Jan 2024 17:30:23 +1000 Subject: [PATCH] add tests for `validateResultsButBased` --- .../src/tests/testUtils.test.ts | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 packages/pyright-internal/src/tests/testUtils.test.ts diff --git a/packages/pyright-internal/src/tests/testUtils.test.ts b/packages/pyright-internal/src/tests/testUtils.test.ts new file mode 100644 index 0000000000..a5597037ee --- /dev/null +++ b/packages/pyright-internal/src/tests/testUtils.test.ts @@ -0,0 +1,81 @@ +import { AssertionError } from 'assert'; +import { Diagnostic } from '../common/diagnostic'; +import { Uri } from '../common/uri/uri'; +import { FileAnalysisResult, ExpectedResults, validateResultsButBased } from './testUtils'; +import { DiagnosticRule } from '../common/diagnosticRules'; + +const fakeUri = {} as Uri; +const fakeDiagnostic = (line: number, code?: DiagnosticRule) => + ({ getRule: () => code, range: { start: { line } } } as Diagnostic); + +test('validateResults pass 😀', () => { + const expectedResults: ExpectedResults = { + errors: [{ line: 1 }, { line: 2 }], + }; + const actualResults: FileAnalysisResult[] = [ + { + errors: [fakeDiagnostic(1), fakeDiagnostic(2)], + fileUri: fakeUri, + warnings: [], + infos: [], + unusedCodes: [], + deprecateds: [], + unreachableCodes: [], + }, + ]; + validateResultsButBased(actualResults, expectedResults); +}); + +test('validateResults wrong number of errors', () => { + const expectedResults: ExpectedResults = { + errors: [{ line: 1 }, { line: 2 }], + }; + const actualResults: FileAnalysisResult[] = [ + { + errors: [fakeDiagnostic(1)], + fileUri: fakeUri, + warnings: [], + infos: [], + unusedCodes: [], + deprecateds: [], + unreachableCodes: [], + }, + ]; + expect(() => validateResultsButBased(actualResults, expectedResults)).toThrow(AssertionError); +}); + +test('validateResults wrong line number', () => { + const expectedResults: ExpectedResults = { + errors: [{ line: 2 }], + }; + const actualResults: FileAnalysisResult[] = [ + { + errors: [fakeDiagnostic(1)], + fileUri: fakeUri, + warnings: [], + infos: [], + unusedCodes: [], + deprecateds: [], + unreachableCodes: [], + }, + ]; + expect(() => validateResultsButBased(actualResults, expectedResults)).toThrow(AssertionError); +}); + +test('validateResults wrong code', () => { + const expectedResults: ExpectedResults = { + errors: [{ line: 1, code: DiagnosticRule.reportUnboundVariable }], + }; + const actualResults: FileAnalysisResult[] = [ + { + errors: [fakeDiagnostic(1, DiagnosticRule.analyzeUnannotatedFunctions)], + fileUri: fakeUri, + warnings: [], + infos: [], + unusedCodes: [], + deprecateds: [], + unreachableCodes: [], + }, + ]; + expect(() => validateResultsButBased(actualResults, expectedResults)).toThrow(AssertionError); +});