Below is Lexer.java, Token.java, StringHandler.java, and Main.java. I need help in writing test cases. Please write unit tests for lexer.java and make sure to show the full code with the screenshot of the output. Make sure to include the test cases as well. Lexer.java import java.util.LinkedList; public class Lexer { private StringHandler stringHandler; private int lineNumber; private int charPosition; public Lexer(String input) { stringHandler = new StringHandler(input); lineNumber = 1; charPosition = 0; } public LinkedList lex() { LinkedList tokens = new LinkedList<>(); while (!stringHandler.isDone()) { char c = stringHandler.peek(0); if (c == ' ' || c == '\t') { stringHandler.swallow(1); charPosition++; } else if (c == '\n') { tokens.add(new Token(TokenType.SEPARATOR, lineNumber, charPosition)); stringHandler.swallow(1); lineNumber++; charPosition = 0; } else if (Character.isLetter(c)) { tokens.add(processWord()); } else if (Character.isDigit(c)) { tokens.add(processNumber()); } else { throw new RuntimeException("Unrecognized character: " + c); } } return tokens; } private Token processWord() { StringBuilder value = new StringBuilder(); while (!stringHandler.isDone() && (Character.isLetterOrDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '_' || stringHandler.peek(0) == ',')) { value.append(stringHandler.getChar()); charPosition++; } return new Token(TokenType.WORD, value.toString(), lineNumber, charPosition - value.length()); } private Token processNumber() { StringBuilder value = new StringBuilder(); while (!stringHandler.isDone() && (Character.isDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '.')) { value.append(stringHandler.getChar()); charPosition++; } return new Token(TokenType.NUMBER, value.toString(), lineNumber, charPosition - value.length()); } } Token.java enum TokenType { WORD, NUMBER, SEPARATOR } public class Token { private TokenType type; private String value; private int lineNumber; private int charPosition; public Token(TokenType type, int lineNumber, int charPosition) { this.type = type; this.lineNumber = lineNumber; this.charPosition = charPosition; } public Token(TokenType type, String value, int lineNumber, int charPosition) { this(type, lineNumber, charPosition); this.value = value; } public String toString() { if (value == null) { return type.toString(); } else { return type.toString() + "(" + value + ")"; } } } Main.java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.LinkedList; public class Main { public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java Main "); return; } try { String content = new String(Files.readAllBytes(Paths.get(args[0]))); Lexer lexer = new Lexer(content); LinkedList tokens = lexer.lex(); for (Token token : tokens) { System.out.println(token); } } catch (IOException e) { e.printStackTrace(); } } } StringHandler.java public class StringHandler { private String document; private int index; public StringHandler(String document) { this.document = document; this.index = 0; } public char peek(int i) { return document.charAt(index + i); } public String peekString(int i) { return document.substring(index, index + i); } public char getChar() { char c = document.charAt(index); index++; return c; } public void swallow(int i) { index += i; } public boolean isDone() { return index >= document.length(); } public String remainder() { return document.substring(index); }
Below is Lexer.java, Token.java, StringHandler.java, and Main.java. I need help in writing test cases. Please write unit tests for lexer.java and make sure to show the full code with the screenshot of the output. Make sure to include the test cases as well.
Lexer.java
Example of how you can write test cases for the Lexer class:
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class LexerTest {
@Test
public void testLexSimpleInput() {
String input = "Hello, World!\n123";
Lexer lexer = new Lexer(input);
LinkedList<Token> tokens = lexer.lex();
// Check the first token
Token token = tokens.removeFirst();
assertEquals(TokenType.WORD, token.getType());
assertEquals("Hello", token.getValue());
assertEquals(1, token.getLineNumber());
assertEquals(0, token.getCharPosition());
// Check the second token
token = tokens.removeFirst();
assertEquals(TokenType.SEPARATOR, token.getType());
assertEquals("\n", token.getValue());
assertEquals(1, token.getLineNumber());
assertEquals(5, token.getCharPosition());
// Check the third token
token = tokens.removeFirst();
assertEquals(TokenType.WORD, token.getType());
assertEquals("World", token.getValue());
assertEquals(2, token.getLineNumber());
assertEquals(0, token.getCharPosition());
// Check the fourth token
token = tokens.removeFirst();
assertEquals(TokenType.WORD, token.getType());
assertEquals("123", token.getValue());
assertEquals(2, token.getLineNumber());
assertEquals(6, token.getCharPosition());
// Check that all tokens have been processed
assertTrue(tokens.isEmpty());
}
@Test
public void testLexEmptyInput() {
String input = "";
Lexer lexer = new Lexer(input);
LinkedList<Token> tokens = lexer.lex();
// Check that no tokens were generated for an empty input
assertTrue(tokens.isEmpty());
}
@Test
public void testLexInvalidCharacter() {
String input = "Hello @ World!";
Lexer lexer = new Lexer(input);
// Check that an exception is thrown for an unrecognized character
assertThrows(RuntimeException.class, () -> lexer.lex());
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps