You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LexerTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.calc;
  23. import java.text.ParseException;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import org.junit.Test;
  27. import static org.junit.Assert.*;
  28. public class LexerTest {
  29. @Test
  30. public void testBasicNumber() throws ParseException {
  31. final Lexer lexer = new Lexer("123");
  32. final List<Token> tokens = lexer.tokenise();
  33. assertEquals(3, tokens.size());
  34. assertEquals(TokenType.START, tokens.get(0).getType());
  35. assertEquals(TokenType.NUMBER_INT, tokens.get(1).getType());
  36. assertEquals("123", tokens.get(1).getContent());
  37. assertEquals(TokenType.END, tokens.get(2).getType());
  38. }
  39. @Test
  40. public void testComplexString() throws ParseException {
  41. final Lexer lexer = new Lexer("(123 / 2.0) * ((3)+\t (-1))");
  42. final List<Token> tokens = lexer.tokenise();
  43. assertEquals(18, tokens.size());
  44. assertEquals(TokenType.START, tokens.get(0).getType());
  45. assertEquals(TokenType.BRACKET_OPEN, tokens.get(1).getType());
  46. assertEquals(TokenType.NUMBER_INT, tokens.get(2).getType());
  47. assertEquals("123", tokens.get(2).getContent());
  48. assertEquals(TokenType.OP_DIVIDE, tokens.get(3).getType());
  49. assertEquals(TokenType.NUMBER_FLOAT, tokens.get(4).getType());
  50. assertEquals("2.0", tokens.get(4).getContent());
  51. assertEquals(TokenType.BRACKET_CLOSE, tokens.get(5).getType());
  52. assertEquals(TokenType.OP_MULT, tokens.get(6).getType());
  53. assertEquals(TokenType.BRACKET_OPEN, tokens.get(7).getType());
  54. assertEquals(TokenType.BRACKET_OPEN, tokens.get(8).getType());
  55. assertEquals(TokenType.NUMBER_INT, tokens.get(9).getType());
  56. assertEquals("3", tokens.get(9).getContent());
  57. assertEquals(TokenType.BRACKET_CLOSE, tokens.get(10).getType());
  58. assertEquals(TokenType.OP_PLUS, tokens.get(11).getType());
  59. assertEquals(TokenType.BRACKET_OPEN, tokens.get(12).getType());
  60. assertEquals(TokenType.MOD_NEGATIVE, tokens.get(13).getType());
  61. assertEquals(TokenType.NUMBER_INT, tokens.get(14).getType());
  62. assertEquals("1", tokens.get(14).getContent());
  63. assertEquals(TokenType.BRACKET_CLOSE, tokens.get(15).getType());
  64. assertEquals(TokenType.BRACKET_CLOSE, tokens.get(16).getType());
  65. assertEquals(TokenType.END, tokens.get(17).getType());
  66. }
  67. @Test
  68. public void testBrackets() throws ParseException {
  69. final Lexer lexer = new Lexer("((1))");
  70. final List<Token> tokens = lexer.tokenise();
  71. assertEquals(7, tokens.size());
  72. assertEquals(TokenType.START, tokens.get(0).getType());
  73. assertEquals(TokenType.BRACKET_OPEN, tokens.get(1).getType());
  74. assertEquals(TokenType.BRACKET_OPEN, tokens.get(2).getType());
  75. assertEquals(TokenType.NUMBER_INT, tokens.get(3).getType());
  76. assertEquals(TokenType.BRACKET_CLOSE, tokens.get(4).getType());
  77. assertEquals(TokenType.BRACKET_CLOSE, tokens.get(5).getType());
  78. assertEquals(TokenType.END, tokens.get(6).getType());
  79. }
  80. @Test
  81. public void testImplicitMult() throws ParseException {
  82. final Lexer lexer = new Lexer("1(2)(3)");
  83. final List<Token> tokens = lexer.tokenise();
  84. assertEquals(11, tokens.size());
  85. assertEquals(TokenType.START, tokens.get(0).getType());
  86. assertEquals(TokenType.NUMBER_INT, tokens.get(1).getType());
  87. assertEquals(TokenType.OP_MULT, tokens.get(2).getType());
  88. assertEquals(TokenType.BRACKET_OPEN, tokens.get(3).getType());
  89. assertEquals(TokenType.NUMBER_INT, tokens.get(4).getType());
  90. assertEquals(TokenType.BRACKET_CLOSE, tokens.get(5).getType());
  91. assertEquals(TokenType.OP_MULT, tokens.get(6).getType());
  92. assertEquals(TokenType.BRACKET_OPEN, tokens.get(7).getType());
  93. assertEquals(TokenType.NUMBER_INT, tokens.get(8).getType());
  94. assertEquals(TokenType.BRACKET_CLOSE, tokens.get(9).getType());
  95. assertEquals(TokenType.END, tokens.get(10).getType());
  96. }
  97. private void doIllegalTest(final String input, final int offset) {
  98. try {
  99. fail(Arrays.toString(new Lexer(input).tokenise().toArray()));
  100. } catch (ParseException ex) {
  101. assertEquals(offset, ex.getErrorOffset());
  102. }
  103. }
  104. @Test public void testIllegalEnd1() { doIllegalTest("", 0); }
  105. @Test public void testIllegalEnd2() { doIllegalTest("3+", 2); }
  106. @Test public void testIllegalEnd3() { doIllegalTest("(", 1); }
  107. @Test public void testIllegalOp1() { doIllegalTest("1++2", 2); }
  108. @Test public void testIllegalOp2() { doIllegalTest("*1", 0); }
  109. }