Skip to content

Commit

Permalink
Implemented AssignCalc and ExpressionIsInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
psiinon committed Jan 19, 2015
1 parent 8bb7d05 commit f5a142a
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/org/mozilla/zest/core/v1/ZestAssignCalc.java
@@ -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;
}

}
83 changes: 83 additions & 0 deletions src/org/mozilla/zest/core/v1/ZestExpressionIsInteger.java
@@ -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());
}

}
9 changes: 9 additions & 0 deletions src/org/mozilla/zest/impl/ZestPrinter.java
Expand Up @@ -12,6 +12,7 @@
import org.mozilla.zest.core.v1.ZestActionScan;
import org.mozilla.zest.core.v1.ZestActionSleep;
import org.mozilla.zest.core.v1.ZestAssertion;
import org.mozilla.zest.core.v1.ZestAssignCalc;
import org.mozilla.zest.core.v1.ZestAssignRegexDelimiters;
import org.mozilla.zest.core.v1.ZestAssignReplace;
import org.mozilla.zest.core.v1.ZestAssignStringDelimiters;
Expand All @@ -22,6 +23,7 @@
import org.mozilla.zest.core.v1.ZestExpressionAnd;
import org.mozilla.zest.core.v1.ZestExpressionElement;
import org.mozilla.zest.core.v1.ZestExpressionEquals;
import org.mozilla.zest.core.v1.ZestExpressionIsInteger;
import org.mozilla.zest.core.v1.ZestExpressionLength;
import org.mozilla.zest.core.v1.ZestExpressionOr;
import org.mozilla.zest.core.v1.ZestExpressionRegex;
Expand Down Expand Up @@ -172,6 +174,10 @@ public static void list(ZestStatement stmt, int indent) {
ZestAssignReplace zas = (ZestAssignReplace) za;
System.out.println("Set Variable: " + zas.getVariableName() +
" Replace " + zas.getReplace() + " With " + zas.getReplace());
} else if (za instanceof ZestAssignCalc) {
ZestAssignCalc zas = (ZestAssignCalc) za;
System.out.println("Set Variable: " + zas.getVariableName() +
" " + zas.getOperandA() + " " + zas.getOperation() + " " + zas.getOperandB());
} else {
System.out.println("(Unknown assignment: " + stmt.getElementType() + ")");
}
Expand Down Expand Up @@ -238,6 +244,9 @@ public static void printExpression(ZestExpressionElement element, int indent) {
} else if (element instanceof ZestExpressionStatusCode) {
ZestExpressionStatusCode codeExpr = (ZestExpressionStatusCode) element;
System.out.print("Status Code: " + codeExpr.getCode());
} else if (element instanceof ZestExpressionIsInteger) {
ZestExpressionIsInteger codeExpr = (ZestExpressionIsInteger) element;
System.out.print("Is Integer: " + codeExpr.getVariableName());
} else if (element instanceof ZestExpressionURL) {
// ZestExpressionURL urlExpr=(ZestExpressionURL)element;
System.out.print("URL ");
Expand Down
1 change: 1 addition & 0 deletions test/org/mozilla/zest/test/v1/ZestAllTestSuite.java
Expand Up @@ -28,6 +28,7 @@
ZestConditionalRegexExprUnitTest.class,
ZestConditionalRegexUnitTest.class,
ZestExpressionAndUnitTest.class,
ZestExpressionIsIntegerUnitTest.class,
ZestExpressionLengthUnitTest.class,
ZestExpressionRegexUnitTest.class,
ZestExpressionResponseTimeUnitTest.class,
Expand Down
57 changes: 57 additions & 0 deletions test/org/mozilla/zest/test/v1/ZestExpressionIsIntegerUnitTest.java
@@ -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());
}
}

0 comments on commit f5a142a

Please sign in to comment.