Last active
August 11, 2016 19:29
-
-
Save roryl/cc95907c4837ca032b45 to your computer and use it in GitHub Desktop.
Lucee Array Examples
This file contains hidden or 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
<cfscript> | |
myArray = []; | |
myArray.append("foo"); | |
myArray.append("bar"); | |
myArray.append("baz"); | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = []; | |
myArray.append("foo"); | |
myArray.append("bar"); | |
myArray.append("baz"); | |
dump(myArray); | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
theIndex = "1"; | |
echo(myArray[theIndex]); //otuputs "one" | |
echo(myArray["#theIndex#"]); //otuputs "one" | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
theIndex = "1"; | |
echo(myArray[theIndex]); //otuputs "one" | |
echo(myArray["#theIndex#"]); //otuputs "one" | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
myArray.each(function(item, index){ | |
echo(item & "was at index #index#"); //otuputs each array item "one was at index 1", "two was at index 2", then "three was at index 3" | |
}); | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
for(item in myArray){ | |
echo(item); //otuputs each array item "one", "two", then "three" | |
} | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
for(i=1; i <= myArray.len(); i++){ | |
echo(item); //otuputs each array item "one", "two", then "three" | |
} | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
loop array="#myArray#" item="foo" { | |
echo(foo); //otuputs each array item "one", "two", then "three" | |
} | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
loop array="#myArray#" item="foo" index="i" { | |
echo(foo & "this was at index #i#"); //otuputs each array item "one", "two", then "three" | |
} | |
</cfscript> |
This file contains hidden or 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
/** | |
* 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 checkAllSyntaxTest(){ | |
var files = directoryList(""); | |
for(file IN files){ | |
if(!file CONTAINS ".cfc"){ | |
include template="#getFileFromPath(file)#"; | |
} | |
} | |
} | |
} |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
myArray = []; | |
</cfscript> |
This file contains hidden or 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
// functionsToTest.cfm | |
public any function acceptArrayOfSamples(required Sample[] samples){ | |
return samples; | |
} | |
public Sample[] function returnArrayOfSamples(required array samples){ | |
return samples; | |
} | |
public any function acceptArrayOfStrings(required string[] strings){ | |
return strings; | |
} | |
public string[] function returnArrayOfStrings(required array strings){ | |
return strings; | |
} | |
public any function acceptArrayOfNumerics(required numeric[] numerics){ | |
return numerics; | |
} | |
public numeric[] function returnArrayOfNumerics(required array numerics){ | |
return numerics; | |
} | |
public any function acceptArrayOfStructs(required struct[] structs){ | |
return structs; | |
} | |
public struct[] function returnArrayOfStructs(required array structs){ | |
return structs; | |
} |
This file contains hidden or 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
// NotSample.cfc | |
component { | |
} |
This file contains hidden or 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
<cfscript> | |
myArray = ["one","two","three"]; | |
echo(myArray[1]); //otuputs "one" | |
echo(myArray[3]); //outputs "three" | |
</cfscript> |
This file contains hidden or 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
//Sample.cfc | |
component { | |
} |
This file contains hidden or 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
<cfscript> | |
string function test(){ | |
return 'foo'; | |
} | |
writeOutput(test()); //returns foo | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
string[] function test(){ | |
return ['foo']; | |
} | |
writeDump(test()); | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
string[] function test(string[] myStrings){ | |
return arguments.myStrings; | |
} | |
writeDump(test(['foor', 'bar'])); | |
</cfscript> |
This file contains hidden or 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
<cfscript> | |
string[] function test(){ | |
return 'foo'; | |
} | |
try { | |
writeDump(test()); //Throws error because it is not an array | |
} catch(any e){ | |
writeOutput(e.message); | |
} | |
</cfscript> |
This file contains hidden or 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
// SubSample.cfc | |
component extends="Sample" { | |
} |
This file contains hidden or 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
// TestArraysOfObjects.cfc | |
component extends="testbox.system.BaseSpec" { | |
include "./functionsToTest.cfm"; | |
public void function beforeTests(){ | |
variables.arrayOfSamples = [new Sample(), new Sample()]; | |
variables.arrayofSubSamples = [new SubSample(), new SubSample()]; | |
variables.arrayofNotSamples = [new NotSample(), new NotSample()]; | |
variables.arrayOfStrings = ["array", "of", "strings"]; | |
variables.arrayOfNumerics = [-1, 2.2, pi()]; | |
variables.arrayOfStructs = [{one="tahi"}, {two="rua"}, {three="toru"}, {four="wha"}]; | |
} | |
public any function acceptArrayOfSamples(required Sample[] samples){ | |
return samples; | |
} | |
public Sample[] function returnArrayOfSamples(required array samples){ | |
return samples; | |
} | |
public any function acceptArrayOfStrings(required string[] strings){ | |
return strings; | |
} | |
public string[] function returnArrayOfStrings(required array strings){ | |
return strings; | |
} | |
public any function acceptArrayOfNumerics(required numeric[] numerics){ | |
return numerics; | |
} | |
public numeric[] function returnArrayOfNumerics(required array numerics){ | |
return numerics; | |
} | |
public any function acceptArrayOfStructs(required struct[] structs){ | |
return structs; | |
} | |
public struct[] function returnArrayOfStructs(required array structs){ | |
return structs; | |
} | |
//----------------------------------------------- | |
public void function testAcceptArrayOfSamples(){ | |
acceptArrayOfSamples(arrayOfSamples); | |
} | |
public void function testReturnArrayOfSamples(){ | |
returnArrayOfSamples(arrayOfSamples); | |
} | |
/** | |
* @mxunit:expectedexception expression | |
*/ | |
public void function testAcceptArrayOfSamples_withStrings(){ | |
try { | |
acceptArrayOfSamples(arrayOfStrings); | |
} catch(any e){ | |
assert(e.message == "invalid call of the function acceptArrayOfSamples (C:\websites\luceebook\examples\arrays\typedArrayTests.cfc), first Argument (samples) is of invalid type, can't cast Object type [Array] to a value of type [sample[]]") | |
} | |
} | |
/** | |
* @mxunit:expectedexception expression | |
*/ | |
public void function testReturnArrayOfSamples_withStrings(){ | |
try { | |
returnArrayOfSamples(arrayOfStrings); | |
} catch(any e){ | |
assert(e.message == "the function returnArrayOfSamples has an invalid return value , can't cast Object type [Array] to a value of type [Sample[]]") | |
} | |
} | |
public void function testAcceptArrayOfSamples_withSubSamples(){ | |
acceptArrayOfSamples(arrayOfSubSamples); | |
} | |
public void function testReturnArrayOfSamples_withSubSamples(){ | |
returnArrayOfSamples(arrayOfSubSamples); | |
} | |
/** | |
* | |
*/ | |
public void function acceptArrayOfSamples_withNotSamples(){ | |
acceptArrayOfSamples(arrayOfNotSamples); | |
} | |
/** | |
* | |
*/ | |
public void function testReturnArrayOfSamples_withNotSamples(){ | |
try { | |
returnArrayOfSamples(arrayOfNotSamples); | |
} catch(any e){ | |
assert(e.message == "the function returnArrayOfSamples has an invalid return value , can't cast Object type [Array] to a value of type [Sample[]]") | |
} | |
} | |
public void function testAcceptArrayOfStrings(){ | |
acceptArrayOfStrings(arrayOfStrings); | |
} | |
public void function testReturnArrayOfStrings(){ | |
returnArrayOfStrings(arrayOfStrings); | |
} | |
public void function testAcceptArrayOfNumerics(){ | |
acceptArrayOfNumerics(arrayOfNumerics); | |
} | |
public void function testReturnArrayOfNumerics(){ | |
returnArrayOfNumerics(arrayOfNumerics); | |
} | |
/** | |
* @mxunit:expectedexception expression | |
*/ | |
public void function testAcceptArrayOfNumerics_withStrings(){ | |
try { | |
acceptArrayOfNumerics(arrayOfStrings); | |
} catch(any e){ | |
assert(e.message == "invalid call of the function acceptArrayOfNumerics (C:\websites\luceebook\examples\arrays\typedArrayTests.cfc), first Argument (numerics) is of invalid type, can't cast Object type [Array] to a value of type [numeric[]]") | |
} | |
} | |
/** | |
* @mxunit:expectedexception expression | |
*/ | |
public void function testReturnArrayOfNumerics_withStrings(){ | |
try { | |
returnArrayOfNumerics(arrayOfStrings); | |
} catch(any e){ | |
assert(e.message == "the function returnArrayOfNumerics has an invalid return value , can't cast Object type [Array] to a value of type [numeric[]]") | |
} | |
} | |
public void function testAcceptArrayOfStructs(){ | |
acceptArrayOfStructs(arrayOfStructs); | |
} | |
public void function testReturnArrayOfStructs(){ | |
returnArrayOfStructs(arrayOfStructs); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment