1
0
mirror of synced 2024-11-15 02:37:40 +01:00

update tests to reflect new API behaviour

This commit is contained in:
d98762625 2018-06-06 17:29:30 +01:00
parent 45d2fbc5fc
commit 33f2c89716
4 changed files with 2106 additions and 2132 deletions

4102
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -62,9 +62,7 @@ import "./tests/operations/SetIntersection";
import "./tests/operations/SetUnion";
import "./tests/operations/SymmetricDifference";
import "./tests/nodeApi/nodeApi";
import "./tests/nodeApi/translateTo";
let allTestsPassing = true;
const testStatusCounts = {

View File

@ -13,6 +13,10 @@
import assert from "assert";
import it from "../assertionHandler";
import chef from "../../../src/node/index";
import OperationError from "../../../src/core/errors/OperationError";
import SyncDish from "../../../src/node/SyncDish";
import { toBase32 } from "../../../src/node/index";
import TestRegister from "../../TestRegister";
TestRegister.addApiTests([
@ -23,9 +27,13 @@ TestRegister.addApiTests([
assert(!chef.randomFunction);
}),
it("should have an async/await api", async () => {
it("should export other functions at top level", () => {
assert(toBase32);
}),
it("should be synchronous", () => {
try {
const result = await chef.toBase32("input");
const result = chef.toBase32("input");
assert.notEqual("something", result);
} catch (e) {
// shouldnt reach here
@ -41,45 +49,63 @@ TestRegister.addApiTests([
}
}),
it("should have a callback API", async () => {
await chef.toBase32("something", (err, result) => {
if (err) {
assert(false);
} else {
assert.equal("ONXW2ZLUNBUW4ZY=", result);
}
});
}),
it("should handle errors in callback API", async () => {
await chef.setUnion("1", (err, result) => {
if (err) {
assert(true);
return;
}
it("should not catch Errors", () => {
try {
chef.setUnion("1");
assert(false);
});
} catch (e) {
assert(e instanceof OperationError);
}
}),
it("should accept arguments in object format for operations", async () => {
const result = await chef.setUnion("1 2 3 4:3 4 5 6", {
it("should accept arguments in object format for operations", () => {
const result = chef.setUnion("1 2 3 4:3 4 5 6", {
itemDelimiter: " ",
sampleDelimiter: ":"
});
assert.equal(result, "1 2 3 4 5 6");
assert.equal(result.value, "1 2 3 4 5 6");
}),
it("should accept just some of the optional arguments being overriden", async () => {
const result = await chef.setIntersection("1 2 3 4 5\\n\\n3 4 5", {
it("should accept just some of the optional arguments being overriden", () => {
const result = chef.setIntersection("1 2 3 4 5\\n\\n3 4 5", {
itemDelimiter: " ",
});
assert.equal(result, "3 4 5");
assert.equal(result.value, "3 4 5");
}),
it("should accept no override arguments and just use the default values", async () => {
const result = await chef.powerSet("1,2,3");
assert.equal(result, "\n3\n2\n1\n2,3\n1,3\n1,2\n1,2,3\n");
})
it("should accept no override arguments and just use the default values", () => {
const result = chef.powerSet("1,2,3");
assert.equal(result.value, "\n3\n2\n1\n2,3\n1,3\n1,2\n1,2,3\n");
}),
it("should return an object with a .to method", () => {
const result = chef.toBase32("input");
assert(result.to);
assert.equal(result.to("string"), "NFXHA5LU");
}),
it("should return an object with a .get method", () => {
const result = chef.toBase32("input");
assert(result.get);
assert.equal(result.get("string"), "NFXHA5LU");
}),
it("should return a SyncDish", () => {
const result = chef.toBase32("input");
assert(result instanceof SyncDish);
}),
it("should coerce to a string as you expect", () => {
const result = chef.fromBase32(chef.toBase32("something"));
assert.equal(String(result), "something");
// This kind of coercion uses toValue
assert.equal(""+result, "NaN");
}),
it("should coerce to a number as you expect", () => {
const result = chef.fromBase32(chef.toBase32("32"));
assert.equal(3 + result, 35);
}),
]);

View File

@ -1,50 +0,0 @@
/* eslint no-console: 0 */
/**
* nodeApi.js
*
* Test node api translateTo function
*
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import assert from "assert";
import it from "../assertionHandler";
import chef from "../../../src/node/index";
import TestRegister from "../../TestRegister";
import BigNumber from "bignumber.js";
TestRegister.addApiTests([
it("should have a translateTo function", () => {
assert(chef.translateTo);
}),
it("should translate to number from string", async () => {
const hex = await chef.toHex("1");
const translated = await chef.translateTo(hex, "number");
assert.equal(31, translated);
}),
it("should translate from string to byte array", async () => {
const str = await chef.toBase32("something");
const translated = await chef.translateTo(str, "bytearray");
assert.deepEqual(translated, [79, 78, 88, 87, 50, 90, 76, 85, 78, 66, 85, 87, 52, 90, 89, 61]);
}),
it("should convert a number to a big numner", async () => {
const result = await chef.translateTo(31, "bignumber");
assert.deepEqual(result, BigNumber(31));
}),
it("should be symmetric", async () => {
const result = await chef.setUnion("1 2 3 4:3 4 5 6", {
itemDelimiter: " ",
sampleDelimiter: ":"
});
const bytearray = await chef.translateTo(result, "bytearray");
const translated = await chef.translateTo(bytearray, "string");
assert.equal(translated, "1 2 3 4 5 6");
})
]);