Package org.jostraca.util.test

Source Code of org.jostraca.util.test.RegExpTest

/*
* Name:    RegExpTest
* Author:  Richard Rodger
*
* Copyright (c) 2002-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.util.test;


// import
import org.jostraca.util.RegExp;
import org.jostraca.util.RegExpMatch;
import org.jostraca.util.RegExpProvider;
import org.jostraca.util.RegExpException;
import org.jostraca.util.GnuRegExpProvider;
import org.jostraca.util.OroRegExpProvider;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;



/** Test cases for RegExp providers. */
public class RegExpTest extends TestCase {

  /* Test Framework << */

  public RegExpTest( String pName ) {
    super( pName );
  }

  public static TestSuite suite() {
    return new TestSuite( RegExpTest.class );
  }

  public static void main( String[] pArgs ) {
    TestRunner.run( suite() );
  }

  /* Test Framework >> */




  /* Public Methods << */

  public void testGnuRegExp() throws Exception {
    GnuRegExpProvider rep = new GnuRegExpProvider();
    doMatchRegExpTests( rep );
    doReplaceRegExpTests( rep );
    doMatchAllTests( rep );
  }


  public void testOroRegExp() throws Exception {
    OroRegExpProvider rep = new OroRegExpProvider();
    doMatchRegExpTests( rep );
    doReplaceRegExpTests( rep );
    doMatchAllTests( rep );
  }



  public void doReplaceRegExpTests( RegExpProvider pREP ) throws Exception {

    RegExp re01 = pREP.make( "a", "b" );
    assertEquals( "b",  re01.replaceAll( "a" ) );
    assertEquals( "bb", re01.replaceAll( "aa" ) );
    assertEquals( "ba", re01.replaceFirst( "aa" ) );
    assertEquals( "xb", re01.replaceFirst( "xa" ) );

    RegExp re02 = pREP.make( "a" );
    assertEquals( "",  re02.replaceAll( "a" ) );
   
    try {
      RegExp re03 = pREP.make( "*" );
      fail();
    }
    catch( RegExpException ree ) {
      //System.out.println( ree );
    }

    RegExp re04 = pREP.make( "a(b+)c", "ca$1" );
    assertEquals( "cabbbabbbc", re04.replaceFirst( "abbbcabbbc" ) );

    RegExp re05 = pREP.make( "A", "b", new RegExp.ModeSet( RegExp.Mode.CaseInsensitive ) );
    assertEquals( "bbc", re05.replaceFirst( "abc" ) );

    RegExp re06 = pREP.make( "A.", "b", new RegExp.ModeSet( RegExp.Mode.CaseInsensitive, RegExp.Mode.DotMatchesNewline ) );
    assertEquals( "bbc", re06.replaceFirst( "a\nbc" ) );
  }


  public void doMatchRegExpTests( RegExpProvider pREP ) throws Exception {
    RegExp re01 = pREP.make( "a(.*?)c" );
    assertEquals( "abc",  re01.match( "abcc" ) );
    assertEquals( "b",    re01.matchFirstSub( "abcc" ) );

    RegExp re02 = pREP.make( "\\$a" );
    assertEquals( "$a",  re02.match( "$ab" ) );

    RegExp re03 = pREP.make( "a.b.", new RegExp.ModeSet( RegExp.Mode.DotMatchesNewline ) );
    assertEquals( "a\nb\n",  re03.match( "ca\nb\n" ) );

    RegExp re04 = pREP.make( "^[ \\t]*\\n(.*)", new RegExp.ModeSet( RegExp.Mode.DotMatchesNewline ) );
    assertEquals( "b\n",  re04.matchFirstSub( "\nb\n" ) );
  }


  public void doMatchAllTests( RegExpProvider pREP ) throws Exception {
    RegExp      re01 = pREP.make( "a(.)\n" );
    RegExpMatch m[]  = re01.matchAll( "x\naa\nab\nac\ny\n" );
    assertEquals( 3, m.length );
    assertEquals( "aa\n", m[0].match() );
    assertEquals( "ab\n", m[1].match() );
    assertEquals( "ac\n", m[2].match() );
    assertEquals( "a", m[0].matchSub(1) );
    assertEquals( "b", m[1].matchSub(1) );
    assertEquals( "c", m[2].matchSub(1) );
  }

  /* Public Methods >> */

}
 
TOP

Related Classes of org.jostraca.util.test.RegExpTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.