fpjson.schema.testutils.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. unit fpjson.schema.testutils;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, contnrs,fpcunit, fpjson, fpjson.schema.types;
  6. type
  7. { TTestDef }
  8. TTestDef = Class(TObject)
  9. private
  10. FFileName : String;
  11. FDescription: string;
  12. FSchema: TJSONData;
  13. Public
  14. constructor create (const aDescription : String; aSchema : TJSONData);
  15. destructor Destroy; override;
  16. Property FileName : String Read FFileName Write FFileName;
  17. Property Description : string read FDescription;
  18. Property Schema : TJSONData Read FSchema;
  19. end;
  20. { TTestDefs }
  21. TTestDefs = Class(TFPObjectList)
  22. private
  23. function GetDef(aIndex : Integer): TTestDef;
  24. Public
  25. Property Defs[aIndex : Integer] : TTestDef Read GetDef; default;
  26. end;
  27. TSchemaTestCase = class(TTestCase)
  28. Public
  29. class Procedure AssertEquals(aMsg : String; aExpected,aActual : TJSONSchemaKeyword); overload;
  30. class Procedure AssertEquals(aMsg : String; aExpected,aActual : TJSONSubSchema); overload;
  31. class Procedure AssertEquals(aMsg : String; aExpected,aActual : TStringFormatValidator); overload;
  32. class Procedure AssertEquals(aMsg : String; aExpected,aActual : TSchemaSimpleType); overload;
  33. end;
  34. Function ExtractTestsFromStream(aStream : TStream; aList : TTestDefs; const aFileName : string = '') : Integer;
  35. Function ExtractTestsFromFile(const aFileName : String; aList : TTestDefs) : Integer;
  36. implementation
  37. uses typinfo;
  38. function GetTestDef(aObj : TJSONObject) : TTestDef;
  39. var
  40. Descr : String;
  41. Schema : TJSONData;
  42. begin
  43. Descr:=aObj.Get('description','');
  44. Schema:=aObj.Extract('schema');
  45. if Schema<>Nil then
  46. Result:=TTestDef.Create(Descr,Schema);
  47. end;
  48. function ExtractTestsFromStream(aStream: TStream; aList: TTestDefs; const aFileName : string = ''): Integer;
  49. var
  50. D : TJSONData;
  51. Enum : TJSONEnum;
  52. O : TJSONObject absolute D;
  53. A : TJSONArray absolute D;
  54. Def : TTestDef;
  55. begin
  56. Result:=0;
  57. D:=GetJSON(aStream);
  58. try
  59. if D is TJSONArray then
  60. For Enum in A do
  61. if Enum.Value is TJSONObject then
  62. begin
  63. Def:=GetTestDef(TJSONObject(Enum.Value));
  64. if Assigned(Def) then
  65. begin
  66. Def.FileName:=aFileName;
  67. inc(Result);
  68. aList.Add(Def);
  69. end;
  70. end;
  71. if D is TJSONObject then
  72. begin
  73. Def:=GetTestDef(O);
  74. if Assigned(Def) then
  75. begin
  76. inc(Result);
  77. Def.FileName:=aFileName;
  78. aList.Add(Def);
  79. end;
  80. end;
  81. finally
  82. D.Free;
  83. end;
  84. end;
  85. function ExtractTestsFromFile(const aFileName : String; aList: TTestDefs): Integer;
  86. var
  87. F : TFileStream;
  88. begin
  89. F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
  90. try
  91. Result:=ExtractTestsFromStream(F,aList,aFileName);
  92. finally
  93. F.Free;
  94. end;
  95. end;
  96. { TTestDef }
  97. constructor TTestDef.create(const aDescription: String; aSchema: TJSONData);
  98. begin
  99. FDescription:=aDescription;
  100. FSchema:=aSchema;
  101. end;
  102. destructor TTestDef.Destroy;
  103. begin
  104. FreeAndNil(FSchema);
  105. inherited Destroy;
  106. end;
  107. { TTestDefs }
  108. function TTestDefs.GetDef(aIndex : Integer): TTestDef;
  109. begin
  110. Result:=Items[aIndex] as TTestDef;
  111. end;
  112. { TSchemaTestCase }
  113. class procedure TSchemaTestCase.AssertEquals(aMsg: String; aExpected, aActual: TJSONSchemaKeyword);
  114. begin
  115. AssertEquals(aMsg,GetEnumName(TypeInfo(TJSONSchemaKeyword),Ord(aExpected)),
  116. GetEnumName(TypeInfo(TJSONSchemaKeyword),Ord(aActual)));
  117. end;
  118. class procedure TSchemaTestCase.AssertEquals(aMsg: String; aExpected, aActual: TJSONSubSchema);
  119. begin
  120. AssertEquals(aMsg,GetEnumName(TypeInfo(TJSONSubSchema),Ord(aExpected)),
  121. GetEnumName(TypeInfo(TJSONSubSchema),Ord(aActual)));
  122. end;
  123. class procedure TSchemaTestCase.AssertEquals(aMsg: String; aExpected, aActual: TStringFormatValidator);
  124. begin
  125. AssertEquals(aMsg,GetEnumName(TypeInfo(TStringFormatValidator),Ord(aExpected)),
  126. GetEnumName(TypeInfo(TStringFormatValidator),Ord(aActual)));
  127. end;
  128. class procedure TSchemaTestCase.AssertEquals(aMsg: String; aExpected, aActual: TSchemaSimpleType);
  129. begin
  130. AssertEquals(aMsg,GetEnumName(TypeInfo(TSchemaSimpleType),Ord(aExpected)),
  131. GetEnumName(TypeInfo(TSchemaSimpleType),Ord(aActual)));
  132. end;
  133. end.