eclipse plugin: Add tests for parsing the configs

This commit is contained in:
Timotei Dolean 2011-07-26 15:44:04 +00:00
parent 7e2c8cea76
commit 351a3d803d
6 changed files with 191 additions and 64 deletions

View file

@ -1,5 +1,6 @@
package org.wesnoth.tests;
import java.io.File;
import java.io.StringReader;
import java.util.List;
@ -14,13 +15,17 @@ import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.parser.antlr.ITokenDefProvider;
import org.eclipse.xtext.parser.antlr.Lexer;
import org.eclipse.xtext.parser.antlr.XtextTokenStream;
import org.junit.Ignore;
import org.wesnoth.WMLStandaloneSetup;
import org.wesnoth.services.WMLGrammarAccess;
import org.wesnoth.utils.StringUtils;
@SuppressWarnings( "all" )
public abstract class WMLTests extends AbstractXtextTests
{
protected WMLGrammarAccess grammar;
protected WMLGrammarAccess grammar_;
protected String dataPath_ = "";
private Lexer lexer;
private ITokenDefProvider tokenDefProvider;
private IParser parser;
@ -53,7 +58,14 @@ public abstract class WMLTests extends AbstractXtextTests
lexer = get( Lexer.class );
tokenDefProvider = get( ITokenDefProvider.class );
parser = get( IParser.class );
grammar = ( WMLGrammarAccess ) getGrammarAccess( );
grammar_ = ( WMLGrammarAccess ) getGrammarAccess( );
// get the wesnoth data path from the user
dataPath_ = System.getProperty( "wesnothDataDir" );
if ( StringUtils.isNullOrEmpty( dataPath_ ) || ! new File( dataPath_ ).exists( ) ) {
System.out.println( "Please set the wesnoth data dir before testing!." );
assertTrue( false );
}
}
@Override
@ -64,7 +76,8 @@ public abstract class WMLTests extends AbstractXtextTests
lexer = null;
tokenDefProvider = null;
parser = null;
grammar = null;
grammar_ = null;
dataPath_ = null;
}
/**
@ -174,4 +187,28 @@ public abstract class WMLTests extends AbstractXtextTests
String type = getTokenType( tokens.get( 0 ) );
assertFalse( keyword, type.charAt( 0 ) == '\'' );
}
@Ignore
public void testPath( String path )
{
File theFile = new File( path );
if ( ! theFile.exists( ) ) {
System.out.println( "Skipping non-existent path:" + path );
return;
}
if ( theFile.isFile( ) )
testFile( path );
else {
for ( File file : theFile.listFiles( ) ) {
testPath( file.getAbsolutePath( ) );
}
}
}
@Ignore
public void testFile( String path ) {
}
}

View file

@ -8,23 +8,19 @@
*******************************************************************************/
package org.wesnoth.tests;
import junit.framework.TestSuite;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.wesnoth.tests.grammar.WMLFilesTests;
import org.wesnoth.tests.grammar.WMLGrammarTokensTests;
import org.wesnoth.tests.plugin.WMLParsingCampaignsConfigs;
@RunWith( Suite.class )
@Suite.SuiteClasses(
{
WMLGrammarTokensTests.class,
WMLFilesTests.class,
WMLParsingCampaignsConfigs.class,
} )
public class WMLTestsSuite
{
@Test
public static TestSuite suite( )
{
TestSuite suite = new TestSuite( );
// grammar
suite.addTestSuite( WMLFilesTests.class );
suite.addTestSuite( WMLGrammarTokensTests.class );
return suite;
}
}

View file

@ -8,40 +8,18 @@
*******************************************************************************/
package org.wesnoth.tests.grammar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import org.eclipse.xtext.parser.IParseResult;
import org.junit.Ignore;
import org.wesnoth.tests.WMLTests;
import org.wesnoth.utils.StringUtils;
/**
* This tests test the data/core directory and each campaign
*/
public class WMLFilesTests extends WMLTests
{
protected String dataPath_ = "";
@Override
protected void setUp() throws Exception
{
super.setUp( );
// get the wesnoth data path from the user
dataPath_ = System.getProperty( "wesnothDataDir" );
if ( StringUtils.isNullOrEmpty( dataPath_ ) || ! new File( dataPath_ ).exists( ) )
throw new Exception( "Please set the wesnoth data dir before testing!." );
}
@Override
protected void tearDown() throws Exception
{
super.tearDown( );
dataPath_ = null;
}
public void testCoreFiles() throws FileNotFoundException
{
testPath( dataPath_ + "/core/" );
@ -137,25 +115,7 @@ public class WMLFilesTests extends WMLTests
testPath( dataPath_ + "/campaigns/Under_the_Burning_Suns/" );
}
@Ignore
public void testPath( String path )
{
File theFile = new File( path );
if ( ! theFile.exists( ) ) {
System.out.println( "Skipping non-existent path:" + path );
return;
}
if ( theFile.isFile( ) )
testFile( path );
else {
for ( File file : theFile.listFiles( ) ) {
testPath( file.getAbsolutePath( ) );
}
}
}
@Override
@Ignore
public void testFile( String path )
{
@ -174,7 +134,7 @@ public class WMLFilesTests extends WMLTests
catch ( FileNotFoundException e ) {
e.printStackTrace();
//should not reach here.
assertEquals( true, false );
assertTrue( false );
}
}
}

View file

@ -33,7 +33,7 @@ public class WMLGrammarTokensTests extends WMLTests
public void testTokenSequences()
{
checkParsing( "amount=+$random\r\n", grammar.getWMLKeyRule( ) );
checkParsing( "amount=+$random\r\n", grammar_.getWMLKeyRule( ) );
checkTokenisation( "123 abc", ID, WS, ID );
checkTokenisation( "123 \t#comment\n abc", ID, WS, SL_COMMENT, WS, ID );

View file

@ -0,0 +1,117 @@
/*******************************************************************************
* Copyright (c) 2011 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.tests.plugin;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.wesnoth.tests.WMLTests;
import org.wesnoth.utils.StringUtils;
import org.wesnoth.wml.SimpleWMLParser;
import org.wesnoth.wml.WMLConfig;
/**
* The class tests the parsed campaigns config files
*/
@RunWith( value = Parameterized.class )
public class WMLParsingCampaignsConfigs extends WMLTests
{
private String campaignDir_;
private String campaignId;
private List<String> scenarioIds_;
private List<String> parsedCampaignIds_;
private List<String> parsedScenarioIds_;
public WMLParsingCampaignsConfigs( String[] data )
{
try {
setUp( );
}
catch ( Exception e ) {
e.printStackTrace();
assertTrue ( false );
}
campaignDir_ = data[0];
campaignId = data[1];
scenarioIds_ = new ArrayList<String>();
parsedCampaignIds_ = new ArrayList<String>();
parsedScenarioIds_ = new ArrayList<String>();
for( int i = data.length - 1 ; i > 1; --i )
scenarioIds_.add( data[i] );
// gather all info
testPath( dataPath_ + "/campaigns/" + campaignDir_ + "/" );
}
@Override
@Ignore
public void testFile( String path )
{
// just config files
if ( ! path.endsWith( ".cfg" ) )
return;
try {
SimpleWMLParser wmlParser = new SimpleWMLParser( new File( path ), getParser( ) );
wmlParser.parse( );
WMLConfig config = wmlParser.getParsedConfig( );
if ( !StringUtils.isNullOrEmpty( config.ScenarioId ) )
parsedScenarioIds_.add( config.ScenarioId );
if ( !StringUtils.isNullOrEmpty( config.CampaignId ) )
parsedCampaignIds_.add( config.CampaignId );
}
catch ( FileNotFoundException e ) {
e.printStackTrace();
assertTrue( false );
}
}
@Test
public void testCampaignId()
{
assertTrue( parsedCampaignIds_.size( ) == 1 );
assertEquals( campaignId, parsedCampaignIds_.get( 0 ) );
}
@Test
public void testScenarioIds()
{
assertEquals( scenarioIds_.size( ), parsedScenarioIds_.size( ) );
parsedScenarioIds_.removeAll( scenarioIds_ );
// no scenario should have remained in the list
assertEquals( 0, parsedScenarioIds_.size( ) );
}
@Parameters
public static Collection< Object[] > data()
{
return Arrays.asList( new Object[][] {
{ new String[]{ "An_Orcish_Incursion", "An_Orcish_Incursion", "01_Defend_the_Forest", "02_Assassins", "03_Wasteland", "04_Valley_of_Trolls", "05_Linaera_the_Quick", "06_A_Detour_through_the_Swamp", "07_Showdown" } },
{ new String[]{ "Dead_Water", "Dead_Water", "01_Invasion", "02_Flight", "03_Wolf_Coast", "04_Slavers", "05_Tirigaz", "06_Uncharted_Islands", "07_Bilheld", "08_Talking_to_Tyegea", "09_The_Mage", "10_The_Flaming_Sword", "11_Getting_Help", "12_Revenge", "13_Epilogue" } }
} );
}
}

View file

@ -8,6 +8,9 @@
*******************************************************************************/
package org.wesnoth.wml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
@ -15,6 +18,8 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parser.IParser;
import org.wesnoth.Logger;
import org.wesnoth.projects.ProjectCache;
import org.wesnoth.utils.ResourceUtils;
@ -29,7 +34,7 @@ import com.google.common.base.Preconditions;
public class SimpleWMLParser
{
protected WMLConfig config_;
protected IFile file_;
protected WMLRoot root_;
protected ProjectCache projectCache_;
protected int dependencyIndex_;
protected List<WMLTag> tags_;
@ -55,7 +60,7 @@ public class SimpleWMLParser
public SimpleWMLParser( IFile file, WMLConfig config, ProjectCache projCache )
{
config_ = Preconditions.checkNotNull( config );
file_ = file;
root_ = ResourceUtils.getWMLRoot( file );
projectCache_ = projCache;
tags_ = new ArrayList<WMLTag>();
@ -63,13 +68,25 @@ public class SimpleWMLParser
dependencyIndex_ = ResourceUtils.getDependencyIndex( file );
}
/**
* Creates a new parser that can be called on files outside the workspace
*/
public SimpleWMLParser( File file, IParser parser ) throws FileNotFoundException
{
projectCache_ = null;
IParseResult result = parser.parse( new FileReader( file ) );
root_ = ( WMLRoot ) result.getRootASTElement( );
config_ = new WMLConfig( file.getAbsolutePath( ) );
}
/**
* Parses the config. The resulted config will be available in {@link #getParsedConfig()}
*/
public void parse( )
{
WMLRoot root = ResourceUtils.getWMLRoot( file_ );
TreeIterator<EObject> itor = root.eAllContents( );
TreeIterator<EObject> itor = root_.eAllContents( );
WMLTag currentTag = null;
String currentTagName = "";