mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
Fix tests not checking that validators are called
This commit is contained in:
1 file changed
+59
@@ -11,6 +11,7 @@ describe("lib/config/classes", function() {
|
|||||||
describe("Config", function() {
|
describe("Config", function() {
|
||||||
class TestConfig extends lib.Config {
|
class TestConfig extends lib.Config {
|
||||||
static defaultAccess = ["local", "remote"];
|
static defaultAccess = ["local", "remote"];
|
||||||
|
static validatorCalls = [];
|
||||||
|
|
||||||
static migrations(config) {
|
static migrations(config) {
|
||||||
if (config.hasOwnProperty("test.migration")) {
|
if (config.hasOwnProperty("test.migration")) {
|
||||||
@@ -48,6 +49,7 @@ describe("lib/config/classes", function() {
|
|||||||
initialValue: 1,
|
initialValue: 1,
|
||||||
dependsOn: ["test.base", "test.objDependent"],
|
dependsOn: ["test.base", "test.objDependent"],
|
||||||
validator(value, config) {
|
validator(value, config) {
|
||||||
|
TestConfig.validatorCalls.push(["test.dependent", value]);
|
||||||
if (value < config.get("test.base")) {
|
if (value < config.get("test.base")) {
|
||||||
throw new Error("dependent must be >= base");
|
throw new Error("dependent must be >= base");
|
||||||
}
|
}
|
||||||
@@ -62,6 +64,7 @@ describe("lib/config/classes", function() {
|
|||||||
initialValue: {},
|
initialValue: {},
|
||||||
dependsOn: ["test.base"],
|
dependsOn: ["test.base"],
|
||||||
validator(value, config) {
|
validator(value, config) {
|
||||||
|
TestConfig.validatorCalls.push(["test.objDependent", value]);
|
||||||
if (value.base && value.base !== config.get("test.base")) {
|
if (value.base && value.base !== config.get("test.base")) {
|
||||||
throw new Error("base prop must match test.base");
|
throw new Error("base prop must match test.base");
|
||||||
}
|
}
|
||||||
@@ -71,6 +74,7 @@ describe("lib/config/classes", function() {
|
|||||||
type: "number",
|
type: "number",
|
||||||
optional: true,
|
optional: true,
|
||||||
validator(value) {
|
validator(value) {
|
||||||
|
TestConfig.validatorCalls.push(["test.validated", value]);
|
||||||
if (value !== null && value < 0) {
|
if (value !== null && value < 0) {
|
||||||
throw new Error("must be positive");
|
throw new Error("must be positive");
|
||||||
}
|
}
|
||||||
@@ -80,6 +84,7 @@ describe("lib/config/classes", function() {
|
|||||||
type: "object",
|
type: "object",
|
||||||
initialValue: {},
|
initialValue: {},
|
||||||
validator(value) {
|
validator(value) {
|
||||||
|
TestConfig.validatorCalls.push(["test.objValidated", value]);
|
||||||
if (value && value.bad !== undefined) {
|
if (value && value.bad !== undefined) {
|
||||||
throw new Error("bad key");
|
throw new Error("bad key");
|
||||||
}
|
}
|
||||||
@@ -715,6 +720,7 @@ describe("lib/config/classes", function() {
|
|||||||
describe(".set()", function() {
|
describe(".set()", function() {
|
||||||
let testInstance;
|
let testInstance;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
TestConfig.validatorCalls = [];
|
||||||
testInstance = new TestConfig("local", {
|
testInstance = new TestConfig("local", {
|
||||||
"test.enum": "a",
|
"test.enum": "a",
|
||||||
"test.test": "blah",
|
"test.test": "blah",
|
||||||
@@ -750,24 +756,38 @@ describe("lib/config/classes", function() {
|
|||||||
it("should run field validator successfully", function() {
|
it("should run field validator successfully", function() {
|
||||||
testInstance.set("test.validated", 5);
|
testInstance.set("test.validated", 5);
|
||||||
assert.equal(testInstance.get("test.validated"), 5);
|
assert.equal(testInstance.get("test.validated"), 5);
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.validated", 5], // set test.validated
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should throw if validator fails", function() {
|
it("should throw if validator fails", function() {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => testInstance.set("test.validated", -1),
|
() => testInstance.set("test.validated", -1),
|
||||||
new lib.InvalidValue("Failed validation of test.validated: must be positive")
|
new lib.InvalidValue("Failed validation of test.validated: must be positive")
|
||||||
);
|
);
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.validated", -1], // set test.validated
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should validate dependent fields", function() {
|
it("should validate dependent fields", function() {
|
||||||
testInstance.set("test.dependent", 10);
|
testInstance.set("test.dependent", 10);
|
||||||
testInstance.set("test.base", 5);
|
testInstance.set("test.base", 5);
|
||||||
assert.equal(testInstance.get("test.dependent"), 10);
|
assert.equal(testInstance.get("test.dependent"), 10);
|
||||||
assert.equal(testInstance.get("test.base"), 5);
|
assert.equal(testInstance.get("test.base"), 5);
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.dependent", 10], // set test.dependent
|
||||||
|
["test.dependent", 10], // set test.base
|
||||||
|
["test.objDependent", {}], // set test.base
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should throw if dependent validation fails", function() {
|
it("should throw if dependent validation fails", function() {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => testInstance.set("test.base", 5),
|
() => testInstance.set("test.base", 5),
|
||||||
new lib.InvalidValue("Failed validation of dependent test.dependent: dependent must be >= base")
|
new lib.InvalidValue("Failed validation of dependent test.dependent: dependent must be >= base")
|
||||||
);
|
);
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.dependent", 1], // set test.base
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -823,6 +843,7 @@ describe("lib/config/classes", function() {
|
|||||||
describe(".setProp()", function() {
|
describe(".setProp()", function() {
|
||||||
let testInstance;
|
let testInstance;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
TestConfig.validatorCalls = [];
|
||||||
testInstance = new TestConfig("local", {
|
testInstance = new TestConfig("local", {
|
||||||
"test.enum": "a",
|
"test.enum": "a",
|
||||||
"test.test": "blah",
|
"test.test": "blah",
|
||||||
@@ -859,16 +880,26 @@ describe("lib/config/classes", function() {
|
|||||||
it("should run validator when setting object property", function() {
|
it("should run validator when setting object property", function() {
|
||||||
testInstance.setProp("test.objValidated", "good", true);
|
testInstance.setProp("test.objValidated", "good", true);
|
||||||
assert.deepEqual(testInstance.get("test.objValidated"), { good: true });
|
assert.deepEqual(testInstance.get("test.objValidated"), { good: true });
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.objValidated", { good: true }], // setProp test.objValidated good
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should throw if object validator fails", function() {
|
it("should throw if object validator fails", function() {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => testInstance.setProp("test.objValidated", "bad", true),
|
() => testInstance.setProp("test.objValidated", "bad", true),
|
||||||
new lib.InvalidValue("Failed validation of test.objValidated: bad key")
|
new lib.InvalidValue("Failed validation of test.objValidated: bad key")
|
||||||
);
|
);
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.objValidated", { bad: true }], // setProp test.objValidated bad
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should validate dependent fields", function() {
|
it("should validate dependent fields", function() {
|
||||||
testInstance.setProp("test.objDependent", "base", 1);
|
testInstance.setProp("test.objDependent", "base", 1);
|
||||||
assert.deepEqual(testInstance.get("test.objDependent"), { base: 1 });
|
assert.deepEqual(testInstance.get("test.objDependent"), { base: 1 });
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.objDependent", { base: 1 }], // setProp test.objDependent base
|
||||||
|
["test.dependent", 1], // setProp test.objDependent base
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should throw if dependent validation fails", function() {
|
it("should throw if dependent validation fails", function() {
|
||||||
testInstance.set("test.base", 0);
|
testInstance.set("test.base", 0);
|
||||||
@@ -876,6 +907,12 @@ describe("lib/config/classes", function() {
|
|||||||
() => testInstance.setProp("test.objDependent", "base", 0),
|
() => testInstance.setProp("test.objDependent", "base", 0),
|
||||||
new lib.InvalidValue("Failed validation of dependent test.dependent: objDependent cannot be 0")
|
new lib.InvalidValue("Failed validation of dependent test.dependent: objDependent cannot be 0")
|
||||||
);
|
);
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.dependent", 1], // set test.base
|
||||||
|
["test.objDependent", {}], // set test.base
|
||||||
|
["test.objDependent", { base: 0 }], // setProp test.objDependent base
|
||||||
|
["test.dependent", 1], // setProp test.objDependent base
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -949,6 +986,7 @@ describe("lib/config/classes", function() {
|
|||||||
describe(".commitStaging()", function() {
|
describe(".commitStaging()", function() {
|
||||||
let testInstance;
|
let testInstance;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
TestConfig.validatorCalls = [];
|
||||||
testInstance = new TestConfig("local");
|
testInstance = new TestConfig("local");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -962,6 +1000,13 @@ describe("lib/config/classes", function() {
|
|||||||
assert.equal(testInstance.get("alpha.foo"), null);
|
assert.equal(testInstance.get("alpha.foo"), null);
|
||||||
testInstance.commitStaging();
|
testInstance.commitStaging();
|
||||||
assert.equal(testInstance.get("alpha.foo"), "value");
|
assert.equal(testInstance.get("alpha.foo"), "value");
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
// commitStaging, all values are checked once
|
||||||
|
["test.dependent", 1],
|
||||||
|
["test.objDependent", {}],
|
||||||
|
["test.validated", null],
|
||||||
|
["test.objValidated", {}],
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should throw if staged validation fails", function() {
|
it("should throw if staged validation fails", function() {
|
||||||
testInstance.stage("test.validated", -1);
|
testInstance.stage("test.validated", -1);
|
||||||
@@ -970,6 +1015,12 @@ describe("lib/config/classes", function() {
|
|||||||
() => testInstance.commitStaging(),
|
() => testInstance.commitStaging(),
|
||||||
new lib.InvalidValue("Failed validation of test.validated: must be positive")
|
new lib.InvalidValue("Failed validation of test.validated: must be positive")
|
||||||
);
|
);
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
// commitStaging, all values are checked once
|
||||||
|
["test.dependent", 1],
|
||||||
|
["test.objDependent", {}],
|
||||||
|
["test.validated", -1], // throws, so no fourth item
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should commit stages values with dependent validation", function() {
|
it("should commit stages values with dependent validation", function() {
|
||||||
assert.throws(() => testInstance.set("test.base", 10));
|
assert.throws(() => testInstance.set("test.base", 10));
|
||||||
@@ -979,6 +1030,14 @@ describe("lib/config/classes", function() {
|
|||||||
testInstance.commitStaging();
|
testInstance.commitStaging();
|
||||||
assert.equal(testInstance.get("test.base"), 10);
|
assert.equal(testInstance.get("test.base"), 10);
|
||||||
assert.equal(testInstance.get("test.dependent"), 15);
|
assert.equal(testInstance.get("test.dependent"), 15);
|
||||||
|
assert.deepEqual(TestConfig.validatorCalls, [
|
||||||
|
["test.dependent", 1], // set test.base
|
||||||
|
// commitStaging, all values are checked once
|
||||||
|
["test.dependent", 15],
|
||||||
|
["test.objDependent", {}],
|
||||||
|
["test.validated", null],
|
||||||
|
["test.objValidated", {}],
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
it("should notify fieldChanged listeners", function() {
|
it("should notify fieldChanged listeners", function() {
|
||||||
const changes = [];
|
const changes = [];
|
||||||
|
|||||||
Reference in new issue
Block a user