Skip to content

Commit c0631c8

Browse files
committed
Add support for tuple to collection subjects
`tuple`s are as list-like or more than `depset`s, but previously resulted in a `fail` when passed to collection subject functions. Along the way remove an incorrect comment about `str` being iterable, which it is in Python but not in Starlark.
1 parent f906325 commit c0631c8

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/private/truth_common.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ def to_list(obj):
126126
"""Attempt to convert the object to a list, else error.
127127
128128
NOTE: This only supports objects that are typically understood as
129-
lists, not any iterable. Types like `dict` and `str` are iterable,
130-
but will be rejected.
129+
lists, not any iterable. Types like `dict` are iterable, but will
130+
be rejected.
131131
132132
Args:
133-
obj: ([`list`] | [`depset`]) The object to convert to a list.
133+
obj: ([`list`] | [`depset`] | [`tuple`]) The object to convert to a
134+
list.
134135
135136
Returns:
136137
[`list`] of the object
@@ -141,5 +142,7 @@ def to_list(obj):
141142
return obj
142143
elif types.is_depset(obj):
143144
return obj.to_list()
145+
elif types.is_tuple(obj):
146+
return list(obj)
144147
else:
145148
fail("Unable to convert to list: {}".format(repr_with_type(obj)))

tests/truth_tests.bzl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,34 @@ def _collection_contains_exactly_test(env, _target):
538538
msg = "check same elements out of order",
539539
)
540540

541+
subject = truth.expect(fake_env).that_collection(("one", "four", "three", "two", "five"))
542+
order = subject.contains_exactly(("one", "two", "three", "four", "five"))
543+
_assert_no_failures(
544+
fake_env,
545+
env = env,
546+
msg = "check same elements with expected in different order",
547+
)
548+
order.in_order()
549+
_assert_failure(
550+
fake_env,
551+
[
552+
"expected values all found, but with incorrect order:",
553+
"0: one found at offset 0",
554+
"1: two found at offset 3",
555+
"2: three found at offset 2",
556+
"3: four found at offset 1",
557+
"4: five found at offset 4",
558+
"actual values:",
559+
"0: one",
560+
"1: four",
561+
"2: three",
562+
"3: two",
563+
"4: five",
564+
],
565+
env = env,
566+
msg = "check same elements out of order",
567+
)
568+
541569
_end(env, fake_env)
542570

543571
_suite.append(collection_contains_exactly_test)

0 commit comments

Comments
 (0)