Explorar o código

add tests for File and test based operations. Only unzip to go

d98762625 %!s(int64=6) %!d(string=hai) anos
pai
achega
b8cb7e9ba8

+ 8 - 0
src/node/File.mjs

@@ -42,6 +42,14 @@ class File {
     get size() {
         return this.data.length;
     }
+
+    /**
+     * Return lastModified as Date
+     */
+    get lastModifiedDate() {
+        return new Date(this.lastModified);
+    }
+
 }
 
 export default File;

+ 0 - 6
src/node/config/excludedOperations.mjs

@@ -13,12 +13,6 @@ export default  [
     "Label",
     "Comment",
 
-    // Exclude file ops until HTML5 File Object can be mimicked
-    // "Tar",
-    // "Untar",
-    "Unzip",
-    "Zip",
-
     // esprima doesn't work in .mjs
     "JavaScriptBeautify",
     "JavaScriptMinify",

+ 1 - 0
tests/node/index.mjs

@@ -30,6 +30,7 @@ global.ENVIRONMENT_IS_WEB = function() {
 import TestRegister from "../lib/TestRegister";
 import "./tests/nodeApi";
 import "./tests/ops";
+import "./tests/File";
 
 const testStatus = {
     allTestsPassing: true,

+ 20 - 0
tests/node/tests/File.mjs

@@ -0,0 +1,20 @@
+import assert from "assert";
+import it from "../assertionHandler";
+import TestRegister from "../../lib/TestRegister";
+import File from "../../../src/node/File";
+
+TestRegister.addApiTests([
+    it("File: should exist", () => {
+        assert(File);
+    }),
+
+    it("File: Should have same properties as DOM File object", () => {
+        const uint8Array = new Uint8Array(Buffer.from("hello"));
+        const file = new File([uint8Array], "name.txt");
+        assert.equal(file.name, "name.txt");
+        assert(typeof file.lastModified, "number");
+        assert(file.lastModifiedDate instanceof Date);
+        assert.equal(file.size, uint8Array.length);
+        assert.equal(file.type, "text/plain");
+    }),
+]);

+ 1 - 1
tests/node/tests/nodeApi.mjs

@@ -387,7 +387,7 @@ TestRegister.addApiTests([
 
     it("Operation arguments: should be accessible from operation object if op has array arg", () => {
         assert.ok(chef.toCharcode.argOptions);
-        assert.equal(chef.unzip.argOptions, undefined);
+        assert.deepEqual(chef.unzip.argOptions, {});
     }),
 
     it("Operation arguments: should have key for each array-based argument in operation", () => {

+ 46 - 0
tests/node/tests/ops.mjs

@@ -35,6 +35,9 @@ import {
 } from "../../../src/node/index";
 import chef from "../../../src/node/index";
 import TestRegister from "../../lib/TestRegister";
+import File from "../../../src/node/File";
+
+global.File = File;
 
 TestRegister.addApiTests([
 
@@ -971,5 +974,48 @@ ExifImageWidth: 57
 ExifImageHeight: 57`);
     }),
 
+    it("Tar", () => {
+        const tarred = chef.tar("some file content", {
+            filename: "test.txt"
+        });
+        assert.strictEqual(tarred.type, 7);
+        assert.strictEqual(tarred.value.size, 2048);
+        assert.strictEqual(tarred.value.data.toString().substr(0, 8), "test.txt");
+    }),
+
+    it("Untar", () => {
+        const tarred = chef.tar("some file content", {
+            filename: "filename.txt",
+        });
+        const untarred = chef.untar(tarred);
+        assert.strictEqual(untarred.type, 8);
+        assert.strictEqual(untarred.value.length, 1);
+        assert.strictEqual(untarred.value[0].name, "filename.txt");
+        assert.strictEqual(untarred.value[0].data.toString(), "some file content");
+    }),
+
+    it("Zip", () => {
+        const zipped = chef.zip("some file content", {
+            filename: "sample.zip",
+            comment: "added",
+            operaringSystem: "Unix",
+        });
+
+        assert.strictEqual(zipped.type, 7);
+        assert.equal(zipped.value.data.toString().indexOf("sample.zip"), 30);
+        assert.equal(zipped.value.data.toString().indexOf("added"), 122);
+    }),
+
+    // it("Unzip", () => {
+    //     const zipped = chef.zip("some file content", {
+    //         filename: "zipped.zip",
+    //         comment: "zippy",
+    //     });
+    //     const unzipped = chef.unzip(zipped);
+
+    //     assert.equal(unzipped.type, 8);
+    //     assert.equal(unzipped.value = "zipped.zip");
+    // }),
+
 ]);