Skip to content

Commit

Permalink
FC-2297 Code and unit tests
Browse files Browse the repository at this point in the history
git-svn-id: https://farcry.jira.com/svn/FC/branches/p600@5546 8f7d33b6-1012-0410-9a5a-bb7ecce9c363
  • Loading branch information
Rob Rohan committed Jun 4, 2010
1 parent a65bebb commit 263fa30
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 11 deletions.
101 changes: 90 additions & 11 deletions Application.cfc
Expand Up @@ -38,20 +38,12 @@
<cfparam name="this.botAgents" default="*" />
<cfparam name="this.botIPs" default="*" />


<cfset this.defaultAgents = "bot\b,\brss,slurp,mediapartners-google,googlebot,zyborg,emonitor,jeeves,sbider,findlinks,yahooseeker,mmcrawler,jbrowser,java,pmafind,blogbeat,converacrawler,ocelli,labhoo,validator,sproose,ia_archiver,larbin,psycheclone,arachmo" />
<cfif left(this.botagents,1) eq "+">
<cfset this.botAgents = this.defaultAgents & "," & mid(this.botAgents,2,len(this.botAgents)) />
<cfelseif this.botAgents eq "*">
<cfset this.botAgents = this.defaultAgents />
</cfif>
<cfset this.botAgents = listtoarray(this.botAgents) />
<cfset this.botAgents = __plusMinusStateMachine(this.defaultAgents, this.botagents) />

<cfset this.defaultIPs = "" />
<cfif left(this.botIPs,1) eq "+">
<cfset this.botIPs = this.defaultIPs & "," & mid(this.botIPs,2,len(this.botIPs)) />
<cfelseif this.botIPs eq "*">
<cfset this.botIPs = this.defaultIPs />
</cfif>
<cfset this.botAgents = __plusMinusStateMachine(this.defaultIPs, this.botIPs) />

<cfparam name="cookie.sessionScopeTested" default="false" />
<cfparam name="cookie.hasSessionScope" default="false" />
Expand Down Expand Up @@ -79,6 +71,89 @@
</cfif>


<!--- ////////////////////////////////////////////// --->

<!---
This function can be used to create an array from a string (list) and
also change the contents of that string (list). The first parameter
is a list of default items "one,two,three", and the second parameter
is a command list of operations to perform on that list. The command
list can look like the following: "*:-one,three:+six,four"
This will return the array "[two,six,four]". The command string is made
of the following operators:
* = add everything from the first paramter
+ = do an addition
- = do a subtraction
-* = remove all of the default items
--->
<cffunction name="__plusMinusStateMachine" returntype="array" output="false">
<cfargument name="asteriskDefaults" type="string" required="true" />
<cfargument name="stateCommandString" type="string" required="true" />

<cfset var z = 0 />
<cfset var q = 0 />
<cfset var commandString = "" />
<cfset aStates = arrayNew(1) />
<cfset sStates = "" />
<cfset returnArray = arrayNew(1) />


<cfset aStates = listToArray(arguments.stateCommandString, ":") />

<!---
The reason this gets a bit complicated is to be backwards compatable.
If they just passed a string like "+one,two,three" we'll assume they want
the core default agents, and want to add to them. --->
<cfif not len(stateCommandString) or left(stateCommandString, 1) eq "+">
<cfset sStates = arguments.asteriskDefaults />
<cfelse>
<cfset sStates = "" />
</cfif>

<!--- Loop over the agent addition, subtraction or all commands
and build the string of default agents --->
<cfloop from="1" to="#arrayLen(aStates)#" index="z">
<cfset commandString = aStates[z] />

<!--- Add agents to the list --->
<cfif left(commandString,1) eq "+">
<cfset sStates = sStates & "," & mid(commandString,2,len(commandString)) />

<!--- do and "Add all" - basically add in all the defaults
from core (defined above as this.defaultAgents) --->
<cfelseif commandString eq "*">
<cfset sStates = sStates & "," & arguments.asteriskDefaults />

<!--- remove either single agents or remove all the core defaults--->
<cfelseif left(commandString,1) eq "-">
<!--- if they do "-*" we'll remove all the default bots --->
<cfif left(commandString,2) eq "-*">
<cfset sStates = "" />
<cfelse>
<!--- otherwise they are doing a "-java,jeeves" kind of string --->
<cfset returnArray = listToArray(sStates) />

<cfset removeArray = listToArray(mid(commandString,2,len(commandString))) />
<cfloop from="1" to="#arrayLen(removeArray)#" index="q">
<cfset returnArray.remove(removeArray[q]) />
</cfloop>

<!--- put this back into a list incase they do more commands to the list --->
<cfset sStates = arrayToList(returnArray) />
</cfif>
</cfif>
</cfloop>

<!--- Ok, we should have a built up string, turn it into
an array for later usage--->
<cfset returnArray = listToArray(sStates) />

<cfreturn returnArray />
</cffunction>


<cffunction name="reFindAny" access="private" output="false" returntype="boolean" hint="Looks for any of an array of regular expressions in a string">
<cfargument name="needle" type="array" required="true" hint="The array of regular expressions to find" />
<cfargument name="haystack" type="string" required="true" hint="The string to match against" />
Expand Down Expand Up @@ -308,6 +383,10 @@
<cfset session.loginReturnURL = application.fapi.fixURL(url.returnURL) />
</cfif>

<!--- Hookup any functions here we want available to Farcry. --->
<cfset request.__plusMinusStateMachine = this.__plusMinusStateMachine />


<cfreturn true />
</cffunction>

Expand Down
42 changes: 42 additions & 0 deletions packages/farcry/utils.cfc
Expand Up @@ -751,4 +751,46 @@

<cfreturn prettyDate />
</cffunction>

<!--- @@hint:
<p>This function is used to build an array of items from a set of defaults, and
also a string of commands to add or subtract items form the default. For
example, core defines a list of internet spider user agents ("google,slurp,meany,java"),
and you may want to use all of them except "java" and also add two called
"blarg" and "yuck". You can do that with this function like so:
</p>
<p>
arrayFromStringCommands("google,slurp,meany,java", "*:-java,+blarg,yuck")
</p>
<p>
Currently, this function is only used for the above mentioned example, however
it might be useful in the future to allow plugins to be added and removed
at runtime.
</p>
<p>
This function is defined in Application.cfc because it is used on FarCry
init. It is here (utils.cfc) for Unit testing, and the possibility of
future use.
</p>
@@examples:
<code>
#application.fapi.plusMinusStateMachine(myUglyDate)#
</code>
--->
<cffunction name="arrayFromStringCommands" access="public" returntype="array" output="false">
<cfargument name="asteriskDefaults" type="string" required="true" />
<cfargument name="stateCommandString" type="string" required="true" />

<!--- This function is needed on application startup, and as such is defined in
the cores version of Application.cfc. --->
<cfreturn request.__plusMinusStateMachine(
arguments.asteriskDefaults,
arguments.stateCommandString) />

</cffunction>

</cfcomponent>
76 changes: 76 additions & 0 deletions tests/utilsTest.cfc
@@ -0,0 +1,76 @@
<!--- @@Copyright: Copyright (c) 2009 Rob Rohan. All rights reserved. --->
<!--- @@displayname: fapiTest.cfc --->
<!--- @@description:
This file holds all the tests for the Utils API in FarCry.
WARNING: this file requires it be opened and saved in
UTF-8. There are characters in this file that require UTF-8
encoding. Please ensure you are opening and saving this file in UTF-8
or tests will start to fail and will need to be re-written using the
correct encoding.
On Windows UTF-8 is not the default. If you are using Eclipse please
right click on the file and make sure the encoding is UTF-8. If this
doesn't look like Chinese:
这个我的猴子!那个我的帽子!我写得津津有味!
Then you are not using UTF-8.
--->
<cfcomponent extends="mxunit.framework.TestCase" displayname="Utils Tests">
<!--- setup and teardown --->
<cffunction name="setUp" returntype="void" access="public">
<!--- Any code needed to return your environment to normal goes here --->
</cffunction>

<cffunction name="tearDown" returntype="void" access="public">
<!--- Any code needed to return your environment to normal goes here --->

</cffunction>

<!--- ////////////////////////////////////////////////////////////////// --->

<cffunction name="arrayFromStringCommands_Blank_Test" access="public">
<cfset assertEquals(
application.fc.utils.arrayFromStringCommands("", ""),
[]) />
</cffunction>

<cffunction name="arrayFromStringCommands_MinusAsterisk_Test" access="public">
<cfset assertEquals(
application.fc.utils.arrayFromStringCommands("one,two,three,four", "-*"),
[]) />
</cffunction>

<cffunction name="arrayFromStringCommands_AsteriskMinus_Test" access="public">
<cfset assertEquals(
application.fc.utils.arrayFromStringCommands("one,two,three,four", "*:-four"),
["one","two","three"]) />
</cffunction>

<cffunction name="arrayFromStringCommands_AsteriskPlus_Test" access="public">
<cfset assertEquals(
application.fc.utils.arrayFromStringCommands("one,two,three,four", "*:+five"),
["one","two","three","four","five"]) />
</cffunction>

<cffunction name="arrayFromStringCommands_Plus_Test" access="public">
<cfset assertEquals(
application.fc.utils.arrayFromStringCommands("one,two,three,four", "+five"),
["one","two","three","four","five"]) />
</cffunction>

<cffunction name="arrayFromStringCommands_AsteriskPlusMinus_Test" access="public">
<cfset assertEquals(
application.fc.utils.arrayFromStringCommands("one,two,three,four", "*:+five:-one"),
["two","three","four","five"]) />
</cffunction>

<cffunction name="arrayFromStringCommands_AsteriskPlusListMinusList_Test" access="public">
<cfset assertEquals(
application.fc.utils.arrayFromStringCommands(
"one,two,three,four",
"*:+five,six,seven:-one,four,three"),
["two","five","six","seven"]) />
</cffunction>

<!--- ////////////////////////////////////////////////////////////////// --->

</cfcomponent>

0 comments on commit 263fa30

Please sign in to comment.