package picocli;

import org.junit.Test;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;

/**
 * Testing class for exclusiveOptions
 */
class Issue1380ExclusiveOptions {
    @Option(names = {"-s", "--silent"}, description = "Silent mode", required = false)
    protected boolean silent;

    @Option(names = {"-v", "--verbose"}, description = "Verbose mode", required = false)
    protected boolean verbose;

    @Option(names = {"-j", "--json"}, description = "JSON printing", required = false)
    protected boolean json;
}

/**
 * Testing class for creating a commandline with ArgGroup exclusive tree
 */
@Command(requiredOptionMarker = '*')
class TestingClassExclusiveTrue {

    @ArgGroup(exclusive = true, multiplicity = "0..1")
    protected Issue1380ExclusiveOptions exclusive;
}

/**
 * Testing class for creating a commandline with ArgGroup exclusive false
 */
@Command(requiredOptionMarker = '*')
class TestingClassExclusiveFalse {

    @ArgGroup(exclusive = false, multiplicity = "0..1")
    protected Issue1380ExclusiveOptions exclusive;
}

/**
 * JUnit testing class for issue#1380 // CS427 https://github.com/remkop/picocli/issues/1380
 */
public class Issue1380Test {

    /**
     * JUnit test class for issue#1380 with exclusive set to true // CS427 https://github.com/remkop/picocli/issues/1380
     */
    @Test
    public void testingWithExclusiveTrue() {
        ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(tempOut);
        new CommandLine(new TestingClassExclusiveTrue()).usage(printStream);

        String returnedText = tempOut.toString();
        String expectedText =
        "Usage: <main class> [-s | -v | -j]\n"+
                "  -j, --json      JSON printing\n"+
                "  -s, --silent    Silent mode\n"+
                "  -v, --verbose   Verbose mode\n";

        assertEquals(expectedText, returnedText);
    }

    /**
     * JUnit test class for issue#1380 with exclusive set to false// CS427 https://github.com/remkop/picocli/issues/1380
     */
    @Test
    public void testingWithExclusiveFalse() {
        ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(tempOut);
        new CommandLine(new TestingClassExclusiveFalse()).usage(printStream);

        String returnedText = tempOut.toString();
        String expectedText = "Usage: <main class> [[-s] [-v] [-j]]\n" +
                "  -j, --json      JSON printing\n" +
                "  -s, --silent    Silent mode\n" +
                "  -v, --verbose   Verbose mode\n";

        assertEquals(expectedText, returnedText);
    }
}

@@ -1829,16 +1829,31 @@
 edText =
+ String.format(
 %0A       
@@ -1888,17 +1888,17 @@
 -v %7C -j%5D
-%5C
+%25
 n%22+%0A    
@@ -1941,17 +1941,17 @@
 printing
-%5C
+%25
 n%22+%0A    
@@ -1992,17 +1992,17 @@
 ent mode
-%5C
+%25
 n%22+%0A    
@@ -2036,35 +2036,36 @@
 e   Verbose mode
-%5C
+%25
 n%22
+)
 ;%0A%0A        asser
@@ -2588,16 +2588,47 @@
 edText =
+ String.format(%0A               
  %22Usage:
@@ -2657,17 +2657,17 @@
 v%5D %5B-j%5D%5D
-%5C
+%25
 n%22 +%0A   
@@ -2711,17 +2711,17 @@
 printing
-%5C
+%25
 n%22 +%0A   
@@ -2763,17 +2763,17 @@
 ent mode
-%5C
+%25
 n%22 +%0A   
@@ -2820,11 +2820,12 @@
 mode
-%5C
+%25
 n%22
+)
 ;%0A%0A 

use OS-independent line breaks in test