aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiederick de Vries <diederick@diederickdevries.net>2020-03-27 17:50:06 +0100
committerDiederick de Vries <diederick@diederickdevries.net>2020-03-27 17:50:06 +0100
commit1860439252c30963998ec2cfccc4fe14dacd4410 (patch)
tree6037ad0aa8d549cf2bf388248064cebc341d57cf
parent01ed88f6cd2bee988dae1fd3950404051c43bcb4 (diff)
downloadcalcusaurus-1860439252c30963998ec2cfccc4fe14dacd4410.tar.gz
calcusaurus-1860439252c30963998ec2cfccc4fe14dacd4410.tar.bz2
calcusaurus-1860439252c30963998ec2cfccc4fe14dacd4410.zip
feature(view): add dynamic verifier and test
-rw-r--r--src/main/java/net/diederickdevries/calcusaurus/view/inputverifier/ExerciseInputVerifier.java44
-rw-r--r--src/test/java/net/diederickdevries/calcusaurus/view/inputverifier/ExerciseInputVerifierTest.java65
2 files changed, 109 insertions, 0 deletions
diff --git a/src/main/java/net/diederickdevries/calcusaurus/view/inputverifier/ExerciseInputVerifier.java b/src/main/java/net/diederickdevries/calcusaurus/view/inputverifier/ExerciseInputVerifier.java
new file mode 100644
index 0000000..1531cc9
--- /dev/null
+++ b/src/main/java/net/diederickdevries/calcusaurus/view/inputverifier/ExerciseInputVerifier.java
@@ -0,0 +1,44 @@
1/*
2 * Copyright (C) 2019 - 2020 Diederick de Vries <diederick@diederickdevries.net>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18package net.diederickdevries.calcusaurus.view.inputverifier;
19
20import javax.swing.InputVerifier;
21import javax.swing.JComponent;
22import javax.swing.JTextField;
23
24/**
25 * This verifyer returns true if the text of the JComponent given matches the
26 * regex given.
27 *
28 * @author diederick
29 */
30public class ExerciseInputVerifier extends InputVerifier {
31
32 private final String regex;
33
34 public ExerciseInputVerifier(final String regex)
35 {
36 this.regex = regex;
37 }
38
39 @Override
40 public boolean verify(JComponent component) {
41 JTextField textField = (JTextField)component;
42 return textField.getText().matches(regex);
43 }
44}
diff --git a/src/test/java/net/diederickdevries/calcusaurus/view/inputverifier/ExerciseInputVerifierTest.java b/src/test/java/net/diederickdevries/calcusaurus/view/inputverifier/ExerciseInputVerifierTest.java
new file mode 100644
index 0000000..d61d364
--- /dev/null
+++ b/src/test/java/net/diederickdevries/calcusaurus/view/inputverifier/ExerciseInputVerifierTest.java
@@ -0,0 +1,65 @@
1/*
2 * Copyright (C) 2019 - 2020 Diederick de Vries <diederick@diederickdevries.net>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18package net.diederickdevries.calcusaurus.view.inputverifier;
19import javax.swing.JTextField;
20import static org.junit.Assert.*;
21import org.junit.jupiter.api.BeforeEach;
22
23/**
24 *
25 * @author diederick
26 */
27public class ExerciseInputVerifierTest {
28
29 private ExerciseInputVerifier verifier;
30 private JTextField field;
31
32 private final String integerVerification = "^-?\\d+$";
33
34 @BeforeEach
35 public void setUp()
36 {
37 verifier = new ExerciseInputVerifier(integerVerification);
38 field = new JTextField();
39 }
40
41 @org.junit.jupiter.api.Test
42 public void testVerifyInteger() {
43 field.setText("123");
44 assertTrue(verifier.verify(field));
45 }
46
47 @org.junit.jupiter.api.Test
48 public void testVerifyWord() {
49 field.setText("False");
50 assertFalse(verifier.verify(field));
51 }
52
53 @org.junit.jupiter.api.Test
54 public void testVerifyFraction() {
55 field.setText("1.23");
56 assertFalse(verifier.verify(field));
57 }
58
59 @org.junit.jupiter.api.Test
60 public void testVerifyNegative() {
61 field.setText("-1");
62 assertTrue(verifier.verify(field));
63 }
64
65}