Skip to content

Commit

Permalink
Fix: Escape strings printed by assertEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbartholomew committed Mar 3, 2024
1 parent 955ffe6 commit fc23710
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,14 @@ limitations under the License.
std.map(map_func, std.filter(filter_func, arr)),

assertEqual(a, b)::
// If the values are strings, escape them for printing.
// If not, they'll be JSON-stringified anyway by the later string concatenation.
local astr = if std.type(a) == 'string' then std.escapeStringJson(a) else a;
local bstr = if std.type(b) == 'string' then std.escapeStringJson(b) else b;
if a == b then
true
else
error 'Assertion failed. ' + a + ' != ' + b,
error 'Assertion failed. ' + astr + ' != ' + bstr,

abs(n)::
if !std.isNumber(n) then
Expand Down
17 changes: 17 additions & 0 deletions test_suite/error.assert_equal_obj.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2024 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

std.assertEqual({ a: 1 }, { b: 1 })
3 changes: 3 additions & 0 deletions test_suite/error.assert_equal_obj.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RUNTIME ERROR: Assertion failed. {"a": 1} != {"b": 1}
std.jsonnet:<stdlib_position_redacted> function <anonymous>
error.assert_equal_obj.jsonnet:17:1-36
17 changes: 17 additions & 0 deletions test_suite/error.assert_equal_str.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2024 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

std.assertEqual('one\ntwo', 'three\nfour\n')
3 changes: 3 additions & 0 deletions test_suite/error.assert_equal_str.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RUNTIME ERROR: Assertion failed. "one\ntwo" != "three\nfour\n"
std.jsonnet:<stdlib_position_redacted> function <anonymous>
error.assert_equal_str.jsonnet:17:1-45

0 comments on commit fc23710

Please sign in to comment.