Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1a12e6715691ddb38ee744225ad3c341c6abf07f (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
 * Copyright (c) 2005, 2006 committers of openArchitectureWare and others.
 * 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xpand.internal.tests.output;

import junit.framework.TestCase;

import org.eclipse.internal.xpand2.ast.ExpressionStatement;
import org.eclipse.internal.xpand2.ast.IfStatement;
import org.eclipse.internal.xpand2.ast.TextStatement;
import org.eclipse.xpand2.output.Outlet;
import org.eclipse.xpand2.output.OutputImpl;

public class OutputImplTest extends TestCase {

	private OutputImpl getOutput() {
		final OutputImpl bo = new OutputImpl();
		final Outlet out = new Outlet();
		out.setName("APPEND");
		bo.addOutlet(out);
		return bo;
	}

	public final void testDeleteLine1() {
		final OutputImpl bo = getOutput();
		bo.openFile("test", "APPEND");

		bo.pushStatement(new TextStatement("", false), null);
		bo.write("Hallo test \r\n    ");
		bo.pushStatement(new TextStatement("", true), null);
		bo.write("     \n  \t  Ola");

		assertEquals("Hallo test \r\n  \t  Ola", bo.current__testONLY()
				.getBuffer().toString());
	}

	public final void testDeleteLine2() {
		final OutputImpl bo = getOutput();
		bo.openFile("test", "APPEND");

		bo.pushStatement(new TextStatement("", false), null);
		bo.write("Hallo test \r  \t  ");
		bo.pushStatement(new TextStatement("", true), null);
		bo.write("     \n    Ola");

		assertEquals("Hallo test \r    Ola", bo.current__testONLY().getBuffer()
				.toString());
	}

	public final void testDeleteLine2_5() {
		final OutputImpl bo = getOutput();
		bo.openFile("test", "APPEND");

		bo.pushStatement(new TextStatement("", true), null);
		bo.write("\r\nfirstLine\r\n");

		assertEquals("firstLine\r\n", bo.current__testONLY().getBuffer()
				.toString());
	}

	public final void testDeleteLine3() {
		final OutputImpl bo = getOutput();
		bo.openFile("test", "APPEND");

		bo.pushStatement(new TextStatement("", true), null);
		bo.write("\r\n");
		bo.pushStatement(new TextStatement("", true), null);
		bo.write("\r\n \tTest");

		assertEquals(" \tTest", bo.current__testONLY().getBuffer().toString());
	}

	public final void testDeleteLine4() {
		final OutputImpl bo = getOutput();
		bo.openFile("test", "APPEND");

		bo.pushStatement(new TextStatement("", true), null);
		bo.write("\r\n");
		bo.pushStatement(new ExpressionStatement(null), null);
		bo.write(" mein scholli ");
		bo.pushStatement(new TextStatement("", true), null);
		bo.write("\r\n \tTest");

		assertEquals(" mein scholli  \tTest", bo.current__testONLY()
				.getBuffer().toString());
	}

	public final void testPattern() {
		assertEquals("   ", del("", "   \n   "));
		assertEquals("foobar", del("foo", "\nbar"));
		assertEquals("foo\nbar", del("foo\n   ", "   \nbar"));
		assertEquals("foo\rbar", del("foo\r\t", "\r\nbar"));

		assertEquals("package", del("", "\r\npackage"));
	}

	private String del(String start, String end) {
		final OutputImpl bo = getOutput();
		bo.openFile("test", "APPEND");

		bo.pushStatement(new TextStatement("", false), null);
		bo.write(start);
		bo.pushStatement(new TextStatement("", true), null);
		bo.write(end);

		return bo.current__testONLY().getBuffer().toString();
	}

	public final void testBug152111() {
		final OutputImpl bo = getOutput();
		bo.openFile("test", "APPEND");

		bo.write("LINESTART ");
		bo.pushStatement(new TextStatement("", true), null);
		bo.write("\r\nWe have a ");
		bo.pushStatement(new TextStatement("", true), null);
		bo.write("\r\nLINEEND");

		assertEquals("LINESTART We have a LINEEND", bo.current__testONLY()
				.getBuffer().toString());
	}

	public void testDeleteAfterWS() throws Exception {
		assertEqualsAfterRemove("", "");
		assertEqualsAfterRemove("\n", "\n");
		assertEqualsAfterRemove("\n", "\n    ");
		assertEqualsAfterRemove("\n    Test", "\n    Test");
		assertEqualsAfterRemove("\n\r    Test", "\n\r    Test");
	}

	public void testTrimUntilNewline() throws Exception {
		assertEqualsTrim("", "");
		assertEqualsTrim("T   \n", "T   \n");
		assertEqualsTrim(" ", "    \n ");
		assertEqualsTrim("  \t  Ola", "     \n  \t  Ola");
	}

	private void assertEqualsAfterRemove(String expected, String toProcess) {
		final OutputImpl bo = getOutput();
		StringBuffer top = new StringBuffer(toProcess);
		bo.removeWSAfterLastNewline(top);
		assertEquals(expected, top.toString());
	}

	private void assertEqualsTrim(String expected, String toProcess) {
		final OutputImpl bo = getOutput();
		assertEquals(expected, bo.trimUntilNewline(toProcess));
	}

	public final void testAutomaticHyphenation1() {
		final OutputImpl bo = getOutput();
		bo.setAutomaticHyphens(true);
		bo.openFile("test", "APPEND");

		bo.pushStatement(new TextStatement("", false), null);
		bo.write("\r\n");
		bo.pushStatement(new IfStatement( null, null), null);
		bo.pushStatement(new TextStatement("", false), null);
		bo.write("\r\n \tTest");

		assertEquals(" \tTest", bo.current__testONLY().getBuffer().toString());
	}
}

Back to the top