"use strict"; const expect = require("chai").expect; const TestUtil = require("../../util"); const Utils = require("../../../src/command-line/utils"); describe("Utils", function() { describe(".extraHelp", function() { let originalRaw; beforeEach(function() { originalRaw = log.raw; }); afterEach(function() { log.raw = originalRaw; }); it("should start and end with empty lines to display correctly with --help", function() { // Mock `log.raw` to extract its effect into an array const stdout = []; log.raw = TestUtil.mockLogger((str) => stdout.push(str)); Utils.extraHelp(); // Starts with 2 empty lines expect(stdout[0]).to.equal("\n"); expect(stdout[1]).to.equal("\n"); expect(stdout[2]).to.not.equal("\n"); // Ends with 1 empty line expect(stdout[stdout.length - 2]).to.not.equal("\n"); expect(stdout[stdout.length - 1]).to.equal("\n"); }); it("should contain information about THELOUNGE_HOME env var", function() { // Mock `log.raw` to extract its effect into a concatenated string let stdout = ""; log.raw = TestUtil.mockLogger((str) => stdout += str); Utils.extraHelp(); expect(stdout).to.include("THELOUNGE_HOME"); }); }); describe(".parseConfigOptions", function() { describe("when it's the first option given", function() { it("should return nothing when passed an invalid config", function() { expect(Utils.parseConfigOptions("foo")).to.be.undefined; }); it("should correctly parse boolean values", function() { expect(Utils.parseConfigOptions("foo=true")).to.deep.equal({foo: true}); expect(Utils.parseConfigOptions("foo=false")).to.deep.equal({foo: false}); }); it("should correctly parse empty strings", function() { expect(Utils.parseConfigOptions("foo=")).to.deep.equal({foo: ""}); }); it("should correctly parse null values", function() { expect(Utils.parseConfigOptions("foo=null")).to.deep.equal({foo: null}); }); it("should correctly parse undefined values", function() { expect(Utils.parseConfigOptions("foo=undefined")) .to.deep.equal({foo: undefined}); }); it("should correctly parse array values", function() { expect(Utils.parseConfigOptions("foo=[bar,true]")) .to.deep.equal({foo: ["bar", true]}); expect(Utils.parseConfigOptions("foo=[bar, true]")) .to.deep.equal({foo: ["bar", true]}); }); it("should correctly parse empty array values", function() { expect(Utils.parseConfigOptions("foo=[]")) .to.deep.equal({foo: []}); }); it("should correctly parse values that contain `=` sign", function() { expect(Utils.parseConfigOptions("foo=bar=42")) .to.deep.equal({foo: "bar=42"}); }); it("should correctly parse keys using dot-notation", function() { expect(Utils.parseConfigOptions("foo.bar=value")) .to.deep.equal({foo: {bar: "value"}}); }); it("should correctly parse keys using array-notation", function() { expect(Utils.parseConfigOptions("foo[0]=value")) .to.deep.equal({foo: ["value"]}); }); }); describe("when some options have already been parsed", function() { it("should not modify existing options when passed an invalid config", function() { const memo = {foo: "bar"}; expect(Utils.parseConfigOptions("foo", memo)).to.equal(memo); }); it("should combine a new option with previously parsed ones", function() { expect(Utils.parseConfigOptions("bar=false", {foo: true})) .to.deep.equal({foo: true, bar: false}); }); it("should maintain existing properties of a nested object", function() { expect(Utils.parseConfigOptions("foo.bar=true", {foo: {baz: false}})) .to.deep.equal({foo: {bar: true, baz: false}}); }); it("should maintain existing entries of an array", function() { expect(Utils.parseConfigOptions("foo[1]=baz", {foo: ["bar"]})) .to.deep.equal({foo: ["bar", "baz"]}); }); describe("when given the same key multiple times", function() { let originalWarn; beforeEach(function() { originalWarn = log.warn; }); afterEach(function() { log.warn = originalWarn; }); it("should not override options", function() { log.warn = () => {}; expect(Utils.parseConfigOptions("foo=baz", {foo: "bar"})) .to.deep.equal({foo: "bar"}); }); it("should display a warning", function() { let warning = ""; log.warn = TestUtil.mockLogger((str) => warning += str); Utils.parseConfigOptions("foo=bar", {foo: "baz"}); expect(warning).to.include("foo was already specified"); }); }); }); }); });