1# Copyright 2018 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import copy 16 17import pytest 18 19import proto 20 21 22def test_repeated_scalar_init(): 23 class Foo(proto.Message): 24 bar = proto.RepeatedField(proto.INT32, number=1) 25 26 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 27 assert foo.bar == [1, 1, 2, 3, 5, 8, 13] 28 29 30def test_repeated_scalar_append(): 31 class Foo(proto.Message): 32 bar = proto.RepeatedField(proto.INT32, number=1) 33 34 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 35 foo.bar.append(21) 36 foo.bar.append(34) 37 assert foo.bar == [1, 1, 2, 3, 5, 8, 13, 21, 34] 38 39 40def test_repeated_scalar_iadd(): 41 class Foo(proto.Message): 42 bar = proto.RepeatedField(proto.INT32, number=1) 43 44 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 45 foo.bar += [21, 34] 46 assert foo.bar == [1, 1, 2, 3, 5, 8, 13, 21, 34] 47 48 49def test_repeated_scalar_setitem(): 50 class Foo(proto.Message): 51 bar = proto.RepeatedField(proto.INT32, number=1) 52 53 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 54 foo.bar[4] = 99 55 assert foo.bar == [1, 1, 2, 3, 99, 8, 13] 56 assert foo.bar[4] == 99 57 58 59def test_repeated_scalar_overwrite(): 60 class Foo(proto.Message): 61 bar = proto.RepeatedField(proto.INT32, number=1) 62 63 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 64 foo.bar = [1, 2, 4, 8, 16] 65 assert foo.bar == [1, 2, 4, 8, 16] 66 67 68def test_repeated_scalar_eq_ne(): 69 class Foo(proto.Message): 70 bar = proto.RepeatedField(proto.INT32, number=1) 71 72 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 73 assert foo.bar == copy.copy(foo.bar) 74 assert foo.bar != [1, 2, 4, 8, 16] 75 76 77def test_repeated_scalar_del(): 78 class Foo(proto.Message): 79 bar = proto.RepeatedField(proto.INT32, number=1) 80 81 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 82 del foo.bar 83 assert foo.bar == [] 84 assert not foo.bar 85 86 87def test_repeated_scalar_delitem(): 88 class Foo(proto.Message): 89 bar = proto.RepeatedField(proto.INT32, number=1) 90 91 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 92 del foo.bar[5] 93 del foo.bar[3] 94 assert foo.bar == [1, 1, 2, 5, 13] 95 96 97def test_repeated_scalar_sort(): 98 class Foo(proto.Message): 99 bar = proto.RepeatedField(proto.INT32, number=1) 100 101 foo = Foo(bar=[8, 1, 2, 1, 21, 3, 13, 5]) 102 foo.bar.sort() 103 assert foo.bar == [1, 1, 2, 3, 5, 8, 13, 21] 104 105 106def test_repeated_scalar_wrong_type(): 107 class Foo(proto.Message): 108 bar = proto.RepeatedField(proto.INT32, number=1) 109 110 foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13]) 111 with pytest.raises(TypeError): 112 foo.bar.append(21.0) 113 with pytest.raises(TypeError): 114 foo.bar.append("21") 115