Skip to content
Closed
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions lib/private/collection_subject.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def _collection_subject_new(
contains_none_of = lambda *a, **k: _collection_subject_contains_none_of(self, *a, **k),
contains_predicate = lambda *a, **k: _collection_subject_contains_predicate(self, *a, **k),
has_size = lambda *a, **k: _collection_subject_has_size(self, *a, **k),
is_empty = lambda *a, **k: _collection_subject_is_empty(self, *a, **k),
is_not_empty = lambda *a, **k: _collection_subject_is_not_empty(self, *a, **k),
not_contains = lambda *a, **k: _collection_subject_not_contains(self, *a, **k),
not_contains_predicate = lambda *a, **k: _collection_subject_not_contains_predicate(self, *a, **k),
offset = lambda *a, **k: _collection_subject_offset(self, *a, **k),
Expand Down Expand Up @@ -115,6 +117,23 @@ def _collection_subject_has_size(self, expected):
meta = self.meta.derive("size()"),
).equals(expected)

def _collection_subject_is_empty(self):
"""Asserts that the collection is empty.

Method: CollectionSubject.is_empty
"""
return _collection_subject_has_size(self, 0)

def _collection_subject_is_not_empty(self):
"""Asserts that the collection is not empty.

Method: CollectionSubject.is_not_empty
"""
return IntSubject.new(
len(self.actual),
meta = self.meta.derive("size()"),
).not_equals(0)

def _collection_subject_contains(self, expected):
"""Asserts that `expected` is within the collection.

Expand Down Expand Up @@ -497,6 +516,8 @@ CollectionSubject = struct(
contains_none_of = _collection_subject_contains_none_of,
contains_predicate = _collection_subject_contains_predicate,
has_size = _collection_subject_has_size,
is_empty = _collection_subject_is_empty,
is_not_empty = _collection_subject_is_not_empty,
new = _collection_subject_new,
not_contains_predicate = _collection_subject_not_contains_predicate,
offset = _collection_subject_offset,
Expand Down
58 changes: 58 additions & 0 deletions tests/truth_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,64 @@ def _collection_has_size_test(env, _target):

_suite.append(collection_has_size_test)

def collection_is_empty_test(name):
analysis_test(name = name, impl = _collection_is_empty_test, target = "truth_tests_helper")

def _collection_is_empty_test(env, _target):
fake_env = _fake_env(env)

# Test empty collection
subject_empty = fake_env.expect.that_collection([])
subject_empty.is_empty()
_assert_no_failures(
fake_env,
env = env,
msg = "check empty collection is empty",
)

# Test non-empty collection (should fail)
subject_non_empty = fake_env.expect.that_collection(["a"])
subject_non_empty.is_empty()
_assert_failure(
fake_env,
["value of: collection.size()", "expected: 0", "actual: 1"],
env = env,
msg = "check non-empty collection is not empty",
)

_end(env, fake_env)

_suite.append(collection_is_empty_test)

def collection_is_not_empty_test(name):
analysis_test(name = name, impl = _collection_is_not_empty_test, target = "truth_tests_helper")

def _collection_is_not_empty_test(env, _target):
fake_env = _fake_env(env)

# Test non-empty collection
subject_non_empty = fake_env.expect.that_collection(["a"])
subject_non_empty.is_not_empty()
_assert_no_failures(
fake_env,
env = env,
msg = "check non-empty collection is not empty",
)

# Test empty collection (should fail)
subject_empty = fake_env.expect.that_collection([])
subject_empty.is_not_empty()
_assert_failure(
fake_env,
["value of: collection.size()", "expected not to be: 0", "actual: 0"],
env = env,
msg = "check empty collection is empty",
)

_end(env, fake_env)

_suite.append(collection_is_not_empty_test)

def collection_contains_test(name):
analysis_test(name = name, impl = _collection_contains_test, target = "truth_tests_helper")

Expand Down