Repository: sjbarag/brs
Pull request #529 (2020-09-04 18:11:26)

feat(scenegraph): Implement ifSGNodeField#getFields()
We forgot about this one!



Patch:
diff --git a/src/brsTypes/components/RoSGNode.ts b/src/brsTypes/components/RoSGNode.ts
index a51d1ee..57bebef 100644
--- a/src/brsTypes/components/RoSGNode.ts
+++ b/src/brsTypes/components/RoSGNode.ts
@@ -300,6 +300,7 @@ export class RoSGNode extends BrsComponent implements BrsValue, BrsIterable {
                 this.addfield,
                 this.addfields,
                 this.getfield,
+                this.getfields,
                 this.hasfield,
                 this.observefield,
                 this.unobservefield,
@@ -766,6 +767,30 @@ export class RoSGNode extends BrsComponent implements BrsValue, BrsIterable {
         },
     });
 
+    /** Returns the names and values of all the fields in the node. */
+    private getfields = new Callable("getfields", {
+        signature: {
+            args: [],
+            returns: ValueKind.Object,
+        },
+        impl: (interpreter: Interpreter) => {
+            let packagedFields: AAMember[] = [];
+
+            this.fields.forEach((field, name) => {
+                if (field.isHidden()) {
+                    return;
+                }
+
+                packagedFields.push({
+                    name: new BrsString(name),
+                    value: field.getValue(),
+                });
+            });
+
+            return new RoAssociativeArray(packagedFields);
+        },
+    });
+
     /** Returns true if the field exists */
     protected hasfield = new Callable("hasfield", {
         signature: {


Test patch:
diff --git a/test/brsTypes/components/RoSGNode.test.js b/test/brsTypes/components/RoSGNode.test.js
index 43ccfd6..e1e76ec 100644
--- a/test/brsTypes/components/RoSGNode.test.js
+++ b/test/brsTypes/components/RoSGNode.test.js
@@ -506,6 +506,31 @@ describe("RoSGNode", () => {
             });
         });
 
+        describe("getFields", () => {
+            it("returns all visible fields", () => {
+                let node = new RoSGNode([]);
+
+                let getFields = node.getMethod("getfields");
+                expect(getFields).toBeTruthy();
+
+                let result = getFields.call(interpreter);
+                let expected = new RoAssociativeArray([
+                    { name: new BrsString("change"), value: new RoAssociativeArray([]) },
+                    { name: new BrsString("focusable"), value: BrsBoolean.False },
+                    { name: new BrsString("focusedChild"), value: BrsInvalid.Instance },
+                    { name: new BrsString("id"), value: new BrsString("") },
+                ]);
+                result.elements.forEach((value, name) => {
+                    if (value instanceof RoAssociativeArray) {
+                        expect(value.elements).toBeInstanceOf(Map);
+                        expect(value.elements).toEqual(expected.elements.get(name).elements);
+                    } else {
+                        expect(value).toEqual(expected.elements.get(name));
+                    }
+                });
+            });
+        });
+
         describe("hasfield", () => {
             it("returns presence of a field", () => {
                 let node = new RoSGNode([{ name: new BrsString("foo"), value: new Int32(17) }]);
diff --git a/test/e2e/BrsComponents.test.js b/test/e2e/BrsComponents.test.js
index b6532e5..615d389 100644
--- a/test/e2e/BrsComponents.test.js
+++ b/test/e2e/BrsComponents.test.js
@@ -152,6 +152,8 @@ describe("end to end brightscript functions", () => {
             "false",
             "field3 in node now is: ",
             "true",
+            "number of fields, via getFields().count(): ",
+            "2",
             "field1 in node now is: ",
             "hello",
             "field3 in node now is: ",
diff --git a/test/e2e/resources/components/roSGNode/roSGNode.brs b/test/e2e/resources/components/roSGNode/roSGNode.brs
index f07dcd7..b7ac97c 100644
--- a/test/e2e/resources/components/roSGNode/roSGNode.brs
+++ b/test/e2e/resources/components/roSGNode/roSGNode.brs
@@ -31,6 +31,8 @@ sub init()
     node1.setField("field3", true)
     print "field3 in node now is: " node1.getField("field3")      ' => true
 
+    print "number of fields, via getFields().count(): " node1.getFields().count() ' => 2
+
     node1.setFields({ field1: "hello", field3: false })
     print "field1 in node now is: " node1.getField("field1")      ' => hello
     print "field3 in node now is: " node1.getField("field3")      ' => false