From f462f0d09d1880646c11cb3f9363a024576c7634 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 5 Feb 2021 10:18:18 -0800 Subject: [PATCH] Fix small issues with new wasm bindings --- lib/binding_web/binding.js | 2 +- lib/binding_web/test/language-test.js | 113 +++++++------------------- 2 files changed, 31 insertions(+), 84 deletions(-) diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index b2b3c73b..3d3be350 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -652,7 +652,7 @@ class Language { stringToUTF8(type, typeAddress, typeLength + 1); const result = C._ts_language_symbol_for_name(this[0], typeAddress, typeLength, named); C._free(typeAddress); - return result; + return result || null; } get nodeTypeCount() { diff --git a/lib/binding_web/test/language-test.js b/lib/binding_web/test/language-test.js index 691a7ae7..385b77ed 100644 --- a/lib/binding_web/test/language-test.js +++ b/lib/binding_web/test/language-test.js @@ -4,94 +4,41 @@ let JavaScript; describe("Language", () => { before(async () => ({ JavaScript } = await require("./helper"))); - describe(".fieldCount", () => { - it("returns a number", () => { - assert.equal(34, JavaScript.fieldCount); + describe(".fieldIdForName, .fieldNameForId", () => { + it("converts between the string and integer representations of fields", () => { + const nameId = JavaScript.fieldIdForName("name"); + const bodyId = JavaScript.fieldIdForName("body"); + + assert.isBelow(nameId, JavaScript.fieldCount); + assert.isBelow(bodyId, JavaScript.fieldCount); + assert.equal("name", JavaScript.fieldNameForId(nameId)); + assert.equal("body", JavaScript.fieldNameForId(bodyId)); + }); + + it("handles invalid inputs", () => { + assert.equal(null, JavaScript.fieldIdForName("namezzz")); + assert.equal(null, JavaScript.fieldNameForId(-1)); + assert.equal(null, JavaScript.fieldNameForId(10000)); }); }); - describe(".fieldIdForName", () => { - it("returns null, if not defined", () => { - const fieldName = "nonExistentFieldName"; - assert.equal(null, JavaScript.fieldIdForName(fieldName)); + describe(".idForNodeType, .nodeTypeForId, .nodeTypeIsNamed", () => { + it("converts between the string and integer representations of a node type", () => { + const exportStatementId = JavaScript.idForNodeType("export_statement", true); + const starId = JavaScript.idForNodeType("*", false); + + assert.isBelow(exportStatementId, JavaScript.nodeTypeCount); + assert.isBelow(starId, JavaScript.nodeTypeCount); + assert.equal(true, JavaScript.nodeTypeIsNamed(exportStatementId)) + assert.equal("export_statement", JavaScript.nodeTypeForId(exportStatementId)) + assert.equal(false, JavaScript.nodeTypeIsNamed(starId)) + assert.equal("*", JavaScript.nodeTypeForId(starId)) }); - it("returns a number, if defined", () => { - const fieldName = "decorator"; - assert.equal(12, JavaScript.fieldIdForName(fieldName)); - }); - }); - - describe(".fieldNameForId", () => { - it("returns null, if not defined", () => { - const fieldId = -1; - assert.equal(null, JavaScript.fieldNameForId(fieldId)); - }); - - it("returns a string, if defined", () => { - const fieldId = 12; - assert.equal("decorator", JavaScript.fieldNameForId(fieldId)); - }); - }); - - describe(".idForNodeType", () => { - it("returns a number", () => { - const type = "export_statement"; - const named = true; - assert.equal(125, JavaScript.idForNodeType(type, named)); - }); - }); - - describe(".nodeTypeCount", () => { - it("returns a number", () => { - assert.equal(239, JavaScript.nodeTypeCount); - }); - }); - - describe(".nodeTypeForId", () => { - it("returns null, if not defined", () => { - const typeId = -1; - assert.equal(null, JavaScript.nodeTypeForId(typeId)); - }); - - it("returns a string, if not defined", () => { - const typeId = 125; - assert.equal("export_statement", JavaScript.nodeTypeForId(typeId)); - }); - }); - - describe(".nodeTypeIsNamed", () => { - it("returns false, if node type is not named", () => { - const typeId = 4; - assert.equal("*", JavaScript.nodeTypeForId(typeId)); - assert.equal(false, JavaScript.nodeTypeIsNamed(typeId)); - }); - - it("returns true, if node type is named", () => { - const typeId = 125; - assert.equal("export_statement", JavaScript.nodeTypeForId(typeId)); - assert.equal(true, JavaScript.nodeTypeIsNamed(typeId)); - }); - }); - - describe(".nodeTypeIsVisible", () => { - it("returns false, if node type is not visible", () => { - let typeId; - typeId = 100; - assert.equal(false, JavaScript.nodeTypeIsVisible(typeId)); - typeId = 102; - assert.equal(false, JavaScript.nodeTypeIsVisible(typeId)); - }); - - it("returns true, if node type is visible", () => { - const typeId = 101; - assert.equal(true, JavaScript.nodeTypeIsVisible(typeId)); - }); - }); - - describe(".version", () => { - it("returns a number", () => { - assert.equal(12, JavaScript.version); + it("handles invalid inputs", () => { + assert.equal(null, JavaScript.nodeTypeForId(-1)); + assert.equal(null, JavaScript.nodeTypeForId(10000)); + assert.equal(null, JavaScript.idForNodeType("export_statement", false)); }); }); });