Skip to content

Commit

Permalink
addresses regression that prevents stubCall allowing non-function ret…
Browse files Browse the repository at this point in the history
…urn values (#283)
  • Loading branch information
georgejecook authored Jun 15, 2024
1 parent 9c584e0 commit 9b04568
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 4 deletions.
27 changes: 27 additions & 0 deletions bsc-plugin/src/lib/rooibos/MockUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.sayhello
__stubOrMockResult = __stubFunction(a1, a2)
return __stubOrMockResult
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("sayhello")
value = __stubs_globalAa.__globalStubs.sayhello
return value
end if
print "hello"
end function
Expand Down Expand Up @@ -122,6 +125,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.sayhello
__stubOrMockResult = __stubFunction(a1, a2)
return __stubOrMockResult
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("sayhello")
value = __stubs_globalAa.__globalStubs.sayhello
return value
end if
print "hello"
end function
Expand Down Expand Up @@ -162,6 +168,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.redlines_setrulerlines
__stubOrMockResult = __stubFunction(rulerLines)
return
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("redlines_setrulerlines")
value = __stubs_globalAa.__globalStubs.redlines_setrulerlines
return
end if
For Each line In rulerLines.Items()
RedLines_AddLine(line.key, line.value.position, line.value.coords, m.node, m.childMap)
Expand All @@ -177,6 +186,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.redlines_addline
__stubOrMockResult = __stubFunction(id, position, coords, node, childMap)
return __stubOrMockResult
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("redlines_addline")
value = __stubs_globalAa.__globalStubs.redlines_addline
return value
end if
line = CreateObject("roSGNode", "Rectangle")
line.setField("id", id)
Expand Down Expand Up @@ -213,6 +225,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.sayhello
__stubOrMockResult = __stubFunction(a1, a2)
return
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("sayhello")
value = __stubs_globalAa.__globalStubs.sayhello
return
end if
print "hello"
end sub
Expand Down Expand Up @@ -250,6 +265,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.person_utils_sayhello
__stubOrMockResult = __stubFunction(a1, a2)
return __stubOrMockResult
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("person_utils_sayhello")
value = __stubs_globalAa.__globalStubs.person_utils_sayhello
return value
end if
print "hello"
end function
Expand Down Expand Up @@ -287,6 +305,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.person_utils_sayhello
__stubOrMockResult = __stubFunction(a1, a2)
return
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("person_utils_sayhello")
value = __stubs_globalAa.__globalStubs.person_utils_sayhello
return
end if
print "hello"
end sub
Expand Down Expand Up @@ -378,6 +399,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.beings_sayhello
__stubOrMockResult = __stubFunction()
return __stubOrMockResult
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("beings_sayhello")
value = __stubs_globalAa.__globalStubs.beings_sayhello
return value
end if
print "hello2"
end function
Expand All @@ -391,6 +415,9 @@ describe('MockUtil', () => {
__stubFunction = __stubs_globalAa.__globalStubs.sayhello
__stubOrMockResult = __stubFunction()
return __stubOrMockResult
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("sayhello")
value = __stubs_globalAa.__globalStubs.sayhello
return value
end if
print "hello3"
end function
Expand Down
3 changes: 3 additions & 0 deletions bsc-plugin/src/lib/rooibos/MockUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export class MockUtil {
__stubFunction = ${globalAaName}.${storageName}.${methodName}
${resultName} = __stubFunction(${paramNames})
return${requiresReturnValue ? ` ${resultName}` : ''}
else if ${globalAaName}?.${storageName} <> invalid and ${globalAaName}.${storageName}.doesExist("${methodName}")
value = ${globalAaName}.${storageName}.${methodName}
return${requiresReturnValue ? ` value` : ''}
end if
`;
const astCodeToInject = Parser.parse(template).ast.statements;
Expand Down
6 changes: 6 additions & 0 deletions bsc-plugin/src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,9 @@ describe('RooibosPlugin', () => {
__stubFunction = __stubs_globalAa.__globalStubs.sayhello
__stubOrMockResult = __stubFunction(firstName, lastName)
return __stubOrMockResult
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("sayhello")
value = __stubs_globalAa.__globalStubs.sayhello
return value
end if
print firstName + " " + lastName
end function
Expand Down Expand Up @@ -1151,6 +1154,9 @@ describe('RooibosPlugin', () => {
__stubFunction = __stubs_globalAa.__globalStubs.utils_sayhello
__stubOrMockResult = __stubFunction(firstName, lastName)
return __stubOrMockResult
else if __stubs_globalAa?.__globalStubs <> invalid and __stubs_globalAa.__globalStubs.doesExist("utils_sayhello")
value = __stubs_globalAa.__globalStubs.utils_sayhello
return value
end if
print firstName + " " + lastName
end function
Expand Down
14 changes: 14 additions & 0 deletions bsconfig-prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"rootDir": "framework/src",
"files": ["manifest", "source/**/*.*", "components/**/*.*"],
"autoImportComponentScript": true,
"createPackage": false,
"stagingFolderPath": "dist",
"diagnosticFilters": [
{
"src": "**/roku_modules/**/*.*",
"codes": [1107, 1009]
}
],
"plugins": ["@rokucommunity/bslint"]
}
2 changes: 1 addition & 1 deletion framework/src/source/BaseTestSuite.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ namespace rooibos
end if

if not type(stubOrReturnValue).endsWith("Function")
throw "Did not provide a stub function"
' throw "Did not provide a stub function"
end if

' Store the stub on the component scope
Expand Down
4 changes: 2 additions & 2 deletions tests/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"stopDebuggerOnAppExit": true,
"enableVariablesPanel": false,
"injectRaleTrackerTask": false,
"enableDebugProtocol": false
"enableDebugProtocol": true
},
{
"name": "watch tests",
Expand Down Expand Up @@ -61,7 +61,7 @@
"stopDebuggerOnAppExit": true,
"enableVariablesPanel": false,
"injectRaleTrackerTask": false,
"enableDebugProtocol": false
"enableDebugProtocol": true,
}
]
}
2 changes: 1 addition & 1 deletion tests/bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"rooibos": {
"showOnlyFailures": true,
"catchCrashes": true,
"catchCrashes": false,
"lineWidth": 70,
"failFast": false,
"sendHomeOnFinish": false,
Expand Down
34 changes: 34 additions & 0 deletions tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions tests/src/source/Charlies-Issues.spec.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace tests
@suite
class MockExample extends rooibos.BaseTestSuite

protected override function setUp()
super.setUp()
end function

protected override function beforeEach()
m.SUT = SomeModel()
end function

protected override function afterEach()
m.cleanMocks()
m.cleanStubs()
m.SUT = invalid
end function

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@describe("Example Tests on SomeModel public methods")
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

@it("globalMockExample1_passing")
function _()
m.stubCall(getManifestValue, function(key) : return "stubCall hardcoded title" : end function)
m.SUT.globalMockExample()
m.assertEqual(m.SUT.appTitle, "stubCall hardcoded title")
end function

@it("globalMockExample2_crashing_due_to_returnValue")
@params("stubCall dynamic title")
function _(manifestValue as string)
m.stubCall(getManifestValue, manifestValue)
m.SUT.globalMockExample()
m.assertEqual(m.SUT.appTitle, manifestValue)
end function

@it("stubExample1_passing")
@params({ configSetVia_config: true }, { configSetViaStub: true })
function _(config1 as object, config2 as object)
m.SUT._config = config1
m.stub(m.SUT, "_getConfig", config2)
m.SUT.setUpdatedConfig1()
expectedKeyNameConfig2 = config2.keys()[0]
resultKeyName = m.SUT.updatedConfig.keys()[0]
m.assertEqual(expectedKeyNameConfig2, resultKeyName)
end function

@ignore("this is not supported - we do not support mocking singleton objects between test runs")
@it("stubExample2_crashing_stub_from_stubExample1_persisting")
@params({ configSetVia_config: true }, { configSetViaStub: true })
function _(config1 as object, config2 as object)
m.SUT._config = config1
m.SUT.setUpdatedConfig2()
expectedKeyNameConfig1 = config1.keys()[0]
resultKeyName = m.SUT.updatedConfig.keys()[0]
m.assertEqual(expectedKeyNameConfig1, resultKeyName)
end function

end class
end namespace
34 changes: 34 additions & 0 deletions tests/src/source/SomeModel.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function SomeModel() as object
if (m._someModel = invalid)

obj = {}
obj.appTitle = ""
obj._config = invalid
obj.updatedConfig = invalid

obj.globalMockExample = sub()
appTitle = getManifestValue("title")
m.appTitle = appTitle
end sub

obj.setUpdatedConfig1 = sub()
m.updatedConfig = m._getConfig()
end sub

obj.setUpdatedConfig2 = sub()
m.updatedConfig = m._getConfig()
end sub

obj._getConfig = function() as object
return m._config
end function

m._someModel = obj
end if

return m._someModel
end function

function getManifestValue(key as string) as string
return createObject("roAppInfo").getValue(key)
end function

0 comments on commit 9b04568

Please sign in to comment.