/*
* Name: SectionModifiersTest
* Authors: Richard Rodger
*
* Copyright (c) 2000-2003 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
// package
package org.jostraca.section.test;
// import
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.jostraca.section.Section;
import org.jostraca.section.BasicSection;
import org.jostraca.section.SectionSet;
import org.jostraca.section.Identity;
import org.jostraca.section.Collapser;
import org.jostraca.section.BlockIndenter;
import org.jostraca.section.PythonIndenter;
import org.jostraca.section.ModifierManager;
import org.jostraca.Property;
import org.jostraca.PropertyPython;
import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import java.util.Enumeration;
/** Test cases for Section Modifiers.
*/
public class SectionModifiersTest extends TestCase {
private ModifierManager iModifierManager;
public SectionModifiersTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( SectionModifiersTest.class );
}
public static void main( String[] pArgs ) {
TestRunner.run( suite() );
}
public void setUp() throws Exception {
iModifierManager = new ModifierManager();
}
public void testLoadModifiers() throws Exception {
String classList = "Identity,Marker";
iModifierManager.loadModifierClasses( classList );
}
public void testIdentity() throws Exception {
PropertySet mps = new PropertySet();
String fooname = "fooname";
String footext = "footext";
String barname = "barname";
String bartext = "bartext";
Section fooin = new BasicSection( fooname );
fooin.setContent( footext );
Section barin = new BasicSection( barname );
barin.setContent( bartext );
SectionSet foobarin = new SectionSet();
foobarin.addSection( fooin );
foobarin.addSection( barin );
Identity identity = new Identity();
Section fooout = identity.modify( fooin, mps );
assertTrue( fooout.getContent().equals( footext ) );
assertTrue( fooout.getName().equals( fooname ) );
identity.modify( barin, mps );
assertTrue( barin.getContent().equals( bartext ) );
assertTrue( barin.getName().equals( barname ) );
SectionSet foobarout = identity.modify( foobarin, mps );
Enumeration sectionEnum = foobarout.enumerateSections();
while( sectionEnum.hasMoreElements() ) {
Section section = (Section) sectionEnum.nextElement();
if( fooname.equals( section.getName() ) ) {
assertTrue( section.getContent().equals( footext ) );
}
else if( barname.equals( section.getName() ) ) {
assertTrue( section.getContent().equals( bartext ) );
}
else {
fail();
}
}
}
public void testCollapser() throws Exception {
String commentLinePrefix = "/*";
String commentLineSuffix = "*/";
PropertySet mps = new PropertySet();
mps.set( Property.lang_CommentLinePrefix, commentLinePrefix );
mps.set( Property.lang_CommentLineSuffix, commentLineSuffix );
String fooname = "fooname";
String footext = "footext";
String footextcollapse = commentLinePrefix+" Section: "+fooname+" "+commentLineSuffix;
String barname = "barname";
String bartext = "bartext";
String bartextcollapse = commentLinePrefix+" Section: "+barname+" "+commentLineSuffix;
Section fooin = new BasicSection( fooname );
fooin.setContent( footext );
Section barin = new BasicSection( barname );
barin.setContent( bartext );
SectionSet foobarin = new SectionSet();
foobarin.addSection( fooin );
foobarin.addSection( barin );
Collapser collapser = new Collapser();
Section fooout = collapser.modify( fooin, mps );
assertTrue( fooout.getContent().equals( footextcollapse ) );
assertTrue( fooout.getName().equals( fooname ) );
collapser.modify( barin, mps );
assertTrue( barin.getContent().equals( bartextcollapse ) );
assertTrue( barin.getName().equals( barname ) );
fooin.setContent( footext );
barin.setContent( bartext );
SectionSet foobarout = collapser.modify( foobarin, mps );
Enumeration sectionEnum = foobarout.enumerateSections();
while( sectionEnum.hasMoreElements() ) {
Section section = (Section) sectionEnum.nextElement();
if( fooname.equals( section.getName() ) ) {
assertTrue( section.getContent().equals( footextcollapse ) );
}
else if( barname.equals( section.getName() ) ) {
assertTrue( section.getContent().equals( bartextcollapse ) );
}
else {
fail();
}
}
}
public void testBlockIndenter() throws Exception {
PropertySet mps = new PropertySet();
String fooname = "foo";
String footext1 = "footext1" + Standard.NEWLINE;
String footext2 = "footext2";
String barname = "bar";
String bartext1 = "bartext1" + Standard.NEWLINE;
String bartext2 = "bartext2";
String naffname = "naff";
String nafftext1 = "nafftext1" + Standard.NEWLINE;
String nafftext2 = "nafftext2";
Section foos = new BasicSection( fooname );
foos.setContent( footext1 + footext2 );
Section bars = new BasicSection( barname );
bars.setContent( bartext1 + bartext2 );
Section naffs = new BasicSection( naffname );
naffs.setContent( nafftext1 + nafftext2 );
SectionSet foobarss = new SectionSet();
foobarss.addSection( foos );
foobarss.addSection( bars );
foobarss.addSection( naffs );
String in_1 = " ";
String in_1_2 = " ";
String in_1_2_3 = " ";
String wfsource1
= in_1+"!<foo>" + Standard.NEWLINE
+ in_1+"--" + Standard.NEWLINE
+ in_1_2+"!<bar>" + Standard.NEWLINE
+ in_1+"--" + Standard.NEWLINE
+ in_1_2_3+"!<naff>" + Standard.NEWLINE
+ in_1+"--" + Standard.NEWLINE
;
mps.set( Property.jostraca_writerformat_source, wfsource1 );
BlockIndenter bi = new BlockIndenter();
bi.modify( foobarss, mps );
assertTrue( ( footext1+in_1+footext2 ).equals( foos.getContent() ) );
assertTrue( ( bartext1+in_1_2+bartext2 ).equals( bars.getContent() ) );
assertTrue( ( nafftext1+in_1_2_3+nafftext2 ).equals( naffs.getContent() ) );
}
public void testPythonIndenter() throws Exception {
PythonIndenter pi = new PythonIndenter();
PropertySet mps = new PropertySet();
String tabAsSpaces = " ";
mps.set( PropertyPython.standardIndent, " " );
mps.set( PropertyPython.tabAsSpaces, tabAsSpaces);
mps.set( PropertyPython.nonStatementLineRegExp, "^[ \\t]*(#.*)?$" );
mps.set( PropertyPython.controlFlowRegExp, ":[ \\t]*(#.*)?$" );
mps.set( Property.lang_InsertPrefix, "_py_insert( " );
Section sec = new BasicSection( "sec" );
String in1 = "print 'foo' ";
String out1 = "print 'foo' ";
sec.setContent( in1 );
pi.modify( sec, mps );
//System.out.println( in1 );
//System.out.println( out1 );
//System.out.println( sec.getContent() );
assertTrue( out1.equals( sec.getContent() ) );
String in2 = " print 'foo' ";
String out2 = " print 'foo' ";
sec.setContent( in2 );
pi.modify( sec, mps );
//System.out.println( in2 );
//System.out.println( out2 );
//System.out.println( sec.getContent() );
assertTrue( out2.equals( sec.getContent() ) );
String in3
= "print 'foo'"+Standard.NEWLINE
+ "_py_insert( \"abc\" )"
;
String out3
= "print 'foo'"+Standard.NEWLINE
+ "_py_insert( \"abc\" )"
;
sec.setContent( in3 );
pi.modify( sec, mps );
//System.out.println( in3 );
//System.out.println( out3 );
//System.out.println( sec.getContent() );
assertTrue( out3.equals( sec.getContent() ) );
String in4
= " print 'foo'"+Standard.NEWLINE
+ "_py_insert( \"abc\" )"
;
String out4
= " print 'foo'"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
sec.setContent( in4 );
pi.modify( sec, mps );
//System.out.println( in4 );
//System.out.println( out4 );
//System.out.println( sec.getContent() );
assertTrue( out4.equals( sec.getContent() ) );
String in4_1
= " print 'foo'"+Standard.NEWLINE
+ " "+Standard.NEWLINE
+ "_py_insert( \"abc\" )"
;
String out4_1
= " print 'foo'"+Standard.NEWLINE
+ " "+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
sec.setContent( in4_1 );
pi.modify( sec, mps );
//System.out.println( in4_1 );
//System.out.println( out4_1 );
//System.out.println( sec.getContent() );
assertTrue( out4_1.equals( sec.getContent() ) );
String in4_2
= " print 'foo'"+Standard.NEWLINE
+ " # foo"+Standard.NEWLINE
+ "_py_insert( \"abc\" )"
;
String out4_2
= " print 'foo'"+Standard.NEWLINE
+ " # foo"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
sec.setContent( in4_2 );
pi.modify( sec, mps );
//System.out.println( in4_2 );
//System.out.println( out4_2 );
//System.out.println( sec.getContent() );
assertTrue( out4_2.equals( sec.getContent() ) );
String in5
= "if foo == bar :"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
String out5
= "if foo == bar :"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
sec.setContent( in5 );
pi.modify( sec, mps );
//System.out.println( in5 );
//System.out.println( out5 );
//System.out.println( sec.getContent() );
assertTrue( out5.equals( sec.getContent() ) );
String in6
= "if foo == bar : "+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
String out6
= "if foo == bar : "+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
sec.setContent( in6 );
pi.modify( sec, mps );
//System.out.println( in6 );
//System.out.println( out6 );
//System.out.println( sec.getContent() );
assertTrue( out6.equals( sec.getContent() ) );
String in7
= "if foo == bar : # fooby"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
String out7
= "if foo == bar : # fooby"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
sec.setContent( in7 );
pi.modify( sec, mps );
//System.out.println( in7 );
//System.out.println( out7 );
//System.out.println( sec.getContent() );
assertTrue( out7.equals( sec.getContent() ) );
String in8
= "if foo == bar : # fooby"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"+Standard.NEWLINE
+ " pass"
;
String out8
= "if foo == bar : # fooby"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"+Standard.NEWLINE
+ " pass"
;
sec.setContent( in8 );
pi.modify( sec, mps );
//System.out.println( in8 );
//System.out.println( out8 );
//System.out.println( sec.getContent() );
assertTrue( out8.equals( sec.getContent() ) );
String in9
= "if foo == bar : # fooby"+Standard.NEWLINE
+ " pass"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
String out9
= "if foo == bar : # fooby"+Standard.NEWLINE
+ " pass"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"
;
sec.setContent( in9 );
pi.modify( sec, mps );
//System.out.println( in9 );
//System.out.println( out9 );
//System.out.println( sec.getContent() );
assertTrue( out9.equals( sec.getContent() ) );
String in10
= " if foo == bar : # fooby"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"+Standard.NEWLINE
+ " pass"
;
String out10
= " if foo == bar : # fooby"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"+Standard.NEWLINE
+ " pass"
;
sec.setContent( in10 );
pi.modify( sec, mps );
//System.out.println( in10 );
//System.out.println( out10 );
//System.out.println( sec.getContent() );
assertTrue( out10.equals( sec.getContent() ) );
String in11
= " if foo == bar : # fooby"+Standard.NEWLINE
+ "_py_insert( \"abc\" )"+Standard.NEWLINE
+ " pass"
;
String out11
= " if foo == bar : # fooby"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"+Standard.NEWLINE
+ " pass"
;
sec.setContent( in11 );
pi.modify( sec, mps );
//System.out.println( in11 );
//System.out.println( out11 );
//System.out.println( sec.getContent() );
assertTrue( out11.equals( sec.getContent() ) );
String in12
= " if foo == bar : # fooby"+Standard.NEWLINE
+ "_py_insert( \"abc\" )"+Standard.NEWLINE
+ "pass"
;
String out12
= " if foo == bar : # fooby"+Standard.NEWLINE
+ " _py_insert( \"abc\" )"+Standard.NEWLINE
+ "pass"
;
sec.setContent( in12 );
pi.modify( sec, mps );
//System.out.println( in12 );
//System.out.println( out12 );
//System.out.println( sec.getContent() );
assertTrue( out12.equals( sec.getContent() ) );
String in13
= "\tif foo == bar : # fooby"+Standard.NEWLINE
+ "_py_insert( \"abc\" )"+Standard.NEWLINE
+ "pass"
;
String out13
= "\tif foo == bar : # fooby"+Standard.NEWLINE
+ tabAsSpaces+" _py_insert( \"abc\" )"+Standard.NEWLINE
+ "pass"
;
sec.setContent( in13 );
pi.modify( sec, mps );
//System.out.println( in13 );
//System.out.println( out13 );
//System.out.println( sec.getContent() );
assertTrue( out13.equals( sec.getContent() ) );
String in14
= "def list( max ) :"+Standard.NEWLINE
+ ""+Standard.NEWLINE
+ " _py_insert( \"\\nlist\" )"+Standard.NEWLINE
+ "_py_insert( max )"+Standard.NEWLINE
+ "_py_insert( \" = \" )"+Standard.NEWLINE
+ ""+Standard.NEWLINE
+ ""+Standard.NEWLINE
+ " for i in range( 0, max ) :"+Standard.NEWLINE
+ " _py_insert( \"[\" )"+Standard.NEWLINE
+ "_py_insert( i )"+Standard.NEWLINE
+ "_py_insert( \"]\" )"+Standard.NEWLINE
;
String out14
= "def list( max ) :"+Standard.NEWLINE
+ " _py_insert( \"\\nlist\" )"+Standard.NEWLINE
+ " _py_insert( max )"+Standard.NEWLINE
+ " _py_insert( \" = \" )"+Standard.NEWLINE
+ " for i in range( 0, max ) :"+Standard.NEWLINE
+ " _py_insert( \"[\" )"+Standard.NEWLINE
+ " _py_insert( i )"+Standard.NEWLINE
+ " _py_insert( \"]\" )"+Standard.NEWLINE
;
sec = new BasicSection( "declare" );
sec.setContent( in14 );
pi.modify( sec, mps );
//System.out.println( in14 );
//System.out.println( out14 );
//System.out.println( sec.getContent() );
assertTrue( out14.equals( sec.getContent() ) );
}
}