Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
homestar9 committed Sep 23, 2022
1 parent f83d815 commit c814c57
Show file tree
Hide file tree
Showing 9 changed files with 486 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.DS_Store
settings.xml
WEB-INF
.vscode
.vscode
testbox/
62 changes: 62 additions & 0 deletions box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"author":"Angry Sam Productions, Inc.",
"bugs":"https://github.com/homestar9/nap-time/issues",
"changelog":"https://github.com/homestar9/nap-time",
"contributors":[],
"dependencies":{},
"description":"An app that will sleep for a specified amount of time and then return a response. Useful for testing timeout handling in your other apps.",
"devDependencies":{
"testbox":"^4.5.0+5"
},
"documentation":"",
"homepage":"https://github.com/homestar9/nap-time",
"ignore":[
"**/.*",
"/test/",
"/tests/"
],
"installPaths":{
"testbox":"testbox/"
},
"instructions":"",
"keywords":[],
"license":[
{
"type":"MIT",
"URL":"https://github.com/homestar9/nap-time/blob/master/LICENSE.md"
}
],
"location":"forgeboxStorage",
"name":"Nap Time",
"private":false,
"projectURL":"https://github.com/homestar9/nap-time",
"repository":{
"type":"git",
"URL":"https://github.com/homestar9/nap-time"
},
"scripts":{},
"shortDescription":"A sweet package",
"slug":"nap-time",
"testbox":{
"bundles":"",
"directory":"tests.specs",
"excludes":"",
"labels":"",
"options":{},
"recurse":true,
"reporter":"",
"runner":[
{
"default":""
}
],
"testBundles":"",
"testSpecs":"",
"testSuites":"",
"verbose":true,
"watchDelay":500,
"watchPaths":"**.cfc"
},
"type":"modules",
"version":"1.0.0"
}
2 changes: 1 addition & 1 deletion index.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sleep( sleepSeconds * 1000 );
writeOutput(
serializeJSON( {
"data" : "",
"message" : "What a nice #sleepSeconds# second nap",
"messages" : [ "What a nice #sleepSeconds# second nap" ],
"error" : false
} )
);
Expand Down
39 changes: 39 additions & 0 deletions tests/Application.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2005-2007 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
* www.ortussolutions.com
* ---
*/
component {

// APPLICATION CFC PROPERTIES
this.name = "ColdBoxTestingSuite";
this.sessionManagement = true;
this.setClientCookies = true;
this.sessionTimeout = createTimespan( 0, 0, 15, 0 );
this.applicationTimeout = createTimespan( 0, 0, 15, 0 );
// Turn on/off white space management
this.whiteSpaceManagement = "smart";
this.enableNullSupport = shouldEnableFullNullSupport();

// Create testing mapping
this.mappings[ "/tests" ] = getDirectoryFromPath( getCurrentTemplatePath() );
// Map back to its root
rootPath = reReplaceNoCase( this.mappings[ "/tests" ], "tests(\\|/)", "" );
this.mappings[ "/root" ] = rootPath;

public boolean function onRequestStart( targetPage ){
// Set a high timeout for long running tests
setting requestTimeout="9999";
return true;
}

public void function onRequestEnd( required targetPage ){
}

private boolean function shouldEnableFullNullSupport(){
var system = createObject( "java", "java.lang.System" );
var value = system.getEnv( "FULL_NULL" );
return isNull( value ) ? false : !!value;
}

}
148 changes: 148 additions & 0 deletions tests/index.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<cfscript>
// No cf debugging
cfsetting( showdebugoutput="false" );
// Path Navigation
param name="url.path" default="";
// Root Tests Directory
rootMapping = "/tests/specs";
rootPath = expandPath( rootMapping );
targetPath = rootPath;
// Append navigation path
if( len( url.path ) ){
targetPath = getCanonicalPath( rootpath & "/" & url.path );
// Avoid traversals
if( !findNoCase( rootpath, targetPath ) ){
targetPath = rootpath;
}
}
// Get the actual execution path
executePath = rootMapping & ( len( url.path ) ? "/#url.path#" : "/" );
// Directory Runner
if( !isNull( url.action ) ){
if( directoryExists( targetPath ) ){
writeOutput( "#new testbox.system.TestBox( directory=executePath ).run()#" );
} else {
writeOutput( "<h2>Invalid Directory: #encodeForHTML( targetPath )#</h2>" );
}
abort;
}
// Get target path listing
qResults = directoryList( targetPath, false, "query", "", "name" );
// Get the back path
if( len( url.path ) ){
backPath = replacenocase( url.path, listLast( url.path, "/" ), "" );
backPath = reReplace( backpath, "/$", "" );
}
// TestBox Assets
ASSETS_DIR = expandPath( "/testbox/system/reports/assets" );
TESTBOX_VERSION = new testBox.system.TestBox().getVersion();
</cfscript>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="TestBox v#TESTBOX_VERSION#">
<title>TestBox Browser</title>
<cfoutput>
<style>#fileRead( '#ASSETS_DIR#/css/main.css' )#</style>
<script>#fileRead( '#ASSETS_DIR#/js/jquery-3.3.1.min.js' )#</script>
<script>#fileRead( '#ASSETS_DIR#/js/popper.min.js' )#</script>
<script>#fileRead( '#ASSETS_DIR#/js/bootstrap.min.js' )#</script>
<script>#fileRead( '#ASSETS_DIR#/js/stupidtable.min.js' )#</script>
</cfoutput>
</head>
<cfoutput>
<body>
<div id="tb-runner" class="container">
<!--- Header --->
<div class="row">
<div class="col-md-4 text-center mx-auto">
<img class="mt-3" src="http://www.ortussolutions.com/__media/testbox-185.png" alt="TestBox" id="tb-logo"/>
<br>
v#TESTBOX_VERSION#
<br>
<a
href="index.cfm?action=runTestBox&path=#URLEncodedFormat( url.path )#"
target="_blank"
>
<button
class="btn btn-primary btn-sm my-1"
type="button">
Run All
</button>
</a>
</div>
</div>
<!--- Listing --->
<div class="row">
<div class="col-md-12">
<form name="runnerForm" id="runnerForm">
<input type="hidden" name="opt_run" id="opt_run" value="true">
<h2>TestBox Test Browser: </h2>
<p>
Below is a listing of the files and folders starting from your root <code>#rootMapping#</code>. You can click on individual tests in order to execute them
or click on the <strong>Run All</strong> button on your left and it will execute a directory runner from the visible folder.
</p>
<fieldset>
<legend>#targetPath.replace( rootPath, "" )#</legend>
<!--- Show Back If we are traversing --->
<cfif len( url.path )>
<a href="index.cfm?path=#URLEncodedFormat( backPath )#">
<button type="button" class="btn btn-secondary btn-sm my-1">&##xAB; Back</button>
</a>
<br>
<hr>
</cfif>
<cfloop query="qResults">
<!--- Skip . folder file names --->
<cfif refind( "^\.", qResults.name )>
<cfcontinue>
</cfif>
<cfif qResults.type eq "Dir">
<a
class="btn btn-secondary btn-sm my-1"
href="index.cfm?path=#urlEncodedFormat( url.path & qResults.name )#"
>
&##x271A; #qResults.name#
</a>
<br />
<cfelseif listLast( qresults.name, ".") eq "cfm">
<a
class="btn btn-primary btn-sm my-1"
href="#executePath & "/" & qResults.name#"
target="_blank"
>
#qResults.name#
</a>
<br />
<cfelseif listLast( qresults.name, ".") eq "cfc" and qresults.name neq "Application.cfc">
<a
class="btn btn-primary btn-sm my-1"
href="#executePath & "/" & qResults.name#?method=runRemote"
target="_blank"
>
#qResults.name#
</a>
<br />
<cfelse>
#qResults.name#
<br/>
</cfif>
</cfloop>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
</cfoutput>
Empty file added tests/resources/.gitkeep
Empty file.
25 changes: 25 additions & 0 deletions tests/runner.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<cfsetting requesttimeout="999999" >
<cfsetting showDebugOutput="false">
<cfsetting enablecfoutputonly="true">

<!--- Executes all tests in the 'specs' folder with simple reporter by default --->
<cfparam name="url.reporter" default="simple">
<cfparam name="url.directory" default="tests.specs">
<cfparam name="url.recurse" default="true" type="boolean">
<cfparam name="url.bundles" default="">
<cfparam name="url.labels" default="">
<cfparam name="url.excludes" default="">
<cfparam name="url.reportpath" default="#expandPath( "/tests/results" )#">
<cfparam name="url.propertiesFilename" default="TEST.properties">
<cfparam name="url.propertiesSummary" default="false" type="boolean">

<!--- Code Coverage requires FusionReactor --->
<cfparam name="url.coverageEnabled" default="false">
<cfparam name="url.coveragePathToCapture" default="#expandPath( '/root' )#">
<cfparam name="url.coverageWhitelist" default="">
<cfparam name="url.coverageBlacklist" default="/testbox,/coldbox,/tests,/modules,Application.cfc,/index.cfm">
<cfparam name="url.coverageBrowserOutputDir" default="#expandPath( '/tests/results/coverageReport' )#">
<!---<cfparam name="url.coverageSonarQubeXMLOutputPath" default="#expandPath( '/tests/results/SonarQubeCoverage.xml' )#">--->

<!--- Include the TestBox HTML Runner --->
<cfinclude template="/testbox/system/runners/HTMLRunner.cfm" >
55 changes: 55 additions & 0 deletions tests/specs/integration/MainTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
component extends="testbox.system.BaseSpec" {

/*********************************** LIFE CYCLE Methods ***********************************/

function beforeAll(){
}

function afterAll(){
}

/*********************************** BDD SUITES ***********************************/

function run(){
describe( "Nap Time Tests", function(){
beforeEach( function( currentSpec ){
// Setup as a request
} );

it( "Should return a JSON response", function(){
var response = "";
cfhttp( url = "http://127.0.0.1:6969/0/", result = "response" ) {
}


expect( response.responseheader.status_code ).toBe( 200 );
expect( isJSON( response.filecontent ) ).toBeTrue( "fileContent was not JSON" );
} );

it( "Will return the sleep seconds in the response", function(){
var sleepSeconds = 2;
var response = "";
cfhttp( url = "http://127.0.0.1:6969/#sleepSeconds#/", result = "response" ) {
}

expect( response.responseheader.status_code ).toBe( 200 );
expect( deserializeJson( response.filecontent ).messages[ 1 ] ).toBe( "What a nice #sleepSeconds# second nap" );
} );

it( "Should wait for a specified amount of time", function(){
var sleepSeconds = randRange( 1, 10 ); // random number between 1 and 10
var start = getTickCount();

var response = "";
cfhttp( url = "http://127.0.0.1:6969/#sleepSeconds#/", result = "response" ) {
}

var duration = ( getTickCount() - start ) / 1000;

expect( response.responseheader.status_code ).toBe( 200 );
expect( duration ).toBeBetween( sleepSeconds, sleepSeconds + 1 );
} );
} );
}

}
Loading

0 comments on commit c814c57

Please sign in to comment.