From d0376a63e3736622c560f5461bab2660c85554d3 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Thu, 7 Dec 2023 13:42:41 +0100 Subject: [PATCH 1/3] added commandline and stdin tests using testscript --- Makefile | 4 ++-- go.mod | 4 +++- go.sum | 6 ++++++ main.go | 22 ++++++++++++++++++---- main_test.go | 20 ++++++++++++++++++++ t/cmdline-command.txtar | 2 ++ t/cmdline-invalidcommand.txtar | 2 ++ t/cmdline-short-stack.txtar | 2 ++ t/cmdlinecalc-debug.txtar | 2 ++ t/cmdlinecalc-divzero.txtar | 2 ++ t/cmdlinecalc-lua.txtar | 16 ++++++++++++++++ t/cmdlinecalc.txtar | 2 ++ t/getman.txtar | 2 ++ t/getusage.txtar | 2 ++ t/getversion.txtar | 2 ++ t/stdin-batch-cmd.txtar | 4 ++++ t/stdin-batch.txtar | 4 ++++ t/stdin-calc.txtar | 4 ++++ t/stdin-help.txtar | 6 ++++++ t/stdin-use-vars.txtar | 13 +++++++++++++ t/stdint-explicit-batch.txtar | 4 ++++ t/test.lua | 13 +++++++++++++ 22 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 main_test.go create mode 100644 t/cmdline-command.txtar create mode 100644 t/cmdline-invalidcommand.txtar create mode 100644 t/cmdline-short-stack.txtar create mode 100644 t/cmdlinecalc-debug.txtar create mode 100644 t/cmdlinecalc-divzero.txtar create mode 100644 t/cmdlinecalc-lua.txtar create mode 100644 t/cmdlinecalc.txtar create mode 100644 t/getman.txtar create mode 100644 t/getusage.txtar create mode 100644 t/getversion.txtar create mode 100644 t/stdin-batch-cmd.txtar create mode 100644 t/stdin-batch.txtar create mode 100644 t/stdin-calc.txtar create mode 100644 t/stdin-help.txtar create mode 100644 t/stdin-use-vars.txtar create mode 100644 t/stdint-explicit-batch.txtar create mode 100644 t/test.lua diff --git a/Makefile b/Makefile index 848fb3f..6404ab8 100644 --- a/Makefile +++ b/Makefile @@ -57,8 +57,8 @@ test: go test -v ./... singletest: - @echo "Call like this: ''make singletest TEST=TestPrepareColumns" - go test -run $(TEST) + @echo "Call like this: make singletest TEST=TestPrepareColumns ARGS=-v" + go test -run $(TEST) $(ARGS) cover-report: go test ./... -cover -coverprofile=coverage.out diff --git a/go.mod b/go.mod index 486d1d1..9c3c381 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,9 @@ go 1.20 require ( github.com/chzyer/readline v1.5.1 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/yuin/gopher-lua v1.1.0 // indirect - golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect + golang.org/x/tools v0.1.12 // indirect ) diff --git a/go.sum b/go.sum index 1de60da..de11015 100644 --- a/go.sum +++ b/go.sum @@ -2,9 +2,15 @@ github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwys github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI= github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk= github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE= github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng= golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= diff --git a/main.go b/main.go index 3785060..738d126 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ Options: -s, --stack show last 5 items of the stack (off by default) -i --intermediate print intermediate results -m, --manual show manual + -c, --config load containing LUA code -v, --version show version -h, --help show help @@ -51,6 +52,10 @@ this only when working with stdin. E.g.: echo "2 3 4 5" | rpn + Copyright (c) 2023 T.v.Dein` func main() { + os.Exit(Main()) +} + +func Main() int { calc := NewCalc() showversion := false @@ -74,12 +79,12 @@ func main() { if showversion { fmt.Printf("This is rpn version %s\n", VERSION) - return + return 0 } if showhelp { fmt.Println(Usage) - return + return 0 } if enabledebug { @@ -88,7 +93,7 @@ func main() { if showmanual { man() - os.Exit(0) + return 0 } // the lua state object is global, instanciate it early @@ -101,6 +106,13 @@ func main() { luarunner := NewInterpreter(configfile, enabledebug) luarunner.InitLua() calc.SetInt(luarunner) + if calc.debug { + fmt.Println("loaded config") + } + } else { + if calc.debug { + fmt.Println(err) + } } if len(flag.Args()) > 1 { @@ -108,7 +120,7 @@ func main() { // called like rpn 2 2 + calc.stdin = true calc.Eval(strings.Join(flag.Args(), " ")) - return + return 0 } // interactive mode, need readline @@ -152,6 +164,8 @@ func main() { calc.batch = true calc.Eval(flag.Args()[0]) } + + return 0 } func inputIsStdin() bool { diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..1574855 --- /dev/null +++ b/main_test.go @@ -0,0 +1,20 @@ +package main + +import ( + "os" + "testing" + + "github.com/rogpeppe/go-internal/testscript" +) + +func TestMain(m *testing.M) { + os.Exit(testscript.RunMain(m, map[string]func() int{ + "testrpn": Main, + })) +} + +func TestRpn(t *testing.T) { + testscript.Run(t, testscript.Params{ + Dir: "t", + }) +} diff --git a/t/cmdline-command.txtar b/t/cmdline-command.txtar new file mode 100644 index 0000000..681c813 --- /dev/null +++ b/t/cmdline-command.txtar @@ -0,0 +1,2 @@ +exec testrpn 1 2 dump +stdout 'Stack revision 2 .0x' diff --git a/t/cmdline-invalidcommand.txtar b/t/cmdline-invalidcommand.txtar new file mode 100644 index 0000000..f30dbe4 --- /dev/null +++ b/t/cmdline-invalidcommand.txtar @@ -0,0 +1,2 @@ +exec testrpn 1 2 dumb +stdout 'unknown command or operator' diff --git a/t/cmdline-short-stack.txtar b/t/cmdline-short-stack.txtar new file mode 100644 index 0000000..f3ce466 --- /dev/null +++ b/t/cmdline-short-stack.txtar @@ -0,0 +1,2 @@ +exec testrpn 4 + +stdout 'stack doesn''t provide enough arguments' diff --git a/t/cmdlinecalc-debug.txtar b/t/cmdlinecalc-debug.txtar new file mode 100644 index 0000000..d409e18 --- /dev/null +++ b/t/cmdlinecalc-debug.txtar @@ -0,0 +1,2 @@ +exec testrpn -d 44 55 * +stdout 'push to stack: 2420.00\n' diff --git a/t/cmdlinecalc-divzero.txtar b/t/cmdlinecalc-divzero.txtar new file mode 100644 index 0000000..411c636 --- /dev/null +++ b/t/cmdlinecalc-divzero.txtar @@ -0,0 +1,2 @@ +exec testrpn 100 50 50 - / +stdout 'division by null\n' diff --git a/t/cmdlinecalc-lua.txtar b/t/cmdlinecalc-lua.txtar new file mode 100644 index 0000000..16d8a72 --- /dev/null +++ b/t/cmdlinecalc-lua.txtar @@ -0,0 +1,16 @@ +exec testrpn -d -c test.lua 3 5 lower +stdout '3\n' + +-- test.lua -- +function lower(a,b) + if a < b then + return a + else + return b + end +end + +function init() + -- expects 2 args + register("lower", 2, "lower") +end diff --git a/t/cmdlinecalc.txtar b/t/cmdlinecalc.txtar new file mode 100644 index 0000000..f2390d6 --- /dev/null +++ b/t/cmdlinecalc.txtar @@ -0,0 +1,2 @@ +exec testrpn 44 55 * +stdout '2420\n' diff --git a/t/getman.txtar b/t/getman.txtar new file mode 100644 index 0000000..6a91f0c --- /dev/null +++ b/t/getman.txtar @@ -0,0 +1,2 @@ +exec testrpn -m +stdout 'This software is licensed under the GNU GENERAL PUBLIC LICENSE' diff --git a/t/getusage.txtar b/t/getusage.txtar new file mode 100644 index 0000000..86d8f25 --- /dev/null +++ b/t/getusage.txtar @@ -0,0 +1,2 @@ +exec testrpn -h +stdout 'This is rpn' diff --git a/t/getversion.txtar b/t/getversion.txtar new file mode 100644 index 0000000..5ba2efa --- /dev/null +++ b/t/getversion.txtar @@ -0,0 +1,2 @@ +exec testrpn -v +stdout 'This is rpn version' diff --git a/t/stdin-batch-cmd.txtar b/t/stdin-batch-cmd.txtar new file mode 100644 index 0000000..bc108be --- /dev/null +++ b/t/stdin-batch-cmd.txtar @@ -0,0 +1,4 @@ +exec echo 1 2 3 4 5 batch median +stdin stdout +exec testrpn +stdout '3\n' diff --git a/t/stdin-batch.txtar b/t/stdin-batch.txtar new file mode 100644 index 0000000..8505633 --- /dev/null +++ b/t/stdin-batch.txtar @@ -0,0 +1,4 @@ +exec echo 1 2 3 4 5 +stdin stdout +exec testrpn median +stdout '3\n' diff --git a/t/stdin-calc.txtar b/t/stdin-calc.txtar new file mode 100644 index 0000000..bea5944 --- /dev/null +++ b/t/stdin-calc.txtar @@ -0,0 +1,4 @@ +exec echo 10 10 + +stdin stdout +exec testrpn +stdout '20\n' diff --git a/t/stdin-help.txtar b/t/stdin-help.txtar new file mode 100644 index 0000000..bda7a10 --- /dev/null +++ b/t/stdin-help.txtar @@ -0,0 +1,6 @@ +stdin input.txt +exec testrpn +stdout 'Available configuration commands' + +-- input.txt -- +? diff --git a/t/stdin-use-vars.txtar b/t/stdin-use-vars.txtar new file mode 100644 index 0000000..d4dbd1a --- /dev/null +++ b/t/stdin-use-vars.txtar @@ -0,0 +1,13 @@ +stdin input.txt +exec testrpn +stdout '28\n' + +-- input.txt -- +10 +10 ++ +>SUM +clear +8 + Date: Thu, 7 Dec 2023 13:55:12 +0100 Subject: [PATCH 2/3] not supported on windows! --- t/stdin-batch-cmd.txtar | 2 +- t/stdin-batch.txtar | 2 +- t/stdin-calc.txtar | 2 +- t/stdin-help.txtar | 2 +- t/stdin-use-vars.txtar | 2 +- t/stdint-explicit-batch.txtar | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/t/stdin-batch-cmd.txtar b/t/stdin-batch-cmd.txtar index bc108be..af63516 100644 --- a/t/stdin-batch-cmd.txtar +++ b/t/stdin-batch-cmd.txtar @@ -1,4 +1,4 @@ exec echo 1 2 3 4 5 batch median stdin stdout exec testrpn -stdout '3\n' +[unix] stdout '3\n' diff --git a/t/stdin-batch.txtar b/t/stdin-batch.txtar index 8505633..8ffffcb 100644 --- a/t/stdin-batch.txtar +++ b/t/stdin-batch.txtar @@ -1,4 +1,4 @@ exec echo 1 2 3 4 5 stdin stdout exec testrpn median -stdout '3\n' +[unix] stdout '3\n' diff --git a/t/stdin-calc.txtar b/t/stdin-calc.txtar index bea5944..3d3e070 100644 --- a/t/stdin-calc.txtar +++ b/t/stdin-calc.txtar @@ -1,4 +1,4 @@ exec echo 10 10 + stdin stdout exec testrpn -stdout '20\n' +[unix] stdout '20\n' diff --git a/t/stdin-help.txtar b/t/stdin-help.txtar index bda7a10..30c59a0 100644 --- a/t/stdin-help.txtar +++ b/t/stdin-help.txtar @@ -1,6 +1,6 @@ stdin input.txt exec testrpn -stdout 'Available configuration commands' +[unix] stdout 'Available configuration commands' -- input.txt -- ? diff --git a/t/stdin-use-vars.txtar b/t/stdin-use-vars.txtar index d4dbd1a..cc08a7c 100644 --- a/t/stdin-use-vars.txtar +++ b/t/stdin-use-vars.txtar @@ -1,6 +1,6 @@ stdin input.txt exec testrpn -stdout '28\n' +[unix] stdout '28\n' -- input.txt -- 10 diff --git a/t/stdint-explicit-batch.txtar b/t/stdint-explicit-batch.txtar index 6dd4db8..fd6b72a 100644 --- a/t/stdint-explicit-batch.txtar +++ b/t/stdint-explicit-batch.txtar @@ -1,4 +1,4 @@ exec echo 1 2 3 4 5 median stdin stdout exec testrpn -b -stdout '3\n' +[unix] stdout '3\n' From a7fa0def046f7674786cd756ae60b7cdf4a16d68 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Thu, 7 Dec 2023 14:06:11 +0100 Subject: [PATCH 3/3] weird windows panic --- t/stdin-batch.txtar | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/stdin-batch.txtar b/t/stdin-batch.txtar index 8ffffcb..d7391bb 100644 --- a/t/stdin-batch.txtar +++ b/t/stdin-batch.txtar @@ -1,4 +1,4 @@ exec echo 1 2 3 4 5 stdin stdout -exec testrpn median +[unix] exec testrpn median [unix] stdout '3\n'