Skip to content

Instantly share code, notes, and snippets.

@roryl
Last active March 13, 2016 05:34
Show Gist options
  • Save roryl/8b646c334f8d5658e9fe to your computer and use it in GitHub Desktop.
Save roryl/8b646c334f8d5658e9fe to your computer and use it in GitHub Desktop.
Lucee Interfaces
component implements="IMyInterface,IOtherInterface" {
public function myFunc(required string myString){
return arguments.myString;
}
public function myOtherFunc(required array myArray){
return myArray;
}
public function someOtherFunc(){
}
}
interface {
public function myFunc(required string myString){
}
public function myOtherFunc(required array myArray){
}
}
/**
* 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 interfaceTest(){
var myObj = new MyInterfaceImpl();
}
function interfaceFailTest(){
expect(function(){
var myObj = new MyInterfaceFail();
}).toThrow();
}
function useInterfaceTest(){
var myObj = new MyInterfaceImpl();
var useObj = new usesInterface(myObj);
}
function useInterfaceFailTest(){
var myObj = new emptyComponent();
expect(function(){
var useObj = new usesInterface(myObj);
}).toThrow("");
}
function returnInterfaceTest(){
var myObj = new returnInterface().getObject();
}
function returnInterfaceFailTest(){
expect(function(){
var myObj = new returnInterfaceFail().getObject();
})
}
function implementsMultipleTest(){
var myObj = new implementsMultiple();
var useObj = new usesInterface(myObj);
}
function guardInterfaceTest(){
var myObj = new MyInterfaceImpl();
var useObj = new usesInterfaceGuard(myObj);
}
}
interface {
public function someOtherFunc(){
}
}
component implements="IMyInterface" {
public function myFunc(required string myString){
return arguments.myString;
}
}
component implements="IMyInterface" {
public function myFunc(required string myString){
return arguments.myString;
}
public function myOtherFunc(required array myArray){
return myArray;
}
}
component {
public function init(){
return this;
}
public IMyInterface function getObject(){
return new MyInterfaceImpl();
}
}
component {
public function init(){
return this;
}
public IMyInterface function getObject(){
return new emptyComponent();
}
}
component {
public function init(required IMyInterface object){
//Will error if the object passed in did not implement IMyInterface
}
}
component {
public function init(required IMyInterface object){
//Throws an error if the argument does not implement the IOtherInterface
structKeyExists(getMetaData(arguments.object).implements,"IOtherInterface");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment