-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added in substring and startswith string functions
- Loading branch information
synapticloop
committed
Feb 7, 2017
1 parent
5669a4f
commit 71c5575
Showing
6 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
41 changes: 41 additions & 0 deletions
41
src/main/java/synapticloop/templar/function/string/FunctionStartsWith.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package synapticloop.templar.function.string; | ||
|
||
import synapticloop.templar.exception.FunctionException; | ||
import synapticloop.templar.function.Function; | ||
import synapticloop.templar.helper.ObjectHelper; | ||
import synapticloop.templar.utils.TemplarContext; | ||
|
||
/* | ||
* Copyright (c) 2017 Synapticloop. | ||
* | ||
* All rights reserved. | ||
* | ||
* This code may contain contributions from other parties which, where | ||
* applicable, will be listed in the default build file for the project | ||
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project. | ||
* | ||
* This source code and any derived binaries are covered by the terms and | ||
* conditions of the Licence agreement ("the Licence"). You may not use this | ||
* source code or any derived binaries except in compliance with the Licence. | ||
* A copy of the Licence is available in the file named LICENSE.txt shipped with | ||
* this source code or binaries. | ||
*/ | ||
|
||
public class FunctionStartsWith extends Function { | ||
|
||
public FunctionStartsWith() { | ||
super(2); | ||
} | ||
|
||
@Override | ||
protected Object evaluateFunction(Object[] args, TemplarContext templarContext) throws FunctionException { | ||
Object argZero = ObjectHelper.evaluateObjectToDefault(args[0], templarContext); | ||
Object argOne = ObjectHelper.evaluateObjectToDefault(args[1], templarContext); | ||
if(null == argZero || null == argOne) { | ||
throw new FunctionException("Could not evaluate arguments to String, arguments were: " + args[0] + ", " + args[1] + ", values: " + argZero + ", " + argOne); | ||
} | ||
|
||
return(argOne.toString().startsWith(argZero.toString())); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
28 changes: 28 additions & 0 deletions
28
src/test/java/synapticloop/templar/function/string/FunctionSubStringTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package synapticloop.templar.function.string; | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import synapticloop.templar.exception.FunctionException; | ||
|
||
public class FunctionSubStringTest { | ||
private FunctionSubString functionSubString = null; | ||
|
||
@Before | ||
public void setup() { | ||
functionSubString = new FunctionSubString(); | ||
} | ||
|
||
@Test | ||
public void testSubstring() throws FunctionException { | ||
Object evaluate = functionSubString.evaluate("substring", new Object[] { "\"hello\"", "\"1\"" }, null); | ||
assertEquals("ello", evaluate); | ||
} | ||
|
||
@Test | ||
public void testSubstringRange() throws FunctionException { | ||
Object evaluate = functionSubString.evaluate("substring", new Object[] { "\"hello\"", "\"1\"", "\"3\"" }, null); | ||
assertEquals("el", evaluate); | ||
} | ||
} |