github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/starlark/testdata/set.star (about)

     1  # Tests of Starlark 'set'
     2  # option:set
     3  
     4  # Sets are not a standard part of Starlark, so the features
     5  # tested in this file must be enabled in the application by setting
     6  # resolve.AllowSet.  (All sets are created by calls to the 'set'
     7  # built-in or derived from operations on existing sets.)
     8  # The semantics are subject to change as the spec evolves.
     9  
    10  # TODO(adonovan): support set mutation:
    11  # - del set[k]
    12  # - set.remove
    13  # - set.update
    14  # - set.clear
    15  # - set += iterable, perhaps?
    16  # Test iterator invalidation.
    17  
    18  load("assert.star", "assert")
    19  
    20  # literals
    21  # Parser does not currently support {1, 2, 3}.
    22  # TODO(adonovan): add test to syntax/testdata/errors.star.
    23  
    24  # set comprehensions
    25  # Parser does not currently support {x for x in y}.
    26  # See syntax/testdata/errors.star.
    27  
    28  # set constructor
    29  assert.eq(type(set()), "set")
    30  assert.eq(list(set()), [])
    31  assert.eq(type(set([1, 3, 2, 3])), "set")
    32  assert.eq(list(set([1, 3, 2, 3])), [1, 3, 2])
    33  assert.eq(type(set("hello".elems())), "set")
    34  assert.eq(list(set("hello".elems())), ["h", "e", "l", "o"])
    35  assert.eq(list(set(range(3))), [0, 1, 2])
    36  assert.fails(lambda: set(1), "got int, want iterable")
    37  assert.fails(lambda: set(1, 2, 3), "got 3 arguments")
    38  assert.fails(lambda: set([1, 2, {}]), "unhashable type: dict")
    39  
    40  # truth
    41  assert.true(not set())
    42  assert.true(set([False]))
    43  assert.true(set([1, 2, 3]))
    44  
    45  x = set([1, 2, 3])
    46  y = set([3, 4, 5])
    47  
    48  # set + any is not defined
    49  assert.fails(lambda: x + y, "unknown.*: set \+ set")
    50  
    51  # set | set (use resolve.AllowBitwise to enable it)
    52  assert.eq(list(set("a".elems()) | set("b".elems())), ["a", "b"])
    53  assert.eq(list(set("ab".elems()) | set("bc".elems())), ["a", "b", "c"])
    54  assert.fails(lambda: set() | [], "unknown binary op: set | list")
    55  assert.eq(type(x | y), "set")
    56  assert.eq(list(x | y), [1, 2, 3, 4, 5])
    57  assert.eq(list(x | set([5, 1])), [1, 2, 3, 5])
    58  assert.eq(list(x | set((6, 5, 4))), [1, 2, 3, 6, 5, 4])
    59  
    60  # set.union (allows any iterable for right operand)
    61  assert.eq(list(set("a".elems()).union("b".elems())), ["a", "b"])
    62  assert.eq(list(set("ab".elems()).union("bc".elems())), ["a", "b", "c"])
    63  assert.eq(set().union([]), set())
    64  assert.eq(type(x.union(y)), "set")
    65  assert.eq(list(x.union(y)), [1, 2, 3, 4, 5])
    66  assert.eq(list(x.union([5, 1])), [1, 2, 3, 5])
    67  assert.eq(list(x.union((6, 5, 4))), [1, 2, 3, 6, 5, 4])
    68  assert.fails(lambda: x.union([1, 2, {}]), "unhashable type: dict")
    69  
    70  # intersection, set & set (use resolve.AllowBitwise to enable it)
    71  assert.eq(list(set("a".elems()) & set("b".elems())), [])
    72  assert.eq(list(set("ab".elems()) & set("bc".elems())), ["b"])
    73  
    74  # symmetric difference, set ^ set (use resolve.AllowBitwise to enable it)
    75  assert.eq(set([1, 2, 3]) ^ set([4, 5, 3]), set([1, 2, 4, 5]))
    76  
    77  def test_set_augmented_assign():
    78    x = set([1, 2, 3])
    79    x &= set([2, 3])
    80    assert.eq(x, set([2, 3]))
    81    x |= set([1])
    82    assert.eq(x, set([1, 2, 3]))
    83    x ^= set([4, 5, 3])
    84    assert.eq(x, set([1, 2, 4, 5]))
    85  test_set_augmented_assign()
    86  
    87  # len
    88  assert.eq(len(x), 3)
    89  assert.eq(len(y), 3)
    90  assert.eq(len(x | y), 5)
    91  
    92  # str
    93  assert.eq(str(set([1])), "set([1])")
    94  assert.eq(str(set([2, 3])), "set([2, 3])")
    95  assert.eq(str(set([3, 2])), "set([3, 2])")
    96  
    97  # comparison
    98  assert.eq(x, x)
    99  assert.eq(y, y)
   100  assert.true(x != y)
   101  assert.eq(set([1, 2, 3]), set([3, 2, 1]))
   102  assert.fails(lambda: x < y, "set < set not implemented")
   103  
   104  # iteration
   105  assert.true(type([elem for elem in x]), "list")
   106  assert.true(list([elem for elem in x]), [1, 2, 3])
   107  def iter():
   108    list = []
   109    for elem in x:
   110      list.append(elem)
   111    return list
   112  assert.eq(iter(), [1, 2, 3])
   113  
   114  # sets are not indexable
   115  assert.fails(lambda: x[0], "unhandled.*operation")