Skip to content

Commit 7ac8ee1

Browse files
committed
Add tests
1 parent 60285cc commit 7ac8ee1

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2025 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
import * as assert from "assert";
15+
import { afterEach } from "mocha";
16+
import { match, restore } from "sinon";
17+
import * as vscode from "vscode";
18+
19+
import configuration from "@src/configuration";
20+
21+
import { instance, mockFn, mockGlobalObject, mockObject } from "../../MockUtils";
22+
23+
suite.only("Configuration/Settings Test Suite", () => {
24+
const mockWorkspace = mockGlobalObject(vscode, "workspace");
25+
26+
afterEach(() => {
27+
restore();
28+
});
29+
30+
function mockSetting<T>(settingName: string, value: T) {
31+
const [lead, ...rest] = settingName.split(".");
32+
const mockSwiftConfig = mockObject<vscode.WorkspaceConfiguration>({
33+
get: mockFn(s => s.withArgs(rest.join("."), match.any).returns(value)),
34+
});
35+
mockWorkspace.getConfiguration.withArgs(lead).returns(instance(mockSwiftConfig));
36+
}
37+
38+
test("returns a string configuration value", () => {
39+
mockSetting("swift.path", "foo");
40+
assert.equal(configuration.path, "foo");
41+
});
42+
43+
test("throws when a string setting is not a string", () => {
44+
mockSetting("swift.path", 42);
45+
assert.throws(() => {
46+
configuration.path;
47+
});
48+
});
49+
50+
test("returns a boolean configuration value", () => {
51+
mockSetting("swift.recordTestDuration", false);
52+
assert.equal(configuration.recordTestDuration, false);
53+
});
54+
55+
test("throws when a boolean setting is not a boolean", () => {
56+
mockSetting("swift.recordTestDuration", "notaboolean");
57+
assert.throws(() => {
58+
configuration.recordTestDuration;
59+
});
60+
});
61+
62+
test("returns a string array configuration value", () => {
63+
mockSetting("swift.excludeFromCodeCoverage", ["foo", "bar"]);
64+
assert.deepEqual(configuration.excludeFromCodeCoverage, ["foo", "bar"]);
65+
});
66+
67+
test("throws when a string array setting is not a string array", () => {
68+
mockSetting("swift.excludeFromCodeCoverage", [42, true]);
69+
assert.throws(() => {
70+
configuration.excludeFromCodeCoverage;
71+
});
72+
});
73+
74+
test("returns an object configuration value", () => {
75+
const obj = { FOO: "BAR" };
76+
mockSetting("swift.swiftEnvironmentVariables", obj);
77+
assert.deepEqual(configuration.swiftEnvironmentVariables, obj);
78+
});
79+
80+
test("throws when an object setting is not an object", () => {
81+
mockSetting("swift.swiftEnvironmentVariables", "notanobject");
82+
assert.throws(() => {
83+
configuration.swiftEnvironmentVariables;
84+
});
85+
});
86+
});

0 commit comments

Comments
 (0)