提交 20c1caae 创建 作者: Greyson Parrelli's avatar Greyson Parrelli

Added some unit tests.

上级 665c3317
......@@ -14,7 +14,7 @@ public class InputValidator {
return INSTANCE;
}
public boolean isValidInt(String input) {
public boolean isInt(String input) {
try {
Integer.parseInt(input);
return true;
......
......@@ -22,7 +22,7 @@ public class MainActivity extends AppCompatActivity {
String n1Text = num1View.getText().toString();
String n2Text = num2View.getText().toString();
if (InputValidator.getInstance().isValidInt(n1Text) && InputValidator.getInstance().isValidInt(n2Text)) {
if (InputValidator.getInstance().isInt(n1Text) && InputValidator.getInstance().isInt(n2Text)) {
int n1 = Integer.parseInt(n1Text);
int n2 = Integer.parseInt(n2Text);
......
package com.greysonparrelli.gitlabciandroid;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Adds numbers together.
*
* @author Greyson Parrelli (keybase.io/greyson)
*/
public class AdderTest {
private Adder mUnderTest;
@Before
public void setup() {
mUnderTest = Adder.getInstance();
}
@Test
public void add_default() throws Exception {
assertEquals(3, mUnderTest.add(1, 2));
assertEquals(3, mUnderTest.add(2, 1));
}
@Test
public void add_zero() throws Exception {
assertEquals(5, mUnderTest.add(5, 0));
}
@Test
public void add_negative() throws Exception {
assertEquals(0, mUnderTest.add(1, -1));
assertEquals(-1, mUnderTest.add(1, -2));
}
@Test
public void add_overflow() throws Exception {
assertEquals(Integer.MIN_VALUE, mUnderTest.add(Integer.MAX_VALUE, 1));
}
@Test
public void add_underflow() throws Exception {
assertEquals(Integer.MAX_VALUE, mUnderTest.add(Integer.MIN_VALUE, -1));
}
}
\ No newline at end of file
package com.greysonparrelli.gitlabciandroid;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
\ No newline at end of file
package com.greysonparrelli.gitlabciandroid;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Adds numbers together.
*
* @author Greyson Parrelli (keybase.io/greyson)
*/
public class InputValidatorTest {
private InputValidator mUnderTest;
@Before
public void setup() {
mUnderTest = InputValidator.getInstance();
}
@Test
public void isInt_valid() throws Exception {
assertTrue(mUnderTest.isInt("1"));
assertTrue(mUnderTest.isInt("0"));
assertTrue(mUnderTest.isInt("-1"));
assertTrue(mUnderTest.isInt("15"));
assertTrue(mUnderTest.isInt("-15"));
}
@Test
public void isInt_invalid() throws Exception {
assertFalse(mUnderTest.isInt("spider-man"));
assertFalse(mUnderTest.isInt("a"));
assertFalse(mUnderTest.isInt("?"));
assertFalse(mUnderTest.isInt("--1"));
assertFalse(mUnderTest.isInt("Infinity"));
}
}
\ No newline at end of file
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论