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 39load("assert.star", "assert") 40 41# Ordered comparisons require values of the same type. 42assert.fails(lambda: None < None, "not impl") 43assert.fails(lambda: None < False, "not impl") 44assert.fails(lambda: False < list, "not impl") 45assert.fails(lambda: list < {}, "not impl") 46assert.fails(lambda: {} < (lambda: None), "not impl") 47assert.fails(lambda: (lambda: None) < 0, "not impl") 48assert.fails(lambda: 0 < [], "not impl") 49assert.fails(lambda: [] < "", "not impl") 50assert.fails(lambda: "" < (), "not impl") 51# Except int < float: 52assert.lt(1, 2.0) 53assert.lt(2.0, 3) 54 55--- 56# cyclic data structures 57load("assert.star", "assert") 58 59cyclic = [1, 2, 3] # list cycle 60cyclic[1] = cyclic 61assert.eq(str(cyclic), "[1, [...], 3]") 62assert.fails(lambda: cyclic < cyclic, "maximum recursion") 63assert.fails(lambda: cyclic == cyclic, "maximum recursion") 64cyclic2 = [1, 2, 3] 65cyclic2[1] = cyclic2 66assert.fails(lambda: cyclic2 == cyclic, "maximum recursion") 67 68cyclic3 = [1, [2, 3]] # list-list cycle 69cyclic3[1][0] = cyclic3 70assert.eq(str(cyclic3), "[1, [[...], 3]]") 71cyclic4 = {"x": 1} 72cyclic4["x"] = cyclic4 73assert.eq(str(cyclic4), "{\"x\": {...}}") 74cyclic5 = [0, {"x": 1}] # list-dict cycle 75cyclic5[1]["x"] = cyclic5 76assert.eq(str(cyclic5), "[0, {\"x\": [...]}]") 77assert.eq(str(cyclic5), "[0, {\"x\": [...]}]") 78assert.fails(lambda: cyclic5 == cyclic5 ,"maximum recursion") 79cyclic6 = [0, {"x": 1}] 80cyclic6[1]["x"] = cyclic6 81assert.fails(lambda: cyclic5 == cyclic6, "maximum recursion") 82 83--- 84# regression 85load("assert.star", "assert") 86 87# was a parse error: 88assert.eq(("ababab"[2:]).replace("b", "c"), "acac") 89assert.eq("ababab"[2:].replace("b", "c"), "acac") 90 91# test parsing of line continuation, at toplevel and in expression. 92three = 1 + \ 93 2 94assert.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. 104load('assert.star', 'assert', 'freeze') 105assert.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. 110load('assert.star', 'assert', 'matches') ### "matches not found in module" 111 112--- 113# Load does not expose universals accessible in other modules. 114load('assert.star', 'len') ### "len not found in module" 115 116 117--- 118# Test plus folding optimization. 119load('assert.star', 'assert') 120 121s = "s" 122l = [4] 123t = (4,) 124 125assert.eq("a" + "b" + "c", "abc") 126assert.eq("a" + "b" + s + "c", "absc") 127assert.eq(() + (1,) + (2, 3), (1, 2, 3)) 128assert.eq(() + (1,) + t + (2, 3), (1, 4, 2, 3)) 129assert.eq([] + [1] + [2, 3], [1, 2, 3]) 130assert.eq([] + [1] + l + [2, 3], [1, 4, 2, 3]) 131 132assert.fails(lambda: "a" + "b" + 1 + "c", "unknown binary op: string \\+ int") 133assert.fails(lambda: () + () + 1 + (), "unknown binary op: tuple \\+ int") 134assert.fails(lambda: [] + [] + 1 + [], "unknown binary op: list \\+ int") 135 136 137 138--- 139load('assert.star', 'froze') ### `name froze not found .*did you mean freeze` 140