aboutsummaryrefslogtreecommitdiff
path: root/starlarkstruct/testdata/struct.star
blob: e54fe046111f3179209c6834aba20d83749b51cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Tests of Starlark 'struct' extension.
# This is not a standard feature and the Go and Starlark APIs may yet change.

load("assert.star", "assert")

assert.eq(str(struct), "<built-in function struct>")

# struct is a constructor for "unbranded" structs.
s = struct(host = "localhost", port = 80)
assert.eq(s, s)
assert.eq(s, struct(host = "localhost", port = 80))
assert.ne(s, struct(host = "localhost", port = 81))
assert.eq(type(s), "struct")
assert.eq(str(s), 'struct(host = "localhost", port = 80)')
assert.eq(s.host, "localhost")
assert.eq(s.port, 80)
assert.fails(lambda : s.protocol, "struct has no .protocol attribute")
assert.eq(dir(s), ["host", "port"])

# Use gensym to create "branded" struct types.
hostport = gensym(name = "hostport")
assert.eq(type(hostport), "symbol")
assert.eq(str(hostport), "hostport")

# Call the symbol to instantiate a new type.
http = hostport(host = "localhost", port = 80)
assert.eq(type(http), "struct")
assert.eq(str(http), 'hostport(host = "localhost", port = 80)')  # includes name of constructor
assert.eq(http, http)
assert.eq(http, hostport(host = "localhost", port = 80))
assert.ne(http, hostport(host = "localhost", port = 443))
assert.eq(http.host, "localhost")
assert.eq(http.port, 80)
assert.fails(lambda : http.protocol, "hostport struct has no .protocol attribute")

person = gensym(name = "person")
bob = person(name = "bob", age = 50)
alice = person(name = "alice", city = "NYC")
assert.ne(http, bob)  # different constructor symbols
assert.ne(bob, alice)  # different fields

hostport2 = gensym(name = "hostport")
assert.eq(hostport, hostport)
assert.ne(hostport, hostport2)  # same name, different symbol
assert.ne(http, hostport2(host = "localhost", port = 80))  # equal fields but different ctor symbols

# dir
assert.eq(dir(alice), ["city", "name"])
assert.eq(dir(bob), ["age", "name"])
assert.eq(dir(http), ["host", "port"])

# hasattr, getattr
assert.true(hasattr(alice, "city"))
assert.eq(hasattr(alice, "ageaa"), False)
assert.eq(getattr(alice, "city"), "NYC")

# +
assert.eq(bob + bob, bob)
assert.eq(bob + alice, person(age = 50, city = "NYC", name = "alice"))
assert.eq(alice + bob, person(age = 50, city = "NYC", name = "bob"))  # not commutative! a misfeature
assert.fails(lambda : alice + 1, "struct \\+ int")
assert.eq(http + http, http)
assert.fails(lambda : http + bob, "different constructors: hostport \\+ person")