github.com/lab47/exprcore@v0.0.0-20210525052339-fb7d6bd9331e/exprcore/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(=> set(1), "got int, want iterable") 37 assert.fails(=> set(1, 2, 3), "got 3 arguments") 38 assert.fails(=> 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(=> 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(=> 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(=> 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 } 86 87 test_set_augmented_assign() 88 89 # len 90 assert.eq(len(x), 3) 91 assert.eq(len(y), 3) 92 assert.eq(len(x | y), 5) 93 94 # str 95 assert.eq(str(set([1])), "set([1])") 96 assert.eq(str(set([2, 3])), "set([2, 3])") 97 assert.eq(str(set([3, 2])), "set([3, 2])") 98 99 # comparison 100 assert.eq(x, x) 101 assert.eq(y, y) 102 assert.true(x != y) 103 assert.eq(set([1, 2, 3]), set([3, 2, 1])) 104 assert.fails(=> x < y, "set < set not implemented") 105 106 # iteration 107 assert.true(type([elem for elem in x]), "list") 108 assert.true(list([elem for elem in x]), [1, 2, 3]) 109 110 def iter() { 111 list = [] 112 for elem in x { 113 list.append(elem) 114 } 115 return list 116 } 117 118 assert.eq(iter(), [1, 2, 3]) 119 120 # sets are not indexable 121 assert.fails(=> x[0], "unhandled.*operation")