Skip to content

Commit

Permalink
Fix error message for too many arguments whith at least one named arg
Browse files Browse the repository at this point in the history
  • Loading branch information
edgar-db committed Nov 4, 2020
1 parent 5ccdb4c commit 391bcb5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ output/
.DS_STORE
.idea_modules
.idea
.metals
out/
7 changes: 7 additions & 0 deletions sjsonnet/src/sjsonnet/Val.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ object Val{
.defaults
.map{ case (index, default) => (index, Lazy(evalDefault(default, newScope, evaluator)))}

// Check if expecte args match passed args before
if (params.args.length < args.length) {
Error.fail(
"Too many args, function has " + params.args.length + " parameter(s)",
outerOffset
)
}
lazy val passedArgsBindings = try
args.zipWithIndex.map{
case ((Some(name), v), _) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local foo (x) = x;
foo(1, x = "bar")
22 changes: 7 additions & 15 deletions sjsonnet/test/src-jvm/sjsonnet/ErrorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,12 @@ object ErrorTests extends TestSuite{
| at .(sjsonnet/test/resources/imports/error.too_many_arg.jsonnet:3:6)
|""".stripMargin
)
// test("overflow") - check(
// """sjsonnet.Error: my error message
// | at .(sjsonnet/test/resources/test_suite/error.invariant.simple3.jsonnet:18:10)
// |""".stripMargin
// )
// test("overflow2") - check(
// """sjsonnet.Error: my error message
// | at .(sjsonnet/test/resources/test_suite/error.invariant.simple3.jsonnet:18:10)
// |""".stripMargin
// )
// test("overflow3") - check(
// """sjsonnet.Error: my error message
// | at .(sjsonnet/test/resources/test_suite/error.invariant.simple3.jsonnet:18:10)
// |""".stripMargin
// )

test("too_many_arg_with_named_arg") - checkImports(
"""|sjsonnet.Error: Too many args, function has 1 parameter(s)
| at .(sjsonnet/test/resources/imports/error.too_many_arg_with_named_arg.jsonnet:2:4)
| at .(sjsonnet/test/resources/imports/error.too_many_arg_with_named_arg.jsonnet:2:4)
|""".stripMargin
)
}
}
7 changes: 6 additions & 1 deletion sjsonnet/test/src/sjsonnet/EvaluatorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,17 @@ object EvaluatorTests extends TestSuite{
| welcome: 'Hello ' + name + '!',
|};
|{
| person2: Person('Bob', hello=123),
| person2: Person(hello=123),
|}
""".stripMargin
)
}

/*
* Wrong number of params error takes precedence on invalid param
* For that reason, to assert for invalid param we need to pass the
* right number of params in the first place.
*/
assert(ex.getMessage.contains("Function has no parameter hello"))
}

Expand Down

0 comments on commit 391bcb5

Please sign in to comment.