123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- unit utcjson;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, testutils, testregistry, System.JSON;
- type
- { TTestJSONObject }
- TTestJSONObject= class(TTestCase)
- private
- FPair: TJSONPair;
- FValue: TJSONValue;
- protected
- procedure SetUp; override;
- procedure TearDown; override;
- Property Value : TJSONValue Read FValue Write FValue;
- Property Pair : TJSONPair Read FPair Write FPair;
- published
- procedure TestHookUp;
- Procedure TestNull;
- Procedure TestParseNull;
- Procedure TestBoolean;
- Procedure TestParseBoolean;
- Procedure TestBoolTrue;
- Procedure TestParseBoolTrue;
- Procedure TestBoolFalse;
- Procedure TestParseBoolFalse;
- Procedure TestString;
- Procedure TestStringNull;
- Procedure TestParseString;
- Procedure TestNumberInt;
- Procedure TestParseNumberInt;
- Procedure TestNumberInt64;
- Procedure TestParseNumberInt64;
- Procedure TestNumberFloat;
- Procedure TestParseNumberFloat;
- Procedure TestJSONPairPair;
- Procedure TestJSONPairStringString;
- Procedure TestJSONPairStringValue;
- Procedure TestJSONPairStringInt;
- Procedure TestJSONPairStringBool;
- Procedure TestJSONPairStringFloat;
- Procedure TestJSONPairStringInt64;
- Procedure TestJSONObjectEmpty;
- Procedure TestJSONObjectOneElement;
- Procedure TestJSONObjectTwoElements;
- Procedure TestJSONParseObjectEmpty;
- Procedure TestJSONParseObjectOneElement;
- Procedure TestJSONParseObjectTwoElements;
- Procedure TestJSONArrayEmpty;
- Procedure TestJSONArrayOneElement;
- Procedure TestJSONArrayTwoElements;
- Procedure TestFindEmptyValue;
- Procedure TestFindObjectName;
- Procedure TestFindObjectIndexName;
- Procedure TestFindObjectNameRecurse;
- Procedure TestFindArrayIndex;
- Procedure TestFindArrayName;
- Procedure TestAsType;
- procedure TestGetValue;
- procedure TestTryGetValue;
- end;
- { TTestJSONPathParser }
- TTestJSONPathParser = class(TTestCase)
- private
- FParser: TJSONPathParser;
- protected
- procedure SetUp; override;
- procedure TearDown; override;
- Procedure AssertEquals(Msg : string; aExpected, aActual : TJSONPathToken); overload;
- Property Parser : TJSONPathParser Read FParser;
- published
- Procedure TestHookup;
- Procedure TestEof;
- Procedure TestName;
- Procedure TestIndexNumeric;
- Procedure TestIndexName;
- Procedure TestDotName;
- end;
- implementation
- uses typinfo;
- procedure TTestJSONObject.TestHookUp;
- begin
- AssertNull('No value',value);
- end;
- procedure TTestJSONObject.TestNull;
- begin
- Value:=TJSONNull.Create;
- AssertEquals('ToString','null',Value.ToString);
- AssertEquals('ToJSON','null',Value.ToJSON);
- AssertEquals('Format','null',Value.Format);
- AssertEquals('Null',True,Value.Null);
- end;
- procedure TTestJSONObject.TestParseNull;
- begin
- Value:=TJSONValue.ParseJSONValue('null');
- AssertEquals('Class',TJSONNull,Value.ClassType);
- end;
- procedure TTestJSONObject.TestBoolean;
- begin
- Value:=TJSONBool.Create(True);
- AssertEquals('ToString','true',Value.ToString);
- AssertEquals('ToJSON','true',Value.ToJSON);
- AssertEquals('Format','true',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestParseBoolean;
- begin
- Value:=TJSONValue.ParseJSONValue('true',False);
- AssertEquals('Class',TJSONBool,Value.ClassType);
- AssertEquals('ToString','true',Value.ToString);
- end;
- procedure TTestJSONObject.TestBoolTrue;
- begin
- Value:=TJSONTrue.Create;
- AssertEquals('ToString','true',Value.ToString);
- AssertEquals('ToJSON','true',Value.ToJSON);
- AssertEquals('Format','true',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestParseBoolTrue;
- begin
- Value:=TJSONValue.ParseJSONValue('true',True);
- AssertEquals('Class',TJSONTrue,Value.ClassType);
- AssertEquals('ToString','true',Value.ToString);
- end;
- procedure TTestJSONObject.TestBoolFalse;
- begin
- Value:=TJSONfalse.Create;
- AssertEquals('ToString','false',Value.ToString);
- AssertEquals('ToJSON','false',Value.ToJSON);
- AssertEquals('Format','false',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestParseBoolFalse;
- begin
- Value:=TJSONValue.ParseJSONValue('false',True);
- AssertEquals('Class',TJSONFalse,Value.ClassType);
- AssertEquals('ToString','false',Value.ToString);
- end;
- procedure TTestJSONObject.TestString;
- begin
- Value:=TJSONString.Create('string');
- AssertEquals('ToString','"string"',Value.ToString);
- AssertEquals('ToJSON','"string"',Value.ToJSON);
- AssertEquals('Format','"string"',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestStringNull;
- begin
- Value:=TJSONString.Create();
- AssertEquals('ToString','null',Value.ToString);
- AssertEquals('Null',True,Value.Null);
- end;
- procedure TTestJSONObject.TestParseString;
- begin
- Value:=TJSONValue.ParseJSONValue('"string"',True);
- AssertEquals('Class',TJSONString,Value.ClassType);
- AssertEquals('ToString','"string"',Value.ToString);
- end;
- procedure TTestJSONObject.TestNumberInt;
- begin
- Value:=TJSONNumber.Create(12);
- AssertEquals('ToString','12',Value.ToString);
- AssertEquals('ToJSON','12',Value.ToJSON);
- AssertEquals('Format','12',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestParseNumberInt;
- begin
- Value:=TJSONValue.ParseJSONValue('12',True);
- AssertEquals('Class',TJSONNumber,Value.ClassType);
- AssertEquals('ToString','12',Value.ToString);
- end;
- procedure TTestJSONObject.TestNumberInt64;
- begin
- Value:=TJSONNumber.Create(Int64(12));
- AssertEquals('ToString','12',Value.ToString);
- AssertEquals('ToJSON','12',Value.ToJSON);
- AssertEquals('Format','12',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestParseNumberInt64;
- begin
- Value:=TJSONValue.ParseJSONValue('1221212121111',True);
- AssertEquals('Class',TJSONNumber,Value.ClassType);
- AssertEquals('ToString','1221212121111',Value.ToString);
- end;
- procedure TTestJSONObject.TestNumberFloat;
- var
- V : String;
- begin
- V:=FloatToStr(12.34,TFormatSettings.Invariant);
- Value:=TJSONNumber.Create(12.34);
- AssertEquals('ToString',V,Value.ToString);
- AssertEquals('ToJSON',V,Value.ToJSON);
- AssertEquals('Format',V,Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestParseNumberFloat;
- var
- V : String;
- begin
- V:=FloatToStr(12.34,TFormatSettings.Invariant);
- Value:=TJSONValue.ParseJSONValue('12.34',True);
- AssertEquals('Class',TJSONNumber,Value.ClassType);
- AssertEquals('ToString',V,Value.ToString);
- end;
- procedure TTestJSONObject.TestJSONPairPair;
- begin
- Pair:=TJSONPair.Create(TJSONString.Create('a'),TJSONNumber.Create('12'));
- AssertEquals('ToString','"a":12',Pair.ToString);
- AssertEquals('ToJSON','"a":12',Pair.ToJSON);
- AssertEquals('Format','"a":12',Pair.Format);
- AssertEquals('Null',False,Pair.Null);
- end;
- procedure TTestJSONObject.TestJSONPairStringString;
- begin
- Pair:=TJSONPair.Create('a','b');
- AssertEquals('ToString','"a":"b"',Pair.ToString);
- AssertEquals('ToJSON','"a":"b"',Pair.ToJSON);
- AssertEquals('Format','"a":"b"',Pair.Format);
- AssertEquals('Null',False,Pair.Null);
- end;
- procedure TTestJSONObject.TestJSONPairStringValue;
- begin
- Pair:=TJSONPair.Create('a',TJSONString.Create('b'));
- AssertEquals('ToString','"a":"b"',Pair.ToString);
- AssertEquals('ToJSON','"a":"b"',Pair.ToJSON);
- AssertEquals('Format','"a":"b"',Pair.Format);
- AssertEquals('Null',False,Pair.Null);
- end;
- procedure TTestJSONObject.TestJSONPairStringInt;
- begin
- Pair:=TJSONPair.Create('a',1);
- AssertEquals('ToString','"a":1',Pair.ToString);
- AssertEquals('ToJSON','"a":1',Pair.ToJSON);
- AssertEquals('Format','"a":1',Pair.Format);
- AssertEquals('Null',False,Pair.Null);
- end;
- procedure TTestJSONObject.TestJSONPairStringBool;
- begin
- Pair:=TJSONPair.Create('a',True);
- AssertEquals('ToString','"a":true',Pair.ToString);
- AssertEquals('ToJSON','"a":true',Pair.ToJSON);
- AssertEquals('Format','"a":true',Pair.Format);
- AssertEquals('Null',False,Pair.Null);
- end;
- procedure TTestJSONObject.TestJSONPairStringFloat;
- var
- V : String;
- begin
- V:=FloatToStr(12.34,TFormatSettings.Invariant);
- Pair:=TJSONPair.Create('a',12.34);
- AssertEquals('ToString','"a":'+V,Pair.ToString);
- AssertEquals('ToJSON','"a":'+V,Pair.ToJSON);
- AssertEquals('Format','"a":'+V,Pair.Format);
- AssertEquals('Null',False,Pair.Null);
- end;
- procedure TTestJSONObject.TestJSONPairStringInt64;
- begin
- Pair:=TJSONPair.Create('a',1221212121111);
- AssertEquals('ToString','"a":1221212121111',Pair.ToString);
- AssertEquals('ToJSON','"a":1221212121111',Pair.ToJSON);
- AssertEquals('Format','"a":1221212121111',Pair.Format);
- AssertEquals('Null',False,Pair.Null);
- end;
- procedure TTestJSONObject.TestJSONObjectEmpty;
- begin
- Value:=TJSONObject.Create;
- AssertEquals('ToString','{}',Value.ToString);
- AssertEquals('ToJSON','{}',Value.ToJSON);
- AssertEquals('Format','{'+sLineBreak+'}',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestJSONObjectOneElement;
- begin
- Value:=TJSONObject.Create(TJSONPair.Create('a','b'));
- AssertEquals('ToString','{"a":"b"}',Value.ToString);
- AssertEquals('ToJSON','{"a":"b"}',Value.ToJSON);
- AssertEquals('Format','{'+sLineBreak+' "a":"b"'+sLineBreak+'}',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestJSONObjectTwoElements;
- var
- O : TJSONObject;
- begin
- O:=TJSONObject.Create(TJSONPair.Create('a','b'));
- O.AddPair('c','d');
- AssertEquals('Count',2,O.Count);
- Value:=O;
- AssertEquals('ToString','{"a":"b","c":"d"}',Value.ToString);
- AssertEquals('ToJSON','{"a":"b","c":"d"}',Value.ToJSON);
- AssertEquals('Format','{'+sLineBreak+
- ' "a":"b",'+sLineBreak+
- ' "c":"d"'+sLineBreak+'}',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestJSONParseObjectEmpty;
- begin
- Value:=TJSONValue.ParseJSONValue('{}',True);
- AssertEquals('Class',TJSONObject,Value.ClassType);
- AssertEquals('ToString','{}',Value.ToString);
- end;
- procedure TTestJSONObject.TestJSONParseObjectOneElement;
- begin
- Value:=TJSONValue.ParseJSONValue('{"a":"b"}',True);
- AssertEquals('Class',TJSONObject,Value.ClassType);
- AssertEquals('ToString','{"a":"b"}',Value.ToString);
- end;
- procedure TTestJSONObject.TestJSONParseObjectTwoElements;
- begin
- Value:=TJSONValue.ParseJSONValue('{"a":"b","c":"d"}',True);
- AssertEquals('Class',TJSONObject,Value.ClassType);
- AssertEquals('ToString','{"a":"b","c":"d"}',Value.ToString);
- end;
- procedure TTestJSONObject.TestJSONArrayEmpty;
- begin
- Value:=TJSONArray.Create;
- AssertEquals('ToString','[]',Value.ToString);
- AssertEquals('ToJSON','[]',Value.ToJSON);
- AssertEquals('Format','['+sLineBreak+']',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestJSONArrayOneElement;
- begin
- Value:=TJSONArray.Create(TJSONNumber.Create(1));
- AssertEquals('ToString','[1]',Value.ToString);
- AssertEquals('ToJSON','[1]',Value.ToJSON);
- AssertEquals('Format','['+sLineBreak+' 1'+sLineBreak+']',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestJSONArrayTwoElements;
- begin
- Value:=TJSONArray.Create(TJSONNumber.Create(1),TJSONNumber.Create(2));
- AssertEquals('ToString','[1,2]',Value.ToString);
- AssertEquals('ToJSON','[1,2]',Value.ToJSON);
- AssertEquals('Format','['+sLineBreak+
- ' 1,'+sLineBreak+
- ' 2'+sLineBreak+
- ']',Value.Format);
- AssertEquals('Null',False,Value.Null);
- end;
- procedure TTestJSONObject.TestFindEmptyValue;
- begin
- FValue:=TJSONString.Create('Name');
- AssertSame('Empty returns same',FValue,FValue.FindValue(''));
- end;
- procedure TTestJSONObject.TestFindObjectName;
- var
- V : TJSONValue;
- begin
- FValue:=TJSONObject.Create(TJSONPair.Create('a','b'));
- V:=FValue.FindValue('a');
- AssertNotNull('Have JSON value',V);
- AssertEquals('Have correct value','b',V.Value);
- V:=FValue.FindValue('c');
- AssertNull('Have no JSON value',V);
- end;
- procedure TTestJSONObject.TestFindObjectIndexName;
- var
- V : TJSONValue;
- begin
- FValue:=TJSONObject.Create(TJSONPair.Create('a','b'));
- V:=FValue.FindValue('["a"]');
- AssertNotNull('Have JSON value',V);
- AssertEquals('Have correct value','b',V.Value);
- V:=FValue.FindValue('c');
- AssertNull('Have no JSON value',V);
- end;
- procedure TTestJSONObject.TestFindObjectNameRecurse;
- var
- V : TJSONValue;
- begin
- V:=TJSONObject.Create(TJSONPair.Create('b','c'));
- FValue:=TJSONObject.Create(TJSONPair.Create('a',v));
- V:=FValue.FindValue('a.b');
- AssertNotNull('Have JSON value',V);
- AssertEquals('Have correct value','c',V.Value);
- V:=FValue.FindValue('a.c');
- AssertNull('Have no JSON value',V);
- end;
- procedure TTestJSONObject.TestFindArrayIndex;
- var
- V : TJSONValue;
- begin
- FValue:=TJSONArray.Create('a','b');
- V:=FValue.FindValue('[0]');
- AssertNotNull('Have JSON value',V);
- AssertEquals('Have correct value','a',V.Value);
- V:=FValue.FindValue('[1]');
- AssertNotNull('Have JSON value',V);
- AssertEquals('Have correct value','b',V.Value);
- V:=FValue.FindValue('[-1]');
- AssertNull('Have no JSON value -1',V);
- V:=FValue.FindValue('[2]');
- AssertNull('Have no JSON value 2',V);
- end;
- procedure TTestJSONObject.TestFindArrayName;
- var
- V : TJSONValue;
- begin
- FValue:=TJSONArray.Create('a','b');
- V:=FValue.FindValue('a');
- AssertNull('Have no JSON value',V);
- end;
- procedure TTestJSONObject.TestAsType;
- begin
- Value:=TJSONObject.ParseJSONValue('{ "a" : "b" }');
- AssertEquals('Correct class',TJSONObject.ClassName,(Value.specialize AsType<TJSONObject>()).ClassName);
- end;
- procedure TTestJSONObject.TestGetValue;
- begin
- Value:=TJSONObject.ParseJSONValue('{ "a" : "b" }');
- AssertEquals('Correct value','b',Value. specialize GetValue<String>('a'));
- end;
- procedure TTestJSONObject.TestTryGetValue;
- var
- S : String;
- begin
- Value:=TJSONObject.ParseJSONValue('{ "a" : "b" }');
- AssertTrue('Can get value', Value. specialize TryGetValue<String>('a',S));
- AssertEquals('Correct value','b',S);
- end;
- (*
- {$mode objfpc}
- {$h+}
- var
- V : TJSONValue;
- S : String;
- begin
- V:=TJSONObject.ParseJSONValue('{ "a" : "b" }');
- Writeln(V.specialize TryGetValue<String>('a',S));
- Writeln(S);
- Writeln(V.specialize GetValue<String>('a'));
- *)
- procedure TTestJSONObject.SetUp;
- begin
- FreeAndNil(Fvalue);
- FreeAndNil(FPair);
- end;
- procedure TTestJSONObject.TearDown;
- begin
- FreeAndNil(FPair);
- FreeAndNil(Fvalue);
- end;
- { TTestJSONPathParser }
- procedure TTestJSONPathParser.SetUp;
- begin
- inherited SetUp;
- FParser:=Default(TJSONPathParser);
- end;
- procedure TTestJSONPathParser.TearDown;
- begin
- FParser:=Default(TJSONPathParser);
- inherited TearDown;
- end;
- procedure TTestJSONPathParser.AssertEquals(Msg: string; aExpected, aActual: TJSONPathToken);
- begin
- AssertEquals(Msg,GetEnumName(TypeInfo(TJSONPathParser.TToken),Ord(aExpected)),
- GetEnumName(TypeInfo(TJSONPathParser.TToken),Ord(aActual)));
- end;
- procedure TTestJSONPathParser.TestHookup;
- begin
- AssertEquals('Default', TJSONPathToken.UnDefined, FParser.Token);
- end;
- procedure TTestJSONPathParser.TestEof;
- begin
- FParser:=TJSONPathParser.Create('');
- AssertEquals('Empty is eof ',TJSONPathToken.Eof,FParser.NextToken);
- AssertTrue('eof',FParser.IsEOF);
- end;
- procedure TTestJSONPathParser.TestName;
- begin
- FParser:=TJSONPathParser.Create('name');
- AssertEquals('Type',TJSONPathToken.Name,FParser.NextToken);
- AssertEquals('Value','name',FParser.TokenName);
- AssertTrue('eof',FParser.IsEOF);
- end;
- procedure TTestJSONPathParser.TestIndexNumeric;
- begin
- FParser:=TJSONPathParser.Create('[12]');
- AssertEquals('Type',TJSONPathToken.ArrayIndex,FParser.NextToken);
- AssertEquals('Value',12,FParser.TokenArrayIndex);
- AssertTrue('eof',FParser.IsEOF);
- end;
- procedure TTestJSONPathParser.TestIndexName;
- begin
- FParser:=TJSONPathParser.Create('["name"]');
- AssertEquals('Type',TJSONPathToken.name,FParser.NextToken);
- AssertEquals('Value','name',FParser.TokenName);
- AssertTrue('eof',FParser.IsEOF);
- end;
- procedure TTestJSONPathParser.TestDotName;
- begin
- FParser:=TJSONPathParser.Create('.name');
- AssertEquals('Type',TJSONPathToken.Name,FParser.NextToken);
- AssertEquals('Value','name',FParser.TokenName);
- AssertTrue('eof',FParser.IsEOF);
- end;
- initialization
- RegisterTests([TTestJSONObject,TTestJSONPathParser]);
- end.
|