Skip to content

Commit

Permalink
starlark tests: use assert_fails(lambda) instead of '###' annotations
Browse files Browse the repository at this point in the history
The new form avoids the need to put all errors at the end of a chunk.
Instead, they can be grouped with the tests of successful operations.

Not every instance was replaced; the ### notation is more convenient
when exercising statements, or in situations where the regexp needs
a lot of escaping. Also, a new --- chunk can help isolate side effects.

No behavioral changes.

PiperOrigin-RevId: 347390509
  • Loading branch information
adonovan authored and copybara-github committed Dec 14, 2020
1 parent 0349360 commit ddf95df
Show file tree
Hide file tree
Showing 19 changed files with 560 additions and 756 deletions.
16 changes: 6 additions & 10 deletions src/test/java/net/starlark/java/eval/testdata/all_any.star
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ assert_eq(all([[], True]), False)
assert_eq(all([True, 't', 1]), True)

# All with dict
assert_eq(all({1 : None}), True)
assert_eq(all({None : 1}), False)
assert_eq(all({1: None}), True)
assert_eq(all({None: 1}), False)

# Any with empty value
assert_eq(any([]), False)
Expand All @@ -33,11 +33,7 @@ assert_eq(any([False, '', 42]), True)
assert_eq(any({1 : None, '' : None}), True)
assert_eq(any({None : 1, '' : 2}), False)

---
all(None) ### type 'NoneType' is not iterable
---
any(None) ### type 'NoneType' is not iterable
---
any(1) ### type 'int' is not iterable
---
all(1) ### type 'int' is not iterable
assert_fails(lambda: all(None), "type 'NoneType' is not iterable")
assert_fails(lambda: any(None), "type 'NoneType' is not iterable")
assert_fails(lambda: all(1), "type 'int' is not iterable")
assert_fails(lambda: any(1), "type 'int' is not iterable")
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
x = [[1, 2]] # x₀
assert_eq([x for x in x for y in x], [[1, 2], [1, 2]])
# x₁ x₁ x₀ x₁
---

# The scope of z (bound in loop 3) includes the operand of loop 2,
# permitting a forward reference.
assert_eq([1 // 0 for x in [] for y in z for z in ()], [])

# Referring to it on the first iteration (before it is bound) is an error.
[1 // 0 for x in [1] for y in z for z in ()] ### local variable 'z' is referenced before assignment
assert_fails(
lambda: [1 // 0 for x in [1] for y in z for z in ()],
"local variable 'z' is referenced before assignment",
)

# In this example there is a static forward reference
# and a dynamic loop-carried dependence.
Expand Down
41 changes: 14 additions & 27 deletions src/test/java/net/starlark/java/eval/testdata/dict.star
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,15 @@ assert_eq(foo, bar)
assert_eq(foo.pop('a', 0), 0)
assert_eq(foo.popitem(), ('b', [1, 2]))

---
d = {1: 2}
freeze(d)
d.setdefault(1, 2) ### trying to mutate a frozen dict value
---
dict().popitem() ### empty dictionary
---
dict(a=2).pop('z') ### KeyError: "z"
---
d = {}
freeze(d)
d.pop("nonexistent", "default") ### trying to mutate a frozen dict value
---
{}.pop([], 1) ### unhashable type: 'list'
---
assert_fails(lambda: d.setdefault(1, 2), "trying to mutate a frozen dict value")
assert_fails(lambda: d.pop("nonexistent", "default"), "trying to mutate a frozen dict value")

assert_fails(lambda: dict().popitem(), "empty dictionary")
assert_fails(lambda: dict(a=2).pop('z'), 'KeyError: "z"')
assert_fails(lambda: {}.pop([], 1), "unhashable type: 'list'")
---
# update

foo = dict()
Expand Down Expand Up @@ -99,7 +92,6 @@ assert_eq(d1, d3)
assert_eq(1 in {1: 0}, True)
assert_eq(() in {}, False)
assert_eq("a" in dict(a=1), True)
---

# What's going on here? Functions _are_ hashable.
# 'len in {}' and '{}.get(len, False)' should both successfully evaluate to False.
Expand All @@ -108,20 +100,15 @@ assert_eq("a" in dict(a=1), True)
# Starlark functions are already hashable:
def f(): pass
f in {} # no error

# unhashable types
assert_fails(lambda: {} in {}, "unhashable type: 'dict'")
assert_fails(lambda: [] in {}, "unhashable type: 'list'")
assert_fails(lambda: len in {}, "unhashable type: 'builtin_function_or_method'")
assert_fails(lambda: {}.get([]), "unhashable type: 'list'")
assert_fails(lambda: dict().get({}), "unhashable type: 'dict'")
assert_fails(lambda: {1: 2}.get(len), "unhashable type: 'builtin_function_or_method'")

{} in {} ### unhashable type: 'dict'
---
[] in {} ### unhashable type: 'list'
---
len in {} ### unhashable type: 'builtin_function_or_method'
---
{}.get([]) ### unhashable type: 'list'
---
dict().get({}) ### unhashable type: 'dict'
---
{1: 2}.get(len) ### unhashable type: 'builtin_function_or_method'
---
# For composite keys, the error message relates to the
# unhashable subelement of the key, not the key itself.
{(0, "", True, [0]): None} ### unhashable type: 'list'
assert_fails(lambda: {(0, "", True, [0]): None}, "unhashable type: 'list'")
17 changes: 8 additions & 9 deletions src/test/java/net/starlark/java/eval/testdata/fields.star
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ assert_eq(hasattr(x, "a"), True)
assert_eq(hasattr(x, "c"), False)
assert_eq(getattr(x, "c", 3), 3) # missing => default

x.c ### 'struct' value has no field or method 'c'
---
x = struct(a = 1, b = 2)
getattr(x, "c") ### 'struct' value has no field or method 'c'
---
x = struct(a = 1, b = 2)
x.c = 3 ### struct value does not support field assignment
---
assert_fails(lambda: x.c, "'struct' value has no field or method 'c'")
assert_fails(lambda: getattr(x, "c"), "'struct' value has no field or method 'c'")

def set_c(x, val):
x.c = val

assert_fails(lambda: set_c(x, 3), "struct value does not support field assignment")

## mutable

Expand All @@ -38,4 +37,4 @@ y.b = -2 # update existing field
y.c = 3 # set new field

assert_eq(str(y), "mutablestruct(a = 1, b = -2, c = 3)")
y.c = "bad" ### bad field value
assert_fails(lambda: set_c(y, "bad"), "bad field value")
Loading

0 comments on commit ddf95df

Please sign in to comment.