go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/starlark/testdata/misc.star (about) 1 # Miscellaneous tests of Starlark evaluation. 2 # This is a "chunked" file: each "---" effectively starts a new file. 3 4 # TODO(adonovan): move these tests into more appropriate files. 5 # TODO(adonovan): test coverage: 6 # - stmts: pass; if cond fail; += and failures; 7 # for x fail; for x not iterable; for can't assign; for 8 # error in loop body 9 # - subassign fail 10 # - x[i]=x fail in both operands; frozen x; list index not int; boundscheck 11 # - x.f = ... 12 # - failure in list expr [...]; tuple expr; dict expr (bad key) 13 # - cond expr semantics; failures 14 # - x[i] failures in both args; dict and iterator key and range checks; 15 # unhandled operand types 16 # - +: list/list, int/int, string/string, tuple+tuple, dict/dict; 17 # - * and ** calls: various errors 18 # - call of non-function 19 # - slice x[ijk] 20 # - comprehension: unhashable dict key; 21 # scope of vars (local and toplevel); noniterable for clause 22 # - unknown unary op 23 # - ordering of values 24 # - freeze, transitivity of its effect. 25 # - add an application-defined type to the environment so we can test it. 26 # - even more: 27 # 28 # eval 29 # pass statement 30 # assign to tuple l-value -- illegal 31 # assign to list l-value -- illegal 32 # assign to field 33 # tuple + tuple 34 # call with *args, **kwargs 35 # slice with step 36 # tuple slice 37 # interpolate with %c, %% 38 39 load("assert.star", "assert") 40 41 # Ordered comparisons require values of the same type. 42 assert.fails(lambda: None < None, "not impl") 43 assert.fails(lambda: None < False, "not impl") 44 assert.fails(lambda: False < list, "not impl") 45 assert.fails(lambda: list < {}, "not impl") 46 assert.fails(lambda: {} < (lambda: None), "not impl") 47 assert.fails(lambda: (lambda: None) < 0, "not impl") 48 assert.fails(lambda: 0 < [], "not impl") 49 assert.fails(lambda: [] < "", "not impl") 50 assert.fails(lambda: "" < (), "not impl") 51 # Except int < float: 52 assert.lt(1, 2.0) 53 assert.lt(2.0, 3) 54 55 --- 56 # cyclic data structures 57 load("assert.star", "assert") 58 59 cyclic = [1, 2, 3] # list cycle 60 cyclic[1] = cyclic 61 assert.eq(str(cyclic), "[1, [...], 3]") 62 assert.fails(lambda: cyclic < cyclic, "maximum recursion") 63 assert.fails(lambda: cyclic == cyclic, "maximum recursion") 64 cyclic2 = [1, 2, 3] 65 cyclic2[1] = cyclic2 66 assert.fails(lambda: cyclic2 == cyclic, "maximum recursion") 67 68 cyclic3 = [1, [2, 3]] # list-list cycle 69 cyclic3[1][0] = cyclic3 70 assert.eq(str(cyclic3), "[1, [[...], 3]]") 71 cyclic4 = {"x": 1} 72 cyclic4["x"] = cyclic4 73 assert.eq(str(cyclic4), "{\"x\": {...}}") 74 cyclic5 = [0, {"x": 1}] # list-dict cycle 75 cyclic5[1]["x"] = cyclic5 76 assert.eq(str(cyclic5), "[0, {\"x\": [...]}]") 77 assert.eq(str(cyclic5), "[0, {\"x\": [...]}]") 78 assert.fails(lambda: cyclic5 == cyclic5 ,"maximum recursion") 79 cyclic6 = [0, {"x": 1}] 80 cyclic6[1]["x"] = cyclic6 81 assert.fails(lambda: cyclic5 == cyclic6, "maximum recursion") 82 83 --- 84 # regression 85 load("assert.star", "assert") 86 87 # was a parse error: 88 assert.eq(("ababab"[2:]).replace("b", "c"), "acac") 89 assert.eq("ababab"[2:].replace("b", "c"), "acac") 90 91 # test parsing of line continuation, at toplevel and in expression. 92 three = 1 + \ 93 2 94 assert.eq(1 + \ 95 2, three) 96 97 --- 98 # A regression test for error position information. 99 100 _ = {}.get(1, default=2) ### "get: unexpected keyword arguments" 101 102 --- 103 # Load exposes explicitly declared globals from other modules. 104 load('assert.star', 'assert', 'freeze') 105 assert.eq(str(freeze), '<built-in function freeze>') 106 107 --- 108 # Load does not expose pre-declared globals from other modules. 109 # See github.com/google/skylark/issues/75. 110 load('assert.star', 'assert', 'matches') ### "matches not found in module" 111 112 --- 113 # Load does not expose universals accessible in other modules. 114 load('assert.star', 'len') ### "len not found in module" 115 116 117 --- 118 # Test plus folding optimization. 119 load('assert.star', 'assert') 120 121 s = "s" 122 l = [4] 123 t = (4,) 124 125 assert.eq("a" + "b" + "c", "abc") 126 assert.eq("a" + "b" + s + "c", "absc") 127 assert.eq(() + (1,) + (2, 3), (1, 2, 3)) 128 assert.eq(() + (1,) + t + (2, 3), (1, 4, 2, 3)) 129 assert.eq([] + [1] + [2, 3], [1, 2, 3]) 130 assert.eq([] + [1] + l + [2, 3], [1, 4, 2, 3]) 131 132 assert.fails(lambda: "a" + "b" + 1 + "c", "unknown binary op: string \\+ int") 133 assert.fails(lambda: () + () + 1 + (), "unknown binary op: tuple \\+ int") 134 assert.fails(lambda: [] + [] + 1 + [], "unknown binary op: list \\+ int") 135 136 137 138 --- 139 load('assert.star', 'froze') ### `name froze not found .*did you mean freeze`