Skip to content

Instantly share code, notes, and snippets.

@lukeramsden
Created May 16, 2023 22:24
Show Gist options
  • Save lukeramsden/ae5f35dbbad29b560ff11b82708151fd to your computer and use it in GitHub Desktop.
Save lukeramsden/ae5f35dbbad29b560ff11b82708151fd to your computer and use it in GitHub Desktop.
GPT's example of a test DSL
createUser "John Doe"
addProductToCart "Product ID" 5
checkout
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]);
}
}
}
}
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