microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/compiler/testing/expect.ts
108lines · modecode
| 1 | import assert, { fail, match, strictEqual } from "assert"; |
| 2 | import { Diagnostic, formatDiagnostic, getSourceLocation, NoTarget } from "../core/index.js"; |
| 3 | import { isArray } from "../core/util.js"; |
| 4 | import { resolveVirtualPath } from "./test-host.js"; |
| 5 | |
| 6 | /** |
| 7 | * Assert there is no diagnostics. |
| 8 | * @param diagnostics Diagnostics |
| 9 | */ |
| 10 | export function expectDiagnosticEmpty(diagnostics: readonly Diagnostic[]) { |
| 11 | if (diagnostics.length > 0) { |
| 12 | assert.fail(`Unexpected diagnostics:\n${formatDiagnostics(diagnostics)}`); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | function formatDiagnostics(diagnostics: readonly Diagnostic[]) { |
| 17 | return diagnostics.map(formatDiagnostic).join("\n"); |
| 18 | } |
| 19 | /** |
| 20 | * Condition to match |
| 21 | */ |
| 22 | export interface DiagnosticMatch { |
| 23 | /** |
| 24 | * Match the code. |
| 25 | */ |
| 26 | code: string; |
| 27 | |
| 28 | /** |
| 29 | * Match the message. |
| 30 | */ |
| 31 | message?: string | RegExp; |
| 32 | |
| 33 | /** |
| 34 | * Match the severity. |
| 35 | */ |
| 36 | severity?: "error" | "warning"; |
| 37 | |
| 38 | /** |
| 39 | * Name of the file for this diagnostic. |
| 40 | */ |
| 41 | file?: string | RegExp; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Validate the diagnostic array contains exactly the given diagnostics. |
| 46 | * @param diagnostics Array of the diagnostics |
| 47 | */ |
| 48 | export function expectDiagnostics( |
| 49 | diagnostics: readonly Diagnostic[], |
| 50 | match: DiagnosticMatch | DiagnosticMatch[] |
| 51 | ) { |
| 52 | const array = isArray(match) ? match : [match]; |
| 53 | |
| 54 | if (array.length !== diagnostics.length) { |
| 55 | assert.fail( |
| 56 | `Expected ${array.length} diagnostics but found ${diagnostics.length}:\n ${formatDiagnostics( |
| 57 | diagnostics |
| 58 | )}` |
| 59 | ); |
| 60 | } |
| 61 | for (let i = 0; i < array.length; i++) { |
| 62 | const diagnostic = diagnostics[i]; |
| 63 | const expectation = array[i]; |
| 64 | const sep = "-".repeat(100); |
| 65 | const message = `Diagnostics found:\n${sep}\n${formatDiagnostics(diagnostics)}\n${sep}`; |
| 66 | strictEqual( |
| 67 | diagnostic.code, |
| 68 | expectation.code, |
| 69 | `Diagnostics at index ${i} has non matching code.\n${message}` |
| 70 | ); |
| 71 | |
| 72 | if (expectation.message !== undefined) { |
| 73 | matchStrOrRegex( |
| 74 | diagnostic.message, |
| 75 | expectation.message, |
| 76 | `Diagnostics at index ${i} has non matching message.\n${message}` |
| 77 | ); |
| 78 | } |
| 79 | if (expectation.severity !== undefined) { |
| 80 | strictEqual( |
| 81 | diagnostic.severity, |
| 82 | expectation.severity, |
| 83 | `Diagnostics at index ${i} has non matching severity.\n${message}` |
| 84 | ); |
| 85 | } |
| 86 | if (expectation.file !== undefined) { |
| 87 | if (diagnostic.target === NoTarget) { |
| 88 | fail(`Diagnostics at index ${i} expected to have a target.\n${message}`); |
| 89 | } |
| 90 | const source = getSourceLocation(diagnostic.target); |
| 91 | matchStrOrRegex( |
| 92 | source.file.path, |
| 93 | typeof expectation.file === "string" |
| 94 | ? resolveVirtualPath(expectation.file) |
| 95 | : expectation.file, |
| 96 | `Diagnostics at index ${i} has non matching file.\n${message}` |
| 97 | ); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function matchStrOrRegex(value: string, expectation: string | RegExp, assertMessage: string) { |
| 103 | if (typeof expectation === "string") { |
| 104 | strictEqual(value, expectation, assertMessage); |
| 105 | } else { |
| 106 | match(value, expectation, assertMessage); |
| 107 | } |
| 108 | } |
| 109 | |