brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 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 | // http://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 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
jcater | 0a57d3d | 2018-05-01 20:33:18 -0700 | [diff] [blame] | 23 | import java.util.Map; |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
laurentlb | bc6e41d | 2019-06-13 14:56:58 -0700 | [diff] [blame] | 28 | /** Tests for {@link StarlarkMutable}. */ |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 29 | @RunWith(JUnit4.class) |
| 30 | public final class SkylarkMutableTest { |
| 31 | |
| 32 | @Test |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 33 | public void testListViewsCheckMutability() throws Exception { |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 34 | Mutability mutability = Mutability.create("test"); |
Googler | 942e1c4 | 2019-11-12 13:11:44 -0800 | [diff] [blame] | 35 | StarlarkList<Object> list = StarlarkList.copyOf(mutability, ImmutableList.of(1, 2, 3)); |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 36 | mutability.freeze(); |
| 37 | |
| 38 | { |
| 39 | Iterator<?> it = list.iterator(); |
| 40 | it.next(); |
| 41 | assertThrows( |
| 42 | UnsupportedOperationException.class, |
| 43 | () -> it.remove()); |
| 44 | } |
| 45 | { |
| 46 | Iterator<?> it = list.listIterator(); |
| 47 | it.next(); |
| 48 | assertThrows( |
| 49 | UnsupportedOperationException.class, |
| 50 | () -> it.remove()); |
| 51 | } |
| 52 | { |
| 53 | Iterator<?> it = list.listIterator(1); |
| 54 | it.next(); |
| 55 | assertThrows( |
| 56 | UnsupportedOperationException.class, |
| 57 | () -> it.remove()); |
| 58 | } |
| 59 | { |
| 60 | List<Object> sublist = list.subList(1, 2); |
| 61 | assertThrows( |
| 62 | UnsupportedOperationException.class, |
| 63 | () -> sublist.set(0, 4)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testDictViewsCheckMutability() throws Exception { |
| 69 | Mutability mutability = Mutability.create("test"); |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 70 | Dict<Object, Object> dict = Dict.copyOf(mutability, ImmutableMap.of(1, 2, 3, 4)); |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 71 | mutability.freeze(); |
| 72 | |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 73 | { |
jcater | 0a57d3d | 2018-05-01 20:33:18 -0700 | [diff] [blame] | 74 | Iterator<Map.Entry<Object, Object>> it = dict.entrySet().iterator(); |
| 75 | Map.Entry<Object, Object> entry = it.next(); |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 76 | assertThrows( |
| 77 | UnsupportedOperationException.class, |
| 78 | () -> entry.setValue(5)); |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 79 | } |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 80 | { |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 81 | Iterator<Object> it = dict.keySet().iterator(); |
| 82 | it.next(); |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 83 | assertThrows( |
| 84 | UnsupportedOperationException.class, |
| 85 | () -> it.remove()); |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 86 | } |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 87 | { |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 88 | Iterator<Object> it = dict.values().iterator(); |
| 89 | it.next(); |
brandjon | 51c8c7c | 2017-09-22 08:35:35 -0400 | [diff] [blame] | 90 | assertThrows( |
| 91 | UnsupportedOperationException.class, |
| 92 | () -> it.remove()); |
brandjon | bbf9942 | 2017-08-06 19:14:56 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |