Skip to content

Instantly share code, notes, and snippets.

@roryl
Last active June 6, 2016 12:55
Show Gist options
  • Save roryl/08a89aa59430b797cdb4 to your computer and use it in GitHub Desktop.
Save roryl/08a89aa59430b797cdb4 to your computer and use it in GitHub Desktop.
Local Scopes - Examples showing local scopes in functions
component {
function myFunc(){
var myVar = "foo";
echo(myVar); //outputs "foo"
}
function otherFunc(){
echo(myVar); //errors for undefined variable
}
}
component {
function myFunc(){
var myVar = "foo";
echo(local.myVar); //outputs foo
}
}
component {
function myFunc(){
variables.myVar = "foo";
echo(myVar); //outputs "foo"
}
function otherFunc(){
echo(myVar); //outputs "foo";
echo(variables.myVar); //same as above, outputs "foo" because myVar was in the variables scope which is global to this component
}
}
/**
* My xUnit Test
*/
component extends="testbox.system.BaseSpec"{
/*********************************** LIFE CYCLE Methods ***********************************/
// executes before all test cases
function beforeTests(){
}
// executes after all test cases
function afterTests(){
}
// executes before every test case
function setup( currentMethod ){
}
// executes after every test case
function teardown( currentMethod ){
}
/*********************************** TEST CASES BELOW ***********************************/
// Remember that test cases MUST start or end with the keyword 'test'
function localScopeTest(){
var test = new component_local();
savecontent variable="out"{
test.myFunc();
}
expect(out).toBe("foo");
expect(function(){test.otherfunc()}).toThrow();
}
function variablesScopeTest(){
var test = new component_variables();
savecontent variable="out"{
test.myFunc();
}
expect(out).toBe("foo");
savecontent variable="out"{
test.otherFunc();
}
expect(out).toBe("foofoo");
}
function localStructTest(){
var test = new component_local_struct();
savecontent variable="out"{
test.myFunc();
}
expect(out).toBe("foo");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment