123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956 |
- {
- 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;
- end;
-
- { TTestBoolean }
- TTestBoolean = class(TTestJSON)
- published
- procedure TestTrue;
- procedure TestFalse;
- end;
-
- { TTestInteger }
- TTestInteger = class(TTestJSON)
- Private
- Procedure DoTest(I : Integer);
- published
- procedure TestPositive;
- procedure TestNegative;
- procedure TestZero;
- end;
- { TTestInt64 }
- TTestInt64 = class(TTestJSON)
- Private
- Procedure DoTest(I : Int64);
- published
- procedure TestPositive;
- procedure TestNegative;
- procedure TestZero;
- end;
-
- { TTestFloat }
- TTestFloat = class(TTestJSON)
- Private
- Procedure DoTest(F : TJSONFloat);
- published
- procedure TestPositive;
- procedure TestNegative;
- procedure TestZero;
- 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;
- 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;
- 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;
- 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;
- { 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;
- { 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.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;
- { 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;
- { 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;
- { 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;
- { 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.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.
|