/*
 * Copyright 2015 Google Inc.
 *
 * 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.googlejavaformat.java;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

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

/**
 * Tests for command-line flags.
 */
@RunWith(JUnit4.class)
public class CommandLineFlagsTest {

  // TODO(eaftan): Disallow passing both -lines and -offset/-length, like clang-format.

  @Test
  public void formatInPlaceRequiresAtLeastOneFile() {
    try {
      Main.processArgs("-i");
      fail();
    } catch (UsageException e) {
      // expected
    }

    try {
      Main.processArgs("-i", "-");
      fail();
    } catch (UsageException e) {
      // expected
    }

    try {
      Main.processArgs("-i", "Foo.java");
      Main.processArgs("-i", "Foo.java", "Bar.java");
    } catch (UsageException e) {
      fail();
    }
  }

  @Test
  public void formatASubsetRequiresExactlyOneFile() {
    try {
      Main.processArgs("-lines", "10", "Foo.java");
    } catch (UsageException e) {
      fail();
    }

    try {
      Main.processArgs("-lines", "10");
      fail();
    } catch (UsageException e) {
      // expected
    }

    try {
      Main.processArgs("-lines", "10", "Foo.java", "Bar.java");
      fail();
    } catch (UsageException e) {
      // expected
    }

    try {
      Main.processArgs("-offset", "10", "-length", "10", "Foo.java");
    } catch (UsageException e) {
      fail();
    }

    try {
      Main.processArgs("-offset", "10", "-length", "10");
      fail();
    } catch (UsageException e) {
      // expected
    }

    try {
      Main.processArgs("-offset", "10", "-length", "10", "Foo.java", "Bar.java");
      fail();
    } catch (UsageException e) {
      // expected
    }
  }

  // TODO(eaftan): clang-format allows a single offset with no length, which means to format
  // up to the end of the file.  We should match that behavior.
  @Test
  public void numberOfOffsetsMustMatchNumberOfLengths() {
    try {
      Main.processArgs("-offset", "10", "-length", "20", "Foo.java");
    } catch (UsageException e) {
      fail();
    }

    try {
      Main.processArgs("-offset", "10", "-length", "20", "-offset", "50", "Foo.java");
      fail();
    } catch (UsageException e) {
      // expected
    }

    try {
      Main.processArgs("-offset", "10", "-length", "20", "-length", "50", "Foo.java");
      fail();
    } catch (UsageException e) {
      // expected
    }
  }

  @Test
  public void noFilesToFormatRequiresEitherHelpOrVersion() {
    try {
      Main.processArgs("-version");
    } catch (UsageException e) {
      fail();
    }

    try {
      Main.processArgs("-help");
    } catch (UsageException e) {
      fail();
    }

    try {
      Main.processArgs();
      fail();
    } catch (UsageException e) {
      // expected
    }

    try {
      Main.processArgs("-aosp");
      fail();
    } catch (UsageException e) {
      // expected
    }
  }

  @Test
  public void stdinAndFiles() {
    try {
      Main.processArgs("-", "A.java");
      fail();
    } catch (UsageException e) {
      assertThat(e)
          .hasMessageThat()
          .contains("cannot format from standard input and files simultaneously");
    }
  }

  @Test
  public void inPlaceStdin() {
    try {
      Main.processArgs("-i", "-");
      fail();
    } catch (UsageException e) {
      assertThat(e)
          .hasMessageThat()
          .contains("in-place formatting was requested but no files were provided");
    }
  }

  @Test
  public void inPlaceDryRun() {
    try {
      Main.processArgs("-i", "-n", "A.java");
      fail();
    } catch (UsageException e) {
      assertThat(e)
          .hasMessageThat()
          .contains("cannot use --dry-run and --in-place at the same time");
    }
  }

  @Test
  public void assumeFileNameOnlyWorksWithStdin() {
    try {
      Main.processArgs("--assume-filename=Foo.java", "Foo.java");
      fail();
    } catch (UsageException e) {
      assertThat(e)
          .hasMessageThat()
          .contains("--assume-filename is only supported when formatting standard input");
    }
  }
}

@@ -1051,32 +1051,54 @@
 AtLeastOneFile()
+ throws UsageException
  %7B%0A    try %7B%0A   
@@ -1309,36 +1309,24 @@
 cted%0A    %7D%0A%0A
-    try %7B%0A  
     Main.pro
@@ -1349,26 +1349,24 @@
 Foo.java%22);%0A
-  
     Main.pro
@@ -1409,61 +1409,8 @@
 %22);%0A
-    %7D catch (UsageException e) %7B%0A      fail();%0A    %7D%0A
   %7D%0A
@@ -1470,30 +1470,40 @@
 eFile() 
-%7B%0A    try
+throws UsageException
  %7B%0A
-  
     Main
@@ -1547,61 +1547,8 @@
 a%22);
-%0A    %7D catch (UsageException e) %7B%0A      fail();%0A    %7D
 %0A%0A  
@@ -1809,36 +1809,24 @@
 cted%0A    %7D%0A%0A
-    try %7B%0A  
     Main.pro
@@ -1884,61 +1884,8 @@
 a%22);
-%0A    %7D catch (UsageException e) %7B%0A      fail();%0A    %7D
 %0A%0A  
@@ -2407,38 +2407,48 @@
 OfLengths() 
-%7B%0A    try
+throws UsageException
  %7B%0A
-  
     Main.pro
@@ -2506,61 +2506,8 @@
 a%22);
-%0A    %7D catch (UsageException e) %7B%0A      fail();%0A    %7D
 %0A%0A  
@@ -2909,38 +2909,48 @@
 OrVersion() 
-%7B%0A    try
+throws UsageException
  %7B%0A
-  
     Main.pro
@@ -2975,157 +2975,39 @@
 %22);%0A
-    %7D catch (UsageException e) %7B%0A      fail();%0A    %7D%0A%0A    try %7B%0A      Main.processArgs(%22-help%22);%0A    %7D catch (UsageException e) %7B%0A      fail();%0A    %7D
+%0A    Main.processArgs(%22-help%22);
 %0A%0A  

Fix some Error Prone warnings