001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.util.pp;
012:
013: import java.io.IOException;
014:
015: import junit.framework.TestCase;
016:
017: /** Unit-Test the {@link Layouter} class. */
018:
019: public class TestLayouter extends TestCase {
020:
021: /** A backend to remember the result in */
022: StringBackend narrowBack;
023: /** A backend to remember the result in */
024: StringBackend wideBack;
025: /** A backend to remember the result in */
026: StringBackend sixBack;
027: /** A layouter which breaks everything */
028: MarkingBackend markBack;
029: /** A layouter which breaks everything */
030: Layouter narrow;
031: /** A layouter which breaks nothing */
032: Layouter wide;
033: /** A layouter which breaks nothing */
034: Layouter six;
035: /** A layouter which breaks nothing */
036: Layouter marking;
037:
038: int[] marks;
039: int markPtr;
040:
041: public TestLayouter(String name) {
042: super (name);
043: }
044:
045: public void setUp() {
046: narrowBack = new StringBackend(1);
047: wideBack = new StringBackend(10000);
048: sixBack = new StringBackend(6);
049: markBack = new MarkingBackend(1);
050: narrow = new Layouter(narrowBack, 2);
051: wide = new Layouter(wideBack, 2);
052: six = new Layouter(sixBack, 2);
053: marking = new Layouter(markBack, 2);
054: marks = new int[100];
055: markPtr = 0;
056: }
057:
058: class MarkingBackend extends StringBackend implements Backend {
059: public MarkingBackend(int lineWidth) {
060: super (lineWidth);
061: }
062:
063: public void mark(Object o) {
064: marks[markPtr++] = count();
065: }
066: }
067:
068: public void testNarrowConsistent()
069: throws UnbalancedBlocksException, IOException {
070: narrow.beginC().print("A").beginC().print("B").brk(1, 2).print(
071: "C").brk(2, 3).print("D").end().print("E").end()
072: .close();
073: assertEquals("break consistent", "AB\n C\n DE",
074: narrowBack.getString());
075: }
076:
077: public void testWideConsistent() throws UnbalancedBlocksException,
078: IOException {
079: wide.beginC().print("A").beginC().print("B").brk(1, 2).print(
080: "C").brk(2, 3).print("D").end().print("E").end()
081: .close();
082: assertEquals("no break consistent", "AB C DE", wideBack
083: .getString());
084: }
085:
086: public void testNarrowInconsistent()
087: throws UnbalancedBlocksException, IOException {
088: narrow.beginC().print("A").beginI().print("B").brk(1, 2).print(
089: "C").brk(2, 3).print("D").end().print("E").end()
090: .close();
091: assertEquals("break inconsistent", "AB\n C\n DE",
092: narrowBack.getString());
093: }
094:
095: public void testWideInconsistent()
096: throws UnbalancedBlocksException, IOException {
097: wide.beginC().print("A").beginI().print("B").brk(1, 2).print(
098: "C").brk(2, 3).print("D").end().print("E").end()
099: .close();
100: assertEquals("no break inconsistent", "AB C DE", wideBack
101: .getString());
102: }
103:
104: public void testSixInconsistent() throws UnbalancedBlocksException,
105: IOException {
106: six.beginC().print("A").beginI().print("B").brk(1, 2)
107: .print("C").brk(2, 3).print("D").end().print("E").end()
108: .close();
109: assertEquals("some breaks inconsistent", "AB C\n DE",
110: sixBack.getString());
111: }
112:
113: public void testNarrowPre() throws UnbalancedBlocksException,
114: IOException {
115: narrow.beginC().print("[").pre("A\nB\nC").print("]").end()
116: .close();
117: assertEquals("preformatted", "[A\n B\n C]", narrowBack
118: .getString());
119:
120: }
121:
122: public void testWidePre() throws UnbalancedBlocksException,
123: IOException {
124: wide.beginC().print("[").pre("A\nB\nC").print("]").end()
125: .close();
126: assertEquals("preformatted", "[A\n B\n C]", wideBack
127: .getString());
128:
129: }
130:
131: public void testNarrowInd() throws UnbalancedBlocksException,
132: IOException {
133: narrow.beginC().print("A").beginC().ind(1, 2).print("B").brk(1,
134: 2).print("C").ind(3, 4).print("D").brk(2, 3).print("E")
135: .end().print("F").end().close();
136: assertEquals("ind consistent", "A B\n C D\n EF",
137: narrowBack.getString());
138: }
139:
140: public void testWideInd() throws UnbalancedBlocksException,
141: IOException {
142: wide.beginC().print("A").beginC().ind(1, 2).print("B")
143: .brk(1, 2).print("C").ind(3, 4).print("D").brk(2, 3)
144: .print("E").end().print("F").end().close();
145: assertEquals("ind consistent", "A B C D EF", wideBack
146: .getString());
147: }
148:
149: public void testMark() throws UnbalancedBlocksException,
150: IOException {
151: marking.beginC().mark(null).print("A").mark(null).beginC()
152: .mark(null).print("B").mark(null).brk(1, 2).mark(null)
153: .print("C").mark(null).brk(2, 3).mark(null).print("D")
154: .mark(null).end().mark(null).print("E").mark(null)
155: .end().mark(null).close();
156: assertEquals("break consistent", "AB\n C\n DE",
157: markBack.getString());
158: assertEquals("number marks", 11, markPtr);
159: assertEquals("marks pos 1", 0, marks[0]);
160: assertEquals("marks pos 2", 1, marks[1]);
161: assertEquals("marks pos 3", 1, marks[2]);
162: assertEquals("marks pos 4", 2, marks[3]);
163: assertEquals("marks pos 5", 8, marks[4]);
164: assertEquals("marks pos 6", 9, marks[5]);
165: assertEquals("marks pos 7", 16, marks[6]);
166: assertEquals("marks pos 8", 17, marks[7]);
167: assertEquals("marks pos 9", 17, marks[8]);
168: assertEquals("marks pos 10", 18, marks[9]);
169: assertEquals("marks pos 11", 18, marks[10]);
170: }
171: }
|