1# Tests of Starlark built-in functions
2# option:set
3
4load("assert.star", "assert")
5
6# len
7assert.eq(len([1, 2, 3]), 3)
8assert.eq(len((1, 2, 3)), 3)
9assert.eq(len({1: 2}), 1)
10assert.fails(lambda: len(1), "int.*has no len")
11
12# and, or
13assert.eq(123 or "foo", 123)
14assert.eq(0 or "foo", "foo")
15assert.eq(123 and "foo", "foo")
16assert.eq(0 and "foo", 0)
17none = None
18_1 = none and none[0] # rhs is not evaluated
19_2 = (not none) or none[0] # rhs is not evaluated
20
21# abs
22assert.eq(abs(2.0), 2.0)
23assert.eq(abs(0.0), 0.0)
24assert.eq(abs(-2.0), 2.0)
25assert.eq(abs(2), 2)
26assert.eq(abs(0), 0)
27assert.eq(abs(-2), 2)
28assert.eq(abs(float("inf")), float("inf"))
29assert.eq(abs(float("-inf")), float("inf"))
30assert.eq(abs(float("nan")), float("nan"))
31assert.fails(lambda: abs("0"), "got string, want int or float")
32maxint32 = (1 << 31) - 1
33assert.eq(abs(+123 * maxint32), +123 * maxint32)
34assert.eq(abs(-123 * maxint32), +123 * maxint32)
35
36# any, all
37assert.true(all([]))
38assert.true(all([1, True, "foo"]))
39assert.true(not all([1, True, ""]))
40assert.true(not any([]))
41assert.true(any([0, False, "foo"]))
42assert.true(not any([0, False, ""]))
43
44# in
45assert.true(3 in [1, 2, 3])
46assert.true(4 not in [1, 2, 3])
47assert.true(3 in (1, 2, 3))
48assert.true(4 not in (1, 2, 3))
49assert.fails(lambda: 3 in "foo", "in.*requires string as left operand")
50assert.true(123 in {123: ""})
51assert.true(456 not in {123:""})
52assert.true([] not in {123: ""})
53
54# sorted
55assert.eq(sorted([42, 123, 3]), [3, 42, 123])
56assert.eq(sorted([42, 123, 3], reverse=True), [123, 42, 3])
57assert.eq(sorted(["wiz", "foo", "bar"]), ["bar", "foo", "wiz"])
58assert.eq(sorted(["wiz", "foo", "bar"], reverse=True), ["wiz", "foo", "bar"])
59assert.fails(lambda: sorted([1, 2, None, 3]), "int < NoneType not implemented")
60assert.fails(lambda: sorted([1, "one"]), "string < int not implemented")
61# custom key function
62assert.eq(sorted(["two", "three", "four"], key=len),
63 ["two", "four", "three"])
64assert.eq(sorted(["two", "three", "four"], key=len, reverse=True),
65 ["three", "four", "two"])
66assert.fails(lambda: sorted([1, 2, 3], key=None), "got NoneType, want callable")
67# sort is stable
68pairs = [(4, 0), (3, 1), (4, 2), (2, 3), (3, 4), (1, 5), (2, 6), (3, 7)]
69assert.eq(sorted(pairs, key=lambda x: x[0]),
70 [(1, 5),
71 (2, 3), (2, 6),
72 (3, 1), (3, 4), (3, 7),
73 (4, 0), (4, 2)])
74assert.fails(lambda: sorted(1), 'sorted: for parameter iterable: got int, want iterable')
75
76# reversed
77assert.eq(reversed([1, 144, 81, 16]), [16, 81, 144, 1])
78
79# set
80assert.contains(set([1, 2, 3]), 1)
81assert.true(4 not in set([1, 2, 3]))
82assert.eq(len(set([1, 2, 3])), 3)
83assert.eq(sorted([x for x in set([1, 2, 3])]), [1, 2, 3])
84
85# dict
86assert.eq(dict([(1, 2), (3, 4)]), {1: 2, 3: 4})
87assert.eq(dict([(1, 2), (3, 4)], foo="bar"), {1: 2, 3: 4, "foo": "bar"})
88assert.eq(dict({1:2, 3:4}), {1: 2, 3: 4})
89assert.eq(dict({1:2, 3:4}.items()), {1: 2, 3: 4})
90
91# range
92assert.eq("range", type(range(10)))
93assert.eq("range(10)", str(range(0, 10, 1)))
94assert.eq("range(1, 10)", str(range(1, 10)))
95assert.eq(range(0, 5, 10), range(0, 5, 11))
96assert.eq("range(0, 10, -1)", str(range(0, 10, -1)))
97assert.fails(lambda: {range(10): 10}, "unhashable: range")
98assert.true(bool(range(1, 2)))
99assert.true(not(range(2, 1))) # an empty range is false
100assert.eq([x*x for x in range(5)], [0, 1, 4, 9, 16])
101assert.eq(list(range(5)), [0, 1, 2, 3, 4])
102assert.eq(list(range(-5)), [])
103assert.eq(list(range(2, 5)), [2, 3, 4])
104assert.eq(list(range(5, 2)), [])
105assert.eq(list(range(-2, -5)), [])
106assert.eq(list(range(-5, -2)), [-5, -4, -3])
107assert.eq(list(range(2, 10, 3)), [2, 5, 8])
108assert.eq(list(range(10, 2, -3)), [10, 7, 4])
109assert.eq(list(range(-2, -10, -3)), [-2, -5, -8])
110assert.eq(list(range(-10, -2, 3)), [-10, -7, -4])
111assert.eq(list(range(10, 2, -1)), [10, 9, 8, 7, 6, 5, 4, 3])
112assert.eq(list(range(5)[1:]), [1, 2, 3, 4])
113assert.eq(len(range(5)[1:]), 4)
114assert.eq(list(range(5)[:2]), [0, 1])
115assert.eq(list(range(10)[1:]), [1, 2, 3, 4, 5, 6, 7, 8, 9])
116assert.eq(list(range(10)[1:9:2]), [1, 3, 5, 7])
117assert.eq(list(range(10)[1:10:2]), [1, 3, 5, 7, 9])
118assert.eq(list(range(10)[1:11:2]), [1, 3, 5, 7, 9])
119assert.eq(list(range(10)[::-2]), [9, 7, 5, 3, 1])
120assert.eq(list(range(0, 10, 2)[::2]), [0, 4, 8])
121assert.eq(list(range(0, 10, 2)[::-2]), [8, 4, 0])
122# range() is limited by the width of the Go int type (int32 or int64).
123assert.fails(lambda: range(1<<64), "... out of range .want value in signed ..-bit range")
124assert.eq(len(range(0x7fffffff)), 0x7fffffff) # O(1)
125# Two ranges compare equal if they denote the same sequence:
126assert.eq(range(0), range(2, 1, 3)) # []
127assert.eq(range(0, 3, 2), range(0, 4, 2)) # [0, 2]
128assert.ne(range(1, 10), range(2, 10))
129assert.fails(lambda: range(0) < range(0), "range < range not implemented")
130# <number> in <range>
131assert.contains(range(3), 1)
132assert.contains(range(3), 2.0) # acts like 2
133assert.fails(lambda: True in range(3), "requires integer.*not bool") # bools aren't numbers
134assert.fails(lambda: "one" in range(10), "requires integer.*not string")
135assert.true(4 not in range(4))
136assert.true(1e15 not in range(4)) # too big for int32
137assert.true(1e100 not in range(4)) # too big for int64
138# https://github.com/google/starlark-go/issues/116
139assert.fails(lambda: range(0, 0, 2)[:][0], "index 0 out of range: empty range")
140
141# list
142assert.eq(list("abc".elems()), ["a", "b", "c"])
143assert.eq(sorted(list({"a": 1, "b": 2})), ['a', 'b'])
144
145# min, max
146assert.eq(min(5, -2, 1, 7, 3), -2)
147assert.eq(max(5, -2, 1, 7, 3), 7)
148assert.eq(min([5, -2, 1, 7, 3]), -2)
149assert.eq(min("one", "two", "three", "four"), "four")
150assert.eq(max("one", "two", "three", "four"), "two")
151assert.fails(min, "min requires at least one positional argument")
152assert.fails(lambda: min(1), "not iterable")
153assert.fails(lambda: min([]), "empty")
154assert.eq(min(5, -2, 1, 7, 3, key=lambda x: x*x), 1) # min absolute value
155assert.eq(min(5, -2, 1, 7, 3, key=lambda x: -x), 7) # min negated value
156
157# enumerate
158assert.eq(enumerate("abc".elems()), [(0, "a"), (1, "b"), (2, "c")])
159assert.eq(enumerate([False, True, None], 42), [(42, False), (43, True), (44, None)])
160
161# zip
162assert.eq(zip(), [])
163assert.eq(zip([]), [])
164assert.eq(zip([1, 2, 3]), [(1,), (2,), (3,)])
165assert.eq(zip("".elems()), [])
166assert.eq(zip("abc".elems(),
167 list("def".elems()),
168 "hijk".elems()),
169 [("a", "d", "h"), ("b", "e", "i"), ("c", "f", "j")])
170z1 = [1]
171assert.eq(zip(z1), [(1,)])
172z1.append(2)
173assert.eq(zip(z1), [(1,), (2,)])
174assert.fails(lambda: zip(z1, 1), "zip: argument #2 is not iterable: int")
175z1.append(3)
176
177# dir for builtin_function_or_method
178assert.eq(dir(None), [])
179assert.eq(dir({})[:3], ["clear", "get", "items"]) # etc
180assert.eq(dir(1), [])
181assert.eq(dir([])[:3], ["append", "clear", "extend"]) # etc
182
183# hasattr, getattr, dir
184# hasfields is an application-defined type defined in eval_test.go.
185hf = hasfields()
186assert.eq(dir(hf), [])
187assert.true(not hasattr(hf, "x"))
188assert.fails(lambda: getattr(hf, "x"), "no .x field or method")
189assert.eq(getattr(hf, "x", 42), 42)
190hf.x = 1
191assert.true(hasattr(hf, "x"))
192assert.eq(getattr(hf, "x"), 1)
193assert.eq(hf.x, 1)
194hf.x = 2
195assert.eq(getattr(hf, "x"), 2)
196assert.eq(hf.x, 2)
197# built-in types can have attributes (methods) too.
198myset = set([])
199assert.eq(dir(myset), ["union"])
200assert.true(hasattr(myset, "union"))
201assert.true(not hasattr(myset, "onion"))
202assert.eq(str(getattr(myset, "union")), "<built-in method union of set value>")
203assert.fails(lambda: getattr(myset, "onion"), "no .onion field or method")
204assert.eq(getattr(myset, "onion", 42), 42)
205
206# dir returns a new, sorted, mutable list
207assert.eq(sorted(dir("")), dir("")) # sorted
208dir("").append("!") # mutable
209assert.true("!" not in dir("")) # new
210
211# error messages should suggest spelling corrections
212hf.one = 1
213hf.two = 2
214hf.three = 3
215hf.forty_five = 45
216assert.fails(lambda: hf.One, 'no .One field.*did you mean .one')
217assert.fails(lambda: hf.oone, 'no .oone field.*did you mean .one')
218assert.fails(lambda: hf.FortyFive, 'no .FortyFive field.*did you mean .forty_five')
219assert.fails(lambda: hf.trhee, 'no .trhee field.*did you mean .three')
220assert.fails(lambda: hf.thirty, 'no .thirty field or method$') # no suggestion
221
222# spell check in setfield too
223def setfield(): hf.noForty_Five = 46 # "no" prefix => SetField returns NoSuchField
224assert.fails(setfield, 'no .noForty_Five field.*did you mean .forty_five')
225
226# repr
227assert.eq(repr(1), "1")
228assert.eq(repr("x"), '"x"')
229assert.eq(repr(["x", 1]), '["x", 1]')
230
231# fail
232---
233fail() ### `fail: $`
234x = 1//0 # unreachable
235---
236fail(1) ### `fail: 1`
237---
238fail(1, 2, 3) ### `fail: 1 2 3`
239---
240fail(1, 2, 3, sep="/") ### `fail: 1/2/3`
View as plain text