microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/qdk_package

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/npm/qsharp/test/diagnostics.js

92lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//@ts-check
5
6import assert from "node:assert/strict";
7import { test } from "node:test";
8import { QdkDiagnostics } from "../dist/diagnostics.js";
9import { log } from "../dist/log.js";
10import {
11 getCompiler,
12 getCompilerWorker,
13 getProjectLoader,
14} from "../dist/main.js";
15
16/** @type {import("../dist/log.js").TelemetryEvent[]} */
17const telemetryEvents = [];
18log.setLogLevel("warn");
19log.setTelemetryCollector((event) => telemetryEvents.push(event));
20
21/**
22 * @returns {import ("../dist/compiler/compiler.js").ProgramConfig}
23 */
24function getInvalidQirProgramConfig() {
25 /** @type {[string, string][]} */
26 const sources = [
27 ["test.qs", `namespace Test { function Main() : Int { return 1; } }`],
28 ];
29 return {
30 sources,
31 languageFeatures: [],
32 profile: "base",
33 };
34}
35
36test("getQir throws QdkDiagnostics", async () => {
37 const compiler = getCompiler();
38 const invalidConfig = getInvalidQirProgramConfig();
39 await assert.rejects(
40 () => compiler.getQir(invalidConfig),
41 (err) => {
42 assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics");
43 assert(err.diagnostics.length > 0, "diagnostics should not be empty");
44 assert.match(err.message, /cannot use an integer value as an output/);
45 return true;
46 },
47 "getQir should throw on invalid input",
48 );
49});
50
51test("getQir throws QdkDiagnostics - worker", async () => {
52 const compiler = getCompilerWorker();
53 const invalidConfig = getInvalidQirProgramConfig();
54 try {
55 await assert.rejects(
56 () => compiler.getQir(invalidConfig),
57 (err) => {
58 assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics");
59 assert(err.diagnostics.length > 0, "diagnostics should not be empty");
60 assert.match(err.message, /cannot use an integer value as an output/);
61 return true;
62 },
63 "getQir should throw on invalid input",
64 );
65 } finally {
66 compiler.terminate();
67 }
68});
69
70// Minimal IProjectHost implementation for testing
71const dummyHost = {
72 readFile: async () => null,
73 listDirectory: async () => [],
74 resolvePath: async (a, b) => b,
75 fetchGithub: async () => "",
76 findManifestDirectory: async () => null,
77};
78
79test("loadQSharpProject throws QdkDiagnostics", async () => {
80 const loader = getProjectLoader(dummyHost);
81 await assert.rejects(
82 () => loader.loadQSharpProject("/not/a/real/dir"),
83 (err) => {
84 assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics");
85 assert(err.diagnostics.length > 0, "diagnostics should not be empty");
86 assert.match(err.message, /Failed to parse manifest/i);
87 return true;
88 },
89 "loadQSharpProject should throw on invalid directory",
90 );
91 loader.dispose();
92});
93