aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar brandjon <brandjon@google.com>2018-03-20 10:47:31 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-20 10:48:43 -0700
commit7ba9d764dfdea96da470f612d360824269fda132 (patch)
tree76dae3fc33f32c2e1c9bbe396a37dcf4bab39c09 /src/test
parentadb27dc3783509768f8a2ba5f2913185ccd90eec (diff)
Add ability to shallow-freeze individual objects
Clarify that the IMMUTABLE Mutability should only be used for deeply immutable things, not tuples. Created a new SHALLOW_IMMUTABLE Mutability for them. Note that the new shallow-freezing functionality marks things as deeply IMMUTABLE without traversing its contents. I.e., it lies, and it is up to the caller to ensure this doesn't cause problems. RELNOTES: NONE PiperOrigin-RevId: 189767422
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java22
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java14
2 files changed, 36 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java
index 67c3e0ec8b..b9b896f2bc 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java
@@ -233,4 +233,26 @@ public final class MutabilityTest {
assertThat(expected).hasMessageThat().contains(
"trying to check the lock of an object from a different context");
}
+
+ @Test
+ public void checkUnsafeShallowFreezePrecondition_FailsWhenAlreadyFrozen() throws Exception {
+ Mutability mutability = Mutability.create("test").freeze();
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> Freezable.checkUnsafeShallowFreezePrecondition(new DummyFreezable(mutability)));
+ }
+
+ @Test
+ public void checkUnsafeShallowFreezePrecondition_FailsWhenDisallowed() throws Exception {
+ Mutability mutability = Mutability.create("test");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> Freezable.checkUnsafeShallowFreezePrecondition(new DummyFreezable(mutability)));
+ }
+
+ @Test
+ public void checkUnsafeShallowFreezePrecondition_SucceedsWhenAllowed() throws Exception {
+ Mutability mutability = Mutability.createAllowingShallowFreeze("test");
+ Freezable.checkUnsafeShallowFreezePrecondition(new DummyFreezable(mutability));
+ }
}
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 b2768390da..016c30f85a 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
@@ -279,6 +279,20 @@ public class SkylarkListTest extends EvaluationTestCase {
}
@Test
+ public void testCannotMutateAfterShallowFreeze() throws Exception {
+ Mutability mutability = Mutability.createAllowingShallowFreeze("test");
+ MutableList<Object> list = MutableList.copyOf(mutability, ImmutableList.of(1, 2, 3));
+ list.unsafeShallowFreeze();
+
+ try {
+ list.add(4, null, mutability);
+ fail("expected exception");
+ } catch (EvalException e) {
+ assertThat(e).hasMessage("trying to mutate a frozen object");
+ }
+ }
+
+ @Test
public void testCopyOfTakesCopy() throws EvalException {
ArrayList<String> copyFrom = Lists.newArrayList("hi");
Mutability mutability = Mutability.create("test");