From 1fc7bb030a5a5e95b4698d98470c528d58a978b4 Mon Sep 17 00:00:00 2001 From: Cédric Bosdonnat Date: Thu, 27 Jan 2011 15:13:01 +0100 Subject: Added unit tests for the cpp templates --- .../tools/tests/CppToolsTemplateTest.java | 279 +++++++++++++++++++++ 1 file changed, 279 insertions(+) create mode 100644 src/org/libreoffice/tools/tests/CppToolsTemplateTest.java diff --git a/src/org/libreoffice/tools/tests/CppToolsTemplateTest.java b/src/org/libreoffice/tools/tests/CppToolsTemplateTest.java new file mode 100644 index 0000000..f2305b4 --- /dev/null +++ b/src/org/libreoffice/tools/tests/CppToolsTemplateTest.java @@ -0,0 +1,279 @@ +package org.libreoffice.tools.tests; + + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.libreoffice.tools.TypeChecker; +import org.libreoffice.tools.language.Language; +import org.libreoffice.tools.language.LanguageHelper; +import org.libreoffice.tools.types.TypeManager; + +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +public class CppToolsTemplateTest { + + private static final String[] TEST_EXCEPTIONS = new String[]{ "some.type.Exception1", "some.type.Exception2" }; + private static final String EXPECTED_EXCEPTIONS = "::com::sun::star::uno::RuntimeException, ::some::type::Exception1, ::some::type::Exception2"; + + private static final String[] TEST_EXCEPTIONS_EMPTY = new String[]{ }; + private static final String EXPECTED_EXCEPTIONS_EMPTY = "::com::sun::star::uno::RuntimeException"; + + private static final String LANGS_DIR = "langs"; + private static final String CPP_LANG = "cpp"; + + private Template mTestedTemplate; + + @Before + public void setUp() throws Exception { + File jarFile = new File(CppToolsTemplateTest.class.getProtectionDomain() + .getCodeSource().getLocation().toURI()); + File dir = new File( jarFile.getParentFile(), LANGS_DIR ); + LanguageHelper.setLanguagesDir( dir ); + + Language language = LanguageHelper.getLanguage( CPP_LANG ); + mTestedTemplate = language.getTemplate( "tools.tpl" ); + } + + @After + public void tearDown() throws Exception { + mTestedTemplate = null; + } + + @Test + public void scopedCppNameTest( ) throws IOException, TemplateException { + String result = processTemplate( "${scopedCppName(\"some.test.Type\")}", new HashMap() ); + assertEquals( "Wrong output", "::some::test::Type", result ); + } + + @Test + public void scopedCppNameShortnamesTest( ) throws IOException, TemplateException { + String result = processTemplate( "${scopedCppName(\"some.test.Type\", true)}", new HashMap() ); + assertEquals( "This type shouldn't be shortened", "::some::test::Type", result ); + + result = processTemplate( "${scopedCppName(\"com.sun.star.Type\", true)}", new HashMap() ); + assertEquals( "This type should be shortened", "css::Type", result ); + } + + @Test + public void classnameFqdnTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( "implname", "some.test.Type" ); + String result = processTemplate( "${classname()}", model ); + assertEquals( "Wrong classname", "Type", result ); + } + + @Test + public void classnameSimpleTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( "implname", "Type" ); + String result = processTemplate( "${classname()}", model ); + assertEquals( "Wrong classname", "Type", result ); + } + + @Test + public void generateNamespaceFqdnNoPrefixTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( "implname", "some.test.Type" ); + String result = processTemplate( "${generateNamespace()}", model ); + assertEquals( "Wrong classname", "::some::test::Type", result ); + } + + @Test + public void generateNamespaceFqdnPrefixTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( "implname", "some.test.Type" ); + String result = processTemplate( "${generateNamespace( \"comp_\" )}", model ); + assertEquals( "Wrong classname", "::some::test::comp_Type", result ); + } + + @Test + public void generateNamespaceSimpleNoPrefixTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( "implname", "Type" ); + String result = processTemplate( "${generateNamespace()}", model ); + assertEquals( "Wrong classname", "::Type", result ); + } + + @Test + public void generateNamespaceSimplePrefixTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( "implname", "Type" ); + String result = processTemplate( "${generateNamespace( \"comp_\" )}", model ); + assertEquals( "Wrong classname", "::comp_Type", result ); + } + + @Test + public void printExceptionsTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( "exceptions", Arrays.asList( TEST_EXCEPTIONS ) ); + String result = processTemplate( "${printExceptions( exceptions )}", model ); + assertEquals( EXPECTED_EXCEPTIONS, result ); + } + + @Test + public void printExceptionsEmptyTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( "exceptions", Arrays.asList( TEST_EXCEPTIONS_EMPTY ) ); + String result = processTemplate( "${printExceptions( exceptions )}", model ); + assertEquals( EXPECTED_EXCEPTIONS_EMPTY, result ); + } + + @Test + public void propertySetMixinFlagsTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( TypeChecker.PROPERTY_INTERFACES, Arrays.asList( new String[]{ "com.sun.star.beans.XPropertySet", "com.sun.star.beans.XPropertyAccess" } ) ); + String result = processTemplate( "${propertySetMixinFlags( )}", model ); + assertEquals( "IMPLEMENTS_PROPERTY_SET | IMPLEMENTS_PROPERTY_ACCESS", result ); + } + + @Test + public void propertySetMixinFlagsOneFlagTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( TypeChecker.PROPERTY_INTERFACES, Arrays.asList( new String[]{ "com.sun.star.beans.XFastPropertySet" } ) ); + String result = processTemplate( "${propertySetMixinFlags( )}", model ); + assertEquals( "IMPLEMENTS_FAST_PROPERTY_SET", result ); + } + + @Test + public void propertySetMixinFlagsEmptyTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + model.put( TypeChecker.PROPERTY_INTERFACES, new ArrayList() ); + String result = processTemplate( "${propertySetMixinFlags( )}", model ); + assertEquals( new String(), result ); + } + + @Test + public void printFieldSignatureGetterTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + boolean readonly = false; + model.put( "field", getTestField( readonly ) ); + String result = processTemplate( "${printFieldSignature( field, false )}", model ); + assertEquals( getFieldGetterSignature(), result ); + } + + @Test + public void printFieldSignatureSetterReadwriteTest( ) throws IOException, TemplateException { + HashMap model = new HashMap(); + boolean readonly = false; + model.put( "field", getTestField( readonly ) ); + String result = processTemplate( "${printFieldSignature( field, true )}", model ); + assertEquals( getFieldSetterSignature( readonly ), result ); + } + + @Test + public void cppTypeSimpleTypesTest() throws IOException, TemplateException { + HashMap mappings = new HashMap(); + mappings.put( "void", "void" ); + mappings.put( "boolean", "::sal_Bool" ); + mappings.put( "byte", "::sal_Int8" ); + mappings.put( "short", "::sal_Int16" ); + mappings.put( "unsigned short", "::sal_uInt16" ); + mappings.put( "long", "::sal_Int32" ); + mappings.put( "unsigned long", "::sal_uInt32" ); + mappings.put( "hyper", "::sal_Int64" ); + mappings.put( "unsigned hyper", "::sal_uInt64" ); + mappings.put( "float", "float" ); + mappings.put( "double", "double" ); + mappings.put( "char", "::sal_Unicode" ); + mappings.put( "string", "::rtl::OUString" ); + mappings.put( "type", "::com::sun::star::uno::Type" ); + mappings.put( "any", "::com::sun::star::uno::Any" ); + + Iterator it = mappings.keySet().iterator(); + while ( it.hasNext() ) { + String unoType = it.next(); + String result = processTemplate( "${cppType( \"" + unoType + "\" )}", new HashMap() ); + assertEquals( "Wrong c++ type for UNO: " + unoType, mappings.get( unoType ), result ); + } + } + + @Test + public void cppTypeInterfaceTest() throws IOException, TemplateException { + + // Populate the template model with a test interface type + HashMap model = new HashMap(); + + HashMap type = new HashMap(); + type.put( TypeManager.UNO_TYPE_NAME, "some.type.XType" ); + type.put( TypeManager.UNO_NESTED_LEVELS, 0 ); + type.put( TypeManager.UNO_TYPE_CLASS, "interface" ); + + model.put( "testtype", type ); + + // Run the test + String result = processTemplate( "${cppType( testtype )}", model ); + + // Compare + assertEquals( "Wrong c++ type for interfaces", "::com::sun::star::uno::Reference< ::some::type::XType >", result ); + } + + @Test + public void cppTypeSequenceTest() throws IOException, TemplateException { + + // Populate the template model with a test interface type + HashMap model = new HashMap(); + + HashMap type = new HashMap(); + type.put( TypeManager.UNO_TYPE_NAME, "some.type.XType" ); + type.put( TypeManager.UNO_NESTED_LEVELS, 2 ); + type.put( TypeManager.UNO_TYPE_CLASS, "interface" ); + + model.put( "testtype", type ); + + // Run the test + String result = processTemplate( "${cppType( testtype )}", model ); + + // Compare + assertEquals( "Wrong c++ type for interfaces", "::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::some::type::XType > > >", result ); + } + + private Object getTestField( boolean pReadonly) { + + HashMap field = new HashMap(); + field.put( TypeManager.FIELD_NAME, "Foo"); + field.put( TypeManager.FIELD_TYPE, "string" ); + field.put( TypeManager.FIELD_GET_EXCEPTIONS, TEST_EXCEPTIONS ); + field.put( TypeManager.FIELD_SET_EXCEPTIONS, TEST_EXCEPTIONS ); + field.put( TypeManager.FIELD_READONLY, Boolean.valueOf( pReadonly ) ); + + return field; + } + + private Object getFieldGetterSignature() { + return "::rtl::OUString SAL_CALL getFoo() throw (" + EXPECTED_EXCEPTIONS + ")"; + } + + private Object getFieldSetterSignature( boolean pReadonly ) { + String result = new String(); + if ( !pReadonly ) { + result = "void SAL_CALL setFoo(::rtl::OUString the_value) throw (" + EXPECTED_EXCEPTIONS + ")"; + } + return result; + } + + private String processTemplate( String pTemplateContent, HashMap pModel ) throws IOException, TemplateException { + Configuration config = mTestedTemplate.getConfiguration(); + config.addAutoInclude( mTestedTemplate.getName() ); + Template tester = new Template( "tester", new StringReader( pTemplateContent ), config ); + + StringWriter out = new StringWriter(); + tester.process( pModel, out); + + return out.toString(); + } +} -- cgit v1.2.3