Test Mnemonic, simplify class

refs #53
This commit is contained in:
Fabian Becker 2015-12-14 15:46:54 +01:00
parent 0002b5a797
commit 07066578d5
2 changed files with 50 additions and 10 deletions

View File

@ -5,30 +5,26 @@ package eva2.gui;
*/
class Mnemonic {
private char mnemonic;
private String text;
private final char mnemonic;
private final String text;
/**
*
*/
public Mnemonic(String s) {
setString(s);
}
/**
*
*/
public void setString(String s) {
StringBuilder buf = new StringBuilder(s);
char c = Character.MIN_VALUE;
for (int i = 0; i < buf.length(); i++) {
if (buf.charAt(i) == '&') {
buf.deleteCharAt(i);
i++;
if (i < buf.length() && buf.charAt(i) != '&') {
mnemonic = buf.charAt(i - 1);
c = buf.charAt(i - 1);
}
}
}
mnemonic = c;
text = buf.toString();
}

View File

@ -0,0 +1,44 @@
package eva2.gui;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MnemonicTest {
@Test
public void testMnemonic() throws Exception {
Mnemonic m = new Mnemonic("No mnemonic");
// String without ampersand does not have a mnemonic
assertEquals(0, m.getMnemonic());
// Detects ampersand at beginning
m = new Mnemonic("&Fancy");
assertEquals('F', m.getMnemonic());
// Detects ampersand in the middle
m = new Mnemonic("Super&fancy");
assertEquals('f', m.getMnemonic());
// Handles ampersand at the end
m = new Mnemonic("Hellyea&");
assertEquals(0, m.getMnemonic());
}
@Test
public void testGetMnemonic() throws Exception {
Mnemonic m = new Mnemonic("Super &great");
assertEquals('g', m.getMnemonic());
}
@Test
public void testGetText() throws Exception {
Mnemonic m1 = new Mnemonic("The text");
assertEquals("The text", m1.getText());
Mnemonic m2 = new Mnemonic("&Mnemonic");
assertEquals("Mnemonic", m2.getText());
}
}