001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.util;
016:
017: import org.apache.tapestry.ioc.test.IOCTestCase;
018: import org.testng.annotations.Test;
019:
020: public class BodyBuilderTest extends IOCTestCase {
021: @Test
022: public void simple_nesting_and_indentation() {
023: BodyBuilder b = new BodyBuilder();
024:
025: b.begin();
026: b.addln("invoke();");
027: b.end();
028:
029: assertEquals(b.toString(), join("{", " invoke();", "}"));
030: }
031:
032: @Test
033: public void block_nesting() {
034: BodyBuilder b = new BodyBuilder();
035:
036: b.begin();
037:
038: b.add("while(true)");
039: b.begin();
040: b.add("_i += 1;");
041: b.end();
042:
043: b.end();
044:
045: assertEquals(b.toString(), join("{", " while(true)", " {",
046: " _i += 1;", " }", "}"));
047: }
048:
049: @Test
050: public void addln_idents_subsequent_line() {
051: BodyBuilder b = new BodyBuilder();
052:
053: b.begin();
054: b.addln("invoke(fred);");
055: b.addln("invoke(barney);");
056: b.end();
057:
058: assertEquals(b.toString(), join("{", " invoke(fred);",
059: " invoke(barney);", "}"));
060: }
061:
062: @Test
063: public void clear() {
064: BodyBuilder b = new BodyBuilder();
065:
066: b.begin();
067: b.add("fred");
068: b.end();
069:
070: assertEquals(b.toString(), "{\n fred\n}\n");
071:
072: b.clear();
073:
074: b.begin();
075: b.add("barney");
076: b.end();
077:
078: assertEquals(b.toString(), "{\n barney\n}\n");
079: }
080:
081: @Test
082: public void add_with_format_and_args() {
083: BodyBuilder b = new BodyBuilder();
084:
085: b.add("%s = %d;", "i", 3);
086:
087: assertEquals(b.toString(), "i = 3;");
088: }
089:
090: @Test
091: public void addln_with_format_and_args() {
092: BodyBuilder b = new BodyBuilder();
093:
094: b.addln("%s = %d;", "i", 3);
095:
096: assertEquals(b.toString(), "i = 3;\n");
097: }
098:
099: @Test
100: public void indent_only_on_new_line() {
101: BodyBuilder b = new BodyBuilder();
102:
103: b.begin();
104: b.add("if");
105: b.addln(" (debug)");
106: b.add(" log.debug(\"%s\"", "foo");
107: b.addln(");");
108: b.addln("while (true)");
109: b.begin();
110: b.addln("if (%s > 10)", "i");
111: b.addln(" return;");
112: b.add("%s++;", "i");
113: b.end();
114: b.end();
115:
116: assertEquals(b.toString(), join("{", " if (debug)",
117: " log.debug(\"foo\");", " while (true)", " {",
118: " if (i > 10)", " return;", " i++;", " }",
119: "}"
120:
121: ));
122:
123: }
124: }
|