Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape strings printed by std.assertEqual #1139

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion stdlib/std.jsonnet
Original file line number Diff line number Diff line change
@@ -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
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