Skip to content

Commit

Permalink
Unit test to validate IntrospectionParser behaviour.
Browse files Browse the repository at this point in the history
Add tests to validate the IntrospectionParser behaviour.
Change IntrospectionParser input to Reader instead of File. In this
way it is possible to feed the parser with a String instead of a
File.
Add needed JARs to the classpath to compile and run tests.
  • Loading branch information
gmazoyer committed Nov 15, 2014
1 parent 7925b54 commit 07f9e3b
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build/faster
Expand Up @@ -552,7 +552,7 @@ def compileTestClasses():
if not changed:
return

compileJavaCode("tmp/tests/", "tmp/generator:tmp/bindings/:"+config['JUNIT_JARS'], "tests/generator/:tests/bindings/:tests/prototype/:tests/screenshots/:doc/examples/", changed)
compileJavaCode("tmp/tests/", "tmp/generator:tmp/bindings/:"+config['JUNIT_JARS']+":"+config['XOM_JARS']+":"+config['JAXEN_JARS'], "tests/generator/:tests/bindings/:tests/prototype/:tests/screenshots/:doc/examples/", changed)


def compileMessageCatalogue(source, target):
Expand Down Expand Up @@ -693,7 +693,7 @@ def generateDocumentation():
#

def runTests():
runJavaClass("UnitTests", "tmp/gtk-4.1.jar:tmp/generator/:tmp/tests/:"+config['JUNIT_JARS'])
runJavaClass("UnitTests", "tmp/gtk-4.1.jar:tmp/generator/:tmp/tests/:"+config['JUNIT_JARS']+":"+config['XOM_JARS']+":"+config['JAXEN_JARS'])

def runDemo():
runJavaClass("button.ExamplePressMe", "tmp/gtk-4.1.jar:tmp/tests/")
Expand Down
14 changes: 9 additions & 5 deletions src/generator/BindingsGenerator.java
Expand Up @@ -134,10 +134,14 @@ private static void runGeneratorOutputIntrospectionToFiles(final String[] files,
*/

for (String file : files) {
parser = new IntrospectionParser(new File(file));
final FileReader reader;

try {
reader = new FileReader(file);
parser = new IntrospectionParser(reader);

introspected.putAll(parser.parseData());
reader.close();
} catch (ParsingException e) {
System.out.println("Malformed XML data in " + file + ":");
System.out.println(e.getMessage());
Expand Down Expand Up @@ -215,14 +219,14 @@ public boolean accept(File dir, String name) {
}

in.close();
} catch (IOException ioe) {
} catch (IOException e) {
System.out.println("I/O problem when trying to parse " + overriders[i]);
System.out.println(ioe.getMessage());
System.out.println(e.getMessage());
System.out.println("[continuing next file]\n");
continue;
} catch (ImproperDefsFileException idfe) {
} catch (ImproperDefsFileException e) {
System.out.println("Couldn't get sufficient information from " + overriders[i] + ":");
System.out.println(idfe.getMessage());
System.out.println(e.getMessage());
System.out.println("[continuing next file]\n");
continue;
} finally {
Expand Down
Expand Up @@ -26,10 +26,10 @@
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -75,7 +75,7 @@ public class IntrospectionParser

private static final TypesList typesList;

private File introspectionFile;
private Reader introspectionData;

static {
CORE_NAMESPACE = "http://www.gtk.org/introspection/core/1.0";
Expand Down Expand Up @@ -110,14 +110,14 @@ public class IntrospectionParser
}

/**
* Initialize the parser for a given file containing Introspection data.
* Initialize the parser for a given Reader reading Introspection data.
*
* @param introspectionFile
* a file object that is a reference to a .gir file to be
* parsed.
* @param introspectionData
* a Reader object that is a reference to a .gir file or GIR
* data to be parsed.
*/
public IntrospectionParser(final File introspectionFile) {
this.introspectionFile = introspectionFile;
public IntrospectionParser(final Reader introspectionData) {
this.introspectionData = introspectionData;
}

/**
Expand Down Expand Up @@ -1068,7 +1068,7 @@ public Map<String, DefsFile> parseData() throws ParsingException, IOException {
* Start the parsing of the XML data.
*/

document = builder.build(this.introspectionFile);
document = builder.build(this.introspectionData);

/*
* Get the first elements that are available (includes and
Expand Down
4 changes: 4 additions & 0 deletions src/generator/com/operationaldynamics/driver/DefsFile.java
Expand Up @@ -101,6 +101,10 @@ public final Thing getType() {
return forObject;
}

public final Block[] getBlocks() {
return blocks;
}

/**
* Add a new FunctionBlock to the array of Blocks defined this DefsFile.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/bindings/UnitTests.java
Expand Up @@ -80,6 +80,7 @@
import com.operationaldynamics.codegen.ValidateUtilityMethods;
import com.operationaldynamics.defsparser.ValidateBlockUsage;
import com.operationaldynamics.defsparser.ValidateDefsParsing;
import com.operationaldynamics.defsparser.ValidateIntrospectionParsing;
import com.operationaldynamics.junit.VerboseTestRunner;

/**
Expand Down Expand Up @@ -142,6 +143,7 @@ private static Test suite(String[] args) {

suite.addTestSuite(ValidateUtilityMethods.class);
suite.addTestSuite(ValidateBlockUsage.class);
suite.addTestSuite(ValidateIntrospectionParsing.class);
suite.addTestSuite(ValidateDefsParsing.class);
suite.addTestSuite(ValidateThingUsage.class);
suite.addTestSuite(ValidateEnvironment.class);
Expand Down
@@ -0,0 +1,172 @@
/*
* java-gnome, a UI library for writing GTK and GNOME programs from Java!
*
* Copyright © 2007-2010 Operational Dynamics Consulting, Pty Ltd
*
* The code in this file, and the program it is a part of, is made available
* to you by its authors as open source software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License version
* 2 ("GPL") as published by the Free Software Foundation.
*
* 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 GPL for more details.
*
* You should have received a copy of the GPL along with this program. If not,
* see http://www.gnu.org/licenses/. The authors of this program may be
* contacted through http://java-gnome.sourceforge.net/.
*/
package com.operationaldynamics.defsparser;

import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.util.Map;

import nu.xom.ParsingException;
import junit.framework.TestCase;

import com.operationaldynamics.codegen.Thing;
import com.operationaldynamics.driver.DefsFile;

/**
* Evaluate the internal methods in the DefsParser class.
*
* @author Andrew Cowie
*/
public class ValidateIntrospectionParsing extends TestCase
{
private static final String inputGirData;

static {
final StringBuffer buffer;
final String[] raw;

raw = new String[] {
"<repository version=\"1.2\"",
" xmlns=\"http://www.gtk.org/introspection/core/1.0\"",
" xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"",
" xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\">",
" <namespace name=\"Gtk\"",
" version=\"3.0\"",
" shared-library=\"libgtk-3.so.0,libgdk-3.so.0\"",
" c:identifier-prefixes=\"Gtk\"",
" c:symbol-prefixes=\"gtk\">",
" <class name=\"Button\"",
" c:symbol-prefix=\"button\"",
" c:type=\"GtkButton\"",
" parent=\"Bin\"",
" glib:type-name=\"GtkButton\"",
" glib:get-type=\"gtk_button_get_type\"",
" glib:type-struct=\"ButtonClass\">",
" <constructor name=\"new\" c:identifier=\"gtk_button_new\">",
" <return-value transfer-ownership=\"none\">",
" <type name=\"Widget\" c:type=\"GtkWidget*\"/>",
" </return-value>",
" </constructor>",
" <method name=\"set_label\" c:identifier=\"gtk_button_set_label\">",
" <return-value transfer-ownership=\"none\">",
" <type name=\"none\" c:type=\"void\"/>",
" </return-value>",
" <parameters>",
" <instance-parameter name=\"button\" transfer-ownership=\"none\">",
" <type name=\"Button\" c:type=\"GtkButton*\"/>",
" </instance-parameter>",
" <parameter name=\"label\" transfer-ownership=\"none\">",
" <type name=\"utf8\" c:type=\"const gchar*\"/>",
" </parameter>",
" </parameters>",
" </method>",
" <glib:signal name=\"clicked\" when=\"first\" action=\"1\">",
" <return-value transfer-ownership=\"none\">",
" <type name=\"none\" c:type=\"void\"/>",
" </return-value>",
" </glib:signal>",
" </class>",
" </namespace>",
"</repository>"
};

buffer = new StringBuffer();
for (String line : raw) {
buffer.append(line);
buffer.append("\n");
}

inputGirData = buffer.toString();
}

protected IntrospectionParser parser;

StringReader in;

public void setUp() throws IOException {
in = new StringReader(inputGirData);

parser = new IntrospectionParser(in);
}

public void tearDown() throws IOException {
in.close();
}

public final void testObjectBlockCreated() throws ParsingException, IOException {
Map<String, DefsFile> results;
Block[] blocks;
ObjectBlock o;

results = parser.parseData();

assertEquals(1, results.size());
assertNotNull(results.get("GtkButton"));

blocks = results.get("GtkButton").getBlocks();

assertTrue(blocks[0] instanceof ObjectBlock);

o = (ObjectBlock) blocks[0];

assertEquals("Gtk", o.inModule);
assertEquals("GtkBin", o.parent);
assertEquals("GtkButton", o.cName);
assertEquals("Button", o.blockName);
}

public final void testCantCreateThingFromNonTypeBlock() throws ParsingException, IOException {
Map<String, DefsFile> results;
Block[] blocks;
Thing t;

results = parser.parseData();
blocks = results.get("GtkButton").getBlocks();
t = null;

assertFalse(blocks[1] instanceof TypeBlock);
try {
t = blocks[1].createThing();
fail("Should have thrown UnsupportedOperationException");
} catch (UnsupportedOperationException uoe) {
// good
}
assertNull(t);
}

public final void testMethodReferenceToSelfInsertion() throws ParsingException, IOException {
Map<String, DefsFile> results;
Block[] blocks;
MethodBlock block;

results = parser.parseData();
blocks = results.get("GtkButton").getBlocks();

assertTrue(blocks[2] instanceof MethodBlock);

block = (MethodBlock) blocks[2];

assertTrue(block.parameters.length == 2);
assertEquals("GtkButton*", block.parameters[0][0]);
assertEquals("self", block.parameters[0][1]);
assertEquals("const-gchar*", block.parameters[1][0]);
assertEquals("label", block.parameters[1][1]);
}
}

0 comments on commit 07f9e3b

Please sign in to comment.