Created
May 16, 2023 22:24
-
-
Save lukeramsden/ae5f35dbbad29b560ff11b82708151fd to your computer and use it in GitHub Desktop.
GPT's example of a test DSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
createUser "John Doe" | |
addProductToCart "Product ID" 5 | |
checkout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DslInterpreter { | |
private final EcommerceSystem system = new EcommerceSystem(); | |
public void interpret(String script) { | |
String[] lines = script.split("\n"); | |
for (String line : lines) { | |
String[] parts = line.split(" "); | |
switch (parts[0]) { | |
case "createUser": | |
system.createUser(parts[1]); | |
break; | |
case "addProductToCart": | |
system.addProductToCart(parts[1], Integer.parseInt(parts[2])); | |
break; | |
case "checkout": | |
system.checkout(); | |
break; | |
default: | |
throw new IllegalArgumentException("Unknown command: " + parts[0]); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EcommerceSystem { | |
public void createUser(String name) { | |
// implementation | |
} | |
public void addProductToCart(String productId, int quantity) { | |
// implementation | |
} | |
public void checkout() { | |
// implementation | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment