-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stdlib): Implement roAppInfo (#643)
fixes #537
- Loading branch information
Showing
9 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { BrsBoolean, BrsString, BrsValue, ValueKind } from "../BrsType"; | ||
import { BrsComponent } from "./BrsComponent"; | ||
import { BrsType } from ".."; | ||
import { Callable, StdlibArgument } from "../Callable"; | ||
import { Interpreter } from "../../interpreter"; | ||
|
||
export class RoAppInfo extends BrsComponent implements BrsValue { | ||
readonly kind = ValueKind.Object; | ||
|
||
constructor() { | ||
super("roAppInfo"); | ||
|
||
this.registerMethods({ | ||
ifAppInfo: [ | ||
this.getID, | ||
this.isDev, | ||
this.getVersion, | ||
this.getTitle, | ||
this.getSubtitle, | ||
this.getDevID, | ||
this.getValue, | ||
], | ||
}); | ||
} | ||
|
||
toString(parent?: BrsType) { | ||
return "<Component: roAppInfo>"; | ||
} | ||
|
||
equalTo(other: BrsType) { | ||
return BrsBoolean.False; | ||
} | ||
|
||
/** | ||
* Originally returns the app's channel ID or 'dev' for sideloaded applications. | ||
* @returns {string} - 'dev' | ||
*/ | ||
private getID = new Callable("getID", { | ||
signature: { | ||
args: [], | ||
returns: ValueKind.String, | ||
}, | ||
impl: (interpreter: Interpreter) => { | ||
return new BrsString("dev"); | ||
}, | ||
}); | ||
|
||
/** | ||
* Returns true if the application is sideloaded, i.e. the channel ID is "dev". | ||
* @returns {boolean} - true | ||
*/ | ||
private isDev = new Callable("isDev", { | ||
signature: { | ||
args: [], | ||
returns: ValueKind.Boolean, | ||
}, | ||
impl: (interpreter: Interpreter) => { | ||
return BrsBoolean.True; | ||
}, | ||
}); | ||
|
||
/** | ||
* Returns the conglomerate version number from the manifest, as formatted major_version + minor_version + build_version. | ||
* @returns {string} - Channel version number. e.g. "1.2.3" or ".." if not available | ||
*/ | ||
private getVersion = new Callable("getVersion", { | ||
signature: { | ||
args: [], | ||
returns: ValueKind.String, | ||
}, | ||
impl: (interpreter: Interpreter) => { | ||
let manifest = interpreter.manifest; | ||
|
||
let version = ["major_version", "minor_version", "build_version"] | ||
.map((key) => manifest.get(key)) | ||
.filter((key) => !!key) | ||
.join("."); | ||
version = version !== "" ? version : ".."; | ||
|
||
return new BrsString(version); | ||
}, | ||
}); | ||
|
||
/** | ||
* Returns the title value from the manifest. | ||
* @returns {string} - title of the channel | ||
*/ | ||
private getTitle = new Callable("getTitle", { | ||
signature: { | ||
args: [], | ||
returns: ValueKind.String, | ||
}, | ||
impl: (interpreter: Interpreter) => { | ||
let title = interpreter.manifest.get("title"); | ||
|
||
return title != null ? new BrsString(title.toString()) : new BrsString(""); | ||
}, | ||
}); | ||
|
||
/** | ||
* Returns the subtitle value from the manifest. | ||
* @returns {string} - possible subtitle configuration | ||
*/ | ||
private getSubtitle = new Callable("getSubtitle", { | ||
signature: { | ||
args: [], | ||
returns: ValueKind.String, | ||
}, | ||
impl: (interpreter: Interpreter) => { | ||
let subtitle = interpreter.manifest.get("subtitle"); | ||
|
||
return subtitle != null ? new BrsString(subtitle.toString()) : new BrsString(""); | ||
}, | ||
}); | ||
|
||
/** | ||
* Returns the app's developer ID, or the keyed developer ID, if the application is sideloaded. | ||
* @returns {string} - "34c6fceca75e456f25e7e99531e2425c6c1de443" (default value for sideloaded channels) | ||
*/ | ||
private getDevID = new Callable("getDevID", { | ||
signature: { | ||
args: [], | ||
returns: ValueKind.String, | ||
}, | ||
impl: (interpreter: Interpreter) => { | ||
// return default value for sideloaded channels | ||
return new BrsString("34c6fceca75e456f25e7e99531e2425c6c1de443"); | ||
}, | ||
}); | ||
|
||
/** | ||
* Returns the named manifest value, or an empty string if the entry is does not exist. | ||
*/ | ||
private getValue = new Callable("getValue", { | ||
signature: { | ||
args: [new StdlibArgument("key", ValueKind.String)], | ||
returns: ValueKind.String, | ||
}, | ||
impl: (interpreter: Interpreter, key: BrsString) => { | ||
let value = interpreter.manifest.get(key.value); | ||
|
||
return value != null ? new BrsString(value.toString()) : new BrsString(""); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
const brs = require("brs"); | ||
const { BrsBoolean, BrsString, RoAppInfo } = brs.types; | ||
const { Interpreter } = require("../../../lib/interpreter"); | ||
|
||
describe("RoAppInfo", () => { | ||
let interpreter; | ||
|
||
beforeEach(() => { | ||
interpreter = new Interpreter(); | ||
interpreter.manifest = new Map(); | ||
}); | ||
|
||
describe("stringification", () => { | ||
it("lists stringified value", () => { | ||
let appInfo = new RoAppInfo(); | ||
expect(appInfo.toString()).toEqual(`<Component: roAppInfo>`); | ||
}); | ||
}); | ||
|
||
describe("getID", () => { | ||
it("returns default value for sideloaded app", () => { | ||
let appInfo = new RoAppInfo(); | ||
let getID = appInfo.getMethod("getID"); | ||
|
||
expect(getID).toBeTruthy(); | ||
expect(getID.call(interpreter)).toEqual(new BrsString("dev")); | ||
}); | ||
}); | ||
|
||
describe("isDev", () => { | ||
it("returns true for sideloaded app", () => { | ||
let appInfo = new RoAppInfo(); | ||
let isDev = appInfo.getMethod("isDev"); | ||
|
||
expect(isDev).toBeTruthy(); | ||
expect(isDev.call(interpreter)).toEqual(BrsBoolean.True); | ||
}); | ||
}); | ||
|
||
describe("getVersion", () => { | ||
it("returns version based on data in the manifest file", () => { | ||
interpreter.manifest = new Map([ | ||
["major_version", "4"], | ||
["minor_version", "3"], | ||
["build_version", "0"], | ||
]); | ||
let appInfo = new RoAppInfo(); | ||
let getVersion = appInfo.getMethod("getVersion"); | ||
|
||
expect(getVersion).toBeTruthy(); | ||
expect(getVersion.call(interpreter)).toEqual(new BrsString("4.3.0")); | ||
}); | ||
|
||
it("returns two dots if sub versions aren't defined", () => { | ||
let appInfo = new RoAppInfo(); | ||
let getVersion = appInfo.getMethod("getVersion"); | ||
|
||
expect(getVersion).toBeTruthy(); | ||
expect(getVersion.call(interpreter)).toEqual(new BrsString("..")); | ||
}); | ||
}); | ||
|
||
describe("getTitle", () => { | ||
it("returns title based on data in the manifest file", () => { | ||
interpreter.manifest = new Map([["title", "Some title"]]); | ||
let appInfo = new RoAppInfo(); | ||
let getTitle = appInfo.getMethod("getTitle"); | ||
|
||
expect(getTitle).toBeTruthy(); | ||
expect(getTitle.call(interpreter)).toEqual(new BrsString("Some title")); | ||
}); | ||
|
||
it("returns an empty string if title isn't defined", () => { | ||
let appInfo = new RoAppInfo(); | ||
let getTitle = appInfo.getMethod("getTitle"); | ||
|
||
expect(getTitle).toBeTruthy(); | ||
expect(getTitle.call(interpreter)).toEqual(new BrsString("")); | ||
}); | ||
}); | ||
|
||
describe("getSubtitle", () => { | ||
it("returns subtitle based on data in the manifest file", () => { | ||
interpreter.manifest = new Map([["subtitle", "Some message"]]); | ||
let appInfo = new RoAppInfo(); | ||
let getSubtitle = appInfo.getMethod("getSubtitle"); | ||
|
||
expect(getSubtitle).toBeTruthy(); | ||
expect(getSubtitle.call(interpreter)).toEqual(new BrsString("Some message")); | ||
}); | ||
|
||
it("returns an empty string if subtitle isn't defined", () => { | ||
let appInfo = new RoAppInfo(); | ||
let getSubtitle = appInfo.getMethod("getSubtitle"); | ||
|
||
expect(getSubtitle).toBeTruthy(); | ||
expect(getSubtitle.call(interpreter)).toEqual(new BrsString("")); | ||
}); | ||
}); | ||
|
||
describe("getDevID", () => { | ||
it("returns default value for sideloaded app", () => { | ||
let appInfo = new RoAppInfo(); | ||
let getDevID = appInfo.getMethod("getDevID"); | ||
|
||
expect(getDevID).toBeTruthy(); | ||
expect(getDevID.call(interpreter)).toEqual( | ||
new BrsString("34c6fceca75e456f25e7e99531e2425c6c1de443") | ||
); | ||
}); | ||
}); | ||
|
||
describe("getValue", () => { | ||
it("returns value based on data in the manifest file", () => { | ||
interpreter.manifest = new Map([["some_field", "Some text"]]); | ||
let appInfo = new RoAppInfo(); | ||
let getValue = appInfo.getMethod("getValue"); | ||
|
||
expect(getValue).toBeTruthy(); | ||
expect(getValue.call(interpreter, new BrsString("some_field"))).toEqual( | ||
new BrsString("Some text") | ||
); | ||
}); | ||
|
||
it("returns an empty string if field isn't defined", () => { | ||
let appInfo = new RoAppInfo(); | ||
let getValue = appInfo.getMethod("getValue"); | ||
|
||
expect(getValue).toBeTruthy(); | ||
expect(getValue.call(interpreter, new BrsString("nonexistentfield"))).toEqual( | ||
new BrsString("") | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
sub main() | ||
appInfo = createObject("roAppInfo") | ||
|
||
print appInfo.getID() | ||
print appInfo.isDev() | ||
print appInfo.getVersion() | ||
print appInfo.getTitle() | ||
print appInfo.getSubtitle() | ||
print appInfo.getDevID() | ||
print appInfo.getValue("some_field") | ||
|
||
end sub |
Oops, something went wrong.