Repository: sjbarag/brs
Pull request #413 (2020-05-13 17:14:18)

feat(stdlib): implement Font component
# Change Summary
This implements the `<Font>` component.


Patch:
diff --git a/src/brsTypes/BrsType.ts b/src/brsTypes/BrsType.ts
index 09b57b3..c7ff1b3 100644
--- a/src/brsTypes/BrsType.ts
+++ b/src/brsTypes/BrsType.ts
@@ -129,6 +129,7 @@ export function getBrsValueFromFieldType(type: string, value?: string): BrsType
         case "assocarray":
             returnValue = BrsInvalid.Instance;
             break;
+        case "uri":
         case "string":
             returnValue = value ? new BrsString(value) : new BrsString("");
             break;
diff --git a/src/brsTypes/components/ComponentFactory.ts b/src/brsTypes/components/ComponentFactory.ts
index d683878..7192947 100644
--- a/src/brsTypes/components/ComponentFactory.ts
+++ b/src/brsTypes/components/ComponentFactory.ts
@@ -1,10 +1,11 @@
-import { RoSGNode, Group, Rectangle, Label } from "..";
+import { RoSGNode, Group, Rectangle, Label, Font } from "..";
 
 export enum BrsComponentName {
     Node = "Node",
     Group = "Group",
     Rectangle = "Rectangle",
     Label = "Label",
+    Font = "Font",
 }
 
 // TODO: update with more components as they're implemented.
@@ -23,6 +24,8 @@ export class ComponentFactory {
                 return new Rectangle([], name);
             case BrsComponentName.Label:
                 return new Label([], name);
+            case BrsComponentName.Font:
+                return new Font([], name);
             default:
                 return;
         }
diff --git a/src/brsTypes/components/Font.ts b/src/brsTypes/components/Font.ts
new file mode 100644
index 0000000..7d2971d
--- /dev/null
+++ b/src/brsTypes/components/Font.ts
@@ -0,0 +1,17 @@
+import { RoSGNode, FieldModel } from "./RoSGNode";
+import { AAMember } from "./RoAssociativeArray";
+
+export class Font extends RoSGNode {
+    readonly defaultFields: FieldModel[] = [
+        { name: "uri", type: "uri" },
+        { name: "size", type: "integer", value: "1" },
+        { name: "fallbackGlyph", type: "string" },
+    ];
+
+    constructor(members: AAMember[] = [], readonly name: string = "Font") {
+        super([], name);
+
+        this.registerDefaultFields(this.defaultFields);
+        this.registerInitializedFields(members);
+    }
+}
diff --git a/src/brsTypes/index.ts b/src/brsTypes/index.ts
index 2a310c9..60ef23a 100644
--- a/src/brsTypes/index.ts
+++ b/src/brsTypes/index.ts
@@ -17,6 +17,9 @@ import { Callable } from "./Callable";
 import { BrsComponent } from "./components/BrsComponent";
 import { RoString } from "./components/RoString";
 import { BrsInterface } from "./BrsInterface";
+import { RoSGNode } from "./components/RoSGNode";
+import { Font } from "./components/Font";
+import { Group } from "./components/Group";
 
 export * from "./BrsType";
 export * from "./Int32";
@@ -42,6 +45,7 @@ export * from "./components/RoSGNode";
 export * from "./components/Group";
 export * from "./components/Rectangle";
 export * from "./components/Label";
+export * from "./components/Font";
 
 /**
  * Determines whether or not the given value is a number.


Test patch:
diff --git a/test/brsTypes/components/Font.test.js b/test/brsTypes/components/Font.test.js
new file mode 100644
index 0000000..e27470a
--- /dev/null
+++ b/test/brsTypes/components/Font.test.js
@@ -0,0 +1,23 @@
+const brs = require("brs");
+const { Font } = brs.types;
+
+describe("Font", () => {
+    describe("stringification", () => {
+        it("inits a new Font component", () => {
+            let group = new Font();
+
+            expect(group.toString()).toEqual(
+                `<Component: roSGNode:Font> =
+{
+    change: <UNINITIALIZED>
+    focusable: false
+    focusedchild: invalid
+    id: 
+    uri: 
+    size: 1
+    fallbackglyph: 
+}`
+            );
+        });
+    });
+});
diff --git a/test/e2e/BrsComponents.test.js b/test/e2e/BrsComponents.test.js
index 263a5c0..29bd100 100644
--- a/test/e2e/BrsComponents.test.js
+++ b/test/e2e/BrsComponents.test.js
@@ -434,4 +434,24 @@ describe("end to end brightscript functions", () => {
             "5.5",
         ]);
     });
+
+    test("components/Font.brs", async () => {
+        outputStreams.root = __dirname + "/resources";
+        await execute([resourceFile("components", "Font.brs")], outputStreams);
+
+        expect(allArgs(outputStreams.stdout.write).filter(arg => arg !== "\n")).toEqual([
+            "font node type:",
+            "Font",
+            "font node uri:",
+            "",
+            "font node size:",
+            "1",
+            "font node fallbackGlyph:",
+            "",
+            "font as child size:",
+            "56",
+            "font as child uri:",
+            "font/as/child/uri",
+        ]);
+    });
 });
diff --git a/test/e2e/resources/components/ComponentsAsChildren.xml b/test/e2e/resources/components/ComponentsAsChildren.xml
index 580036f..1507a11 100644
--- a/test/e2e/resources/components/ComponentsAsChildren.xml
+++ b/test/e2e/resources/components/ComponentsAsChildren.xml
@@ -9,6 +9,9 @@
                 numLines="10"
                 wrap="true"
                 lineSpacing="5.5" />
+            <Font id="font"
+                size="56"
+                uri="font/as/child/uri" />
         </Group>
     </children>
 </component>
diff --git a/test/e2e/resources/components/Font.brs b/test/e2e/resources/components/Font.brs
new file mode 100644
index 0000000..271e130
--- /dev/null
+++ b/test/e2e/resources/components/Font.brs
@@ -0,0 +1,12 @@
+sub Main()
+    font = createObject("roSGNode", "Font")
+    print "font node type:" type(font)
+    print "font node uri:" font.uri
+    print "font node size:" font.size
+    print "font node fallbackGlyph:" font.fallbackGlyph
+
+    parent = createObject("roSGNode", "ComponentsAsChildren")
+    fontAsChild = parent.findNode("font")
+    print "font as child size:" fontAsChild.size
+    print "font as child uri:" fontAsChild.uri
+end sub