From 266929c6c4dccde959132bc3f13825f67956df6e Mon Sep 17 00:00:00 2001 From: Mark Umnus Date: Sun, 3 Nov 2019 01:51:58 +0100 Subject: [PATCH] Add more test cases (for whitespace, mainly) --- src/com/merkrafter/lexing/ScannerTest.java | 61 +++++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/com/merkrafter/lexing/ScannerTest.java b/src/com/merkrafter/lexing/ScannerTest.java index f5a5148a..95e3aea7 100644 --- a/src/com/merkrafter/lexing/ScannerTest.java +++ b/src/com/merkrafter/lexing/ScannerTest.java @@ -54,13 +54,59 @@ void scanSingleIdentifier() { shouldScan(programCode, expectedTokenList); } + /** + * The scanner should be able to handle a string containing a single identifier that also has + * numbers. + */ + @org.junit.jupiter.api.Test + void scanSingleIdentifierWithNumbers() { + final String programCode = "x1"; + final TokenType[] expectedTokenList = {IDENT, EOF}; + shouldScan(programCode, expectedTokenList); + } + + /** + * The scanner should be able to handle a string containing a single identifier that has mixed + * case letters. + */ + @org.junit.jupiter.api.Test + void scanSingleIdentifierWithMixedCase() { + final String programCode = "xXcoolNameXx"; + final TokenType[] expectedTokenList = {IDENT, EOF}; + shouldScan(programCode, expectedTokenList); + } + /** * The scanner should be able to handle an assignment operation where the right side is a * complex calculation including all four basic arithmetic operations as well as parentheses. + * The program code makes extended use of spaces around operators. */ @org.junit.jupiter.api.Test - void scanAssignment() { - final String programCode = "int result = a+(b-c)*d/e;"; + void scanAssignmentWithSpaces() { + final String programCode = "int result = a + ( b - c ) * d / e;"; + final TokenType[] expectedTokenList = {IDENT, IDENT, ASSIGN, IDENT, PLUS, L_PAREN, IDENT, MINUS, IDENT, R_PAREN, TIMES, IDENT, DIVIDE, IDENT, SEMICOLON, EOF}; + shouldScan(programCode, expectedTokenList); + } + + /** + * The scanner should be able to handle a simple assignment operation. + * The program code makes extended use of whitespace (spaces, tabs and newlines). + */ + @org.junit.jupiter.api.Test + void scanSimpleAssignmentWithWhitespace() { + final String programCode = "int\n\t a \n=\n5\t\t\t ;"; + final TokenType[] expectedTokenList = {IDENT, IDENT, ASSIGN, NUMBER, SEMICOLON, EOF}; + shouldScan(programCode, expectedTokenList); + } + + /** + * The scanner should be able to handle an assignment operation where the right side is a + * complex calculation including all four basic arithmetic operations as well as parentheses. + * The program code only has mandatory whitespace. + */ + @org.junit.jupiter.api.Test + void scanAssignmentWithoutWhitespace() { + final String programCode = "int result=a+(b-c)*d/e;"; final TokenType[] expectedTokenList = {IDENT, IDENT, ASSIGN, IDENT, PLUS, L_PAREN, IDENT, MINUS, IDENT, R_PAREN, TIMES, IDENT, DIVIDE, IDENT, SEMICOLON, EOF}; shouldScan(programCode, expectedTokenList); } @@ -75,6 +121,17 @@ void scanAndIgnoreComments() { shouldScan(programCode, expectedTokenList); } + /** + * The scanner should be able to handle asterisks in comments. That is, it should not stop + * processing the comment then. + */ + @org.junit.jupiter.api.Test + void scanAndIgnoreAsterisksInComments() { + final String programCode = "/***/"; + final TokenType[] expectedTokenList = {EOF}; + shouldScan(programCode, expectedTokenList); + } + /** * The scanner should be able to handle a basic main function including an array as arguments. * This test includes the detection of square brackets and braces.