add dish translation tests for node

This commit is contained in:
d98762625 2019-04-29 17:09:01 +01:00
parent aeb08caf77
commit 65a3897f87
7 changed files with 147 additions and 24 deletions

View File

@ -208,7 +208,7 @@ class Dish {
valid() {
switch (this.type) {
case Dish.BYTE_ARRAY:
if (!(this.value instanceof Array)) {
if (!(this.value instanceof Uint8Array) && !(this.value instanceof Array)) {
return false;
}

View File

@ -1039,8 +1039,7 @@ class Utils {
if (!Utils.isNode()) {
throw new TypeError("Browser environment cannot support readFileSync");
}
// Resist using node's Buffer.buffer here - this yields a 8192 length byteArray
// regardless of the length of the buffer.
const arrayBuffer = Uint8Array.from(file.data);
return arrayBuffer.buffer;
}

View File

@ -19,7 +19,6 @@ class DishFile extends DishTranslationType {
static toArrayBuffer() {
DishFile.checkForValue(this.value);
if (Utils.isNode()) {
// TODO
this.value = Utils.readFileSync(this.value);
} else {
return new Promise((resolve, reject) => {

View File

@ -4,14 +4,13 @@
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
import DishString from "./DishString";
import Utils from "../Utils";
/**
* Translation methods for HTML Dishes
*/
class DishHTML extends DishTranslationType {
class DishHTML extends DishString {
/**
* convert the given value to a ArrayBuffer
@ -22,13 +21,6 @@ class DishHTML extends DishTranslationType {
this.value = this.value ? Utils.strToArrayBuffer(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : new ArrayBuffer;
}
/**
* convert the given value from a ArrayBuffer
* @param {boolean} notUTF8
*/
static fromArrayBuffer(notUTF8) {
DishString.fromByteArray(this.value, notUTF8);
}
}
export default DishHTML;

View File

@ -5,6 +5,7 @@
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils.mjs";
/**
* Translation methods for ListFile Dishes
@ -16,6 +17,10 @@ class DishListFile extends DishTranslationType {
*/
static toArrayBuffer() {
DishListFile.checkForValue(this.value);
if (Utils.isNode()) {
this.value = this.value.map(file => Uint8Array.from(file.data));
}
this.value = DishListFile.concatenateTypedArrays(...this.value).buffer;
}

View File

@ -1,9 +1,13 @@
import assert from "assert";
import it from "../assertionHandler";
import TestRegister from "../../lib/TestRegister";
import { Dish, toBase32, SHA3 } from "../../../src/node/index";
import fs from "fs";
import BigNumber from "bignumber.js";
import { Dish, toBase32, SHA3 } from "../../../src/node/index";
import File from "../../../src/node/File";
import TestRegister from "../../lib/TestRegister";
TestRegister.addApiTests([
it("Composable Dish: Should have top level Dish object", () => {
assert.ok(Dish);
@ -61,8 +65,135 @@ TestRegister.addApiTests([
assert.strictEqual(result.toString(), "493e8136b759370a415ef2cf2f7a69690441ff86592aba082bc2e2e0");
}),
// it("Dish translation: ArrayBuffer to ArrayBuffer", () => {
// const dish = new Dish();
// }),
it("Dish translation: ArrayBuffer to ArrayBuffer", () => {
const dish = new Dish(new ArrayBuffer(10), 4);
dish.get("array buffer");
assert.strictEqual(dish.value.byteLength, 10);
assert.strictEqual(dish.type, 4);
}),
it("Dish translation: ArrayBuffer and String", () => {
const dish = new Dish("some string", 1);
dish.get("array buffer");
assert.strictEqual(dish.type, 4);
assert.deepStrictEqual(dish.value, new ArrayBuffer(11));
assert.deepEqual(dish.value.byteLength, 11);
dish.get("string");
assert.strictEqual(dish.type, 1);
assert.strictEqual(dish.value, "some string");
}),
it("Dish translation: ArrayBuffer and number", () => {
const dish = new Dish(100, 2);
dish.get(4);
assert.strictEqual(dish.type, 4);
assert.deepStrictEqual(dish.value, new ArrayBuffer(10));
assert.strictEqual(dish.value.byteLength, 3);
// Check the data in ArrayBuffer represents 100 as a string.
const view = new DataView(dish.value, 0);
assert.strictEqual(String.fromCharCode(view.getUint8(0), view.getUint8(1), view.getUint8(2)), "100");
dish.get("number");
assert.strictEqual(dish.type, 2);
assert.strictEqual(dish.value, 100);
}),
it("Dish translation: ArrayBuffer and byte array", () => {
const dish = new Dish(new Uint8Array([1, 2, 3]), 0);
dish.get(4);
// Check intermediate value
const check = new Uint8Array(dish.value);
assert.deepEqual(check, [1, 2, 3]);
// Check converts back OK
dish.get(0);
assert.deepEqual(dish.value, new Uint8Array([1, 2, 3]));
}),
it("Dish translation: ArrayBuffer and HTML", () => {
const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<a href="https://github.com">Click here</a>
<script src="script.js"></script>
</body>
</html>`.replace(/\n|\s{4}/g, ""); //remove newlines, tabs
const dish = new Dish(html, Dish.HTML);
dish.get(4);
dish.get(3);
assert.strictEqual(dish.value, "Click here");
}),
it("Dish translation: ArrayBuffer and BigNumber", () => {
const number = BigNumber(4001);
const dish = new Dish(number, Dish.BIG_NUMBER);
dish.get(Dish.ARRAY_BUFFER);
assert.deepStrictEqual(dish.value, new ArrayBuffer(10));
assert.strictEqual(dish.value.byteLength, 4);
// Check the data in ArrayBuffer represents 4001 as a string.
const view = new DataView(dish.value, 0);
assert.strictEqual(String.fromCharCode(view.getUint8(0), view.getUint8(1), view.getUint8(2), view.getUint8(3)), "4001");
dish.get(5);
assert.deepStrictEqual(dish.value, number);
}),
it("Dish translation: ArrayBuffer and JSON", () => {
const jsonString = "{\"a\": 123455, \"b\": { \"aa\": [1,2,3]}}";
const dish = new Dish(JSON.parse(jsonString), Dish.JSON);
dish.get(Dish.ARRAY_BUFFER);
dish.get(Dish.JSON);
assert.deepStrictEqual(dish.value, JSON.parse(jsonString));
}),
it("Dish translation: ArrayBuffer and File", () => {
const file = new File("abcd", "unknown");
const dish = new Dish(file, Dish.FILE);
dish.get(Dish.ARRAY_BUFFER);
assert.deepStrictEqual(dish.value, new ArrayBuffer(10));
assert.strictEqual(dish.value.byteLength, 4);
// Check the data in ArrayBuffer represents "abcd"
const view = new DataView(dish.value, 0);
assert.strictEqual(String.fromCharCode(view.getUint8(0), view.getUint8(1), view.getUint8(2), view.getUint8(3)), "abcd");
dish.get(Dish.FILE);
assert.deepStrictEqual(dish.value.data, file.data);
assert.strictEqual(dish.value.name, file.name);
assert.strictEqual(dish.value.type, file.type);
// Do not test lastModified
}),
it("Dish translation: ArrayBuffer and ListFile", () => {
const file1 = new File("abcde", "unknown");
const file2 = new File("fghijk", "unknown");
const dish = new Dish([file1, file2], Dish.LIST_FILE);
dish.get(Dish.ARRAY_BUFFER);
assert.deepStrictEqual(dish.value, new ArrayBuffer(10));
assert.strictEqual(dish.value.byteLength, 11);
dish.get(Dish.LIST_FILE);
const dataArray = new Uint8Array(dish.value[0].data);
// cant store chars in a Uint8Array, so make it a normal one.
const actual = Array.prototype.slice.call(dataArray).map(c => String.fromCharCode(c)).join("");
assert.strictEqual(actual, "abcdefghijk");
}),
]);

View File

@ -15,9 +15,8 @@ import it from "../assertionHandler";
import chef from "../../../src/node/index";
import OperationError from "../../../src/core/errors/OperationError";
import NodeDish from "../../../src/node/NodeDish";
import fs from "fs";
import { toBase32, Dish, SHA3 } from "../../../src/node/index";
import { toBase32} from "../../../src/node/index";
import TestRegister from "../../lib/TestRegister";
TestRegister.addApiTests([
@ -325,8 +324,6 @@ TestRegister.addApiTests([
assert.strictEqual(result.toString(), "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something");
}),
it("Excluded operations: throw a sensible error when you try and call one", () => {
try {
chef.fork();