// Copyright 2016 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.analysis;

import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.analysis.OutputGroupProvider.determineOutputGroups;
import static java.util.Arrays.asList;

import com.google.common.collect.ImmutableSet;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.HashSet;
import java.util.Set;

/**
 * Tests for {@link OutputGroupProvider}.
 */
@RunWith(JUnit4.class)
public final class OutputGroupProviderTest {

  @Test
  public void testDetermineOutputGroupsOverridesDefaults() throws Exception {
    Set<String> outputGroups =
        determineOutputGroups(ImmutableSet.of("x", "y", "z"), asList("a", "b", "c"));
    assertThat(outputGroups).containsExactly("a", "b", "c");
  }

  @Test
  public void testDetermineOutputGroupsAddsToDefaults() throws Exception {
    Set<String> outputGroups = determineOutputGroups(ImmutableSet.of("x", "y", "z"), asList("+a"));
    Set<String> expected = new HashSet<>(ImmutableSet.of("x", "y", "z"));
    expected.add("a");
    assertThat(outputGroups).containsExactlyElementsIn(expected);
  }

  @Test
  public void testDetermineOutputGroupsRemovesFromDefaults() throws Exception {
    Set<String> outputGroups = determineOutputGroups(ImmutableSet.of("x", "y", "z"), asList("-y"));

    Set<String> expected = new HashSet<>(ImmutableSet.of("x", "y", "z"));
    expected.remove("y");
    assertThat(outputGroups).containsExactlyElementsIn(expected);
  }

  @Test
  public void testDetermineOutputGroupsMixedOverrideAdditionOverrides() throws Exception {
    Set<String> outputGroups =
        determineOutputGroups(ImmutableSet.of("x", "y", "z"), asList("a", "+b"));
    // The plain "a" causes the default output groups to be overridden.
    assertThat(outputGroups).containsExactly("a", "b");
  }

  @Test
  public void testDetermineOutputGroupsIgnoresUnknownGroup() throws Exception {
    Set<String> outputGroups =
        determineOutputGroups(ImmutableSet.of("x", "y", "z"), asList("-foo"));
    // "foo" doesn't exist, but that shouldn't be a problem.
    assertThat(outputGroups).containsExactlyElementsIn(ImmutableSet.of("x", "y", "z"));
  }

  @Test
  public void testDetermineOutputGroupsRemovesPreviouslyAddedGroup() throws Exception {
    Set<String> outputGroups;
    outputGroups = determineOutputGroups(ImmutableSet.of("x", "y", "z"), asList("+a", "-a"));
    assertThat(outputGroups).containsExactlyElementsIn(ImmutableSet.of("x", "y", "z"));

    // Order matters here.
    outputGroups = determineOutputGroups(ImmutableSet.of("x", "y", "z"), asList("-a", "+a"));
    Set<String> expected = new HashSet<>(ImmutableSet.of("x", "y", "z"));
    expected.add("a");
    assertThat(outputGroups).containsExactlyElementsIn(expected);
  }
}

@@ -992,34 +992,8 @@
 4;%0A%0A
-import java.util.HashSet;%0A
 impo
@@ -1010,16 +1010,16 @@
 il.Set;%0A
+
 %0A/**%0A * 
@@ -1586,105 +1586,8 @@
 ));%0A
-    Set%3CString%3E expected = new HashSet%3C%3E(ImmutableSet.of(%22x%22, %22y%22, %22z%22));%0A    expected.add(%22a%22);%0A
     
@@ -1626,35 +1626,35 @@
 sExactly
-ElementsIn(expected
+(%22x%22, %22y%22, %22z%22, %22a%22
 );%0A  %7D%0A%0A
@@ -1844,109 +1844,8 @@
 %22));
-%0A%0A    Set%3CString%3E expected = new HashSet%3C%3E(ImmutableSet.of(%22x%22, %22y%22, %22z%22));%0A    expected.remove(%22y%22);
 %0A   
@@ -1885,35 +1885,25 @@
 sExactly
-ElementsIn(expected
+(%22x%22, %22z%22
 );%0A  %7D%0A%0A
@@ -2554,34 +2554,8 @@
 ctly
-ElementsIn(ImmutableSet.of
 (%22x%22
@@ -2561,25 +2561,24 @@
 %22, %22y%22, %22z%22)
-)
 ;%0A  %7D%0A%0A  @Te
@@ -2840,34 +2840,8 @@
 ctly
-ElementsIn(ImmutableSet.of
 (%22x%22
@@ -2851,17 +2851,16 @@
 y%22, %22z%22)
-)
 ;%0A%0A    /
@@ -2979,105 +2979,8 @@
 ));%0A
-    Set%3CString%3E expected = new HashSet%3C%3E(ImmutableSet.of(%22x%22, %22y%22, %22z%22));%0A    expected.add(%22a%22);%0A
     
@@ -3023,27 +3023,27 @@
 ctly
-ElementsIn(expected
+(%22x%22, %22y%22, %22z%22, %22a%22
 );%0A 

Simplify OutputGroupProviderTest.