Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented AssignCalc and ExpressionIsInteger
- Loading branch information
Showing
5 changed files
with
263 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.zest.core.v1; | ||
|
||
/** | ||
* The Class ZestAssignString assigns a string (which can include other variables) to the specified variable. | ||
*/ | ||
public class ZestAssignCalc extends ZestAssignment { | ||
|
||
public static final String OPERAND_ADD = "add"; | ||
public static final String OPERAND_SUBTRACT = "subtract"; | ||
public static final String OPERAND_MULTIPLY = "multiply"; | ||
public static final String OPERAND_DIVIDE = "divide"; | ||
|
||
private String operandA = null; | ||
private String operandB = null; | ||
private String operation = null; | ||
|
||
/** | ||
* Instantiates a new zest assign random integer. | ||
*/ | ||
public ZestAssignCalc() { | ||
} | ||
|
||
/** | ||
* Instantiates a new zest assign random integer. | ||
* | ||
* @param variableName the variable name | ||
*/ | ||
public ZestAssignCalc(String variableName) { | ||
super(variableName); | ||
} | ||
|
||
/** | ||
* Instantiates a new zest assign random integer. | ||
* | ||
* @param variableName the variable name | ||
* @param minInt the min int | ||
* @param maxInt the max int | ||
*/ | ||
public ZestAssignCalc(String variableName, String operandA, String operation, String operandB) { | ||
super(variableName); | ||
this.operandA = operandA; | ||
this.operation = operation; | ||
this.operandB = operandB; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.mozilla.zest.core.v1.ZestTransformation#transform(org.mozilla.zest.core.v1.ZestRunner, org.mozilla.zest.core.v1.ZestRequest) | ||
*/ | ||
@Override | ||
public String assign (ZestResponse response, ZestRuntime runtime) throws ZestAssignFailException { | ||
int operA; | ||
int operB; | ||
try { | ||
operA = Integer.parseInt(runtime.replaceVariablesInString(this.operandA, false)); | ||
} catch (NumberFormatException e) { | ||
throw new ZestAssignFailException(this, "operandA not a number"); | ||
} | ||
try { | ||
operB = Integer.parseInt(runtime.replaceVariablesInString(this.operandB, false)); | ||
} catch (NumberFormatException e) { | ||
throw new ZestAssignFailException(this, "operandB not a number"); | ||
} | ||
if (OPERAND_ADD.equals(operation)) { | ||
return Integer.toString(operA + operB); | ||
} else if (OPERAND_SUBTRACT.equals(operation)) { | ||
return Integer.toString(operA + operB); | ||
} else if (OPERAND_MULTIPLY.equals(operation)) { | ||
return Integer.toString(operA * operB); | ||
} else if (OPERAND_DIVIDE.equals(operation)) { | ||
return Integer.toString(operA / operB); | ||
} | ||
throw new ZestAssignFailException(this, "Invalid operation"); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.mozilla.zest.core.v1.ZestElement#deepCopy() | ||
*/ | ||
@Override | ||
public ZestAssignCalc deepCopy() { | ||
ZestAssignCalc copy = new ZestAssignCalc(this.getVariableName(), operandA, operation, operandB); | ||
copy.setEnabled(this.isEnabled()); | ||
return copy; | ||
} | ||
|
||
public String getOperandA() { | ||
return operandA; | ||
} | ||
|
||
public String getOperandB() { | ||
return operandB; | ||
} | ||
|
||
public String getOperation() { | ||
return operation; | ||
} | ||
|
||
public void setOperandA(String operandA) { | ||
this.operandA = operandA; | ||
} | ||
|
||
public void setOperandB(String operandB) { | ||
this.operandB = operandB; | ||
} | ||
|
||
public void setOperation(String operation) { | ||
this.operation = operation; | ||
} | ||
|
||
} |
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,83 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.zest.core.v1; | ||
|
||
/** | ||
* Represent an expression that tests to see if a variable value is an integer. | ||
*/ | ||
public class ZestExpressionIsInteger extends ZestExpression{ | ||
|
||
/** The variableName to test. */ | ||
private String variableName; | ||
|
||
/** | ||
* Instantiates a new zest expression regex. | ||
*/ | ||
public ZestExpressionIsInteger(){ | ||
this(""); | ||
} | ||
|
||
/** | ||
* Instantiates a new zest expression isInteger. | ||
* | ||
* @param variableName the variableName | ||
*/ | ||
public ZestExpressionIsInteger(String variableName) { | ||
super (); | ||
this.variableName = variableName; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.mozilla.zest.core.v1.ZestExpressionElement#isTrue(org.mozilla.zest.core.v1.ZestResponse) | ||
*/ | ||
public boolean isTrue (ZestRuntime runtime) { | ||
String str = runtime.getVariable(variableName); | ||
if (str == null) { | ||
return false; | ||
} | ||
|
||
try { | ||
Integer.parseInt(str); | ||
return true; | ||
} catch (NumberFormatException e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the variable name. | ||
* | ||
* @return the variable name | ||
*/ | ||
public String getVariableName() { | ||
return variableName; | ||
} | ||
|
||
/** | ||
* Sets the variable name. | ||
* | ||
* @param variableName the new variable name | ||
*/ | ||
public void setVariableName(String variableName) { | ||
this.variableName = variableName; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.mozilla.zest.core.v1.ZestExpression#isLeaf() | ||
*/ | ||
@Override | ||
public boolean isLeaf() { | ||
return true; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.mozilla.zest.core.v1.ZestExpression#deepCopy() | ||
*/ | ||
@Override | ||
public ZestExpressionIsInteger deepCopy() { | ||
return new ZestExpressionIsInteger(this.getVariableName()); | ||
} | ||
|
||
} |
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
57 changes: 57 additions & 0 deletions
57
test/org/mozilla/zest/test/v1/ZestExpressionIsIntegerUnitTest.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,57 @@ | ||
/** | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* @author Alessandro Secco: seccoale@gmail.com | ||
*/ | ||
package org.mozilla.zest.test.v1; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.mozilla.zest.core.v1.ZestExpressionIsInteger; | ||
|
||
/** | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class ZestExpressionIsIntegerUnitTest { | ||
|
||
@Test | ||
public void testZestExpressionIsIntegerBadVar() { | ||
ZestExpressionIsInteger ast = new ZestExpressionIsInteger(); | ||
TestRuntime rt = new TestRuntime(); | ||
ast.setVariableName("aaa"); | ||
assertFalse(ast.evaluate(rt)); | ||
} | ||
|
||
@Test | ||
public void testZestExpressionIsIntegerTrue() { | ||
ZestExpressionIsInteger ast = new ZestExpressionIsInteger(); | ||
TestRuntime rt = new TestRuntime(); | ||
rt.setVariable("aaa", "12"); | ||
ast.setVariableName("aaa"); | ||
assertTrue(ast.evaluate(rt)); | ||
} | ||
|
||
@Test | ||
public void testZestExpressionIsIntegerFalse() { | ||
ZestExpressionIsInteger ast = new ZestExpressionIsInteger(); | ||
TestRuntime rt = new TestRuntime(); | ||
rt.setVariable("aaa", " 12b "); | ||
ast.setVariableName("aaa"); | ||
assertFalse(ast.evaluate(rt)); | ||
} | ||
|
||
@Test | ||
public void testZestExpressionIsIntegerCopy() { | ||
ZestExpressionIsInteger ast = new ZestExpressionIsInteger(); | ||
ast.setVariableName("aaa"); | ||
ZestExpressionIsInteger ast2 =ast.deepCopy(); | ||
assertEquals(ast.getVariableName(), ast2.getVariableName()); | ||
} | ||
} |