Repository: w3c/webidl2.js
Pull request #245 (2018-12-02 12:20:56)

Add unit tests for writer template functions


Patch:
diff --git a/lib/webidl2.js b/lib/webidl2.js
index afe1810..572dba8 100644
--- a/lib/webidl2.js
+++ b/lib/webidl2.js
@@ -631,10 +631,12 @@
       }
 
       const str = consume(STR) || error("No value for default");
-      str.value = str.value.slice(1, -1);
       trivia.value = str.trivia;
-      str.trivia = trivia;
-      return str;
+      return {
+        type: "string",
+        value: str.value.slice(1, -1),
+        trivia
+      };
     }
 
     function const_() {


Test patch:
diff --git a/test/invalid/baseline/dict-field-unterminated.txt b/test/invalid/baseline/dict-field-unterminated.txt
new file mode 100644
index 0000000..ef2ff6b
--- /dev/null
+++ b/test/invalid/baseline/dict-field-unterminated.txt
@@ -0,0 +1,3 @@
+Syntax error at line 1, since `dictionary Nene`:
+ { DOMString cpp = "high" };
+                          ^ Unterminated dictionary member
diff --git a/test/invalid/idl/dict-field-unterminated.widl b/test/invalid/idl/dict-field-unterminated.widl
new file mode 100644
index 0000000..d241131
--- /dev/null
+++ b/test/invalid/idl/dict-field-unterminated.widl
@@ -0,0 +1,1 @@
+dictionary Nene { DOMString cpp = "high" };
diff --git a/test/writer.js b/test/writer.js
index 7428074..fb998f0 100644
--- a/test/writer.js
+++ b/test/writer.js
@@ -3,6 +3,7 @@
 const { collect } = require("./util/collect");
 const wp = require("..");
 const expect = require("expect");
+const webidl2 = require("..");
 
 describe("Rewrite and parses all of the IDLs to produce the same ASTs", () => {
   for (const test of collect("syntax")) {
@@ -14,3 +15,85 @@ describe("Rewrite and parses all of the IDLs to produce the same ASTs", () => {
     });
   }
 });
+
+describe("Writer template functions", () => {
+  function rewrite(text, templates) {
+    return webidl2.write(webidl2.parse(text), { templates });
+  }
+  function bracket(str) {
+    return `<${str}>`;
+  }
+  function flatten(array) {
+    return [].concat(...array.map(item => Array.isArray(item) ? flatten(item) : item));
+  }
+
+  it("wraps in array", () => {
+    const result = rewrite("interface X {};", {
+      wrap: items => flatten(items).filter(i => i)
+    });
+    expect(result).toEqual(["interface", " ", "X", " ", "{", "}", ";"]);
+  });
+
+  it("catches trivia", () => {
+    const result = rewrite("/* longcat is long */ interface X {};", {
+      trivia: bracket
+    });
+    expect(result).toBe("</* longcat is long */ >interface< >X< >{<>}<>;<>");
+  });
+
+  it("catches names", () => {
+    const result = rewrite("interface Momo { attribute long iro; };", {
+      name: bracket
+    });
+    expect(result).toBe("interface <Momo> { attribute long <iro>; };");
+  });
+
+  it("catches references", () => {
+    const result = rewrite("[Exposed=Window] interface Momo : Kudamono { attribute Promise<unsigned long> iro; };", {
+      reference: bracket
+    });
+    expect(result).toBe("[Exposed=<Window>] interface Momo : <Kudamono> { attribute <Promise><<unsigned long>> iro; };");
+  });
+
+  it("catches types", () => {
+    const result = rewrite("interface Momo { attribute Promise<unsigned long> iro; };", {
+      type: bracket
+    });
+    expect(result).toBe("interface Momo { attribute< Promise<unsigned long>> iro; };");
+  });
+
+  it("catches value literals", () => {
+    const result = rewrite("dictionary Nene { DOMString cpp = \"high\"; };", {
+      valueLiteral: bracket
+    });
+    expect(result).toBe("dictionary Nene { DOMString cpp = <\"high\">; };");
+  });
+
+  it("catches inheritances", () => {
+    const result = rewrite("dictionary Nene : Member { DOMString cpp = \"high\"; };", {
+      inheritance: bracket
+    });
+    expect(result).toBe("dictionary Nene : <Member> { DOMString cpp = \"high\"; };");
+  });
+
+  it("catches definitions", () => {
+    const result = rewrite("dictionary Nene { DOMString cpp = \"high\"; };", {
+      definition: bracket
+    });
+    expect(result).toBe("<dictionary Nene {< DOMString cpp = \"high\";> };>");
+  });
+
+  it("catches extended attributes", () => {
+    const result = rewrite("[Exposed=Window, Constructor] interface EagleJump { void aoba([Clamp] long work); };", {
+      extendedAttribute: bracket
+    });
+    expect(result).toBe("[<Exposed=Window>, <Constructor>] interface EagleJump { void aoba([<Clamp>] long work); };");
+  });
+
+  it("catches extended attribute references", () => {
+    const result = rewrite("[Exposed=Window, Constructor] interface EagleJump { void aoba([Clamp] long work); };", {
+      extendedAttributeReference: bracket
+    });
+    expect(result).toBe("[<Exposed>=Window, <Constructor>] interface EagleJump { void aoba([<Clamp>] long work); };");
+  });
+});