Added tests to sanity check new blockly scripting feature

This commit is contained in:
Brendan Robert 2014-02-27 01:47:34 -06:00
parent ea92c3e795
commit 6bc1526650
3 changed files with 271 additions and 0 deletions

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gameData xmlns="outlaw">
<map>
<scripts>
<enter>
<name>name</name>
<description>description</description>
<block type="variables_set" inline="true" x="245" y="27">
<field name="VAR">var1</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
<next>
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare" inline="true">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">var1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_whileUntil" inline="false">
<field name="MODE">WHILE</field>
<statement name="DO">
<block type="text_append" inline="false">
<field name="VAR">var1</field>
<value name="TEXT">
<block type="text">
<field name="TEXT">blah</field>
</block>
</value>
</block>
</statement>
</block>
</statement>
</block>
</next>
</block>
</enter>
</scripts>
</map>
</gameData>

View File

@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gameData xmlns="outlaw">
<map>
<scripts>
<enter>
<name>name</name>
<description>description</description>
<block type="procedures_defreturn" inline="false" x="213" y="27">
<mutation>
<arg name="x"></arg>
<arg name="y"></arg>
</mutation>
<field name="NAME">Procedure</field>
<statement name="STACK">
<block type="variables_set" inline="true">
<field name="VAR">var1</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
<next>
<block type="controls_if" inline="false">
<mutation elseif="1" else="1"></mutation>
<value name="IF0">
<block type="logic_compare" inline="true">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">var1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_whileUntil" inline="false">
<field name="MODE">WHILE</field>
<statement name="DO">
<block type="text_append" inline="false">
<field name="VAR">var1</field>
<value name="TEXT">
<block type="text">
<field name="TEXT">blah</field>
</block>
</value>
</block>
</statement>
</block>
</statement>
<value name="IF1">
<block type="logic_operation" inline="true">
<field name="OP">OR</field>
<value name="A">
<block type="logic_boolean">
<field name="BOOL">TRUE</field>
</block>
</value>
<value name="B">
<block type="logic_negate" inline="false">
<value name="BOOL">
<block type="logic_boolean">
<field name="BOOL">FALSE</field>
</block>
</value>
</block>
</value>
</block>
</value>
<statement name="DO1">
<block type="math_change" inline="true">
<field name="VAR">x</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<statement name="ELSE">
<block type="variables_set" inline="true">
<field name="VAR">x</field>
<value name="VALUE">
<block type="procedures_callreturn" inline="false">
<mutation name="Procedure">
<arg name="x"></arg>
<arg name="y"></arg>
</mutation>
<value name="ARG0">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="ARG1">
<block type="variables_get">
<field name="VAR">y</field>
</block>
</value>
</block>
</value>
</block>
</statement>
</block>
</next>
</block>
</statement>
<value name="RETURN">
<block type="logic_null"></block>
</value>
</block>
</enter>
</scripts>
</map>
</gameData>

View File

@ -0,0 +1,100 @@
package org.badvision.outlaweditor.test;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import org.badvision.outlaweditor.data.xml.Block;
import org.badvision.outlaweditor.data.xml.GameData;
import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.Map.Scripts;
import org.badvision.outlaweditor.data.xml.Script;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Various sanity checks of the Mythos script editing feature
*
* @author blurry
*/
public class TestMythosEditor {
static public final String[] testData = {"testData/blocklytest1.xml", "testData/blocklytest2.xml"};
static public final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
@Test
public void deserializeTest() throws Exception {
for (String path : testData) {
System.out.println("testing " + path);
Block theBlock = getBlock(path);
assertNotNull(theBlock);
}
}
@Test
public void roundtripTest() throws Exception {
JAXBContext context = JAXBContext.newInstance("org.badvision.outlaweditor.data.xml");
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, true);
for (String path : testData) {
System.out.println("testing " + path);
Block theBlock = getBlock(path);
StringWriter testWriter = new StringWriter();
GameData d = new GameData();
Map map = new Map();
d.getMap().add(map);
Script script = new Script();
script.setName("name");
script.setDescription("description");
script.setBlock(theBlock);
map.setScripts(new Scripts());
map.getScripts().getEnterOrExitOrStepOn().add(new JAXBElement<Script>(new QName("outlaw", "enter"), Script.class, script));
m.marshal(d, testWriter);
String testOutput = testWriter.getBuffer().toString();
assertNotNull(testOutput);
// assertSimilar(XML_HEADER + testOutput, getFileContents(path));
}
}
public String getFileContents(String path) throws IOException {
BufferedInputStream data = (BufferedInputStream) getClass().getClassLoader().getResource(path).getContent();
byte[] buf = new byte[1024];
StringBuilder contents = new StringBuilder();
while (data.available() > 0) {
int len = data.read(buf);
if (len > 0) {
String append = new String(buf, 0, len);
contents.append(append);
}
}
return contents.toString();
}
public void assertSimilar(String s1, String s2) {
s1 = s1.replaceAll("\\s", "");
s1 = s1.replaceAll("\\n", "");
s2 = s2.replaceAll("\\s", "");
s2 = s2.replaceAll("\\n", "");
assertEquals(s1, s2);
}
public Block getBlock(String resourcePath) throws IOException {
BufferedInputStream data = (BufferedInputStream) getClass().getClassLoader().getResource(resourcePath).getContent();
assertNotNull(data);
GameData gd = JAXB.unmarshal(data, GameData.class);
assertNotNull(gd);
assertNotNull(gd.getMap());
assertEquals(1, gd.getMap().size());
Scripts s = gd.getMap().get(0).getScripts();
assertNotNull(s);
assertNotNull(s.getEnterOrExitOrStepOn());
assertEquals(1, s.getEnterOrExitOrStepOn().size());
Script scr = (Script) s.getEnterOrExitOrStepOn().get(0).getValue();
return scr.getBlock();
}
}