/*
 * Copyright 2012-2019 the original author or authors.
 *
 * 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 io.spring.initializr.generator.io;

import java.io.IOException;
import java.io.StringWriter;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for {@link IndentingWriter}.
 *
 * @author Andy Wilkinson
 */
class IndentingWriterTests {

	private final StringWriter stringWriter = new StringWriter();

	private final IndentingWriter indentingWriter = new IndentingWriter(
			this.stringWriter);

	@Test
	void linesAreNotIndentedByDefault() {
		this.indentingWriter.println("a");
		this.indentingWriter.println("b");
		this.indentingWriter.println("c");
		assertThat(this.stringWriter.toString()).isEqualTo("a\nb\nc\n");
	}

	@Test
	void linesCanBeIndented() {
		this.indentingWriter.println("a");
		this.indentingWriter.indented(() -> this.indentingWriter.println("b"));
		this.indentingWriter.println("c");
		assertThat(this.stringWriter.toString()).isEqualTo("a\n    b\nc\n");
	}

	@Test
	void blankLinesAreNotIndented() {
		this.indentingWriter.println("a");
		this.indentingWriter.indented(() -> {
			this.indentingWriter.println("b");
			this.indentingWriter.println();
		});
		this.indentingWriter.println("c");
		assertThat(this.stringWriter.toString()).isEqualTo("a\n    b\n\nc\n");
	}

	@Test
	void useOfPrintDoesNotAddIndent() {
		this.indentingWriter.println("a");
		this.indentingWriter.indented(() -> {
			this.indentingWriter.print("b");
			this.indentingWriter.print("b");
			this.indentingWriter.println("b");
		});
		this.indentingWriter.println("c");
		assertThat(this.stringWriter.toString()).isEqualTo("a\n    bbb\nc\n");
	}

	@Test
	void customIndentStrategyIsUsed() throws IOException {
		try (IndentingWriter customIndentingWriter = new IndentingWriter(
				this.stringWriter, new SimpleIndentStrategy("\t"))) {
			customIndentingWriter.println("a");
			customIndentingWriter.indented(() -> {
				customIndentingWriter.println("b");
				customIndentingWriter.indented(() -> {
					customIndentingWriter.print("c");
					customIndentingWriter.println("e");
				});
			});
		}
		assertThat(this.stringWriter.toString()).isEqualTo("a\n\tb\n\t\tce\n");
	}

}

@@ -715,16 +715,64 @@
 gWriter;
+%0Aimport java.util.Arrays;%0Aimport java.util.List;
 %0A%0Aimport
@@ -1297,58 +1297,50 @@
 hat(
-this.stringWriter.toString()).isEqualTo(%22a%5Cnb%5Cnc%5Cn
+readLines()).containsSequence(%22a%22, %22b%22, %22c
 %22);%0A
@@ -1544,62 +1544,54 @@
 hat(
-this.stringWriter.toString()).isEqualTo(%22a%5Cn    b%5Cnc%5Cn
+readLines()).containsSequence(%22a%22, %22    b%22, %22c
 %22);%0A
@@ -1846,64 +1846,58 @@
 hat(
-this.stringWriter.toString()).isEqualTo(%22a%5Cn    b%5Cn%5Cnc%5Cn
+readLines()).containsSequence(%22a%22, %22    b%22, %22%22, %22c
 %22);%0A
@@ -2191,64 +2191,56 @@
 hat(
-this.stringWriter.toString()).isEqualTo(%22a%5Cn
+readLines()).containsSequence(%22a%22, %22
     bbb
-%5Cnc%5Cn
+%22, %22c
 %22);%0A
@@ -2708,16 +2708,132 @@
 ertThat(
+readLines()).containsSequence(%22a%22, %22%5Ctb%22, %22%5Ct%5Ctce%22);%0A%09%7D%0A%0A%09private List%3CString%3E readLines() %7B%0A%09%09return Arrays.asList(
 this.str
@@ -2856,39 +2856,26 @@
 ng()
-).isEqualTo(%22a%5Cn%5Ctb%5Cn%5Ct%5Ctce
+.split(%22%5C%5Cr?%5C
 %5Cn%22)
+)
 ;%0A%09%7D

Fix assertions to work on Windows