diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/syntax')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java | 41 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/SkylarkMutableTest.java | 89 |
2 files changed, 130 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java index 161b91341d..df518ca67d 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java @@ -14,7 +14,10 @@ package com.google.devtools.build.lib.syntax; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; import org.junit.Test; @@ -242,4 +245,42 @@ public class SkylarkListTest extends EvaluationTestCase { assertThat(eval("() == (1,)")).isEqualTo(false); assertThat(eval("(1) == (1,)")).isEqualTo(false); } + + @Test + public void testMutatorsCheckMutability() throws Exception { + Mutability mutability = Mutability.create("test"); + MutableList<Object> list = MutableList.copyOf(mutability, ImmutableList.of(1, 2, 3)); + mutability.freeze(); + + try { + list.add(4, null, mutability); + fail("expected exception"); + } catch (EvalException e) { + assertThat(e).hasMessage("trying to mutate a frozen object"); + } + try { + list.add(0, 4, null, mutability); + fail("expected exception"); + } catch (EvalException e) { + assertThat(e).hasMessage("trying to mutate a frozen object"); + } + try { + list.addAll(ImmutableList.of(4, 5, 6), null, mutability); + fail("expected exception"); + } catch (EvalException e) { + assertThat(e).hasMessage("trying to mutate a frozen object"); + } + try { + list.remove(0, null, mutability); + fail("expected exception"); + } catch (EvalException e) { + assertThat(e).hasMessage("trying to mutate a frozen object"); + } + try { + list.set(0, 10, null, mutability); + fail("expected exception"); + } catch (EvalException e) { + assertThat(e).hasMessage("trying to mutate a frozen object"); + } + } } diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkMutableTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkMutableTest.java new file mode 100644 index 0000000000..5232f0446e --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkMutableTest.java @@ -0,0 +1,89 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.syntax; + +import static org.junit.Assert.fail; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; +import java.util.Iterator; +import java.util.List; +import java.util.Map.Entry; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link SkylarkMutable}. */ +@RunWith(JUnit4.class) +public final class SkylarkMutableTest { + + @Test + public void testViewsCheckMutability() throws Exception { + Mutability mutability = Mutability.create("test"); + MutableList<Object> list = MutableList.copyOf(mutability, ImmutableList.of(1, 2, 3)); + SkylarkDict<Object, Object> dict = SkylarkDict.copyOf(mutability, ImmutableMap.of(1, 2, 3, 4)); + mutability.freeze(); + + try { + Iterator<?> it = list.iterator(); + it.next(); + it.remove(); + fail("expected exception"); + } catch (UnsupportedOperationException expected) { + } + try { + Iterator<?> it = list.listIterator(); + it.next(); + it.remove(); + fail("expected exception"); + } catch (UnsupportedOperationException expected) { + } + try { + Iterator<?> it = list.listIterator(1); + it.next(); + it.remove(); + fail("expected exception"); + } catch (UnsupportedOperationException expected) { + } + try { + List<Object> sublist = list.subList(1, 2); + sublist.set(0, 4); + fail("expected exception"); + } catch (UnsupportedOperationException expected) { + } + try { + Iterator<Entry<Object, Object>> it = dict.entrySet().iterator(); + Entry<Object, Object> entry = it.next(); + entry.setValue(5); + fail("expected exception"); + } catch (UnsupportedOperationException expected) { + } + try { + Iterator<Object> it = dict.keySet().iterator(); + it.next(); + it.remove(); + fail("expected exception"); + } catch (UnsupportedOperationException expected) { + } + try { + Iterator<Object> it = dict.values().iterator(); + it.next(); + it.remove(); + fail("expected exception"); + } catch (UnsupportedOperationException expected) { + } + } +} |