From 7879bde8bb3a3a15d9268b5ceade65c6091c577b Mon Sep 17 00:00:00 2001 From: Henrixounez Date: Sat, 10 Aug 2019 00:20:36 +0200 Subject: [PATCH] repl: enabled back and added automated tests fixed typos and macos errors --- Makefile | 1 + compiler/main.v | 2 - compiler/tests/repl/README.md | 14 +++++ compiler/tests/repl/arr_decl.repl | 4 ++ compiler/tests/repl/error.repl | 3 ++ compiler/tests/repl/interpolation.repl | 5 ++ compiler/tests/repl/multiple_decl.repl | 10 ++++ compiler/tests/repl/multiple_println.repl | 7 +++ compiler/tests/repl/newlines.repl | 5 ++ compiler/tests/repl/nothing.repl | 1 + compiler/tests/repl/println.repl | 3 ++ compiler/tests/repl/repl.sh | 64 +++++++++++++++++++++++ compiler/tests/repl/var_decl.repl | 4 ++ 13 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 compiler/tests/repl/README.md create mode 100644 compiler/tests/repl/arr_decl.repl create mode 100644 compiler/tests/repl/error.repl create mode 100644 compiler/tests/repl/interpolation.repl create mode 100644 compiler/tests/repl/multiple_decl.repl create mode 100644 compiler/tests/repl/multiple_println.repl create mode 100644 compiler/tests/repl/newlines.repl create mode 100644 compiler/tests/repl/nothing.repl create mode 100644 compiler/tests/repl/println.repl create mode 100755 compiler/tests/repl/repl.sh create mode 100644 compiler/tests/repl/var_decl.repl diff --git a/Makefile b/Makefile index 2ba867e394..3618653a55 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ test: v find . -name '*_test.v' -print0 | xargs -0 -n1 ./v echo "Building V examples..." find examples -name '*.v' -print0 | xargs -0 -n1 ./v + bash ./compiler/tests/repl/repl.sh clean: -rm -f v.c v*.c v.c.out v vprod thirdparty/**/*.o diff --git a/compiler/main.v b/compiler/main.v index 2c26fe0655..6e714e30f0 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -1292,8 +1292,6 @@ fn new_v(args[]string) *V { } fn run_repl() []string { - println('REPL is temporarily disabled, sorry') - exit(1) println('V $Version') println('Use Ctrl-C or `exit` to exit') file := '.vrepl.v' diff --git a/compiler/tests/repl/README.md b/compiler/tests/repl/README.md new file mode 100644 index 0000000000..6120577428 --- /dev/null +++ b/compiler/tests/repl/README.md @@ -0,0 +1,14 @@ +# V REPL Tests Script + +### How to write a new test + - Create a new file named `*.repl` + - Write the input to be given to REPL + - Add `===output===` + - Write the output expected + +### Example : +``` +a := 1 +println(a) +===output=== +1 \ No newline at end of file diff --git a/compiler/tests/repl/arr_decl.repl b/compiler/tests/repl/arr_decl.repl new file mode 100644 index 0000000000..1e7472a04a --- /dev/null +++ b/compiler/tests/repl/arr_decl.repl @@ -0,0 +1,4 @@ +arr := ['1', '2', '3', '4'] +println(arr) +===output=== +["1", "2", "3", "4"] diff --git a/compiler/tests/repl/error.repl b/compiler/tests/repl/error.repl new file mode 100644 index 0000000000..81b1a5663e --- /dev/null +++ b/compiler/tests/repl/error.repl @@ -0,0 +1,3 @@ +println(a) +===output=== +V panic: .vrepl.v:2 undefined: `a` \ No newline at end of file diff --git a/compiler/tests/repl/interpolation.repl b/compiler/tests/repl/interpolation.repl new file mode 100644 index 0000000000..c699b4d866 --- /dev/null +++ b/compiler/tests/repl/interpolation.repl @@ -0,0 +1,5 @@ +a := 'Hello' +b := 'World' +println('$a $b') +===output=== +Hello World diff --git a/compiler/tests/repl/multiple_decl.repl b/compiler/tests/repl/multiple_decl.repl new file mode 100644 index 0000000000..b7702fde71 --- /dev/null +++ b/compiler/tests/repl/multiple_decl.repl @@ -0,0 +1,10 @@ +name := 'Bob' +age := 20 +large_number := i64(9999999999) +println(name) +println(age) +println(large_number) +===output=== +Bob +20 +9999999999 diff --git a/compiler/tests/repl/multiple_println.repl b/compiler/tests/repl/multiple_println.repl new file mode 100644 index 0000000000..48cd3ab260 --- /dev/null +++ b/compiler/tests/repl/multiple_println.repl @@ -0,0 +1,7 @@ +println('Hello World') +println('Foo Bar') +println('dlroW olleH') +===output=== +Hello World +Foo Bar +dlroW olleH diff --git a/compiler/tests/repl/newlines.repl b/compiler/tests/repl/newlines.repl new file mode 100644 index 0000000000..9f00e0b5e3 --- /dev/null +++ b/compiler/tests/repl/newlines.repl @@ -0,0 +1,5 @@ + + + + +===output=== diff --git a/compiler/tests/repl/nothing.repl b/compiler/tests/repl/nothing.repl new file mode 100644 index 0000000000..c02ea0a8dc --- /dev/null +++ b/compiler/tests/repl/nothing.repl @@ -0,0 +1 @@ +===output=== diff --git a/compiler/tests/repl/println.repl b/compiler/tests/repl/println.repl new file mode 100644 index 0000000000..0a5707c9d7 --- /dev/null +++ b/compiler/tests/repl/println.repl @@ -0,0 +1,3 @@ +println('hello world') +===output=== +hello world diff --git a/compiler/tests/repl/repl.sh b/compiler/tests/repl/repl.sh new file mode 100755 index 0000000000..6aa5f014de --- /dev/null +++ b/compiler/tests/repl/repl.sh @@ -0,0 +1,64 @@ +#!/bin/sh + +score=0 +total=0 +debug=0 + +if [ "$1" = "-h" ]; then + echo "V REPL Tests Script" + echo + echo "-d for debug extra outputs" + echo + echo "Reads test files '*.repl_test' and execute them" + echo "See README.md for details on how to write tests" + exit 0 +fi + +if [ "$1" = "-d" ]; then + debug=1 +fi + +test_command() { + if [ $debug -eq 1 ]; then + echo -en "Testing $1: " + fi + + file=`cat $1` + output_start=`echo -e "$file" | grep -n '===output===' | cut -f1 -d:` + + if [ $output_start -gt 1 ]; then + input=`echo -en "$file" | head -n $((output_start - 1))` + else + input="" + fi + + output=`echo -en "$file" | tail -n +$((output_start + 1))` + result=`echo -en "$input" | ./v | tail -n +3 | sed 's/>>> //g'` + + if [ "$output" = "$result" ]; then + score=$(($score + 1)) + if [ $debug -eq 1 ]; then + echo -e "\033[32mOK\033[0m" + fi + else + if [ $debug -eq 0 ]; then + echo -en "REPL: " + fi + echo -e "\033[31mKO\033[0m" + echo -e "\033[1mGot :\033[0m\n$result" + echo -e "\033[1mExpected :\033[0m\n$output" + fi + + total=$(($total + 1)) + rm -f .vrepl .vrepl_temp +} + +for file in `ls compiler/tests/repl/*.repl`; do + test_command $file +done + +echo -e "\033[1mREPL SCORE:" $score "/" $total "\033[0m" + +if [ $score != $total ]; then + exit 1 +fi diff --git a/compiler/tests/repl/var_decl.repl b/compiler/tests/repl/var_decl.repl new file mode 100644 index 0000000000..26571b7e79 --- /dev/null +++ b/compiler/tests/repl/var_decl.repl @@ -0,0 +1,4 @@ +a := 1 +println(a) +===output=== +1