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.6KB

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