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);     }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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<Token> lex() {
        LinkedList<Token> 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 <filename>");
            return;
        }
 
   
        try {
            String content = new String(Files.readAllBytes(Paths.get(args[0])));
            Lexer lexer = new Lexer(content);
            LinkedList<Token> 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);
    }
 
 
Expert Solution
Step 1: To write unit tests for the Lexer class in your code, you can use a testing framework like JUnit

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Math class and its different methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education