12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186 |
- {
- This file is part of the Free Component Library
- JSON FPCUNit test for data structures
- Copyright (c) 2007 by Michael Van Canneyt [email protected]
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- {$mode objfpc}
- {$h+}
- unit testjsondata;
- interface
- uses
- Classes, SysUtils, fpcunit, testutils, testregistry, fpjson;
- type
- { TTestJSONString }
- TTestJSONString = Class(TTestCase)
- Private
- Procedure TestTo(Const Src,Dest : String);
- Procedure TestFrom(Const Src,Dest : String);
- Published
- Procedure TestJSONStringToString;
- Procedure TestStringToJSONString;
- end;
-
- { TTestJSON }
-
- TTestJSON = Class(TTestCase)
- Protected
- Procedure TestItemCount(J : TJSONData;Expected : Integer);
- Procedure TestJSONType(J : TJSONData;Expected : TJSONType);
- Procedure TestJSON(J : TJSONData;Expected : String);
- Procedure TestIsNull(J : TJSONData;Expected : Boolean);
- Procedure TestAsBoolean(J : TJSONData;Expected : Boolean; ExpectError : boolean = False);
- Procedure TestAsInteger(J : TJSONData; Expected : Integer; ExpectError : boolean = False);
- Procedure TestAsInt64(J : TJSONData; Expected : Int64; ExpectError : boolean = False);
- Procedure TestAsString(J : TJSONData; Expected : String; ExpectError : boolean = False);
- Procedure TestAsFloat(J : TJSONData; Expected : TJSONFloat; ExpectError : boolean = False);
- end;
-
- { TTestNull }
- TTestNull = class(TTestJSON)
- published
- procedure TestNull;
- Procedure TestClone;
- end;
-
- { TTestBoolean }
- TTestBoolean = class(TTestJSON)
- published
- procedure TestTrue;
- procedure TestFalse;
- Procedure TestClone;
- end;
-
- { TTestInteger }
- TTestInteger = class(TTestJSON)
- Private
- Procedure DoTest(I : Integer);
- published
- procedure TestPositive;
- procedure TestNegative;
- procedure TestZero;
- Procedure TestClone;
- end;
- { TTestInt64 }
- TTestInt64 = class(TTestJSON)
- Private
- Procedure DoTest(I : Int64);
- published
- procedure TestPositive;
- procedure TestNegative;
- procedure TestZero;
- Procedure TestClone;
- end;
-
- { TTestFloat }
- TTestFloat = class(TTestJSON)
- Private
- Procedure DoTest(F : TJSONFloat);
- published
- procedure TestPositive;
- procedure TestNegative;
- procedure TestZero;
- Procedure TestClone;
- end;
- { TTestString }
- TTestString = class(TTestJSON)
- private
- procedure DoTestFloat(F: TJSOnFloat; S: String; OK: Boolean);
- published
- procedure TestString;
- procedure TestInteger;
- procedure TestNegativeInteger;
- procedure TestFloat;
- procedure TestNegativeFloat;
- Procedure TestBooleanTrue;
- Procedure TestBooleanFalse;
- Procedure TestClone;
- end;
-
- { TTestArray }
- TTestArray = class(TTestJSON)
- private
- procedure TestAddBoolean(B : Boolean);
- published
- Procedure TestCreate;
- Procedure TestCreateString;
- Procedure TestCreatePchar;
- procedure TestCreateStrings;
- procedure TestCreateInteger;
- procedure TestCreateInt64;
- procedure TestCreateFloat;
- procedure TestCreateBoolean;
- procedure TestCreateObject;
- procedure TestCreateJSONString;
- procedure TestCreateJSONObject;
- procedure TestCreateNilPointer;
- procedure TestCreatePointer;
- procedure TestAddInteger;
- procedure TestAddInt64;
- procedure TestAddFloat;
- procedure TestAddBooleanTrue;
- procedure TestAddBooleanFalse;
- procedure TestAddString;
- procedure TestAddNull;
- procedure TestAddObject;
- procedure TestAddArray;
- procedure TestDelete;
- procedure TestRemove;
- Procedure TestClone;
- end;
-
- { TTestObject }
- TTestObject = class(TTestJSON)
- private
- procedure TestAddBoolean(B : Boolean);
- published
- Procedure TestCreate;
- Procedure TestCreateString;
- Procedure TestCreatePchar;
- procedure TestCreateStrings;
- procedure TestCreateInteger;
- procedure TestCreateInt64;
- procedure TestCreateFloat;
- procedure TestCreateBoolean;
- procedure TestCreateObject;
- procedure TestCreateJSONString;
- procedure TestCreateJSONObject;
- procedure TestCreateNilPointer;
- procedure TestCreatePointer;
- procedure TestAddInteger;
- procedure TestAddInt64;
- procedure TestAddFloat;
- procedure TestAddBooleanTrue;
- procedure TestAddBooleanFalse;
- procedure TestAddString;
- procedure TestAddNull;
- procedure TestAddObject;
- procedure TestAddArray;
- procedure TestDelete;
- procedure TestRemove;
- procedure TestClone;
- procedure TestExtract;
- end;
- implementation
- { TTestJSON }
- procedure TTestJSON.TestItemCount(J: TJSONData; Expected: Integer);
- begin
- AssertEquals(J.ClassName+'.ItemCount',Expected,J.Count);
- end;
- procedure TTestJSON.TestJSONType(J: TJSONData; Expected: TJSONType);
- begin
- AssertEquals(J.ClassName+'.JSONType',Ord(Expected),Ord(J.JSONType));
- end;
- procedure TTestJSON.TestJSON(J: TJSONData; Expected: String);
- begin
- AssertEquals(J.ClassName+'.AsJSON',Expected,J.AsJSON);
- end;
- procedure TTestJSON.TestIsNull(J: TJSONData; Expected: Boolean);
- begin
- AssertEquals(J.ClassName+'.IsNull',Expected,J.IsNull);
- end;
- procedure TTestJSON.TestAsBoolean(J: TJSONData; Expected: Boolean; ExpectError: boolean = False);
- Var
- B : Boolean;
- AssignOK : Boolean;
- Msg : String;
-
- begin
- AssignOK:=False;
- Try
- B:=J.AsBoolean;
- AssignOK:=True;
- If Not ExpectError then
- AssertEquals(J.Classname+'.AsBoolean',Expected,B);
- except
- On E : Exception do
- begin
- AssignOK:=False;
- Msg:=E.Message;
- end;
- end;
- If ExpectError then
- begin
- If AssignOK then
- Fail(J.ClassName+'.AsBoolean must raise error');
- end
- else
- begin
- If not AssignOK then
- Fail(J.ClassName+'.AsBoolean raised unexpected exception: '+Msg)
- end;
- end;
- procedure TTestJSON.TestAsInteger(J: TJSONData; Expected: Integer;
- ExpectError: boolean);
- Var
- I : Integer;
- AssignOK : Boolean;
- Msg : String;
- begin
- AssignOK:=False;
- Try
- I:=J.AsInteger;
- AssignOK:=True;
- If Not ExpectError then
- AssertEquals(J.Classname+'.AsInteger',Expected,I);
- except
- On E : Exception do
- begin
- AssignOK:=False;
- Msg:=E.Message;
- end;
- end;
- If ExpectError then
- begin
- If AssignOK then
- Fail(J.ClassName+'.AsInteger must raise error');
- end
- else
- begin
- If not AssignOK then
- Fail(J.ClassName+'.AsInteger raised unexpected exception: '+Msg)
- end;
- end;
- procedure TTestJSON.TestAsInt64(J: TJSONData; Expected: Int64;
- ExpectError: boolean);
- Var
- I : Int64;
- AssignOK : Boolean;
- Msg : String;
- begin
- AssignOK:=False;
- Try
- I:=J.AsInt64;
- AssignOK:=True;
- If Not ExpectError then
- AssertEquals(J.Classname+'.AsInt64',Expected,I);
- except
- On E : Exception do
- begin
- AssignOK:=False;
- Msg:=E.Message;
- end;
- end;
- If ExpectError then
- begin
- If AssignOK then
- Fail(J.ClassName+'.AsInt64 must raise error');
- end
- else
- begin
- If not AssignOK then
- Fail(J.ClassName+'.AsInt64 raised unexpected exception: '+Msg)
- end;
- end;
- procedure TTestJSON.TestAsString(J: TJSONData; Expected: String;
- ExpectError: boolean);
-
- Var
- S : String;
- AssignOK : Boolean;
- Msg : String;
- begin
- AssignOK:=False;
- Try
- S:=J.AsString;
- AssignOK:=True;
- If Not ExpectError then
- AssertEquals(J.Classname+'.AsString',Expected,S);
- except
- On E : Exception do
- begin
- AssignOK:=False;
- Msg:=E.Message;
- end;
- end;
- If ExpectError then
- begin
- If AssignOK then
- Fail(J.ClassName+'.AsString must raise error');
- end
- else
- begin
- If not AssignOK then
- Fail(J.ClassName+'.AsString raised unexpected exception: '+Msg)
- end;
- end;
- procedure TTestJSON.TestAsFloat(J: TJSONData; Expected: TJSONFloat;
- ExpectError: boolean);
-
- Var
- F : TJSONFloat;
- AssignOK : Boolean;
- Msg : String;
- begin
- AssignOK:=False;
- Try
- F:=J.AsFloat;
- AssignOK:=True;
- If Not ExpectError then
- AssertEquals(J.Classname+'.AsFloat',Expected,F);
- except
- On E : Exception do
- begin
- AssignOK:=False;
- Msg:=E.Message;
- end;
- end;
- If ExpectError then
- begin
- If AssignOK then
- Fail(J.ClassName+'.AsFloat must raise error');
- end
- else
- begin
- If not AssignOK then
- Fail(J.ClassName+'.AsFloat raised unexpected exception: '+Msg)
- end;
- end;
- { TTestBoolean }
- procedure TTestBoolean.TestTrue;
- Var
- J : TJSONBoolean;
- begin
- J:=TJSONBoolean.Create(True);
- try
- TestJSONType(J,jtBoolean);
- TestItemCount(J,0);
- TestJSON(J,'true');
- TestIsNull(J,False);
- TestAsBoolean(J,True);
- TestAsInteger(J,1);
- TestAsInt64(J,1);
- TestAsString(J,BoolToStr(True));
- TestAsFloat(J,1.0);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestBoolean.TestFalse;
- Var
- J : TJSONBoolean;
- begin
- J:=TJSONBoolean.Create(False);
- try
- TestJSONType(J,jtBoolean);
- TestItemCount(J,0);
- TestJSON(J,'false');
- TestIsNull(J,False);
- TestAsBoolean(J,False);
- TestAsInteger(J,0);
- TestAsInt64(J,0);
- TestAsString(J,BoolToStr(False));
- TestAsFloat(J,0.0);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestBoolean.TestClone;
- Var
- B : TJSONBoolean;
- D : TJSONData;
- begin
- B:=TJSONBoolean.Create(true);
- try
- D:=B.Clone;
- try
- TestJSONType(D,jtBoolean);
- TestAsBoolean(D,true);
- finally
- D.Free;
- end;
- finally
- FreeAndNil(B);
- end;
- end;
- { TTestNull }
- procedure TTestNull.TestNull;
- Var
- J : TJSONNull;
- begin
- J:=TJSONNull.Create;
- try
- TestJSONType(J,jtNull);
- TestItemCount(J,0);
- TestJSON(J,'null');
- TestIsNull(J,True);
- TestAsBoolean(J,False,True);
- TestAsInteger(J,0,true);
- TestAsInt64(J,0,true);
- TestAsString(J,BoolToStr(False),true);
- TestAsFloat(J,0.0,true);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestNull.TestClone;
- Var
- J : TJSONNull;
- D : TJSONData;
- begin
- J:=TJSONNull.Create;
- try
- D:=J.Clone;
- try
- TestIsNull(D,True);
- finally
- D.Free;
- end;
- finally
- FreeAndNil(J);
- end;
- end;
- { TTestString }
- procedure TTestString.TestString;
- Const
- S = 'A string';
- Var
- J : TJSONString;
- begin
- J:=TJSONString.Create(S);
- try
- TestJSONType(J,jtString);
- TestItemCount(J,0);
- TestJSON(J,'"'+S+'"');
- TestIsNull(J,False);
- TestAsBoolean(J,False,True);
- TestAsInteger(J,0,true);
- TestAsInt64(J,0,true);
- TestAsString(J,S);
- TestAsFloat(J,0.0,true);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestString.TestInteger;
- Const
- S = '1';
- Var
- J : TJSONString;
- begin
- J:=TJSONString.Create(S);
- try
- TestJSONType(J,jtString);
- TestItemCount(J,0);
- TestJSON(J,'"'+S+'"');
- TestIsNull(J,False);
- TestAsBoolean(J,True,False);
- TestAsInteger(J,1,False);
- TestAsInt64(J,1,False);
- TestAsString(J,S);
- TestAsFloat(J,1.0,False);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestString.TestNegativeInteger;
- Const
- S = '-1';
- Var
- J : TJSONString;
- begin
- J:=TJSONString.Create(S);
- try
- TestJSONType(J,jtString);
- TestItemCount(J,0);
- TestJSON(J,'"'+S+'"');
- TestIsNull(J,False);
- TestAsBoolean(J,True,False);
- TestAsInteger(J,-1,False);
- TestAsInt64(J,-1,False);
- TestAsString(J,S);
- TestAsFloat(J,-1.0,False);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestString.TestFloat;
- begin
- DoTestFloat(1.0,'1.0',True);
- DoTestFloat(1.0,'1',True);
- DoTestFloat(1.0,'1e0',True);
- DoTestFloat(1.2,'1.2',True);
- DoTestFloat(12.0,'1.2e1',True);
- end;
- procedure TTestString.TestNegativeFloat;
- begin
- DoTestFloat(-1.0,'-1.0',True);
- DoTestFloat(-1.0,'-1',True);
- DoTestFloat(-1.0,'-1e0',True);
- DoTestFloat(-1.2,'-1.2',True);
- DoTestFloat(-12.0,'-1.2e1',True);
- end;
- procedure TTestString.TestBooleanTrue;
- Const
- S = 'true';
- Var
- J : TJSONString;
- begin
- J:=TJSONString.Create(S);
- try
- TestJSONType(J,jtString);
- TestItemCount(J,0);
- TestJSON(J,'"'+S+'"');
- TestIsNull(J,False);
- TestAsBoolean(J,True,False);
- TestAsInteger(J,-1,True);
- TestAsInt64(J,-1,True);
- TestAsString(J,S);
- TestAsFloat(J,-1.0,True);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestString.TestBooleanFalse;
- Const
- S = 'false';
- Var
- J : TJSONString;
- begin
- J:=TJSONString.Create(S);
- try
- TestJSONType(J,jtString);
- TestItemCount(J,0);
- TestJSON(J,'"'+S+'"');
- TestIsNull(J,False);
- TestAsBoolean(J,False,False);
- TestAsInteger(J,0,True);
- TestAsInt64(J,0,True);
- TestAsString(J,S);
- TestAsFloat(J,0,True);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestString.TestClone;
- Var
- S : TJSONString;
- D : TJSONData;
- begin
- S:=TJSONString.Create('aloha');
- try
- D:=S.Clone;
- try
- TestJSONType(D,jtString);
- TestAsString(D,'aloha');
- finally
- D.Free;
- end;
- finally
- FreeAndNil(S);
- end;
- end;
- procedure TTestString.DoTestFloat(F : TJSOnFloat;S : String; OK : Boolean);
- Var
- J : TJSONString;
- begin
- J:=TJSONString.Create(S);
- try
- TestJSONType(J,jtString);
- TestItemCount(J,0);
- TestJSON(J,'"'+S+'"');
- TestIsNull(J,False);
- TestAsBoolean(J,(F<>0),Not OK);
- TestAsInteger(J,Round(F),(Pos('.',S)<>0) or (Pos('E',UpperCase(S))<>0));
- TestAsInt64(J,Round(F),(Pos('.',S)<>0) or (Pos('E',UpperCase(S))<>0));
- TestAsString(J,S);
- TestAsFloat(J,F,Not OK);
- finally
- FreeAndNil(J);
- end;
- end;
- { TTestInteger }
- procedure TTestInteger.DoTest(I: Integer);
- Var
- J : TJSONIntegerNumber;
- begin
- J:=TJSONIntegerNumber.Create(I);
- try
- TestJSONType(J,jtNumber);
- TestItemCount(J,0);
- AssertEquals('Numbertype is ntInteger',ord(ntInteger),Ord(J.NumberType));
- TestJSON(J,IntToStr(i));
- TestIsNull(J,False);
- TestAsBoolean(J,(I<>0));
- TestAsInteger(J,I);
- TestAsInt64(J,I);
- TestAsString(J,IntToStr(I));
- TestAsFloat(J,I);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestInteger.TestPositive;
- begin
- DoTest(1);
- end;
- procedure TTestInteger.TestNegative;
- begin
- DoTest(-1);
- end;
- procedure TTestInteger.TestZero;
- begin
- DoTest(0);
- end;
- procedure TTestInteger.TestClone;
- Var
- I : TJSONIntegerNumber;
- D : TJSONData;
- begin
- I:=TJSONIntegerNumber.Create(99);
- try
- D:=I.Clone;
- try
- TestJSONType(D,jtNumber);
- TestAsInteger(D,99);
- finally
- D.Free;
- end;
- finally
- FreeAndNil(I);
- end;
- end;
- { TTestInt64 }
- procedure TTestInt64.DoTest(I: Int64);
- Var
- J : TJSONInt64Number;
- begin
- J:=TJSONInt64Number.Create(I);
- try
- TestJSONType(J,jtNumber);
- TestItemCount(J,0);
- AssertEquals('Numbertype is ntInt64',ord(ntInt64),Ord(J.NumberType));
- TestJSON(J,IntToStr(i));
- TestIsNull(J,False);
- TestAsBoolean(J,(I<>0));
- TestAsInteger(J,I);
- TestAsInt64(J,I);
- TestAsString(J,IntToStr(I));
- TestAsFloat(J,I);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestInt64.TestPositive;
- begin
- DoTest(1);
- end;
- procedure TTestInt64.TestNegative;
- begin
- DoTest(-1);
- end;
- procedure TTestInt64.TestZero;
- begin
- DoTest(0);
- end;
- procedure TTestInt64.TestClone;
- Var
- I : TJSONInt64Number;
- D : TJSONData;
- begin
- I:=TJSONInt64Number.Create(99);
- try
- D:=I.Clone;
- try
- TestJSONType(D,jtNumber);
- AssertEquals('Numbertype is ntInt64',ord(ntInt64),Ord(TJSONInt64Number(D).NumberType));
- TestAsInteger(D,99);
- finally
- D.Free;
- end;
- finally
- FreeAndNil(I);
- end;
- end;
- { TTestFloat }
- procedure TTestFloat.DoTest(F: TJSONFloat);
- Var
- J : TJSONFloatNumber;
- S : String;
-
- begin
- Str(F,S);
- J:=TJSONFloatNumber.Create(F);
- try
- TestJSONType(J,jtNumber);
- TestItemCount(J,0);
- AssertEquals('Numbertype is ntFloat',ord(ntFloat),Ord(J.NumberType));
- TestJSON(J,S);
- TestIsNull(J,False);
- TestAsBoolean(J,(F<>0));
- TestAsInteger(J,Round(F));
- TestAsInt64(J,Round(F));
- TestAsString(J,S);
- TestAsFloat(J,F);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestFloat.TestPositive;
- begin
- DoTest(1.0);
- DoTest(1.2);
- DoTest(1.2e1);
- DoTest(1.2e-1);
- DoTest(1.2e10);
- DoTest(1.2e-10);
- end;
- procedure TTestFloat.TestNegative;
- begin
- DoTest(-1.0);
- DoTest(-1.2);
- DoTest(-1.2e1);
- DoTest(-1.2e-1);
- DoTest(-1.2e10);
- DoTest(-1.2e-10);
- end;
- procedure TTestFloat.TestZero;
- begin
- DoTest(0.0);
- end;
- procedure TTestFloat.TestClone;
- Var
- F : TJSONFloatNumber;
- D : TJSONData;
- begin
- F:=TJSONFloatNumber.Create(1.23);
- try
- D:=F.Clone;
- try
- TestJSONType(D,jtNumber);
- AssertEquals('Numbertype is ntFloat',ord(ntFloat),Ord(TJSONFloatNumber(D).NumberType));
- TestAsFloat(D,1.23);
- finally
- D.Free;
- end;
- finally
- FreeAndNil(F);
- end;
- end;
- { TTestArray }
- procedure TTestArray.TestCreate;
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create;
- try
- TestJSONType(J,jtArray);
- TestItemCount(J,0);
- TestJSON(J,'[]');
- TestIsNull(J,False);
- TestAsBoolean(J,False,True);
- TestAsInteger(J,1,True);
- TestAsInt64(J,1,True);
- TestAsString(J,'',True);
- TestAsFloat(J,0.0,True);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateString;
- Const
- S = 'A string';
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create([S]);
- try
- TestJSONType(J,jtArray);
- TestItemCount(J,1);
- TestJSONType(J[0],jtString);
- TestJSON(J,'["'+S+'"]');
- TestIsNull(J,False);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreatePChar;
- Const
- S = 'A string';
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create([Pchar(S)]);
- try
- TestJSONType(J,jtArray);
- TestItemCount(J,1);
- TestJSONType(J[0],jtString);
- TestJSON(J,'["'+S+'"]');
- TestIsNull(J,False);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateStrings;
- Const
- S = 'A string';
- T = 'B string';
-
- Var
- J : TJSONArray;
- begin
- J:=TJSONArray.Create([S,T]);
- try
- TestJSONType(J,jtArray);
- TestItemCount(J,2);
- TestJSONType(J[0],jtString);
- TestJSONType(J[1],jtString);
- TestJSON(J,'["'+S+'", "'+T+'"]');
- TestIsNull(J,False);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateInteger;
- Const
- S = 3;
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create([S]);
- try
- TestJSONType(J,jtArray);
- TestItemCount(J,1);
- TestJSONType(J[0],jtNumber);
- TestJSON(J,'[3]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateFloat;
- Const
- S = 1.2;
- Var
- J : TJSONArray;
- r : String;
-
- begin
- J:=TJSonArray.Create([S]);
- try
- TestJSONType(J,jtArray);
- TestItemCount(J,1);
- TestJSONType(J[0],jtNumber);
- Str(S,R);
- TestJSON(J,'['+R+']');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateInt64;
- Const
- S : Int64 = $FFFFFFFFFFFFF;
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create([S]);
- try
- TestJSONType(J,jtArray);
- TestItemCount(J,1);
- TestJSONType(J[0],jtNumber);
- TestJSON(J,'['+IntToStr(S)+']');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateBoolean;
- Const
- S = True;
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create([S]);
- try
- TestJSONType(J,jtArray);
- TestItemCount(J,1);
- TestJSONType(J[0],jtBoolean);
- TestJSON(J,'[true]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateJSONObject;
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create([TJSONObject.Create]);
- try
- TestItemCount(J,1);
- TestJSONType(J[0],jtObject);
- TestJSON(J,'[{}]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateJSONString;
- Const
- S = 'A string';
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create([TJSONString.Create(S)]);
- try
- TestItemCount(J,1);
- TestJSONType(J[0],jtString);
- TestJSON(J,'["'+S+'"]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreateObject;
- Var
- J : TJSONArray;
-
- begin
- J:=Nil;
- try
- Try
- J:=TJSONArray.Create([TObject.Create]);
- Fail('Array constructor accepts only TJSONData');
- finally
- FreeAndNil(J);
- end;
- except
- // Should be OK.
- end;
- end;
- procedure TTestArray.TestCreateNilPointer;
- Var
- J : TJSONArray;
- P : Pointer;
-
- begin
- J:=Nil;
- P:=Nil;
- Try
- J:=TJSONArray.Create([P]);
- TestJSONType(J[0],jtNull);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestCreatePointer;
- Var
- J : TJSONArray;
- P : Pointer;
-
- begin
- J:=Nil;
- P:=@Self;
- try
- Try
- J:=TJSONArray.Create([P]);
- Fail('Array constructor accepts only NIL pointers');
- finally
- FreeAndNil(J);
- end;
- except
- // Should be OK.
- end;
- end;
- procedure TTestArray.TestAddInteger;
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create;
- try
- J.Add(Integer(0));
- TestItemCount(J,1);
- TestJSONType(J[0],jtNumber);
- AssertEquals('J[0] is TJSONIntegerNumber',J[0].ClassType,TJSONIntegerNumber);
- AssertEquals('j.Types[0]=jtNumber',ord(J.Types[0]),Ord(jtNumber));
- AssertEquals('J.Integers[0]=0',0,J.integers[0]);
- TestAsInteger(J[0],0);
- TestAsInt64(J[0],0);
- TestJSON(J,'[0]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestAddInt64;
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create;
- try
- J.Add(Int64(0));
- TestItemCount(J,1);
- TestJSONType(J[0],jtNumber);
- AssertEquals('J[0] is TJSONInt64Number',J[0].ClassType,TJSONInt64Number);
- AssertEquals('j.Types[0]=jtNumber',ord(J.Types[0]),Ord(jtNumber));
- AssertEquals('J.Int64s[0]=0',0,J.Int64s[0]);
- TestAsInteger(J[0],0);
- TestAsInt64(J[0],0);
- TestJSON(J,'[0]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestAddFloat;
- Var
- J : TJSONArray;
- S : String;
- F : TJSONFloat;
- begin
- F:=1.2;
- J:=TJSonArray.Create;
- try
- J.Add(F);
- TestItemCount(J,1);
- TestJSONType(J[0],jtNumber);
- AssertEquals('J[0] is TJSONFloatNumber',TJSONfloatNumber,J[0].ClassType);
- AssertEquals('j.Types[0]=jtNumber',Ord(jtNumber),ord(J.Types[0]));
- AssertEquals('J.Floats[0]='+FloatToStr(F),F,J.Floats[0]);
- TestAsFloat(J[0],F);
- Str(F,S);
- TestJSON(J,'['+S+']');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestAddBoolean(B : Boolean);
- Var
- J : TJSONArray;
- begin
- B:=True;
- J:=TJSonArray.Create;
- try
- J.Add(B);
- TestItemCount(J,1);
- TestJSONType(J[0],jtBoolean);
- AssertEquals('J[0] is TJSONBoolean',TJSONBoolean,J[0].ClassType);
- TestAsBoolean(J[0],B);
- AssertEquals('J.Booleans[0]='+BoolToStr(B)+'"',B,J.Booleans[0]);
- If B then
- TestJSON(J,'[true]')
- else
- TestJSON(J,'[false]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestAddBooleanTrue;
- begin
- TestAddBoolean(True);
- end;
- procedure TTestArray.TestAddBooleanFalse;
- begin
- TestAddBoolean(False);
- end;
- procedure TTestArray.TestAddString;
- Var
- J : TJSONArray;
- S : String;
- F : TJSONFloat;
-
- begin
- S:='A string';
- J:=TJSonArray.Create;
- try
- J.Add(S);
- TestItemCount(J,1);
- TestJSONType(J[0],jtString);
- AssertEquals('J[0] is TJSONString',TJSONString,J[0].ClassType);
- TestAsString(J[0],S);
- AssertEquals('J.Strings[0]="'+S+'"',S,J.Strings[0]);
- TestJSON(J,'["'+StringToJSONString(S)+'"]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestAddNull;
- Var
- J : TJSONArray;
- S : String;
- F : TJSONFloat;
- begin
- J:=TJSonArray.Create;
- try
- J.Add;
- TestItemCount(J,1);
- TestJSONType(J[0],jtNull);
- AssertEquals('J[0] is TJSONNull',TJSONNull,J[0].ClassType);
- AssertEquals('J.Nulls[0]=True',True,J.Nulls[0]);
- TestIsNull(J[0],true);
- TestJSON(J,'[null]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestAddArray;
- Var
- J,J2 : TJSONArray;
- begin
- J:=TJSonArray.Create;
- try
- J2:=TJSonArray.Create;
- J2.Add(0);
- J2.Add(1);
- J.Add(J2);
- TestItemCount(J,1);
- TestJSONType(J[0],jtArray);
- AssertEquals('J[0] is TJSONArray',TJSONArray,J[0].ClassType);
- AssertEquals('J.Arrays[0] is TJSONArray',TJSONArray,J.Arrays[0].ClassType);
- TestAsInteger(J.Arrays[0][0],0);
- TestAsInteger(J.Arrays[0][1],1);
- TestAsInt64(J.Arrays[0][0],0);
- TestAsInt64(J.Arrays[0][1],1);
- TestJSON(J,'[[0, 1]]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestAddObject;
- Const
- A = 'a';
- B = 'b';
-
- Var
- J : TJSONArray;
- J2 : TJSONObject;
- begin
- J:=TJSonArray.Create;
- try
- J2:=TJSonObject.Create;
- J2.Add(A,0);
- J2.Add(B,1);
- J.Add(J2);
- TestItemCount(J,1);
- TestJSONType(J[0],jtObject);
- AssertEquals('J[0] is TJSONObject',TJSONObject,J[0].ClassType);
- AssertEquals('J.Objects[0] is TJSONObject',TJSONObject,J.Objects[0].ClassType);
- TestAsInteger(J.Objects[0][A],0);
- TestAsInteger(J.Objects[0][B],1);
- TestAsInt64(J.Objects[0][A],0);
- TestAsInt64(J.Objects[0][B],1);
- TestJSON(J,'[{ "a" : 0, "b" : 1 }]');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestDelete;
- Var
- J : TJSONArray;
- begin
- J:=TJSonArray.Create;
- try
- J.Add(0);
- J.Add(1);
- TestItemCount(J,2);
- TestJSONType(J[0],jtNumber);
- TestJSONType(J[1],jtNumber);
- TestJSON(J,'[0, 1]');
- J.Delete(1);
- TestItemCount(J,1);
- J.Delete(0);
- TestItemCount(J,0);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestRemove;
- Var
- J : TJSONArray;
- I : TJSONData;
- begin
- J:=TJSonArray.Create;
- try
- J.Add(0);
- J.Add(1);
- J.Add(2);
- TestItemCount(J,3);
- TestJSONType(J[0],jtNumber);
- TestJSONType(J[1],jtNumber);
- TestJSONType(J[2],jtNumber);
- TestJSON(J,'[0, 1, 2]');
- I:=J[1];
- J.Remove(I);
- TestItemCount(J,2);
- TestAsInteger(J[0],0);
- TestAsInteger(J[1],2);
- TestAsInt64(J[0],0);
- TestAsInt64(J[1],2);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestArray.TestClone;
- Var
- J,J2 : TJSONArray;
- D : TJSONData;
- begin
- J:=TJSonArray.Create;
- try
- J.Add(1);
- J.Add('aloha');
- D:=J.Clone;
- try
- TestJSONType(D,jtArray);
- J2:=TJSonArray(D);
- TestItemCount(J2,2);
- TestJSONType(J2[0],jtNumber);
- TestJSONType(J2[1],jtString);
- TestAsInteger(J2[0],1);
- TestAsString(J2[1],'aloha');
- finally
- D.Free;
- end;
- finally
- FreeAndNil(J);
- end;
- end;
- { TTestObject }
- procedure TTestObject.TestCreate;
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create;
- try
- TestJSONType(J,jtObject);
- TestItemCount(J,0);
- TestJSON(J,'{}');
- TestIsNull(J,False);
- TestAsBoolean(J,False,True);
- TestAsInteger(J,1,True);
- TestAsInt64(J,1,True);
- TestAsString(J,'',True);
- TestAsFloat(J,0.0,True);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestAddInteger;
- Const
- A = 'a';
- Var
- J : TJSONObject;
- begin
- J:=TJSonObject.Create;
- try
- J.Add(A,Integer(0));
- TestItemCount(J,1);
- TestJSONType(J[A],jtNumber);
- AssertEquals('J[''a''] is TJSONIntegerNumber',J[A].ClassType,TJSONIntegerNumber);
- AssertEquals('j.Types[''a'']=jtNumber',ord(J.Types[A]),Ord(jtNumber));
- AssertEquals('J.Integers[''a'']=0',0,J.integers[A]);
- TestAsInteger(J[A],0);
- TestAsInt64(J[A],0);
- TestJSON(J,'{ "'+A+'" : 0 }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestAddInt64;
- Const
- A = 'a';
- Var
- J : TJSONObject;
- begin
- J:=TJSonObject.Create;
- try
- J.Add(A,Int64(0));
- TestItemCount(J,1);
- TestJSONType(J[A],jtNumber);
- AssertEquals('J[''a''] is TJSONInt64Number',J[A].ClassType,TJSONInt64Number);
- AssertEquals('j.Types[''a'']=jtNumber',ord(J.Types[A]),Ord(jtNumber));
- AssertEquals('J.Int64s[''a'']=0',0,J.Int64s[A]);
- TestAsInteger(J[A],0);
- TestAsInt64(J[A],0);
- TestJSON(J,'{ "'+A+'" : 0 }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestAddFloat;
- Const
- A = 'a';
- Var
- J : TJSONObject;
- S : String;
- F : TJSONFloat;
- begin
- F:=1.2;
- J:=TJSonObject.Create;
- try
- J.Add(A,F);
- TestItemCount(J,1);
- TestJSONType(J[A],jtNumber);
- AssertEquals('J[''a''] is TJSONFloatNumber',TJSONfloatNumber,J[a].ClassType);
- AssertEquals('j.Types[''a'']=jtNumber',Ord(jtNumber),ord(J.Types[a]));
- AssertEquals('J.Floats[''a'']='+FloatToStr(F),F,J.Floats[a]);
- TestAsFloat(J[A],F);
- Str(F,S);
- TestJSON(J,'{ "'+a+'" : '+S+' }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestAddBoolean(B : Boolean);
- Const
- A = 'a';
-
- Var
- J : TJSONObject;
- begin
- B:=True;
- J:=TJSonObject.Create;
- try
- J.Add(A,B);
- TestItemCount(J,1);
- TestJSONType(J[A],jtBoolean);
- AssertEquals('J[''a''] is TJSONBoolean',TJSONBoolean,J[a].ClassType);
- TestAsBoolean(J[a],B);
- AssertEquals('J.Booleans[''a'']='+BoolToStr(B)+'"',B,J.Booleans[a]);
- If B then
- TestJSON(J,'{ "'+a+'" : true }')
- else
- TestJSON(J,'{ "'+a+'" : false }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestAddBooleanTrue;
- begin
- TestAddBoolean(True);
- end;
- procedure TTestObject.TestAddBooleanFalse;
- begin
- TestAddBoolean(False);
- end;
- procedure TTestObject.TestAddString;
- Const
- A = 'a';
- Var
- J : TJSONObject;
- S : String;
- F : TJSONFloat;
- begin
- S:='A string';
- J:=TJSonObject.Create;
- try
- J.Add(A,S);
- TestItemCount(J,1);
- TestJSONType(J[a],jtString);
- AssertEquals('J[''a''] is TJSONString',TJSONString,J[A].ClassType);
- TestAsString(J[a],S);
- AssertEquals('J.Strings[''a'']="'+S+'"',S,J.Strings[A]);
- TestJSON(J,'{ "'+a+'" : "'+StringToJSONString(S)+'" }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestAddNull;
- Const
- A = 'a';
- Var
- J : TJSONObject;
- S : String;
- F : TJSONFloat;
- begin
- J:=TJSonObject.Create;
- try
- J.Add(a);
- TestItemCount(J,1);
- TestJSONType(J[a],jtNull);
- AssertEquals('J[''a''] is TJSONNull',TJSONNull,J[A].ClassType);
- AssertEquals('J.Nulls[''a'']=True',True,J.Nulls[A]);
- TestIsNull(J[a],true);
- TestJSON(J,'{ "'+a+'" : null }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestAddObject;
- Const
- A = 'a';
- B = 'b';
- C = 'c';
- Var
- J,J2 : TJSONObject;
- begin
- J:=TJSonObject.Create;
- try
- J2:=TJSonObject.Create;
- J2.Add(B,0);
- J2.Add(C,1);
- J.Add(A,J2);
- TestItemCount(J,1);
- TestJSONType(J[A],jtObject);
- AssertEquals('J[''a''] is TJSONObject',TJSONObject,J[A].ClassType);
- AssertEquals('J.Objects[''a''] is TJSONObject',TJSONObject,J.Objects[A].ClassType);
- TestAsInteger(J.Objects[A][B],0);
- TestAsInteger(J.Objects[A][C],1);
- TestAsInt64(J.Objects[A][B],0);
- TestAsInt64(J.Objects[A][C],1);
- TestJSON(J,'{ "a" : { "b" : 0, "c" : 1 } }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestAddArray;
- Const
- A = 'a';
- Var
- J : TJSONObject;
- J2 : TJSONArray;
- begin
- J:=TJSONObject.Create;
- try
- J2:=TJSonArray.Create;
- J2.Add(0);
- J2.Add(1);
- J.Add(A,J2);
- TestItemCount(J,1);
- TestJSONType(J[A],jtArray);
- AssertEquals('J[''a''] is TJSONArray',TJSONArray,J[A].ClassType);
- AssertEquals('J.Arrays[0] is TJSONArray',TJSONArray,J.Arrays[A].ClassType);
- TestAsInteger(J.Arrays[A][0],0);
- TestAsInteger(J.Arrays[A][1],1);
- TestAsInt64(J.Arrays[A][0],0);
- TestAsInt64(J.Arrays[A][1],1);
- TestJSON(J,'{ "a" : [0, 1] }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestDelete;
- Const
- A = 'a';
- B = 'b';
-
- Var
- J : TJSONObject;
- begin
- J:=TJSonObject.Create;
- try
- J.Add(A,0);
- J.Add(B,1);
- TestItemCount(J,2);
- TestJSONType(J[A],jtNumber);
- TestJSONType(J[A],jtNumber);
- TestJSON(J,'{ "a" : 0, "b" : 1 }');
- J.Delete(1);
- TestItemCount(J,1);
- J.Delete(0);
- TestItemCount(J,0);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestRemove;
- Const
- A = 'a';
- B = 'b';
- C = 'c';
-
- Var
- J : TJSONObject;
- I : TJSONData;
- begin
- J:=TJSonObject.Create;
- try
- J.Add(A,1);
- J.Add(B,2);
- J.Add(C,3);
- TestItemCount(J,3);
- TestJSONType(J[A],jtNumber);
- TestJSONType(J[B],jtNumber);
- TestJSONType(J[C],jtNumber);
- TestJSON(J,'{ "a" : 1, "b" : 2, "c" : 3 }');
- I:=J[b];
- J.Remove(I);
- TestItemCount(J,2);
- TestAsInteger(J[a],1);
- TestAsInteger(J[c],3);
- TestAsInt64(J[a],1);
- TestAsInt64(J[c],3);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestClone;
- Var
- J,J2 : TJSONObject;
- D : TJSONData;
- begin
- J:=TJSonObject.Create;
- try
- J.Add('p1',1);
- J.Add('p2','aloha');
- D:=J.Clone;
- try
- TestJSONType(D,jtObject);
- J2:=TJSonObject(D);
- TestItemCount(J2,2);
- TestJSONType(J2['p1'],jtNumber);
- TestJSONType(J2['p2'],jtString);
- TestAsInteger(J2['p1'],1);
- TestAsString(J2['p2'],'aloha');
- finally
- D.Free;
- end;
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestExtract;
- Const
- A = 'a';
- B = 'b';
- Var
- J : TJSONObject;
- JA,JB : TJSONData;
- E : TJSONData;
- begin
- J:=TJSonObject.Create;
- try
- J.Add(A,0);
- J.Add(B,1);
- TestItemCount(J,2);
- JA:=J[A];
- JB:=J[B];
- TestJSONType(JA,jtNumber);
- TestJSONType(JB,jtNumber);
- TestJSON(J,'{ "a" : 0, "b" : 1 }');
- E:=J.Extract(1);
- AssertSame('Extracted JA',JB,E);
- E.Free;
- TestItemCount(J,1);
- E:=J.Extract(0);
- AssertSame('Extracted JB',JA,E);
- E.Free;
- TestItemCount(J,0);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateString;
- Const
- A = 'A';
- S = 'A string';
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create([A,S]);
- try
- TestJSONType(J,jtObject);
- TestItemCount(J,1);
- TestJSONType(J[A],jtString);
- TestJSON(J,'{ "A" : "'+S+'" }');
- TestIsNull(J,False);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreatePChar;
- Const
- A = 'A';
- S = 'A string';
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create([A,Pchar(S)]);
- try
- TestJSONType(J,jtObject);
- TestItemCount(J,1);
- TestJSONType(J[A],jtString);
- TestJSON(J,'{ "A" : "'+S+'" }');
- TestIsNull(J,False);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateStrings;
- Const
- A = 'A';
- B = 'B';
- S = 'A string';
- T = 'B string';
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create([A,S,B,T]);
- try
- TestJSONType(J,jtObject);
- TestItemCount(J,2);
- TestJSONType(J[A],jtString);
- TestJSONType(J[B],jtString);
- TestJSON(J,'{ "A" : "'+S+'", "B" : "'+T+'" }');
- TestIsNull(J,False);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateInteger;
- Const
- A = 'A';
- S = 3;
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create([A,S]);
- try
- TestJSONType(J,jtObject);
- TestItemCount(J,1);
- TestJSONType(J[A],jtNumber);
- TestJSON(J,'{ "A" : 3 }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateFloat;
- Const
- A = 'A';
- S = 1.2;
- Var
- J : TJSONObject;
- r : String;
- begin
- J:=TJSONObject.Create([A,S]);
- try
- TestJSONType(J,jtObject);
- TestItemCount(J,1);
- TestJSONType(J[A],jtNumber);
- Str(S,R);
- TestJSON(J,'{ "A" : '+R+' }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateInt64;
- Const
- A = 'A';
- S : Int64 = $FFFFFFFFFFFFF;
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create([A,S]);
- try
- TestJSONType(J,jtObject);
- TestItemCount(J,1);
- TestJSONType(J[A],jtNumber);
- TestJSON(J,'{ "A" : '+IntToStr(S)+' }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateBoolean;
- Const
- A = 'A';
- S = True;
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create([A,S]);
- try
- TestJSONType(J,jtObject);
- TestItemCount(J,1);
- TestJSONType(J[A],jtBoolean);
- TestJSON(J,'{ "A" : true }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateJSONObject;
- Const
- A = 'A';
-
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create([A,TJSONObject.Create]);
- try
- TestItemCount(J,1);
- TestJSONType(J[A],jtObject);
- TestJSON(J,'{ "A" : {} }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateJSONString;
- Const
- A = 'A';
- S = 'A string';
- Var
- J : TJSONObject;
- begin
- J:=TJSONObject.Create([A,TJSONString.Create(S)]);
- try
- TestItemCount(J,1);
- TestJSONType(J[A],jtString);
- TestJSON(J,'{ "A" : "'+S+'" }');
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreateObject;
- Const
- A = 'A';
- Var
- J : TJSONObject;
- begin
- J:=Nil;
- try
- Try
- J:=TJSONObject.Create([A,TObject.Create]);
- Fail('Array constructor accepts only TJSONData');
- finally
- FreeAndNil(J);
- end;
- except
- // Should be OK.
- end;
- end;
- procedure TTestObject.TestCreateNilPointer;
- Const
- A = 'A';
- Var
- J : TJSONObject;
- P : Pointer;
- begin
- J:=Nil;
- P:=Nil;
- Try
- J:=TJSONObject.Create([A,P]);
- TestJSONType(J[A],jtNull);
- finally
- FreeAndNil(J);
- end;
- end;
- procedure TTestObject.TestCreatePointer;
- Const
- A = 'A';
- Var
- J : TJSONObject;
- P : Pointer;
- begin
- J:=Nil;
- P:=@Self;
- try
- Try
- J:=TJSONObject.Create([A,P]);
- Fail('Array constructor accepts only NIL pointers');
- finally
- FreeAndNil(J);
- end;
- except
- // Should be OK.
- end;
- end;
- { TTestJSONString }
- procedure TTestJSONString.TestTo(const Src, Dest: String);
- Var
- S : String;
- begin
- S:='StringToJSONString('''+Src+''')='''+Dest+'''';
- AssertEquals(S,Dest,StringToJSONString(Src));
- end;
- procedure TTestJSONString.TestFrom(const Src, Dest: String);
- Var
- S : String;
- begin
- S:='JSONStringToString('''+Src+''')='''+Dest+'''';
- AssertEquals(S,Dest,JSONStringToString(Src));
- end;
- procedure TTestJSONString.TestJSONStringToString;
- begin
- TestFrom('','');
- TestFrom('A','A');
- TestFrom('AB','AB');
- TestFrom('ABC','ABC');
- TestFrom('\\','\');
- TestFrom('\/','/');
- TestFrom('\"','"');
- TestFrom('\b',#8);
- TestFrom('\t',#9);
- TestFrom('\n',#10);
- TestFrom('\f',#12);
- TestFrom('\r',#13);
- TestFrom('\bBC',#8'BC');
- TestFrom('\tBC',#9'BC');
- TestFrom('\nBC',#10'BC');
- TestFrom('\fBC',#12'BC');
- TestFrom('\rBC',#13'BC');
- TestFrom('A\b','A'#8);
- TestFrom('A\t','A'#9);
- TestFrom('A\n','A'#10);
- TestFrom('A\f','A'#12);
- TestFrom('A\r','A'#13);
- TestFrom('A\bBC','A'#8'BC');
- TestFrom('A\tBC','A'#9'BC');
- TestFrom('A\nBC','A'#10'BC');
- TestFrom('A\fBC','A'#12'BC');
- TestFrom('A\rBC','A'#13'BC');
- TestFrom('\\\\','\\');
- TestFrom('\/\/','//');
- TestFrom('\"\"','""');
- TestFrom('\b\b',#8#8);
- TestFrom('\t\t',#9#9);
- TestFrom('\n\n',#10#10);
- TestFrom('\f\f',#12#12);
- TestFrom('\r\r',#13#13);
- end;
- procedure TTestJSONString.TestStringToJSONString;
- begin
- TestTo('','');
- TestTo('A','A');
- TestTo('AB','AB');
- TestTo('ABC','ABC');
- TestTo('\','\\');
- TestTo('/','\/');
- TestTo('"','\"');
- TestTo(#8,'\b');
- TestTo(#9,'\t');
- TestTo(#10,'\n');
- TestTo(#12,'\f');
- TestTo(#13,'\r');
- TestTo(#8'BC','\bBC');
- TestTo(#9'BC','\tBC');
- TestTo(#10'BC','\nBC');
- TestTo(#12'BC','\fBC');
- TestTo(#13'BC','\rBC');
- TestTo('A'#8,'A\b');
- TestTo('A'#9,'A\t');
- TestTo('A'#10,'A\n');
- TestTo('A'#12,'A\f');
- TestTo('A'#13,'A\r');
- TestTo('A'#8'BC','A\bBC');
- TestTo('A'#9'BC','A\tBC');
- TestTo('A'#10'BC','A\nBC');
- TestTo('A'#12'BC','A\fBC');
- TestTo('A'#13'BC','A\rBC');
- TestTo('\\','\\\\');
- TestTo('//','\/\/');
- TestTo('""','\"\"');
- TestTo(#8#8,'\b\b');
- TestTo(#9#9,'\t\t');
- TestTo(#10#10,'\n\n');
- TestTo(#12#12,'\f\f');
- TestTo(#13#13,'\r\r');
- end;
- initialization
- RegisterTest(TTestJSONString);
- RegisterTest(TTestNull);
- RegisterTest(TTestBoolean);
- RegisterTest(TTestInteger);
- RegisterTest(TTestInt64);
- RegisterTest(TTestFloat);
- RegisterTest(TTestString);
- RegisterTest(TTestArray);
- RegisterTest(TTestObject);
- end.
|