Commits

Lars J. Nilsson  committed d9fa979

Issue #1: fixing getnormalized object for lists of enums within structs

  • Participants
  • Parent commits d046268

Comments (0)

Files changed (5)

File maven/protocol-generator-plugin/src/main/scripts/generate_source.rb

 end
 
 class ProtoList < ProtoScalar
+
   def initialize(element)
     super(element)
   end

File maven/protocol-generator-plugin/src/main/scripts/javascript_generator.rb

         wi("for ( i = 0; i < this.#{camelcase(member.name)}.length; i ++ ) {",1)
         if  member.obj_type == "param" 
           wi("normalizedObject.details[\"#{camelcase(member.name)}\"].push(FIREBASE.Styx.getParam(this.#{camelcase(member.name)}[i]));",2)
+        elsif enum?(member.obj_type)
+         wi("normalizedObject.details[\"#{camelcase(member.name)}\"].push(#{$javascript_namespace}.#{camelcasetype(member.obj_type)}Enum.toString(this.#{camelcase(member.name)}[i]));",2)
         else
           wi("normalizedObject.details[\"#{camelcase(member.name)}\"].push(this.#{camelcase(member.name)}[i].getNormalizedObject());",2)
         end

File maven/protocol-generator-plugin/src/test/java/com/cubeia/ProtocolGeneratorMojoTest.java

 package com.cubeia;
 
 import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 
+import javax.script.Invocable;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
 import org.apache.commons.io.FileUtils;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
     private void deleteGeneratedSource() throws IOException {
         FileUtils.deleteDirectory(new File(TEST_CODE_DIR));
     }
-    
-    /**
-     * @throws Exception
-     */
+
     public void testMojoGoal() throws Exception {
         File testPom = new File(getBasedir(), "src/test/resources/mojo-test-pom.xml");
         ProtocolGeneratorMojo mojo = (ProtocolGeneratorMojo) lookupMojo("generate", testPom);
         assertEquals(lastModified, f.lastModified());
     }
     
+    /**
+     * @throws Exception
+     */
+    public void testGenerateJs() throws Exception {
+        File testPom = new File(getBasedir(), "src/test/resources/mojo-test-js-pom.xml");
+        ProtocolGeneratorMojo mojo = (ProtocolGeneratorMojo) lookupMojo("generate", testPom);
+        assertNotNull(mojo);
+        setVariableValueToObject(mojo, "project", new MavenProjectStub());
+        mojo.execute();
+        
+        ScriptEngineManager factory = new ScriptEngineManager();
+
+        // Create a JRuby engine.
+        ScriptEngine engine = factory.getEngineByName("javascript");
+        String s = fileToString(new File(new File(TEST_CODE_DIR), "javascript/header/header.js"));
+        s += fileToString(new File(new File(TEST_CODE_DIR), "javascript/script/EnumTypeEnum.js"));
+        s += fileToString(new File(new File(TEST_CODE_DIR), "javascript/script/ListOfEnums.js"));
+        s += "function testList() { var list = new testpackage.ListOfEnums(); list.l.push(testpackage.EnumTypeEnum.INT); return list.getNormalizedObject(); }";
+        engine.eval(s);
+        Object o = ((Invocable)engine).invokeFunction("testList");
+        assertNotNull(o);
+    }
+    
+    private String fileToString(File file) throws Exception {
+    	FileReader r = new FileReader(file);
+    	char[] buff = new char[256];
+    	int len = 0;
+    	StringBuilder b = new StringBuilder();
+    	while((len = r.read(buff)) != -1) {
+    		b.append(buff, 0, len);
+    	}
+    	r.close();
+    	return b.toString();
+    }
+    
     public void testFailOnBadStructOrder() throws Exception {
         final String firstPacketJavaFile = TEST_CODE_FULL_DIR + File.separator + "FirstPacket.java";
         

File maven/protocol-generator-plugin/src/test/resources/mojo-test-js-pom.xml

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>protocol-generator-plugin</artifactId>
+        <configuration>
+          <protocol_file>src/test/resources/protocol.xml</protocol_file>
+          <language>javascript</language>
+          <package_name>com.cubeia.test.protocol</package_name>
+          <output_base_dir>target/jruby-protocol-plugin/test-generated-sources</output_base_dir>
+          <output_dir>target/deprecated and should have no effect...</output_dir>
+          <generate_visitors>true</generate_visitors>
+          <javascript_package_name>testpackage</javascript_package_name>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  
+  
+</project>

File maven/protocol-generator-plugin/src/test/resources/protocol.xml

         <var name="t" type="type_test_packet"/>
     </struct>
     
+    <struct id="5" name="list_of_enums">
+    	<list name="l" type="enum_type" />
+    </struct>
   
 </protocol>