Skip to main content
summaryrefslogtreecommitdiffstats
blob: 33d331b5f66b92f8d8759784b02fe1eb1ccacf66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * Copyright (c) 2005, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal;

import java.io.StringWriter;
import junit.framework.TestCase;

import org.eclipse.jpt.common.utility.IndentingPrintWriter;

@SuppressWarnings("nls")
public class IndentingPrintWriterTests extends TestCase {
	StringWriter sw1;
	StringWriter sw2;
	IndentingPrintWriter ipw1;
	IndentingPrintWriter ipw2;

	static final String CR = System.getProperty("line.separator");

	public IndentingPrintWriterTests(String name) {
		super(name);
	}

	@Override
	public void setUp() throws Exception {
		super.setUp();
		this.sw1 = new StringWriter();
		this.ipw1 = new IndentingPrintWriter(this.sw1);
		this.sw2 = new StringWriter();
		this.ipw2 = new IndentingPrintWriter(this.sw2, "    "); // indent with 4 spaces instead of a tab
	}

	@Override
	protected void tearDown() throws Exception {
		TestTools.clear(this);
		super.tearDown();
	}

	public void testIndent() {
		assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
		this.ipw1.indent();
		assertEquals("wrong indent level", 1, this.ipw1.getIndentLevel());
	}

	public void testUndent() {
		assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
		this.ipw1.indent();
		assertEquals("wrong indent level", 1, this.ipw1.getIndentLevel());
		this.ipw1.undent();
		assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
	}

	public void testIncrementIndentLevel() {
		assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
		this.ipw1.incrementIndentLevel();
		assertEquals("wrong indent level", 1, this.ipw1.getIndentLevel());
	}

	public void testDecrementIndentLevel() {
		assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
		this.ipw1.incrementIndentLevel();
		assertEquals("wrong indent level", 1, this.ipw1.getIndentLevel());
		this.ipw1.decrementIndentLevel();
		assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
	}

	public void testPrintTab() {
		String expected = "foo0" + CR + "\tfoo1" + CR + "\tfoo1" + CR + "\t\tfoo2" + CR + "\tfoo1" + CR + "\tfoo1" + CR + "foo0" + CR;

		this.ipw1.println("foo0");
		this.ipw1.indent();
		this.ipw1.println("foo1");
		this.ipw1.println("foo1");
		this.ipw1.indent();
		this.ipw1.println("foo2");
		this.ipw1.undent();
		this.ipw1.println("foo1");
		this.ipw1.println("foo1");
		this.ipw1.undent();
		this.ipw1.println("foo0");

		assertEquals("bogus output", expected, this.sw1.toString());
	}

	public void testPrintSpaces() {
		String expected = "foo0" + CR + "    foo1" + CR + "    foo1" + CR + "        foo2" + CR + "    foo1" + CR + "    foo1" + CR + "foo0" + CR;

		this.ipw2.println("foo0");
		this.ipw2.indent();
		this.ipw2.println("foo1");
		this.ipw2.println("foo1");
		this.ipw2.indent();
		this.ipw2.println("foo2");
		this.ipw2.undent();
		this.ipw2.println("foo1");
		this.ipw2.println("foo1");
		this.ipw2.undent();
		this.ipw2.println("foo0");

		assertEquals("bogus output", expected, this.sw2.toString());
	}

}

Back to the top