// 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) {
    }
  }
}

@@ -674,29 +674,71 @@
 tic 
-org.junit.Assert.fail
+com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows
 ;%0A%0Ai
@@ -1199,16 +1199,20 @@
 oid test
+List
 ViewsChe
@@ -1394,108 +1394,8 @@
 ));%0A
-    SkylarkDict%3CObject, Object%3E dict = SkylarkDict.copyOf(mutability, ImmutableMap.of(1, 2, 3, 4));%0A
     
@@ -1416,28 +1416,24 @@
 eze();%0A%0A    
-try 
 %7B%0A      Iter
@@ -1489,109 +1489,98 @@
     
-it.remove();%0A      fail(%22expected exception%22);%0A    %7D catch (UnsupportedOperationException expected) %7B
+assertThrows(%0A          UnsupportedOperationException.class,%0A          () -%3E it.remove());
 %0A   
@@ -1578,36 +1578,32 @@
 e());%0A    %7D%0A    
-try 
 %7B%0A      Iterator
@@ -1659,109 +1659,98 @@
     
-it.remove();%0A      fail(%22expected exception%22);%0A    %7D catch (UnsupportedOperationException expected) %7B
+assertThrows(%0A          UnsupportedOperationException.class,%0A          () -%3E it.remove());
 %0A   
@@ -1748,36 +1748,32 @@
 e());%0A    %7D%0A    
-try 
 %7B%0A      Iterator
@@ -1830,109 +1830,98 @@
     
-it.remove();%0A      fail(%22expected exception%22);%0A    %7D catch (UnsupportedOperationException expected) %7B
+assertThrows(%0A          UnsupportedOperationException.class,%0A          () -%3E it.remove());
 %0A   
@@ -1919,36 +1919,32 @@
 e());%0A    %7D%0A    
-try 
 %7B%0A      List%3CObj
@@ -1983,16 +1983,93 @@
 );%0A     
+ assertThrows(%0A          UnsupportedOperationException.class,%0A          () -%3E
  sublist
@@ -2082,40 +2082,91 @@
 , 4)
+)
 ;%0A    
-  fail(%22expected e
+%7D%0A  %7D%0A%0A  @Test%0A  public void testDictViewsCheckMutability() throws E
 xception
 %22);%0A
@@ -2165,80 +2165,195 @@
 tion
-%22);
+ %7B
 %0A    
-%7D catch (UnsupportedOperationException expected) %7B%0A    %7D%0A    try
+Mutability mutability = Mutability.create(%22test%22);%0A    SkylarkDict%3CObject, Object%3E dict = SkylarkDict.copyOf(mutability, ImmutableMap.of(1, 2, 3, 4));%0A    mutability.freeze();%0A%0A   
  %7B%0A 
@@ -2479,115 +2479,104 @@
     
-entry.setValue(5);%0A      fail(%22expected exception%22);%0A    %7D catch (UnsupportedOperationException expected) %7B
+assertThrows(%0A          UnsupportedOperationException.class,%0A          () -%3E entry.setValue(5));
 %0A   
@@ -2574,36 +2574,32 @@
 (5));%0A    %7D%0A    
-try 
 %7B%0A      Iterator
@@ -2665,109 +2665,98 @@
     
-it.remove();%0A      fail(%22expected exception%22);%0A    %7D catch (UnsupportedOperationException expected) %7B
+assertThrows(%0A          UnsupportedOperationException.class,%0A          () -%3E it.remove());
 %0A   
@@ -2762,20 +2762,16 @@
   %7D%0A    
-try 
 %7B%0A      
@@ -2845,109 +2845,98 @@
     
-it.remove();%0A      fail(%22expected exception%22);%0A    %7D catch (UnsupportedOperationException expected) %7B
+assertThrows(%0A          UnsupportedOperationException.class,%0A          () -%3E it.remove());
 %0A   

Improve style in SkylarkMutableTest