Debian Codegraph @

starlarkstruct/testdata/struct.star

raw
1 # Tests of Starlark 'struct' extension.
2 # This is not a standard feature and the Go and Starlark APIs may yet change.
3
4 load("assert.star", "assert")
5
6 assert.eq(str(struct), "<built-in function struct>")
7
8 # struct is a constructor for "unbranded" structs.
9 s = struct(host = "localhost", port = 80)
10 assert.eq(s, s)
11 assert.eq(s, struct(host = "localhost", port = 80))
12 assert.ne(s, struct(host = "localhost", port = 81))
13 assert.eq(type(s), "struct")
14 assert.eq(str(s), 'struct(host = "localhost", port = 80)')
15 assert.eq(s.host, "localhost")
16 assert.eq(s.port, 80)
17 assert.fails(lambda : s.protocol, "struct has no .protocol attribute")
18 assert.eq(dir(s), ["host", "port"])
19
20 # Use gensym to create "branded" struct types.
21 hostport = gensym(name = "hostport")
22 assert.eq(type(hostport), "symbol")
23 assert.eq(str(hostport), "hostport")
24
25 # Call the symbol to instantiate a new type.
26 http = hostport(host = "localhost", port = 80)
27 assert.eq(type(http), "struct")
28 assert.eq(str(http), 'hostport(host = "localhost", port = 80)') # includes name of constructor
29 assert.eq(http, http)
30 assert.eq(http, hostport(host = "localhost", port = 80))
31 assert.ne(http, hostport(host = "localhost", port = 443))
32 assert.eq(http.host, "localhost")
33 assert.eq(http.port, 80)
34 assert.fails(lambda : http.protocol, "hostport struct has no .protocol attribute")
35
36 person = gensym(name = "person")
37 bob = person(name = "bob", age = 50)
38 alice = person(name = "alice", city = "NYC")
39 assert.ne(http, bob) # different constructor symbols
40 assert.ne(bob, alice) # different fields
41
42 hostport2 = gensym(name = "hostport")
43 assert.eq(hostport, hostport)
44 assert.ne(hostport, hostport2) # same name, different symbol
45 assert.ne(http, hostport2(host = "localhost", port = 80)) # equal fields but different ctor symbols
46
47 # dir
48 assert.eq(dir(alice), ["city", "name"])
49 assert.eq(dir(bob), ["age", "name"])
50 assert.eq(dir(http), ["host", "port"])
51
52 # hasattr, getattr
53 assert.true(hasattr(alice, "city"))
54 assert.eq(hasattr(alice, "ageaa"), False)
55 assert.eq(getattr(alice, "city"), "NYC")
56
57 # +
58 assert.eq(bob + bob, bob)
59 assert.eq(bob + alice, person(age = 50, city = "NYC", name = "alice"))
60 assert.eq(alice + bob, person(age = 50, city = "NYC", name = "bob")) # not commutative! a misfeature
61 assert.fails(lambda : alice + 1, "struct \\+ int")
62 assert.eq(http + http, http)
63 assert.fails(lambda : http + bob, "different constructors: hostport \\+ person")
64
65 # Check that a struct with a circular reference doesn't crash when it gets frozen.
66 def struct_with_a_circular_reference():
67 foo = lambda: print(self)
68 self = struct(foo = foo)
69 return self
70
71 # A top-level assigment is what causes this value to be frozen at the end of the load.
72 toplevel_struct_with_a_circular_reference = struct_with_a_circular_reference()