Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GPT + TDD: Create Doclet Parser

GPT + TDD: Create Doclet Parser

Julio Faerman

March 20, 2023
Tweet

More Decks by Julio Faerman

Other Decks in Education

Transcript

  1. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 1/29
    Help me write a test class in Java. I will give you the requirements and you should
    write me the test class when i say "i'm done"
    Sure, please provide the requirements for the class that you want to test.
    First, name the test class DocletParserTest and the class under test DocletParser
    Got it. Please continue with the requirements for the DocletParser class and any

    View Slide

  2. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 2/29
    specific methods or scenarios you want me to cover in the test class.
    Let the parser accept a String or a Text file
    Understood. Please provide the methods available in the DocletParser class for
    parsing a String and a Text file, and any additional requirements or scenarios you
    want me to cover in the test class.

    View Slide

  3. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 3/29
    In the content, search for code comments. accept comments in either bash, java or xml
    style
    Great, now I understand that the DocletParser class should have methods to parse
    content and find comments in the bash, Java, or XML style. I'll assume the following
    methods for the DocletParser class:
    1. parseString(String content)
    : This method will accept a String and parse it for
    comments.
    2. parseFile(File file)
    : This method will accept a File object and parse it for
    comments.
    Here is the test class DocletParserTest
    :
    java Copy code
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    public class DocletParserTest {

    View Slide

  4. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 4/29
    private DocletParser parser;
    @BeforeEach
    public void setUp() {
    parser = new DocletParser();
    }
    @Test
    public void testParseStringJava() {
    String content = "/* This is a Java comment */\n" +
    "public class HelloWorld {}";
    assertEquals("/* This is a Java comment */", parser.parseString
    }
    @Test
    public void testParseStringBash() {
    String content = "# This is a Bash comment\n" +
    "echo \"Hello, World!\"";
    assertEquals("# This is a Bash comment", parser.parseString(con
    }
    @Test
    public void testParseStringXML() {
    String content = "\n" +
    "";
    assertEquals("", parser.parse

    View Slide

  5. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 5/29
    let the parser return only the content of the comment, not the content delimiters
    Understood. I've updated the test cases in the DocletParserTest
    class to
    reflect the requirement of returning only the content of the comments, without the
    comment delimiters:
    java Copy code
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import static org.junit.jupiter.api.Assertions.assertEquals;

    View Slide

  6. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 6/29
    public class DocletParserTest {
    private DocletParser parser;
    @BeforeEach
    public void setUp() {
    parser = new DocletParser();
    }
    @Test
    public void testParseStringJava() {
    String content = "/* This is a Java comment */\n" +
    "public class HelloWorld {}";
    assertEquals("This is a Java comment", parser.parseString(
    }
    @Test
    public void testParseStringBash() {
    String content = "# This is a Bash comment\n" +
    "echo \"Hello, World!\"";
    assertEquals("This is a Bash comment", parser.parseString(
    }
    @Test
    public void testParseStringXML() {
    String content = "\n" +
    "";
    assertEquals("This is an XML comment", parser.parseString(
    }

    View Slide

  7. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 7/29
    // Add test cases for the parseFile method with different file
    }
    Please note that you'll need to implement the parseString
    and parseFile
    add a method where there is no comment at all
    I added a new test case to the DocletParserTest
    class to test the scenario
    where there is no comment in the content. The expected result should be an empty
    string or null
    depending on your implementation:
    java Copy code

    View Slide

  8. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 8/29
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    public class DocletParserTest {
    private DocletParser parser;
    @BeforeEach
    public void setUp() {
    parser = new DocletParser();
    }
    @Test
    public void testParseStringJava() {
    String content = "/* This is a Java comment */\n" +
    "public class HelloWorld {}";
    assertEquals("This is a Java comment", parser.parseString(
    }
    @Test
    public void testParseStringBash() {
    String content = "# This is a Bash comment\n" +
    "echo \"Hello, World!\"";
    assertEquals("This is a Bash comment", parser.parseString(

    View Slide

  9. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 9/29
    }
    @Test
    public void testParseStringXML() {
    String content = "\n" +
    "";
    assertEquals("This is an XML comment", parser.parseString(
    }
    @
    consider a doclet to be a instruction inside comments in the format "$key:
    $value" where $key must be a single word and $value is the remainder of the
    comment. the key may be preceded by spaces. create a new record to represent a
    doclet. make the Parser return List containing the doclet, if found, or empty

    View Slide

  10. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 10/29
    list if not found. for example in the string "# assert: something else" the key would be
    "assert" and the value "something else"
    Based on your requirements, I have created a Doclet
    record to represent the
    doclet and updated the DocletParserTest
    class to include tests for parsing
    doclets inside comments. The DocletParser
    class is now expected to return a list of
    Doclet
    objects, or an empty list if no doclet is found.
    Here's the updated test class:
    java Copy code
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.util.List;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    public class DocletParserTest {
    private DocletParser parser;
    @BeforeEach
    public void setUp() {

    View Slide

  11. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 11/29
    parser = new DocletParser();
    }
    @Test
    public void testParseStringJava() {
    String content = "/* $assert: This is a Java doclet */\n"
    "public class HelloWorld {}";
    List doclets = parser.parseString(content);
    assertEquals(1, doclets.size());
    assertEquals("assert", doclets.get(0).key());
    assertEquals("This is a Java doclet", doc
    Use CDI, adding @ApplicationScoped and @Inject instead of @BeforeEach

    View Slide

  12. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 12/29
    I've updated the DocletParserTest
    the key does not need to be preceded by $
    I've updated the DocletParserTest
    class to consider the key without the $
    symbol. Here's the revised test class using CDI with @ApplicationScoped
    and
    @Inject
    annotations:
    java Copy code
    import org.junit.jupiter.api.Test;
    import javax.inject.Inject;
    import javax.enterprise.context.ApplicationScoped;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;

    View Slide

  13. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 13/29
    import java.nio.file.Path;
    import java.util.List;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    @ApplicationScoped
    public class DocletParserTest {
    @Inject
    private DocletParser parser;
    @Test
    public void testParseStringJava() {
    String content = "/* assert: This is a Java doclet */\n"
    "public class HelloWorld {}";
    List doclets = parser.parseString(content);
    assertEquals(1, doclets.size());
    assertEquals("assert", doclets.get(0).key());
    assertEquals("This is a Java doclet", doclets.get(0).valu
    }
    @Test
    public void testParseStringBash() {
    String content = "# assert: This is a Bash doclet\n" +
    "echo \"Hello, World!\"";
    List doclets = parser.parseString(content);
    assertEquals(1, doclets.size());
    assertEquals("assert", doclets.get(0).key());
    assertEquals("This is a Bash doclet", doclets.get(0).valu

    View Slide

  14. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 14/29
    }
    @Test
    public void testParseStringXML() {
    String content = "\
    "";
    List doclets = parser.parseString(content);
    first write me the test declaration, then each method separately
    Here's the test class declaration with CDI annotations and the injected
    DocletParser
    :

    View Slide

  15. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 15/29
    java Copy code
    import org.junit.jupiter.api.Test;
    import javax.inject.Inject;
    import javax.enterprise.context.ApplicationScoped;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.util.List;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    @ApplicationScoped
    public class DocletParserTest {
    @Inject
    private DocletParser parser;
    }
    Now, I'll provide each test method separately.
    1. Test for Java-style comments:
    java Copy code

    View Slide

  16. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 16/29
    @Test
    public void testParseStringJava() {
    String content = "/* assert: This is a Java doclet */\n" +
    "public class HelloWorld {}";
    List doclets = parser.parseString(content);
    assertEquals(1, doclets.size());
    assertEquals("assert", doclets.get(0).key());
    assertEquals("This is a Java doclet", doclets.get(0).value())
    }
    2. Test for Bash-style comments:
    java Copy code
    @Test
    public void testParseStringBash() {
    String content = "# assert: This is a Bash doclet\n" +
    "echo \"Hello, World!\"";
    List doclets = parser.parseString(content);
    assertEquals(1, doclets.size());
    assertEquals("assert", doclets.get(0).key());
    assertEquals("This

    View Slide

  17. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 17/29

    View Slide

  18. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 18/29
    show me the Doclet record
    Here's the Doclet
    record that represents a doclet with a key and a value:
    java Copy code
    public record Doclet(String key, String value) {
    }
    This record will store the key and value of a doclet found in the comments. The
    DocletParser
    class is expected to return a list of Doclet
    objects when parsing the
    content.

    View Slide

  19. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 19/29
    now testParseStringBash please
    Here's the test method for Bash-style comments:
    java Copy code
    @Test
    public void testParseStringBash() {
    String content = "# assert: This is a Bash doclet\n" +
    "echo \"Hello, World!\"";
    List doclets = parser.parseString(content);
    assertEquals(1, doclets.size());
    assertEquals("assert", doclets.get(0).key());
    assertEquals("This is a Bash doclet", doclets.get(0).value())
    }

    View Slide

  20. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 20/29
    This test method checks whether the DocletParser
    correctly parses a Bash-style
    comment containing a doclet and returns a list with a single Doclet
    object having the
    expected key and value.
    now the testParseStringXml and the testParseStringWithoutDoclets
    Here's the test method for XML-style comments:
    java Copy code
    @Test
    public void testParseStringXML() {
    String content = "\n" +
    "";
    List doclets = parser.parseString(content);
    assertEquals(1, doclets.size());

    View Slide

  21. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 21/29
    assertEquals("assert", doclets.get(0).key());
    assertEquals("This is an XML doclet", doclets.get(0).value())
    }
    And here's the test method for a scenario without any doclets:
    java Copy code
    @Test
    public void testParseStringWithoutDoclets() {
    String content = "/* This is a regular Java comment */\n" +
    "public class HelloWorld {}";
    List doclets = parser.parseString(content);
    assertEquals(0, doclets.size());
    }
    These test methods check whether the DocletParser
    correctly parses an XML-style
    comment containing a doclet and a string without any doclets, returning the expected
    Doclet
    objects or an empty list, respectively.

    View Slide

  22. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 22/29
    now please implement the parser class that would pass this test: package doclet; import
    javax.inject.Inject; import javax.enterprise.context.ApplicationScoped; import java.io.File
    java.nio.file.Files; import java.nio.file.Path; import java.util.List; import static org.junit.jupiter.ap
    @ApplicationScoped public class DocletParserTest { @Inject private DocletParser parser; @Te
    String content = "/* assert: This is a Java doclet */\n" + "public class HelloWorld {}"; Listparser.parseString(content); assertEquals(1, doclets.size()); assertEquals("assert", doclets.ge
    Java doclet", doclets.get(0).value()); } @Test public void testParseStringBash() { String conten
    + "echo \"Hello, World!\""; List doclets = parser.parseString(content); assertEquals(1
    assertEquals("assert", doclets.get(0).key()); assertEquals("This is a Bash doclet", doclets.get
    testParseStringXML() { String content = "\n" + "
    parser.parseString(content); assertEquals(1, doclets.size()); assertEquals("assert", doclets.ge
    XML doclet", doclets.get(0).value()); } @Test public void testParseStringWithoutDoclets() { Str
    Java comment */\n" + "public class HelloWorld {}"; List doclets = parser.parseString(c
    doclets.size()); } }
    Here's the implementation of the DocletParser
    class that should pass the provided te

    View Slide

  23. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 23/29
    java Copy code
    package doclet;
    import javax.enterprise.context.ApplicationScoped;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    @ApplicationScoped
    public class DocletParser {
    private static final Pattern JAVA_COMMENT_PATTERN = Pattern.compil
    private static final Pattern BASH_COMMENT_PATTERN = Pattern.compil
    private static final Pattern XML_COMMENT_PATTERN = Pattern.compile
    private static final Pattern DOCLET_PATTERN = Pattern.compile("^\\
    public List parseString(String content) {
    List doclets = new ArrayList<>();
    Matcher javaMatcher = JAVA_COMMENT_PATTERN.matcher(content);
    Matcher bashMatcher = BASH_COMMENT_PATTERN.matcher(content);
    Matcher xmlMatcher = XML_COMMENT_PATTERN.matcher(content);

    View Slide

  24. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 24/29
    extractDoclets(javaMatcher, doclets
    use var declarations where possible
    Here's the updated implementation of the DocletParser
    class using var
    declarations
    java Copy code
    package doclet;
    import javax.enterprise.context.ApplicationScoped;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;

    View Slide

  25. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 25/29
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    @ApplicationScoped
    public class DocletParser {
    private static final Pattern JAVA_COMMENT_PATTERN = Pattern.compil
    private static final Pattern BASH_COMMENT_PATTERN = Pattern.compil
    private static final Pattern XML_COMMENT_PATTERN = Pattern.compile
    private static final Pattern DOCLET_PATTERN = Pattern.compile("^\\
    public List parseString(String content) {
    var doclets = new ArrayList();
    var javaMatcher = JAVA_COMMENT_PATTERN.matcher(content);
    var bashMatcher = BASH_COMMENT_PATTERN.matcher(content);
    var xmlMatcher = XML_COMMENT_PATTERN.matcher(content);
    extractDoclets(javaMatcher, doclets);
    extractDoclets(bashMatcher, doclets);
    extractDoclets(xmlMatcher, doclets);
    return doclets;
    }
    private void extractDoclets(Matcher matcher, List doclets)

    View Slide

  26. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 26/29
    while (matcher.find()) {
    var comment = matcher.group(1);
    var docletMatcher = DOCLET_PATTERN.matcher(comment);
    while (docletMatcher.find()) {
    var key = docletMatcher.group(1);
    var value = docletMatcher.group(2);
    doc
    write only the extractDoclets method
    Here's the extractDoclets
    method in isolation:
    java Copy code

    View Slide

  27. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 27/29
    private void extractDoclets(Matcher matcher, List doclets
    while (matcher.find()) {
    var comment = matcher.group(1);
    var docletMatcher = DOCLET_PATTERN.matcher(comment);
    while (docletMatcher.find()) {
    var key = docletMatcher.group(1);
    var value = docletMatcher.group(2);
    doclets.add(new Doclet(key, value));
    }
    }
    }
    This method takes a Matcher
    object that matches comments and a List
    object to store the extracted doclets. It iterates through the matched comments, finds
    the doclets within the comments, and adds them to the provided list.

    View Slide

  28. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 28/29
    thanks! how can i share this conversation?
    You're welcome! To share this conversation, you can simply copy the URL from
    your web browser's address bar and share it with whoever you'd like to share it
    with. This will allow others to view the entire conversation between us.
    print me that url
    I'm sorry, as an AI language model, I don't have the capability to access your web
    browser's address bar or any external services. However, you can simply copy the
    URL from the address bar at the top of your web browser to share this conversation.

    View Slide

  29. 20/03/2023, 12:03 Export ChatGPT Conversation As Images
    https://gptsave.xyz/images?id=9d1bf121-e14a-48af-ba1b-08e65f660055 29/29
    Unable to load conversation 4d802020-7cd2-4e01-9d9b-3199807fc3f1
    I apologize for the inconvenience. As an AI language model, I don't have the
    capability to store conversations or access past conversations. Once the
    conversation is completed, it cannot be accessed again. If you have any other
    questions or concerns, feel free to ask!
    Buy me a coffee
    © 2023 GPTSave, all rights reserved

    View Slide