Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
aaulia committed Feb 21, 2015
1 parent d7454c9 commit 1d49e09
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 3 deletions.
6 changes: 3 additions & 3 deletions org/msgpack/Decoder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ class Decoder {
}

if (pairs.length == 0)
return new StringMap<Dynamic>();
return new StringMap();

switch(Type.typeof(pairs[0].k))
{
case TInt:
var out = new IntMap<Dynamic>();
var out = new IntMap();
for (p in pairs)
out.set(p.k, p.v);

Expand All @@ -135,7 +135,7 @@ class Decoder {
switch(Type.getClassName(c))
{
case "String":
var out = new StringMap<Dynamic>();
var out = new StringMap();
for (p in pairs)
out.set(p.k, p.v);

Expand Down
6 changes: 6 additions & 0 deletions unit-test/.munit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version=2.1.0
src=src
bin=build
report=report
hxml=unit-test.hxml
classPaths=../
50 changes: 50 additions & 0 deletions unit-test/src/ArrayTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ;

import massive.munit.util.Timer;
import massive.munit.Assert;
import massive.munit.async.AsyncFactory;
import org.msgpack.MsgPack;


class ArrayTest
{


public function new()
{

}

@BeforeClass
public function beforeClass():Void
{
}

@AfterClass
public function afterClass():Void
{
}

@Before
public function setup():Void
{
}

@After
public function tearDown():Void
{
}

@Test
public function testExample():Void
{
var a = [ 3, 2, 1, 7, 8, 9 ];
var e = MsgPack.encode(a);
var d = MsgPack.decode(e);

Assert.isType(d, Array);
Assert.isTrue(Lambda.fold(d, function(v, b) {
return b && a[Lambda.indexOf(d, v)] == v;
}, true));
}
}
71 changes: 71 additions & 0 deletions unit-test/src/BasicTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ;

import massive.munit.util.Timer;
import massive.munit.Assert;
import massive.munit.async.AsyncFactory;
import org.msgpack.MsgPack;


class BasicTest
{


public function new()
{

}

@BeforeClass
public function beforeClass():Void
{
}

@AfterClass
public function afterClass():Void
{
}

@Before
public function setup():Void
{
}

@After
public function tearDown():Void
{
}

@Test
public function testExample():Void
{
Assert.isTrue(doTest(null, "TNull" ));
Assert.isTrue(doTest(true, "TBool" ));
Assert.isTrue(doTest(1000, "TInt" ));
Assert.isTrue(doTest(1.01, "TFloat"));
Assert.isTrue(doTest("ab", "String"));
}

function doTest<T>(a:T, n:String)
{
var e = MsgPack.encode(a);
var d = MsgPack.decode(e);

switch (Type.typeof(a))
{
case TNull : return n == "TNull";
case TBool : return n == "TBool";
case TInt : return n == "TInt";
case TFloat : return n == "TFloat";
case TClass(c):
switch (Type.getClassName(c))
{
case "String":
return n == "String";
}

default:
}

return false;
}
}
74 changes: 74 additions & 0 deletions unit-test/src/MapTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ;

import haxe.ds.IntMap;
import haxe.ds.StringMap;
import massive.munit.util.Timer;
import massive.munit.Assert;
import massive.munit.async.AsyncFactory;
import org.msgpack.Decoder.DecodeOption.AsMap;
import org.msgpack.MsgPack;


class MapTest
{


public function new()
{

}

@BeforeClass
public function beforeClass():Void
{
}

@AfterClass
public function afterClass():Void
{
}

@Before
public function setup():Void
{
}

@After
public function tearDown():Void
{
}

@Test
public function testExample():Void
{
var im = new IntMap<String>();
im.set(1, "one");
im.set(3, "Three");
im.set(9, "Nine");

var e = MsgPack.encode(im);
var d = MsgPack.decode(e, AsMap);

Assert.isType(d, IntMap);

var ni = cast(d, IntMap<Dynamic>);
for (k in im.keys())
Assert.isTrue(ni.exists(k) && ni.get(k) == im.get(k));


var sm = new StringMap<Int>();
sm.set("one", 1);
sm.set("Three", 3);
sm.set("Nine", 9);

e = MsgPack.encode(sm);
d = MsgPack.decode(e, AsMap);

Assert.isType(d, StringMap);

var ns = cast(d, StringMap<Dynamic>);
for (k in sm.keys())
Assert.isTrue(ns.exists(k) && ns.get(k) == sm.get(k));

}
}
50 changes: 50 additions & 0 deletions unit-test/src/ObjectTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ;

import massive.munit.util.Timer;
import massive.munit.Assert;
import massive.munit.async.AsyncFactory;
import org.msgpack.Decoder.DecodeOption.AsObject;
import org.msgpack.MsgPack;


class ObjectTest
{


public function new()
{

}

@BeforeClass
public function beforeClass():Void
{
}

@AfterClass
public function afterClass():Void
{
}

@Before
public function setup():Void
{
}

@After
public function tearDown():Void
{
}

@Test
public function testExample():Void
{
var e = MsgPack.encode({ a: 10, b: "abc"});
var d = MsgPack.decode(e, AsObject);

Assert.isTrue(Reflect.hasField(d, "a")
&& Reflect.hasField(d, "b")
&& Reflect.field(d, "a") == 10
&& Reflect.field(d, "b") == "abc");
}
}
62 changes: 62 additions & 0 deletions unit-test/src/TestMain.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import massive.munit.client.PrintClient;
import massive.munit.client.RichPrintClient;
import massive.munit.client.HTTPClient;
import massive.munit.client.JUnitReportClient;
import massive.munit.client.SummaryReportClient;
import massive.munit.TestRunner;

#if js
import js.Lib;
#end

/**
* Auto generated Test Application.
* Refer to munit command line tool for more information (haxelib run munit)
*/
class TestMain
{
static function main(){ new TestMain(); }

public function new()
{
var suites = new Array<Class<massive.munit.TestSuite>>();
suites.push(TestSuite);

#if MCOVER
var client = new mcover.coverage.munit.client.MCoverPrintClient();
var httpClient = new HTTPClient(new mcover.coverage.munit.client.MCoverSummaryReportClient());
#else
var client = new RichPrintClient();
var httpClient = new HTTPClient(new SummaryReportClient());
#end

var runner:TestRunner = new TestRunner(client);
runner.addResultClient(httpClient);
//runner.addResultClient(new HTTPClient(new JUnitReportClient()));

runner.completionHandler = completionHandler;
runner.run(suites);
}

/*
updates the background color and closes the current browser
for flash and html targets (useful for continous integration servers)
*/
function completionHandler(successful:Bool):Void
{
try
{
#if flash
flash.external.ExternalInterface.call("testResult", successful);
#elseif js
js.Lib.eval("testResult(" + successful + ");");
#elseif sys
Sys.exit(0);
#end
}
// if run from outside browser can get error which we can ignore
catch (e:Dynamic)
{
}
}
}
25 changes: 25 additions & 0 deletions unit-test/src/TestSuite.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import massive.munit.TestSuite;

import MapTest;
import BasicTest;
import ArrayTest;
import ObjectTest;

/**
* Auto generated Test Suite for MassiveUnit.
* Refer to munit command line tool for more information (haxelib run munit)
*/

class TestSuite extends massive.munit.TestSuite
{

public function new()
{
super();

add(MapTest);
add(BasicTest);
add(ArrayTest);
add(ObjectTest);
}
}
Loading

0 comments on commit 1d49e09

Please sign in to comment.