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