|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +import { assert } from "chai"; |
| 4 | +import sinon from "sinon"; |
| 5 | + |
| 6 | +import { profileConfig } from "../../../src/commands/profile"; |
| 7 | +import { ConnectionType } from "../../../src/components/profile"; |
| 8 | +import { getCodeDocumentConstructionParameters } from "../../../src/components/utils/SASCodeDocumentHelper"; |
| 9 | + |
| 10 | +describe("sas code document helper", () => { |
| 11 | + describe("getCodeDocumentConstructionParameters", () => { |
| 12 | + let getConfigurationStub: sinon.SinonStub; |
| 13 | + let colorThemeStub: sinon.SinonStub; |
| 14 | + let htmlStyle = "(auto)"; |
| 15 | + const htmlCustomStyle = { dark: "MyCustomDarkStyle" }; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + getConfigurationStub = sinon.stub(vscode.workspace, "getConfiguration"); |
| 19 | + getConfigurationStub.withArgs("SAS").returns({ |
| 20 | + get: (key: string) => { |
| 21 | + if (key === "results.html.style") { |
| 22 | + return htmlStyle; |
| 23 | + } |
| 24 | + if (key === "results.html.custom.style") { |
| 25 | + return htmlCustomStyle; |
| 26 | + } |
| 27 | + return undefined; |
| 28 | + }, |
| 29 | + }); |
| 30 | + colorThemeStub = sinon |
| 31 | + .stub(vscode.window, "activeColorTheme") |
| 32 | + .value({ kind: vscode.ColorThemeKind.Dark }); |
| 33 | + profileConfig.getActiveProfileDetail = () => ({ |
| 34 | + name: "mock", |
| 35 | + profile: { |
| 36 | + connectionType: ConnectionType.Rest, |
| 37 | + endpoint: "http://example.com", |
| 38 | + }, |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + getConfigurationStub.restore(); |
| 44 | + colorThemeStub.restore(); |
| 45 | + }); |
| 46 | + |
| 47 | + const mockTextDocument = { |
| 48 | + languageId: "sas", |
| 49 | + getText: function () { |
| 50 | + return "data _null_; run;"; |
| 51 | + }, |
| 52 | + uri: vscode.Uri.file("/fake/path/test.sas"), |
| 53 | + fileName: "/fake/path/test.sas", |
| 54 | + lineCount: 1, |
| 55 | + lineAt: function (lineOrPos: number | vscode.Position = 0) { |
| 56 | + const lineNumber = |
| 57 | + typeof lineOrPos === "number" ? lineOrPos : lineOrPos.line; |
| 58 | + return { |
| 59 | + lineNumber, |
| 60 | + text: "data _null_; run;", |
| 61 | + range: new vscode.Range(lineNumber, 0, lineNumber, 16), |
| 62 | + rangeIncludingLineBreak: new vscode.Range( |
| 63 | + lineNumber, |
| 64 | + 0, |
| 65 | + lineNumber, |
| 66 | + 17, |
| 67 | + ), |
| 68 | + firstNonWhitespaceCharacterIndex: 0, |
| 69 | + isEmptyOrWhitespace: false, |
| 70 | + }; |
| 71 | + }, |
| 72 | + isUntitled: false, |
| 73 | + version: 1, |
| 74 | + isDirty: false, |
| 75 | + isClosed: false, |
| 76 | + save: async () => true, |
| 77 | + eol: 1, |
| 78 | + offsetAt: () => 0, |
| 79 | + positionAt: () => new vscode.Position(0, 0), |
| 80 | + validateRange: (range: vscode.Range) => range, |
| 81 | + validatePosition: (pos: vscode.Position) => pos, |
| 82 | + getWordRangeAtPosition: () => undefined, |
| 83 | + }; |
| 84 | + |
| 85 | + it("should construct parameters with default values", () => { |
| 86 | + const params = getCodeDocumentConstructionParameters(mockTextDocument); |
| 87 | + assert.equal(params.languageId, "sas"); |
| 88 | + assert.equal(params.code, "data _null_; run;"); |
| 89 | + assert.equal( |
| 90 | + params.uri, |
| 91 | + vscode.Uri.file("/fake/path/test.sas").toString(), |
| 92 | + ); |
| 93 | + assert.equal(params.fileName, "/fake/path/test.sas"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should use custom style from results.html.custom.style for dark theme", () => { |
| 97 | + const params = getCodeDocumentConstructionParameters(mockTextDocument); |
| 98 | + // the custom style should be applied |
| 99 | + assert.equal(params.htmlStyle, "MyCustomDarkStyle"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should not use custom style from results.html.custom.style for dark theme when not defined", () => { |
| 103 | + htmlCustomStyle.dark = undefined; |
| 104 | + const params = getCodeDocumentConstructionParameters(mockTextDocument); |
| 105 | + // the default Ignite style should be used |
| 106 | + assert.equal(params.htmlStyle, "Ignite"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should use no style when results.html.style is '(server default)'", () => { |
| 110 | + htmlStyle = "(server default)"; |
| 111 | + const params = getCodeDocumentConstructionParameters(mockTextDocument); |
| 112 | + // no style should be applied |
| 113 | + assert.equal(params.htmlStyle, ""); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should use results.html.style when results.html.style is neither '(auto)' nor '(server default)'", () => { |
| 117 | + htmlStyle = "HTMLBlue"; |
| 118 | + const params = getCodeDocumentConstructionParameters(mockTextDocument); |
| 119 | + // the results.html.style should be applied |
| 120 | + assert.equal(params.htmlStyle, "HTMLBlue"); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments