Skip to content

Commit

Permalink
passing initial unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JWambaugh committed Oct 18, 2012
1 parent 95dae09 commit 27185d2
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 0 deletions.
Binary file added TJSONTest.n
Binary file not shown.
181 changes: 181 additions & 0 deletions lib/tjson/TJSON.hx
@@ -0,0 +1,181 @@

package tjson;

class TJSON {

static var pos:Int;
static var json:String;
public static function parse(json:String):Dynamic{
TJSON.json = json;
pos = 0;
var symbol:String;
return doParse();
}

private static function doParse():Dynamic{
//determine if objector array
var s = getNextSymbol();
if(s=='{'){
return doObject();
}

if(s=='['){
return doArray();
}
return null;
}

private static function doObject():Dynamic{
var o = {};
var val:Dynamic='';
var key:String;
while((key=getNextSymbol()) != ""){
if(key==",")continue;
if(key == "}"){
return o;
}
var seperator = getNextSymbol();
if(seperator != ":"){
throw("Expected ':' but got '"+seperator+"' instead.");
}

var v = getNextSymbol();
if(v=="{"){
val = doObject();
}else if(v=="["){
val = doArray();
}else{
val = v;
}

Reflect.setField(o,key,val);
}
throw "Unexpected end of file. Expected '}'";

}

private static function doArray():Dynamic{
var a=[];
var val:String;
while((val=getNextSymbol()) != ""){
if(val == ','){
continue;
}
if(val == ']'){
return a;
}
if(val=="{"){
val = doObject();
}else if(val=="["){
val = doArray();
}
a.push(val);
}
throw "Unexpected end of file. Expected ']'";
}

private static function getNextSymbol(){
var c:String = '';
var inQuote:Bool = false;
var quoteType:String="";
var symbol:String = '';
var inEscape:Bool = false;
var inSymbol:Bool = false;
var inLineComment = false;
var inBlockComment = false;

while(pos < json.length){
c = json.charAt(pos++);

if(inLineComment){
if(c=="\n" || c=="\r"){
inLineComment = false;
pos++;
}
continue;
}

if(inBlockComment){
if(c=="*" && json.charAt(pos) == "/"){
inBlockComment = false;
pos++;
}
continue;
}

if(inQuote){
if(inEscape){
//TODO: do this
}else{
if(c == "\\"){
inEscape = true;
continue;
}
if(c == quoteType){
return symbol;
}
symbol+=c;
continue;
}
}


//handle comments
else if(c == "/"){
var c2 = json.charAt(pos);
//handle single line comments.
//These can even interrupt a symbol.
if(c2 == "/"){
inLineComment=true;
pos++;
continue;
}
//handle block comments.
//These can even interrupt a symbol.
else if(c2 == "*"){
inBlockComment=true;
pos++;
continue;
}
}



if (inSymbol){
if(c==' ' || c=="\n" || c==',' || c==":" || c=="}" || c=="]"){ //end of symbol, return it
pos--;
return symbol;
}else{
symbol+=c;
continue;
}

}
else {
if(c==' ' || c=="\t" || c=="\n" || c=="\r"){
continue;
}

if(c=="{" || c=="}" || c=="[" || c=="]" || c=="," || c == ":"){
return c;
}



if(c=="'" || c=='"'){
inQuote = true;
quoteType = c;
continue;
}else{
inSymbol=true;
symbol = c;
continue;
}


}
}
return symbol;
}

}
2 changes: 2 additions & 0 deletions runTests.bat
@@ -0,0 +1,2 @@
haxe test.hxml
neko TJSONTest.n
4 changes: 4 additions & 0 deletions test.hxml
@@ -0,0 +1,4 @@
-neko TJSONTest.n
-main TestAll
-cp lib
-cp tests
16 changes: 16 additions & 0 deletions tests/TestAll.hx
@@ -0,0 +1,16 @@

package ;
import TestParser;
class TestAll {
function new(){

}
static function main(){
var r = new haxe.unit.TestRunner();
r.add(new TestParser());
// your can add others TestCase here

// finally, run the tests
r.run();
}
}
42 changes: 42 additions & 0 deletions tests/TestParser.hx
@@ -0,0 +1,42 @@
package ;

import tjson.TJSON;

class TestParser extends haxe.unit.TestCase{

public function new(){
super();
}



public function testSimple(){
var res =TJSON.parse("{key:'value'}");
assertEquals("{ key => value }",Std.string(res));
}

public function testSimple2(){
var res =TJSON.parse("{key1:'value', key2:'value 2'}");
assertEquals("{ key1 => value, key2 => value 2 }",Std.string(res));
}
public function testComplex1(){
var res =TJSON.parse("{key1:'value', key2:'value 2'
myOb:{subkey1:'subkey1 value!!! Yah'}, 'anArray':['happy', 323, {key:val}]}");
assertEquals('{ key1 => value, key2 => value 2, myOb => { subkey1 => subkey1 value!!! Yah }, anArray => [happy,323,{ key => val }] }',Std.string(res));
}

public function testComplexWithComments(){
var res =TJSON.parse("{key1:'value',/* block comment*/ key2:'value 2'
//this is a line comment.
myOb:{subkey1:'subkey1 value!!! Yah'}, 'anArray':['happy', 323, {key:val}]}");
assertEquals('{ key1 => value, key2 => value 2, myOb => { subkey1 => subkey1 value!!! Yah }, anArray => [happy,323,{ key => val }] }',Std.string(res));
}


public function testArray1(){
var res =TJSON.parse("[1,2,3,4,5,6,'A string']");
assertEquals("[1,2,3,4,5,6,A string]",Std.string(res));
}

}
8 changes: 8 additions & 0 deletions tests/com/firmamentengine/tjson/TestParser.hx
@@ -0,0 +1,8 @@

package ;

class TestParser {



}

0 comments on commit 27185d2

Please sign in to comment.