Skip to content

Commit 31556a8

Browse files
authored
Added all single-file functionality to the lj script (#79)
* Added testing of files to lj script * Replaced error message of programs not having main with a warning, and stopped the lj script from running programs without a main function * Rebuilt dotests.sh so it works with the lj script where all output is in a single file (fixes #50) * Reset all test outputs to match the new format. * Set the travis file to call dotests.sh over the entire tests directory * Added the exit 1 condition for any failing outputs in dotests.sh (travis will fail incorrect outputs) * Fixed the primitive-numbers test and removed it from the failing directory * Fixed incorrect io tests that didn't match the language and reset their output. * Added .in files for any io tests that required them and were lacking them. * Updated the README
1 parent 63f0124 commit 31556a8

File tree

154 files changed

+359
-623
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+359
-623
lines changed

.travis.yml

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,4 @@ cache:
99
install:
1010
- ./gradlew clean build
1111
script:
12-
- ./compile.sh tests/bracket-access.lj && ./test.sh
13-
- ./compile.sh tests/comp/3np1for.lj && ./test.sh
14-
- ./compile.sh tests/comp/3np1while.lj && ./test.sh
15-
- ./compile.sh tests/comp/different.lj && ./test.sh
16-
- ./compile.sh tests/comp/oddgnome.lj && ./test.sh
17-
- ./compile.sh tests/collections.lj && ./test.sh
18-
- ./compile.sh tests/demo.lj && ./test.sh
19-
- ./compile.sh tests/dog.lj && ./test.sh
20-
- ./compile.sh tests/eqtests.lj && ./test.sh
21-
- ./compile.sh tests/fact.lj && ./test.sh
22-
- ./compile.sh tests/forloop.lj && ./test.sh
23-
- ./compile.sh tests/format.lj && ./test.sh
24-
- ./compile.sh tests/fp.lj && ./test.sh
25-
- ./compile.sh tests/oop.lj && ./test.sh
26-
- ./compile.sh tests/precedence.lj && ./test.sh
27-
- ./compile.sh tests/quicksort.lj && ./test.sh
28-
- ./compile.sh tests/shadow.lj && ./test.sh
29-
- ./compile.sh tests/type-inference.lj && ./test.sh
30-
- ./compile.sh tests/unit-test.lj && ./test.sh
12+
- ./dotests.sh -r tests/

README.md

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,69 +18,31 @@ Gradle wrapper so installing Gradle is not necessary.
1818
```
1919
./gradlew build
2020
````
21-
### Compiling a Less-Java Program
22-
23-
A script is provided to compile any Less-Java program, and there are several
24-
examples in the `tests/` directory. Compiling is fairly straightforward.
21+
### Compiling and Running a Less-Java Program
2522
23+
A script is provided to compile, run, and test any Less-Java program with a single command.
2624
```
27-
./compile.sh <file-name>
25+
./lj <file-name>
2826
````
2927
3028
For example,
3129
3230
```
33-
./compile.sh tests/fact.lj
34-
```
35-
## Running
36-
37-
It is possible to just run the most recently compiled program, or run a specific
38-
previously compiled Less-Java program.
39-
40-
### Running A Less-Java Program
41-
42-
Ensure that the program you wish to run has already been compiled following the
43-
instructions above. To run the most recently compiled or ran program, simply type:
44-
45-
```
46-
./run.sh
47-
```
48-
49-
To run a specific Less-Java program, use the name of the file you compiled. The
50-
extension is optional. For example:
51-
52-
```
53-
./run.sh fact.lj
31+
./lj tests/fact.lj
5432
```
5533
5634
You will need to make sure that your program has a `main()` method if you want to
57-
run it. If it does not have a `main()` method, you will receive an error similar
35+
run it. If it does not have a `main()` method, you will receive a warning similar
5836
to the following.
5937
6038
```
61-
Error: Main method not found in class Main, please define the main method as:
62-
public static void main(String[] args)
63-
or a JavaFX application class must extend javafx.application.Application
64-
```
65-
66-
You can also compile and run a Less-Java program with a single instruction using the
67-
go script. It works the same way as compilation:
68-
69-
```
70-
./go.sh tests/fact.lj
39+
Warning: This program does not contain a main() function and will not be run
7140
```
7241
42+
The program will still be compiled and tested correctly even without a main() method, but no program output
43+
will be released.
7344
##Testing
7445
75-
### Running Tests From A File
76-
77-
Ensure that the file you wish to run the test from has been compiled following the
78-
instructions above. Then simply run:
79-
80-
```
81-
./test.sh
82-
```
83-
8446
### Running All Tests
8547
8648
It is possible to run all the tests in the `tests/` directory and to set the

compile.sh

Lines changed: 0 additions & 51 deletions
This file was deleted.

dotests.sh

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ help() {
1111
echo " If -s option not present, LJ file output will be compared to the previous expected output"
1212
}
1313

14-
# Takes the name of a file. If the input file is a LJ file, compile and run it.
15-
# If the -s option is used, copy the outputs to the expected outputs file.
16-
# Otherwise, show a diff between the expected and actual output.
17-
# If the input file is a directory and the -r option is given, call test on the
18-
# directory's contents
1914
test() {
2015
local file=${1%"/"}
2116
if [ -f $file ] && [[ $file =~ .*\.lj ]]; then
@@ -25,64 +20,40 @@ test() {
2520
outdir=$parent/outputs
2621
base=$outdir/$name
2722
mkdir -p $outdir
28-
echo "Compiling $file"
29-
./compile.sh $file 2>&1 | grep -wvi time > $base\_compile.out
30-
echo "Done compiling"
31-
echo
32-
if [ -n "$(grep "main(" $file)" ]; then
33-
echo "Running $file"
34-
if [ -e "$parent/$name.in" ]; then
35-
./run.sh < "$parent/$name.in" 2>&1 | tee $base\_run.out
36-
else
37-
./run.sh 2>&1 | tee $base\_run.out
38-
fi
39-
echo "Done running"
40-
echo
23+
echo "***************************************"
24+
echo "Compiling, Running, and Testing $file"
25+
if [ -e "$parent/$name.in" ]; then
26+
./lj $file < "$parent/$name.in" 2>&1 > $base.out
27+
else
28+
./lj $file 2>&1 | grep -wvi time > $base.out
4129
fi
42-
echo "Running tests for $file"
43-
./test.sh 2>&1 | grep -wvi time > $base\_test.out
44-
echo "Done testing"
30+
echo "Finished"
4531
echo
4632
if $set_expected; then
47-
cp $base\_compile.out $base\_compile.exp
48-
cp $base\_run.out $base\_run.exp
49-
cp $base\_test.out $base\_test.exp
33+
cp $base.out $base.exp
34+
echo "Setting the expected output of $file to this actual output."
35+
echo
5036
else
51-
touch $base\_compile.exp
52-
touch $base\_run.exp
53-
touch $base\_test.exp
54-
diff -uB $base\_compile.out $base\_compile.exp > $base\_compile.diff
55-
if [ -s $base\_compile.diff ]; then
56-
((changes_found += 1))
57-
echo "Changes detected in compile output; use -s option to set expected output"
37+
#touch $base.out
38+
diff -uB $base.out $base.exp > $base.diff
39+
if [ -s $base.diff ]; then
40+
echo "Differences detected in actual output; use -s option to set the expected output."
5841
echo
59-
cat $base\_compile.diff
42+
cat $base.diff
6043
echo
61-
fi
62-
if [ -s "$(grep "main(" $file)" ]; then
63-
diff -uB $base\_run.out $base\_run.exp > $base\_run.diff
64-
if [ -s $base\_run.diff ]; then
65-
((changes_found += 1))
66-
echo "Changes detected in run output; use -s option to set expected output"
67-
echo
68-
cat $base\_run.diff
69-
echo
70-
fi
71-
fi
72-
diff -uB $base\_test.out $base\_test.exp > $base\_test.diff
73-
if [ -s $base\_test.diff ]; then
74-
((changes_found += 1))
75-
echo "Changes detected in test output; use -s option to set expected output"
76-
echo
77-
cat $base\_test.diff
44+
exit 1
45+
else
46+
echo "No differences detected between expected and actual output."
7847
echo
7948
fi
8049
fi
8150
elif [ -d $file ] && $recursive; then
8251
for sub in $(ls $file); do
83-
test $file/$sub
52+
if [ ! "$(basename $file)" == "failing" ]; then
53+
test $file/$sub
54+
fi
8455
done
85-
fi
56+
fi
8657
}
8758

8859
help_flag=false
@@ -105,7 +76,7 @@ while getopts :hrs flag; do
10576
echo "Unexpected flag $OPTARG" 1>&2
10677
echo
10778
help
108-
exit
79+
exit 1
10980
;;
11081
esac
11182
done
@@ -121,17 +92,9 @@ if [ $# == 0 ]; then
12192
echo "Expecting at least one file" 1>&2
12293
echo
12394
help
124-
exit
95+
exit 1
12596
fi
12697

127-
changes_found=0
128-
129-
# For each input file f:
13098
for f in $@; do
13199
test $f
132100
done
133-
134-
# Show how many changes were detected
135-
if [ $set_expected == false ]; then
136-
echo "Detected $changes_found changed outputs"
137-
fi

lj

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,37 @@ fi
4949

5050
args="${args} -Ptestfile=${1}"
5151
"$GRADLE_WRAPPER" $args
52+
53+
# If the less-java file contains the string "main()", attempt to run it.
54+
# Otherwise, release a warning and continue
55+
parent=$(dirname $1)
5256
base=$(basename $1)
53-
upbase="$(tr '[:lower:]' '[:upper:]' <<< ${base:0:1})${base:1}"
54-
name="LJ${upbase%.*}"
55-
java -cp generated $name
56-
exit
57+
name=${base%.*}
58+
count=$(grep -c "main()" "$1")
59+
if [ "$count" -gt "0" ];
60+
then
61+
if [ -e "$parent/$name.in" ];
62+
then
63+
java -cp generated Main < "$parent/$name.in"
64+
else
65+
java -cp generated Main
66+
fi
67+
else
68+
echo "Warning: This program does not contain a main() function and will not be run"
69+
fi
70+
71+
# --class-path allows specifying where JUnit should look for tests
72+
# --include-classname allows the name to be anything
73+
# --disable-banner disables the banner asking contributing to JUnit
74+
# --scan-class-path checks the full classpath for tests
75+
# Get the output but do not print it yet
76+
test_output="$(java -jar libs/junit-platform-console-standalone-1.4.2.jar --class-path ".:generated" --include-classname='.*' --disable-banner --scan-class-path)"
77+
# Preserve the exit code of the tests
78+
test_status="$?"
79+
80+
# The grep -v filter removes lines that are unnecessary or potentially
81+
# confusing to an end user.
82+
echo "$test_output" | grep -v -e '\[.*containers.*\]' -e 'JUnit Vintage' -e 'Test run finished after'
83+
84+
# Ensure the exit code from the tests is the exit code of the script
85+
exit "$test_status"

run.sh

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/com/github/lessjava/LJCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static void main(String[] args) throws IOException {
7878
BuildSymbolTables buildSymbolTables = new BuildSymbolTables();
7979
LJASTInferTypes inferTypes = new LJASTInferTypes();
8080
// PrintDebugTree printTree = new PrintDebugTree();
81-
LJGenerateJava generateJava = new LJGenerateJava(fileName);
81+
LJGenerateJava generateJava = new LJGenerateJava();
8282
LJASTCheckTypesHaveChanged checkTypesHaveChanged = new LJASTCheckTypesHaveChanged();
8383
LJInstantiateFunctions instantiateFunctions = new LJInstantiateFunctions();
8484
LJASTInferConstructors inferConstructors = new LJASTInferConstructors();

src/main/java/com/github/lessjava/visitor/impl/LJGenerateJava.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@
4545

4646
public class LJGenerateJava extends LJDefaultASTVisitor {
4747

48-
private String baseName = null;
49-
50-
public LJGenerateJava(String filename)
48+
public LJGenerateJava()
5149
{
52-
String[] parts = filename.split("[\\.\\\\\\/]");
53-
baseName = parts[parts.length-2];
54-
baseName = baseName.substring(0, 1).toUpperCase() + baseName.substring(1);
55-
mainFile = Paths.get("generated/LJ" + baseName + ".java");
50+
mainFile = Paths.get("generated/Main.java");
5651
}
5752

5853
// Create generated directory if it doesn't exist
@@ -100,7 +95,7 @@ public void preVisit(ASTProgram node) {
10095

10196
lines.addAll(Arrays.asList(imports));
10297

103-
lines.add("public class LJ" + baseName);
98+
lines.add("public class Main");
10499
lines.add("{");
105100
}
106101

test.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)