blob: bb41a829e65a7011911891ebc080d6a76b68690d [file] [log] [blame]
brandjonbbf99422017-08-06 19:14:56 +02001// 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
15package com.google.devtools.build.lib.syntax;
16
brandjon51c8c7c2017-09-22 08:35:35 -040017import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
brandjonbbf99422017-08-06 19:14:56 +020018
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableMap;
brandjonbbf99422017-08-06 19:14:56 +020021import java.util.Iterator;
22import java.util.List;
jcater0a57d3d2018-05-01 20:33:18 -070023import java.util.Map;
brandjonbbf99422017-08-06 19:14:56 +020024import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
27
laurentlbbc6e41d2019-06-13 14:56:58 -070028/** Tests for {@link StarlarkMutable}. */
brandjonbbf99422017-08-06 19:14:56 +020029@RunWith(JUnit4.class)
30public final class SkylarkMutableTest {
31
32 @Test
brandjon51c8c7c2017-09-22 08:35:35 -040033 public void testListViewsCheckMutability() throws Exception {
brandjonbbf99422017-08-06 19:14:56 +020034 Mutability mutability = Mutability.create("test");
Googler942e1c42019-11-12 13:11:44 -080035 StarlarkList<Object> list = StarlarkList.copyOf(mutability, ImmutableList.of(1, 2, 3));
brandjon51c8c7c2017-09-22 08:35:35 -040036 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");
Googlera9c93632019-11-13 10:48:07 -080070 Dict<Object, Object> dict = Dict.copyOf(mutability, ImmutableMap.of(1, 2, 3, 4));
brandjonbbf99422017-08-06 19:14:56 +020071 mutability.freeze();
72
brandjon51c8c7c2017-09-22 08:35:35 -040073 {
jcater0a57d3d2018-05-01 20:33:18 -070074 Iterator<Map.Entry<Object, Object>> it = dict.entrySet().iterator();
75 Map.Entry<Object, Object> entry = it.next();
brandjon51c8c7c2017-09-22 08:35:35 -040076 assertThrows(
77 UnsupportedOperationException.class,
78 () -> entry.setValue(5));
brandjonbbf99422017-08-06 19:14:56 +020079 }
brandjon51c8c7c2017-09-22 08:35:35 -040080 {
brandjonbbf99422017-08-06 19:14:56 +020081 Iterator<Object> it = dict.keySet().iterator();
82 it.next();
brandjon51c8c7c2017-09-22 08:35:35 -040083 assertThrows(
84 UnsupportedOperationException.class,
85 () -> it.remove());
brandjonbbf99422017-08-06 19:14:56 +020086 }
brandjon51c8c7c2017-09-22 08:35:35 -040087 {
brandjonbbf99422017-08-06 19:14:56 +020088 Iterator<Object> it = dict.values().iterator();
89 it.next();
brandjon51c8c7c2017-09-22 08:35:35 -040090 assertThrows(
91 UnsupportedOperationException.class,
92 () -> it.remove());
brandjonbbf99422017-08-06 19:14:56 +020093 }
94 }
95}