Skip to content

Commit

Permalink
Add types.is_depset. (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
allevato authored and laurentlb committed Feb 8, 2019
1 parent 4b67f5f commit 6741f73
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/types.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ _a_list_type = type([])
_a_string_type = type("")
_a_tuple_type = type(())
_an_int_type = type(1)
_a_depset_type = type(depset())

def _a_function():
pass
Expand Down Expand Up @@ -114,6 +115,17 @@ def _is_function(v):
"""
return type(v) == _a_function_type

def _is_depset(v):
"""Returns True if v is an instance of a `depset`.
Args:
v: The value whose type should be checked.
Returns:
True if v is an instance of a `depset`, False otherwise.
"""
return type(v) == _a_depset_type

types = struct(
is_list = _is_list,
is_string = _is_string,
Expand All @@ -123,4 +135,5 @@ types = struct(
is_tuple = _is_tuple,
is_dict = _is_dict,
is_function = _is_function,
is_depset = _is_depset,
)
35 changes: 34 additions & 1 deletion tests/types_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def _is_string_test(ctx):
asserts.false(env, types.is_string(True))
asserts.false(env, types.is_string(None))
asserts.false(env, types.is_string(_a_function))
asserts.false(env, types.is_string(depset()))

return unittest.end(env)

Expand All @@ -55,6 +56,7 @@ def _is_bool_test(ctx):
asserts.false(env, types.is_bool(""))
asserts.false(env, types.is_bool(None))
asserts.false(env, types.is_bool(_a_function))
asserts.false(env, types.is_bool(depset()))

return unittest.end(env)

Expand All @@ -75,6 +77,7 @@ def _is_list_test(ctx):
asserts.false(env, types.is_list(True))
asserts.false(env, types.is_list(None))
asserts.false(env, types.is_list(_a_function))
asserts.false(env, types.is_list(depset()))

return unittest.end(env)

Expand All @@ -95,6 +98,7 @@ def _is_none_test(ctx):
asserts.false(env, types.is_none([]))
asserts.false(env, types.is_none([1]))
asserts.false(env, types.is_none(_a_function))
asserts.false(env, types.is_none(depset()))

return unittest.end(env)

Expand All @@ -116,6 +120,7 @@ def _is_int_test(ctx):
asserts.false(env, types.is_int([1]))
asserts.false(env, types.is_int(None))
asserts.false(env, types.is_int(_a_function))
asserts.false(env, types.is_int(depset()))

return unittest.end(env)

Expand All @@ -137,6 +142,7 @@ def _is_tuple_test(ctx):
asserts.false(env, types.is_tuple([1]))
asserts.false(env, types.is_tuple(None))
asserts.false(env, types.is_tuple(_a_function))
asserts.false(env, types.is_tuple(depset()))

return unittest.end(env)

Expand All @@ -158,13 +164,14 @@ def _is_dict_test(ctx):
asserts.false(env, types.is_dict([1]))
asserts.false(env, types.is_dict(None))
asserts.false(env, types.is_dict(_a_function))
asserts.false(env, types.is_dict(depset()))

return unittest.end(env)

is_dict_test = unittest.make(_is_dict_test)

def _is_function_test(ctx):
"""Unit tests for types.is_dict."""
"""Unit tests for types.is_function."""

env = unittest.begin(ctx)

Expand All @@ -178,11 +185,36 @@ def _is_function_test(ctx):
asserts.false(env, types.is_function([]))
asserts.false(env, types.is_function([1]))
asserts.false(env, types.is_function(None))
asserts.false(env, types.is_function(depset()))

return unittest.end(env)

is_function_test = unittest.make(_is_function_test)

def _is_depset_test(ctx):
"""Unit tests for types.is_depset."""

env = unittest.begin(ctx)

asserts.true(env, types.is_depset(depset()))
asserts.true(env, types.is_depset(depset(["foo"])))
asserts.true(env, types.is_depset(
depset(["foo"], transitive = [depset(["bar", "baz"])]),
))

asserts.false(env, types.is_depset({}))
asserts.false(env, types.is_depset(1))
asserts.false(env, types.is_depset("s"))
asserts.false(env, types.is_depset(()))
asserts.false(env, types.is_depset(True))
asserts.false(env, types.is_depset([]))
asserts.false(env, types.is_depset([1]))
asserts.false(env, types.is_depset(None))

return unittest.end(env)

is_depset_test = unittest.make(_is_depset_test)

def types_test_suite():
"""Creates the test targets and test suite for types.bzl tests."""
unittest.suite(
Expand All @@ -195,4 +227,5 @@ def types_test_suite():
is_tuple_test,
is_dict_test,
is_function_test,
is_depset_test,
)

0 comments on commit 6741f73

Please sign in to comment.