Skip to content

Instantly share code, notes, and snippets.

@roryl
Last active April 13, 2016 09:44
Show Gist options
  • Save roryl/5fbeecc3f9dde7289f519b7963db6a72 to your computer and use it in GitHub Desktop.
Save roryl/5fbeecc3f9dde7289f519b7963db6a72 to your computer and use it in GitHub Desktop.
Lucee Properties Examples
component {
public function init(){
}
}
<cfscript>
writeDump(new basicComponent());
writeDump(new componentWithProperties());
writeDump(new componentWithPropertiesAccessor());
</cfscript>
component {
property name='myValue';
public function init(){
return this;
}
}
component accessors='true' {
property name='myValue';
public function init(){
return this;
}
}
component accessors='true' {
property name='myValue' default='test this is a default!';
public function init(){
return this;
}
public function getValue(){
return variables.myValue;
}
}
component accessors='true' {
property name='myValue' required='true';
}
component accessors='true' {
property name='myValue' setter='false';
public function init(){
variables.myValue = 'Hello!';
return this;
}
}
component accessors='true' {
property name='myValue' required='true'; //Required has no effect
public function init(){
return this;
}
}
component accessors='true' {
property name='myValue' type='struct';
public function init(){
return this;
}
}
component accessors='true' {
property name='myEmail' validate='email';
property name='myString' validate='string' validateparams="{minLength=0, maxLength=5}";
}
<cfscript>
myComponent = new componentWithPropertiesNoConstructor(myValue:"Hello there!");
echo(myComponent.getMyValue());
myComponent = new componentWithPropertiesNoConstructor({myValue:"Hello there!"}); //Equivalent to the above
echo(myComponent.getMyValue());
myComponent = new componentWithPropertiesNoConstructor(argumentCollection={myValue:"Hello there!"}); //Equivalent to the above
echo(myComponent.getMyValue());
writeDump(now());
</cfscript>
/**
* 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 ".cfm" AND !file CONTAINS "use_validations"){
include template="#getFileFromPath(file)#";
}
}
}
function getDefaultValueTest(){
var myObj = new componentWithPropertiesDefault();
expect(myObj.getValue()).toBe("test this is a default!");
}
function validateParamsTest(){
var myObj = new componentWithPropertyValidations(myEmail:"test@domain.com", myString:"hellothere");
// myObj.setMyString("hellothere");
writeDump(myObj);
}
function useValidationsTest(){
expect(function(){include template="use_validations.cfm"}).toThrow();
}
}
<cfscript>
writeDump(getComponentMetaData("componentWithProperties"));
</cfscript>
<cfscript>
myComponent = new componentWithPropertiesAccessor();
myComponent.setMyValue("Hello!");
echo(myComponent.getMyValue());
</cfscript>
<cfscript>
myComponent = new componentWithPropertiesDefault();
writeDump(myComponent);
echo(myComponent.getMyValue());
writeDump(myComponent.getValue());
</cfscript>
<cfscript>
myComponent = new componentWithPropertiesOverride();
writeDump(myComponent);
echo(myComponent.getMyValue());
//myComponent.setMyValue("Hello!"); //errors that function does not exist
</cfscript>
<cfscript>
myComponent = new componentWithPropertiesRequired({});
myComponent.setMyValue("Hello!");
echo(myComponent.getMyValue());
</cfscript>
<cfscript>
myComponent = new componentWithPropertiesType();
writeDump(myComponent);
// myComponent.setMyValue('test'); //Will throw an error for invalid type
echo(myComponent.getMyValue());
</cfscript>
<cfscript>
myObj = new componentWithPropertyValidations();
myObj.setMyEmail("test@domain.com"); //works
myObj.setMyString("hello"); //works
myObj.setMyEmail("test"); //throws error for invalid email
myObj.setMyString("hello there!"); //Throws error for invalid string
//Validation of auto poplated members is currently broken see https://luceeserver.atlassian.net/browse/LDEV-816
myObj = new componentWithPropertyValidations(myEmail:"test", myString:"hello there!");
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment