|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Patrick Scheibe |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | + * of this software and associated documentation files (the "Software"), to deal |
| 5 | + * in the Software without restriction, including without limitation the rights |
| 6 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | + * copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in |
| 11 | + * all copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | + * THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package de.halirutan.mathematica; |
| 23 | + |
| 24 | +import com.intellij.lexer.Lexer; |
| 25 | +import com.intellij.testFramework.LexerTestCase; |
| 26 | +import de.halirutan.mathematica.lexer.MathematicaLexer; |
| 27 | + |
| 28 | +/** |
| 29 | + * Test small, important code snippets for their lexer output. |
| 30 | + * @author patrick (31.10.16) |
| 31 | + */ |
| 32 | +public class MathematicaLexerTest extends LexerTestCase { |
| 33 | + @Override |
| 34 | + protected Lexer createLexer() { |
| 35 | + return new MathematicaLexer(); |
| 36 | + } |
| 37 | + |
| 38 | + public void testSlots1() throws Exception { |
| 39 | + doTest("#[#Test&, #\"Test\"&]&;", |
| 40 | + "SLOT ('#')\n" + |
| 41 | + "LEFT_BRACKET ('[')\n" + |
| 42 | + "ASSOCIATION_SLOT ('#Test')\n" + |
| 43 | + "FUNCTION ('&')\n" + |
| 44 | + "COMMA (',')\n" + |
| 45 | + "WHITE_SPACE (' ')\n" + |
| 46 | + "ASSOCIATION_SLOT ('#\"Test\"')\n" + |
| 47 | + "FUNCTION ('&')\n" + |
| 48 | + "RIGHT_BRACKET (']')\n" + |
| 49 | + "FUNCTION ('&')\n" + |
| 50 | + "SEMICOLON (';')"); |
| 51 | + } |
| 52 | + |
| 53 | + public void testNumbers() throws Exception { |
| 54 | + doTest("1", "NUMBER ('1')"); |
| 55 | + doTest("1.0", "NUMBER ('1.0')"); |
| 56 | + doTest("16^^abc", "NUMBER ('16^^abc')"); |
| 57 | + doTest("16^^abc.1abc", "NUMBER ('16^^abc.1abc')"); |
| 58 | + doTest("2*^3", "NUMBER ('2*^3')"); |
| 59 | + doTest("2*^-3", "NUMBER ('2*^-3')"); |
| 60 | + doTest("12`", "NUMBER ('12`')"); |
| 61 | + doTest("12`12", "NUMBER ('12`12')"); |
| 62 | + doTest("12``12", "NUMBER ('12``12')"); |
| 63 | + } |
| 64 | + |
| 65 | + public void testRepeatedAmbiguity() throws Exception { |
| 66 | + // Actually, this is not what Mathematica parses which is Repeated[1] |
| 67 | + // but the documentation states that the point for the number 1. should bind stronger |
| 68 | + doTest("1..", "NUMBER ('1.')\n" + "POINT ('.')"); |
| 69 | + |
| 70 | + // These are the workarounds to get Repeated |
| 71 | + doTest("1 ..", "NUMBER ('1')\n" + "WHITE_SPACE (' ')\n" + "REPEATED ('..')"); |
| 72 | + doTest("(1)..", "LEFT_PAR ('(')\n" + "NUMBER ('1')\n" + "RIGHT_PAR (')')\n" + "REPEATED ('..')"); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected String getDirPath() { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Helper method to create and check the lexer output of small code snippets. |
| 82 | + * @param inputCode String with the code to scan |
| 83 | + */ |
| 84 | + @SuppressWarnings("unused") |
| 85 | + private void printLexerOutput(String inputCode) { |
| 86 | + System.out.println(printTokens(inputCode, 0)); |
| 87 | + } |
| 88 | +} |
0 commit comments