Skip to content

Instantly share code, notes, and snippets.

@fourcube
Last active August 29, 2015 13:55
Show Gist options
  • Save fourcube/8692248 to your computer and use it in GitHub Desktop.
Save fourcube/8692248 to your computer and use it in GitHub Desktop.
package svv;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import static org.mockito.Mockito.*;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.internal.matchers.VarargMatcher;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.StringReader;
import java.util.Arrays;
@RunWith(MockitoJUnitRunner.class)
public class InterpreterImplTest {
InterpreterImpl cut;
@Mock Command c;
@Rule ExpectedException exception = ExpectedException.none();
@Before
public void setUp() {
cut = new InterpreterImpl();
cut.registerCommand("EXEC", c);
}
@Test
public void shouldAcceptInteractiveTokenSequence() {
String interactiveValidStatement = "EXEC foo bA0123456789_-r \";.$§%\"<EOF>";
cut.execute(new StringReader(interactiveValidStatement), true);
verify(c, times(1)).matches(Arrays.asList("foo", "bA0123456789_-r", ";.$§%"));
verify(c, times(1)).execute(Arrays.asList("foo", "bA0123456789_-r", ";.$§%"));
}
@Test
public void shouldAcceptNonInteractiveSequenceTerminator() {
String nonInteractiveValidStatement = "EXEC foo bA0123456789_-r \";.$§%\";";
cut.execute(new StringReader(nonInteractiveValidStatement), false);
verify(c, times(1)).matches(Arrays.asList("foo", "bA0123456789_-r"));
verify(c, times(1)).execute(Arrays.asList("foo", "bA0123456789_-r"));
}
@Test
public void shouldNotAcceptNonEscapedSpecialCharacters() {
String invalidStatement = "EXEC foo \n$%.;";
exception.expect(SyntaxException.class);
try {
cut.execute(new StringReader(invalidStatement), false);
} finally {
verifyZeroInteractions(c);
}
}
@Test
public void shouldNotAcceptArgumentWithoutClosingQuotationMarks() {
String invalidStatement = "EXEC foo \"$ ;";
exception.expect(SyntaxException.class);
try {
cut.execute(new StringReader(invalidStatement), false);
} finally {
verifyZeroInteractions(c);
}
}
@Test
public void interactiveScriptShouldTerminateOnEOF() {
String statement = "EXEC foo bar <EOF>";
cut.execute(new StringReader(statement), true);
}
@Test
public void nonInteractiveScriptShouldTerminateOnEOF() {
String invalidStatement = "EXEC foo bar <EOF>";
cut.execute(new StringReader(invalidStatement), false);
}
@Test
public void shouldNotCallExecuteWhenMatchesReturnsFalse() {
String invalidStatement = "EXEC null;";
when(c.matches(Arrays.asList("null"))).thenReturn(false);
cut.execute(new StringReader(invalidStatement), false);
verify(c,times(1)).matches(Arrays.asList("null"));
verifyNoMoreInteractions(c);
}
@Test
public void shouldCallExecuteWhenMatchesReturnsTrue() {
String validStatement = "EXEC foo bar;";
when(c.matches(Arrays.asList("foo", "bar"))).thenReturn(true);
cut.execute(new StringReader(validStatement), false);
verify(c,times(1)).matches(Arrays.asList("foo", "bar"));
verify(c,times(1)).execute(Arrays.asList("foo", "bar"));
}
// Example usage:
// when(c).execute(argThat(new DerpMatcher<List<String>>()));
public class DerpMatcher<T> extends ArgumentMatcher<T> implements VarargMatcher {
@Override
public boolean matches(Object argument) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment