TokenTypesExpression extends NativeExpression implements Matcher { @Override public void execute(Machine machine) { if (machine.length() == 0 || !types.contains(machine.tokenAt(0).getType())) { machine.backtrack(); return; } machine.createLeafNode(this, 1); machine.jump(1); } TokenTypesExpression(TokenType... types); @Override void execute(Machine machine); @Override String toString();  }
@Test public void should_match() { Token token = mock(Token.class); when(token.getType()).thenReturn(type1); when(machine.length()).thenReturn(1); when(machine.tokenAt(0)).thenReturn(token); expression.execute(machine); InOrder inOrder = Mockito.inOrder(machine); inOrder.verify(machine).length(); inOrder.verify(machine).tokenAt(0); inOrder.verify(machine).createLeafNode(expression, 1); inOrder.verify(machine).jump(1); verifyNoMoreInteractions(machine); }
@Test public void should_backtrack() { when(machine.length()).thenReturn(0); expression.execute(machine); InOrder inOrder = Mockito.inOrder(machine); inOrder.verify(machine).length(); inOrder.verify(machine).backtrack(); verifyNoMoreInteractions(machine); }
@Test public void should_backtrack2() { Token token = mock(Token.class); when(token.getType()).thenReturn(mock(TokenType.class)); when(machine.length()).thenReturn(1); when(machine.tokenAt(0)).thenReturn(token); expression.execute(machine); InOrder inOrder = Mockito.inOrder(machine); inOrder.verify(machine).length(); inOrder.verify(machine).tokenAt(0); inOrder.verify(machine).backtrack(); verifyNoMoreInteractions(machine); }