Repository: sjbarag/brs
Pull request #636 (2021-04-07 11:52:34)

add isEmpty method to roString
fix for #575 issue 
what were done:
- added `isEmpty` to `ifStringOps` interface (is undocumented in roku doc)
- added unit test for imlementation
- added e2e test.


Patch:
diff --git a/src/brsTypes/components/RoString.ts b/src/brsTypes/components/RoString.ts
index e537c04..f0d6d28 100644
--- a/src/brsTypes/components/RoString.ts
+++ b/src/brsTypes/components/RoString.ts
@@ -46,6 +46,7 @@ export class RoString extends BrsComponent implements BrsValue, Comparable, Unbo
                 this.decodeUri,
                 this.encodeUriComponent,
                 this.decodeUriComponent,
+                this.isEmpty,
             ],
             ifToStr: [this.toStr],
         });
@@ -414,6 +415,17 @@ export class RoString extends BrsComponent implements BrsValue, Comparable, Unbo
         },
     });
 
+    /** returns whether string is empty or not */
+    private isEmpty = new Callable("isEmpty", {
+        signature: {
+            args: [],
+            returns: ValueKind.Boolean,
+        },
+        impl: (_interpreter) => {
+            return BrsBoolean.from(this.intrinsic.value.length === 0);
+        },
+    });
+
     /**
      * Encode the specified string with escape sequences for reserved Uniform Resource Identifier
      * (URI) characters.


Test patch:
diff --git a/test/brsTypes/components/RoString.test.js b/test/brsTypes/components/RoString.test.js
index c3314db..e9ab020 100644
--- a/test/brsTypes/components/RoString.test.js
+++ b/test/brsTypes/components/RoString.test.js
@@ -637,6 +637,22 @@ describe("RoString", () => {
                 );
             });
         });
+
+        describe("isEmpty", () => {
+            it("check if empty string is empty", () => {
+                let s = new RoString(new BrsString(""));
+                let len = s.getMethod("isEmpty");
+                expect(len).toBeInstanceOf(Callable);
+                expect(len.call(interpreter)).toBe(BrsBoolean.True);
+            });
+
+            it("check if filled string is not empty", () => {
+                let s = new RoString(new BrsString("<3"));
+                let len = s.getMethod("isEmpty");
+                expect(len).toBeInstanceOf(Callable);
+                expect(len.call(interpreter)).toBe(BrsBoolean.False);
+            });
+        });
     });
 
     describe("ifToStr", () => {
diff --git a/test/e2e/BrsComponents.test.js b/test/e2e/BrsComponents.test.js
index 35d5557..648bef9 100644
--- a/test/e2e/BrsComponents.test.js
+++ b/test/e2e/BrsComponents.test.js
@@ -185,6 +185,8 @@ describe("end to end brightscript functions", () => {
             "b", // split("/")[1]
             "%F0%9F%90%B6", // dog emoji, uri-encoded
             "🐶", // uri-encoded dog emoji, decoded
+            "true", // isEmpty for empty string
+            "false", // isEmpty for filled string
         ]);
     });
 
diff --git a/test/e2e/resources/components/roString.brs b/test/e2e/resources/components/roString.brs
index 0f4c98b..d80e4ab 100644
--- a/test/e2e/resources/components/roString.brs
+++ b/test/e2e/resources/components/roString.brs
@@ -26,4 +26,6 @@ sub main()
     u = "🐶"
     print u.encodeUriComponent() ' => %F0%9F%90%B6
     print "%F0%9F%90%B6".decodeUriComponent() ' => 🐶
+    print "".isEmpty() ' => true
+    print "<3".isEmpty() ' => false
 end sub
\ No newline at end of file