Skip to content

Commit

Permalink
feat(stdlib): Implement ifSGNodeField.hasField() (#368)
Browse files Browse the repository at this point in the history
The implementation is simpler than the existing _getField_ method, which
returns a value, while this simply returns a boolean if the field is
present.  

There is at least one bug in hasField in the official implementation
that I'm aware of, where calling hasField() with specific strings will
actually create the field, but this is a bug and I don't think we should
match that for compatibility sake.  

resolves #367
  • Loading branch information
strattonbrazil authored Jan 29, 2020
1 parent 8b28716 commit ca54d7b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/brsTypes/components/RoSGNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class RoSGNode extends BrsComponent implements BrsValue, BrsIterable {
this.addfield,
this.addfields,
this.getfield,
this.hasfield,
this.observefield,
this.removefield,
this.setfield,
Expand Down Expand Up @@ -498,6 +499,19 @@ export class RoSGNode extends BrsComponent implements BrsValue, BrsIterable {
},
});

/** Returns true if the field exists */
private hasfield = new Callable("hasfield", {
signature: {
args: [new StdlibArgument("fieldname", ValueKind.String)],
returns: ValueKind.Boolean,
},
impl: (interpreter: Interpreter, fieldname: BrsString) => {
return this.fields.has(fieldname.value.toLowerCase())
? BrsBoolean.True
: BrsBoolean.False;
},
});

/** Registers a callback to be executed when the value of the field changes */
private observefield = new Callable("observefield", {
signature: {
Expand Down
13 changes: 13 additions & 0 deletions test/brsTypes/components/RoSGNode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,19 @@ describe("RoSGNode", () => {
});
});

describe("hasfield", () => {
it("returns presence of a field", () => {
let node = new RoSGNode([{ name: new BrsString("foo"), value: new Int32(17) }]);

let hasField = node.getMethod("hasfield");
let result1 = hasField.call(interpreter, new BrsString("foo"));
expect(result1).toEqual(new BrsBoolean(true));

let result2 = hasField.call(interpreter, new BrsString("bar"));
expect(result2).toEqual(new BrsBoolean(false));
});
});

describe("observefield", () => {
it("adds an observer", () => {
let node = new RoSGNode([
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/BrsComponents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ describe("end to end brightscript functions", () => {
"hello",
"field3 in node now is: ",
"false",
"field3 present? ",
"true",
"fieldほ present? ",
"false",
"callback 1 called",
"callback 2 called",
"field 3 updated",
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/resources/components/roSGNode.brs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ sub main()
print "field1 in node now is: " node1.getField("field1") ' => hello
print "field3 in node now is: " node1.getField("field3") ' => false

print "field3 present? " node1.hasField("field3")
print "fieldほ present? " node1.hasField("fieldほ")

node1.observeField("field1", "onCB1Called")
node1.observeField("field1", "onCB2Called")
node1.observeField("field2", "onField2Cb") ' This doesn't get called since field was removed
Expand Down

0 comments on commit ca54d7b

Please sign in to comment.