mirror of
https://github.com/fangyidong/json-simple.git
synced 2025-12-06 15:30:54 +03:00
init
This commit is contained in:
75
test/org/json/simple/Test.java
Normal file
75
test/org/json/simple/Test.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* $Id: Test.java,v 1.1 2006/04/15 14:40:06 platform Exp $
|
||||
* Created on 2006-4-15
|
||||
*/
|
||||
package org.json.simple;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* @author FangYidong<fangyidong@yahoo.com.cn>
|
||||
*/
|
||||
public class Test extends TestCase{
|
||||
|
||||
public void testDecode() throws Exception{
|
||||
System.out.println("=======decode=======");
|
||||
|
||||
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
|
||||
Object obj=JSONValue.parse(s);
|
||||
JSONArray array=(JSONArray)obj;
|
||||
System.out.println("======the 2nd element of array======");
|
||||
System.out.println(array.get(1));
|
||||
System.out.println();
|
||||
assertEquals("{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}",array.get(1).toString());
|
||||
|
||||
JSONObject obj2=(JSONObject)array.get(1);
|
||||
System.out.println("======field \"1\"==========");
|
||||
System.out.println(obj2.get("1"));
|
||||
assertEquals("{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}",obj2.get("1").toString());
|
||||
|
||||
s="{}";
|
||||
obj=JSONValue.parse(s);
|
||||
assertEquals("{}",obj.toString());
|
||||
|
||||
s="[5,]";
|
||||
obj=JSONValue.parse(s);
|
||||
assertEquals("[5]",obj.toString());
|
||||
|
||||
s="[5,,2]";
|
||||
obj=JSONValue.parse(s);
|
||||
assertEquals("[5,2]",obj.toString());
|
||||
}
|
||||
|
||||
public void testEncode() throws Exception{
|
||||
System.out.println("=======encode=======");
|
||||
|
||||
JSONArray array1=new JSONArray();
|
||||
array1.add("abc\u0010a/");
|
||||
array1.add(new Integer(123));
|
||||
array1.add(new Double(222.123));
|
||||
array1.add(new Boolean(true));
|
||||
System.out.println("======array1==========");
|
||||
System.out.println(array1);
|
||||
System.out.println();
|
||||
assertEquals("[\"abc\\u0010a\\/\",123,222.123,true]",array1.toString());
|
||||
|
||||
JSONObject obj1=new JSONObject();
|
||||
obj1.put("name","fang");
|
||||
obj1.put("age",new Integer(27));
|
||||
obj1.put("is_developer",new Boolean(true));
|
||||
obj1.put("weight",new Double(60.21));
|
||||
obj1.put("array1",array1);
|
||||
System.out.println("======obj1 with array1===========");
|
||||
System.out.println(obj1);
|
||||
System.out.println();
|
||||
assertEquals("{\"array1\":[\"abc\\u0010a\\/\",123,222.123,true],\"weight\":60.21,\"age\":27,\"name\":\"fang\",\"is_developer\":true}",obj1.toString());
|
||||
|
||||
obj1.remove("array1");
|
||||
array1.add(obj1);
|
||||
System.out.println("======array1 with obj1========");
|
||||
System.out.println(array1);
|
||||
System.out.println();
|
||||
assertEquals("[\"abc\\u0010a\\/\",123,222.123,true,{\"weight\":60.21,\"age\":27,\"name\":\"fang\",\"is_developer\":true}]",array1.toString());
|
||||
}
|
||||
}
|
||||
52
test/org/json/simple/parser/YylexTest.java
Normal file
52
test/org/json/simple/parser/YylexTest.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package org.json.simple.parser;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class YylexTest extends TestCase {
|
||||
|
||||
public void testYylex() throws Exception{
|
||||
String s="\"\\/\"";
|
||||
System.out.println(s);
|
||||
StringReader in = new StringReader(s);
|
||||
Yylex lexer=new Yylex(in);
|
||||
Yytoken token=lexer.yylex();
|
||||
assertEquals(Yytoken.TYPE_VALUE,token.type);
|
||||
assertEquals("/",token.value);
|
||||
|
||||
s="\"abc\\/\\r\\b\\n\\t\\f\\\\\"";
|
||||
System.out.println(s);
|
||||
in = new StringReader(s);
|
||||
lexer=new Yylex(in);
|
||||
token=lexer.yylex();
|
||||
assertEquals(Yytoken.TYPE_VALUE,token.type);
|
||||
assertEquals("abc/\r\b\n\t\f\\",token.value);
|
||||
|
||||
s="[\t \n\r\n{ \t \t\n\r}";
|
||||
System.out.println(s);
|
||||
in = new StringReader(s);
|
||||
lexer=new Yylex(in);
|
||||
token=lexer.yylex();
|
||||
assertEquals(Yytoken.TYPE_LEFT_SQUARE,token.type);
|
||||
token=lexer.yylex();
|
||||
assertEquals(Yytoken.TYPE_LEFT_BRACE,token.type);
|
||||
token=lexer.yylex();
|
||||
assertEquals(Yytoken.TYPE_RIGHT_BRACE,token.type);
|
||||
|
||||
s="\b\f{";
|
||||
System.out.println(s);
|
||||
in = new StringReader(s);
|
||||
lexer=new Yylex(in);
|
||||
Error err=null;
|
||||
try{
|
||||
token=lexer.yylex();
|
||||
}
|
||||
catch(Error e){
|
||||
err=e;
|
||||
System.out.println("error:"+err);
|
||||
}
|
||||
assertTrue(err!=null);
|
||||
}
|
||||
|
||||
}
|
||||
44
test/org/json/simple/rope/RopeTest.java
Normal file
44
test/org/json/simple/rope/RopeTest.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package org.json.simple.rope;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class RopeTest extends TestCase {
|
||||
public void testOpts(){
|
||||
Rope rope = new Rope();
|
||||
|
||||
assertEquals("",rope.toString());
|
||||
assertEquals(0,rope.length());
|
||||
|
||||
rope.append('c');
|
||||
assertEquals("c",rope.toString());
|
||||
assertEquals(1,rope.length());
|
||||
|
||||
rope.append("fang");
|
||||
assertEquals("cfang",rope.toString());
|
||||
assertEquals(5,rope.length());
|
||||
|
||||
rope.append('1');
|
||||
assertEquals("cfang1",rope.toString());
|
||||
assertEquals(6,rope.length());
|
||||
|
||||
rope.append("你好");
|
||||
assertEquals("cfang1你好",rope.toString());
|
||||
assertEquals(8,rope.length());
|
||||
|
||||
rope.append('世');
|
||||
assertEquals("cfang1你好世",rope.toString());
|
||||
assertEquals(9,rope.length());
|
||||
|
||||
rope.append("界");
|
||||
assertEquals("cfang1你好世界",rope.toString());
|
||||
assertEquals(10,rope.length());
|
||||
|
||||
rope.append("");
|
||||
assertEquals("cfang1你好世界",rope.toString());
|
||||
assertEquals(10,rope.length());
|
||||
|
||||
rope.clear();
|
||||
assertEquals("",rope.toString());
|
||||
assertEquals(0,rope.length());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user