-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreateJavaUnittest
More file actions
executable file
·99 lines (81 loc) · 4.92 KB
/
createJavaUnittest
File metadata and controls
executable file
·99 lines (81 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
if [ "$#" -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: $(basename $0) [options] [Java file] [optional additional instructions]"
echo ""
echo "Options:"
echo " -f Overwrite existing test file if it exists"
echo " -y systemmsg Use the given string as system message (compare -y for chatgpt script)"
echo " -h, --help Show this help message and exit"
echo ""
echo "Description:"
echo " This script generates a JUnit 4 test for the given Java file using ChatGPT."
echo " The output test file is placed in the corresponding 'src/test/java'"
echo " directory with a 'Test.java' suffix."
exit 0
fi
# BTW: this is interesting as an external tool for IntelliJ with arguments "$FilePath$" "$Prompt$"
# Parse options
force=false
chatgpt_options=""
while getopts ":fy:" opt; do
case $opt in
f)
force=true
;;
y)
chatgpt_options="$chatgpt_options -y $OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# java_file is the first non-option argument
java_file="${@:$OPTIND:1}"
if [[ ! -s "$java_file" || "${java_file##*.}" != "java" ]]; then
echo "Error: Please provide a non-empty Java file."
exit 1
fi
# additional command line arguments after the java_file are put into additionalprompt.
additionalprompt="${@:$OPTIND+1}"
abs_java_file=$(realpath "$java_file")
src_main_java_path="src/main/java"
if [[ ! "$abs_java_file" =~ .*$src_main_java_path.* ]]; then
echo "Error: The Java file path should contain 'src/main/java'."
exit 1
fi
src_test_java_path="src/test/java"
test_file="${abs_java_file//$src_main_java_path/$src_test_java_path}"
test_file="${test_file%.java}Test.java"
if [ -f "$test_file" ] && [ "$force" = false ]; then
echo "Error: Test file already exists: $test_file"
echo "Use -f to overwrite."
exit 1
fi
test_directory=$(dirname "$test_file")
if [ ! -d "$test_directory" ]; then
mkdir -p "$test_directory"
fi
chatgpt_prompt=$(cat <<EOF
End of $(basename "$java_file"). Please create unittests for the public methods of this Java class using JUnit 4 using an ErrorCollector rule named ec and ec.checkThat.
Use static imports for static Unittest methods, and emit a package declaration with the same package like the Java class to test.
Do only print the Java code for the test, absolutely no additional text.
Mockito is present in the classpath, and org.apache.sling.testing.mock.sling.junit.SlingContext, but use only if really needed.
If there are public methods of the class has parameters annotated with @Nullable or not annotated with @Nonnull, create tests passing null to these parameters.
Try to test various cases for each method, e.g. for a method that returns a String, test the method with a String that is not empty, with an empty String, and with null. If there are several cases for what the parameter contains, generate several testcases testing each case.
EOF
)
echo "Word count of input file:"
wc "$java_file"
chatgpt_output=$(cat "$java_file" | chatgpt $chatgpt_options - "$chatgpt_prompt" "additionalprompt" || exit 1)
echo "$chatgpt_output" > "$test_file"
echo "Test file created: $test_file"
# This script was generated using ChatGPT with the following prompt, and then manually edited slightly:
# Please generate a script for the command line for the following task, runnable on a MacBook Pro with Apple M1 Max, that is, arm64 architecture, and MacOS Ventura 13.3.1 . It can be either a bash script using any of the normally present MacOS command line tools or what is installable with homebrew, or a NodeJS script for version 19.5.0 . If NodeJS, it should not require installing any additional libraries. If called without arguments or with the argument
# --help, the script should describe it's usage and exit - including what options and arguments it expects, and (important!) a short description what it does.
# The name of the script is createJavaUnittest.
# In addition to option --help, it should take exactly one argument: a Java file for which ChatGPT is to create an unittest. The Java file should be piped to the command 'chatgpt' , which should get the following arguments:
# chatgpt - "End of (filename). Please create unittests with JUnit 4 using an ErrorCollector
# rule named ec and ec.checkThat; you can use Mockito if mocking is needed. Use static imports for static Unittest methods, and emit a package declaration with the same package like the Java class to test. Do only print the Java code for the test, absolutely no additional text."
# Before executing that, the file should be checked for being an non-empty file with extension .java, and the file's absolute path should be checked to contain src/main/java . If not, the script should print an error message. If it contains src/main/java, the output of the chatgpt command should be written to that filename with src/main/java replaced by src/test/java and .java replaced by Test.java . If that directory does not exist, please create it.