001package org.apache.maven.doxia.module.twiki.parser;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Arrays;
023
024/**
025 * Tests the {@link org.apache.maven.doxia.module.twiki.parser.FormatedTextParser}
026 *
027 * @author Juan F. Codagnone
028 * @since Nov 2, 2005
029 */
030public class FormatedTextTest
031    extends AbstractBlockTestCase
032{
033
034    /**
035     * test bold text
036     */
037    public final void testBold()
038    {
039        String text;
040        Block[] blocks;
041
042        text = "*bold*";
043        blocks = formatTextParser.parse( text );
044        assertEquals( 1, blocks.length );
045        assertEquals( new BoldBlock( new Block[] { new TextBlock( "bold" ) } ), blocks[0] );
046
047        text = "foo *bold* bar";
048        blocks = formatTextParser.parse( text );
049        assertTrue( Arrays.equals( new Block[] { new TextBlock( "foo " ),
050            new BoldBlock( new Block[] { new TextBlock( "bold" ) } ), new TextBlock( " bar" ) }, blocks ) );
051
052        text = "\t*bold* bar";
053        blocks = formatTextParser.parse( text );
054        assertTrue( Arrays.equals( new Block[] { new TextBlock( "\t" ),
055            new BoldBlock( new Block[] { new TextBlock( "bold" ) } ), new TextBlock( " bar" ) }, blocks ) );
056
057        text = "*nice* foo *bold* bar";
058        blocks = formatTextParser.parse( text );
059        assertTrue( Arrays.equals( new Block[] { new BoldBlock( new Block[] { new TextBlock( "nice" ) } ),
060            new TextBlock( " foo " ), new BoldBlock( new Block[] { new TextBlock( "bold" ) } ),
061            new TextBlock( " bar" ) }, blocks ) );
062    }
063
064    /**
065     * test italic text
066     */
067    public final void testItalic()
068    {
069        String text;
070        Block[] blocks;
071
072        text = "_italic_";
073        blocks = formatTextParser.parse( text );
074        assertEquals( 1, blocks.length );
075        assertEquals( new ItalicBlock( new Block[] { new TextBlock( "italic" ) } ), blocks[0] );
076
077        text = "foo _italic_ bar";
078        blocks = formatTextParser.parse( text );
079        assertTrue( Arrays.equals( new Block[] { new TextBlock( "foo " ),
080            new ItalicBlock( new Block[] { new TextBlock( "italic" ) } ), new TextBlock( " bar" ) }, blocks ) );
081
082        text = "_nice_ foo _italic_ bar";
083        blocks = formatTextParser.parse( text );
084        assertTrue( Arrays.equals( new Block[] { new ItalicBlock( new Block[] { new TextBlock( "nice" ) } ),
085            new TextBlock( " foo " ), new ItalicBlock( new Block[] { new TextBlock( "italic" ) } ),
086            new TextBlock( " bar" ) }, blocks ) );
087    }
088
089    /**
090     * test monospaced text
091     */
092    public final void testMonospaced()
093    {
094        String text;
095        Block[] blocks;
096
097        text = "mary =has= a =little= lamb He followed her (=to school one day=)";
098        blocks = formatTextParser.parse( text );
099        assertTrue( Arrays.equals( new Block[] { new TextBlock( "mary " ),
100            new MonospaceBlock( new Block[] { new TextBlock( "has" ) } ), new TextBlock( " a " ),
101            new MonospaceBlock( new Block[] { new TextBlock( "little" ) } ),
102            new TextBlock( " lamb He followed her (" ),
103            new MonospaceBlock( new Block[] { new TextBlock( "to school one day" ) } ), new TextBlock( ")" ) },
104                                   blocks ) );
105    }
106
107    /**
108     * test monospaced text
109     */
110    public final void testBoldMonospaced()
111    {
112        String text;
113        Block[] blocks;
114
115        text = "mary ==has== a ==little== lamb";
116        blocks = formatTextParser.parse( text );
117        Block[] expected =
118            new Block[] { new TextBlock( "mary " ),
119                new BoldBlock( new Block[] { new MonospaceBlock( new Block[] { new TextBlock( "has" ) } ) } ),
120                new TextBlock( " a " ),
121                new BoldBlock( new Block[] { new MonospaceBlock( new Block[] { new TextBlock( "little" ) } ) } ),
122                new TextBlock( " lamb" ) };
123
124        assertTrue( Arrays.equals( expected, blocks ) );
125    }
126
127    /**
128     * test monospaced text
129     */
130    public final void testBoldItalic()
131    {
132        String text;
133        Block[] blocks;
134
135        text = "mary __has__ a __little__ lamb";
136        blocks = formatTextParser.parse( text );
137        assertTrue( Arrays.equals( new Block[] { new TextBlock( "mary " ),
138            new BoldBlock( new Block[] { new ItalicBlock( new Block[] { new TextBlock( "has" ) } ) } ),
139            new TextBlock( " a " ),
140            new BoldBlock( new Block[] { new ItalicBlock( new Block[] { new TextBlock( "little" ) } ) } ),
141            new TextBlock( " lamb" ) }, blocks ) );
142    }
143
144    /**
145     * test mixed formats side by side
146     */
147    public final void testMultiFormatSideBySide()
148    {
149        String text;
150        Block[] blocks;
151        Block[] expected;
152
153        text = "All *work and* =no play= _makes_ Juan a dull *boy*";
154        blocks = formatTextParser.parse( text );
155
156        expected =
157            new Block[] { new TextBlock( "All " ), new BoldBlock( new Block[] { new TextBlock( "work and" ) } ),
158                new TextBlock( " " ), new MonospaceBlock( new Block[] { new TextBlock( "no play" ) } ),
159                new TextBlock( " " ), new ItalicBlock( new Block[] { new TextBlock( "makes" ) } ),
160                new TextBlock( " Juan a dull " ), new BoldBlock( new Block[] { new TextBlock( "boy" ) } ) };
161        assertTrue( Arrays.equals( expected, blocks ) );
162
163    }
164
165    /**
166     * test mixed formats recursevily
167     */
168    public final void testMultiFormatInside()
169    {
170        String text;
171        Block[] blocks;
172        Block[] expected;
173
174        text = "All *work and =no play _makes_ Juan= a dull* boy";
175        blocks = formatTextParser.parse( text );
176
177        expected =
178            new Block[] {
179                new TextBlock( "All " ),
180                new BoldBlock(
181                               new Block[] {
182                                   new TextBlock( "work and " ),
183                                   new MonospaceBlock( new Block[] { new TextBlock( "no play " ),
184                                       new ItalicBlock( new Block[] { new TextBlock( "makes" ) } ),
185                                       new TextBlock( " Juan" ) } ), new TextBlock( " a dull" ) } ),
186                new TextBlock( " boy" ) };
187        assertTrue( Arrays.equals( expected, blocks ) );
188    }
189
190    /**
191     * test unbonded formats
192     */
193    public final void testUnboundedFormat()
194    {
195        testHanging( "All *work and no play makes Juan a dull boy" );
196        testHanging( "All __work and no play makes Juan a dull boy" );
197        testHanging( "All __work and *no play makes _Juan a = dull boy" );
198        testHanging( "*" );
199        testHanging( "==" );
200        testHanging( "**" ); // hehe
201        testHanging( "*  hello   *" );
202        testHanging( "*  hello   =*" );
203        testHanging( "*=_  hello   _=*" );
204    }
205
206    /**
207     * @param text unbonded text
208     */
209    public final void testHanging( final String text )
210    {
211        assertTrue( Arrays.equals( new Block[] { new TextBlock( text ) }, formatTextParser.parse( text ) ) );
212    }
213}