Repository: robertkrimen/otto
Pull request #419 (2021-09-27 20:25:03)

test: add test for issue #186
Add a test which exercises issue #186

Also:
* Quote types in "can't convert from %q to %q" errors.


Patch:
diff --git a/go.mod b/go.mod
index e4689b8..05fdf00 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.14
 require (
 	github.com/chzyer/logex v1.1.10 // indirect
 	github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
+	github.com/stretchr/testify v1.7.0 // indirect
 	gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375
 	gopkg.in/sourcemap.v1 v1.0.5
 )
diff --git a/go.sum b/go.sum
index aa2319a..79bb53a 100644
--- a/go.sum
+++ b/go.sum
@@ -2,7 +2,17 @@ github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
 github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
 github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
 github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375 h1:hPki/oSSWOLiI9Gc9jyIoj33O3j29fUc9PlLha2yDj0=
 gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375/go.mod h1:lNEQeAhU009zbRxng+XOj5ITVgY24WcbNnQopyfKoYQ=
 gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
 gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/runtime.go b/runtime.go
index 5c400c6..34983d5 100644
--- a/runtime.go
+++ b/runtime.go
@@ -620,7 +620,7 @@ func (self *_runtime) convertCallParameter(v Value, t reflect.Type) (reflect.Val
 		s = v.Class()
 	}
 
-	return reflect.Zero(t), fmt.Errorf("can't convert from %s to %s", s, t)
+	return reflect.Zero(t), fmt.Errorf("can't convert from %q to %q", s, t)
 }
 
 func (self *_runtime) toValue(value interface{}) Value {


Test patch:
diff --git a/bug_test.go b/bug_test.go
index 811dc85..8dd1788 100644
--- a/bug_test.go
+++ b/bug_test.go
@@ -1,8 +1,11 @@
 package otto
 
 import (
+	"errors"
 	"testing"
 	"time"
+
+	"github.com/stretchr/testify/require"
 )
 
 func Test_issue116(t *testing.T) {
@@ -693,3 +696,55 @@ func Test_issue234(t *testing.T) {
 		`, "6,E")
 	})
 }
+
+func Test_issue186(t *testing.T) {
+	tests := []struct {
+		name   string
+		script string
+		err    error
+	}{
+		{
+			name:   "missing",
+			script: `abc("a","b")`,
+			err:    errors.New("RangeError: expected 3 argument(s); got 2"),
+		},
+		{
+			name:   "too-many",
+			script: `abc("a","b","c","d")`,
+			err:    errors.New("RangeError: expected 3 argument(s); got 4"),
+		},
+		{
+			name:   "valid-conversion",
+			script: `abc(1,2,3)`,
+		},
+		{
+			name:   "valid-type",
+			script: `num(1,2,3)`,
+		},
+		{
+			name:   "invalid-type-conversion",
+			script: `num("a","b","c")`,
+			err:    errors.New(`TypeError: can't convert from "string" to "int"`),
+		},
+	}
+
+	vm := New()
+	vm.Set("abc", func(a string, b string, c string) int {
+		return 1
+	})
+	vm.Set("num", func(a int, b int, c int) int {
+		return 1
+	})
+
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			_, err := vm.Run(tc.script)
+			if tc.err != nil {
+				require.Error(t, err)
+				require.Equal(t, tc.err.Error(), err.Error())
+				return
+			}
+			require.NoError(t, err)
+		})
+	}
+}