1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.textclassifier.utils; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import androidx.test.ext.junit.runners.AndroidJUnit4; 22 import androidx.test.filters.SmallTest; 23 import java.io.PrintWriter; 24 import java.io.StringWriter; 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 @SmallTest 30 @RunWith(AndroidJUnit4.class) 31 public final class IndentingPrintWriterTest { 32 33 private static final String TEST_STRING = "sense"; 34 private static final String TEST_KEY = "key"; 35 private static final String TEST_VALUE = "value"; 36 37 private StringWriter stringWriter; 38 private IndentingPrintWriter indentingPrintWriter; 39 40 @Before setUp()41 public void setUp() { 42 stringWriter = new StringWriter(); 43 indentingPrintWriter = 44 new IndentingPrintWriter(new PrintWriter(stringWriter, /* autoFlush= */ true)); 45 } 46 47 @Test println_printString_noIndent()48 public void println_printString_noIndent() throws Exception { 49 indentingPrintWriter.println(TEST_STRING); 50 51 assertThat(stringWriter.toString()).isEqualTo(TEST_STRING + "\n"); 52 } 53 54 @Test println_printString_withIndent()55 public void println_printString_withIndent() throws Exception { 56 indentingPrintWriter.increaseIndent().println(TEST_STRING); 57 58 assertThat(stringWriter.toString()) 59 .isEqualTo(IndentingPrintWriter.SINGLE_INDENT + TEST_STRING + "\n"); 60 } 61 62 @Test decreaseIndent_noIndent()63 public void decreaseIndent_noIndent() throws Exception { 64 indentingPrintWriter.decreaseIndent().println(TEST_STRING); 65 66 assertThat(stringWriter.toString()).isEqualTo(TEST_STRING + "\n"); 67 } 68 69 @Test decreaseIndent_withIndent()70 public void decreaseIndent_withIndent() throws Exception { 71 indentingPrintWriter.increaseIndent().decreaseIndent().println(TEST_STRING); 72 73 assertThat(stringWriter.toString()).isEqualTo(TEST_STRING + "\n"); 74 } 75 76 @Test printPair_singlePair()77 public void printPair_singlePair() throws Exception { 78 indentingPrintWriter.printPair(TEST_KEY, TEST_VALUE); 79 80 assertThat(stringWriter.toString()).isEqualTo(TEST_KEY + "=" + TEST_VALUE + "\n"); 81 } 82 } 83