utSchemaTypes.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. {
  2. This file is part of the Free Component Library
  3. Testsuite for JSONSchema basic types
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit utSchemaTypes;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, fpcunit, testregistry, fpjson.schema.types, fpjson.schema.testutils;
  16. type
  17. { TTestSimpleTypes }
  18. TTestSimpleTypes = class(TSchemaTestcase)
  19. Public
  20. Published
  21. Procedure TestSchemaKeywordToString;
  22. Procedure TestJSONSubschemaToString;
  23. Procedure TestStringFormatValidatorToString;
  24. Procedure TestSchemaSimpleTypeToString;
  25. end;
  26. implementation
  27. uses typinfo,fpjson.schema.consts;
  28. { TTestSimpleTypes }
  29. procedure TTestSimpleTypes.TestSchemaKeywordToString;
  30. Procedure TestAs(S : String; KW : TJSONSchemaKeyword);
  31. var
  32. tmp : TJSONSchemaKeyword;
  33. begin
  34. // kw -> string
  35. tmp:=KW;
  36. AssertEquals(S,S,tmp.AsString);
  37. // string -> KW
  38. tmp.AsString:=S;
  39. AssertEquals(S,KW,tmp);
  40. end;
  41. begin
  42. TestAs('',jskUnknown);
  43. TestAs('$id',jskId);
  44. TestAs('$anchor',jskAnchor);
  45. TestAs('id',jskIdDraft4);
  46. TestAs('$schema',jskSchema);
  47. TestAs('$defs',jskDefs);
  48. TestAs('title',jskTitle);
  49. TestAs('description',jskDescription);
  50. TestAs('default',jskDefault);
  51. TestAs('multipleOf',jskMultipleOf);
  52. TestAs('maximum',jskMaximum);
  53. TestAs('exclusiveMaximum',jskExclusiveMaximum);
  54. TestAs('minimum',jskMinimum);
  55. TestAs('exclusiveMinimum',jskExclusiveMinimum);
  56. TestAs('maxLength',jskMaxLength);
  57. TestAs('minLength',jskMinLength);
  58. TestAs('pattern',jskPattern);
  59. TestAs('additionalItems',jskAdditionalItems);
  60. TestAs('items',jskItems);
  61. TestAs('prefixItems',jskPrefixItems);
  62. TestAs('maxItems',jskMaxItems);
  63. TestAs('minItems',jskMinItems);
  64. TestAs('uniqueItems',jskUniqueItems);
  65. TestAs('maxProperties',jskMaxProperties);
  66. TestAs('minProperties',jskMinProperties);
  67. TestAs('maxContains',jskMaxContains);
  68. TestAs('minContains',jskMinContains);
  69. TestAs('required',jskRequired);
  70. TestAs('additionalProperties',jskAdditionalProperties);
  71. TestAs('definitions',jskDefinitions);
  72. TestAs('properties',jskProperties);
  73. TestAs('patternProperties',jskPatternProperties);
  74. TestAs('propertyNames',jskPropertyNames);
  75. TestAs('dependentSchemas',jskDependentSchemas);
  76. TestAs('dependentRequired',jskDependentRequired);
  77. TestAs('enum',jskEnum);
  78. TestAs('type',jskType);
  79. TestAs('allOf',jskAllOf);
  80. TestAs('anyOf',jskAnyOf);
  81. TestAs('oneOf',jskOneOf);
  82. TestAs('not',jskNot);
  83. TestAs('format',jskFormat);
  84. TestAs('$ref',jskRef);
  85. TestAs('if',jskIf);
  86. TestAs('else',jskElse);
  87. TestAs('then',jskThen);
  88. TestAs('$dynamicRef',jskDynamicRef);
  89. TestAs('$dynamicAnchor',jskDynamicAnchor);
  90. TestAs('contains',jskContains);
  91. TestAs('$comment',jskComment);
  92. TestAs('const',jskConst);
  93. TestAs('unevaluatedItems',jskUnevaluatedItems);
  94. TestAs('unevaluatedProperties',jskUnevaluatedProperties);
  95. TestAs('contentEncoding',jskContentEncoding);
  96. TestAs('contentMediaType',jskContentMediaType);
  97. TestAs('contentSchema',jskContentSchema);
  98. TestAs('examples',jskExamples);
  99. TestAs('deprecated',jskDeprecated);
  100. TestAs('readOnly',jskReadOnly);
  101. TestAs('writeOnly',jskWriteOnly);
  102. TestAs('$vocabulary',jskVocabulary);
  103. end;
  104. procedure TTestSimpleTypes.TestJSONSubschemaToString;
  105. Procedure TestAs(S : String; sst : TJSONSubschema);
  106. var
  107. tmp : TJSONSubschema;
  108. begin
  109. // subschema -> string
  110. tmp:=sst;
  111. AssertEquals(S,S,tmp.AsString);
  112. // string -> subschema
  113. tmp.AsString:=S;
  114. AssertEquals(S,sst,tmp);
  115. end;
  116. begin
  117. TestAs('not',ssNot);
  118. TestAs('if',ssIf);
  119. TestAs('then',ssThen);
  120. TestAs('else',ssElse);
  121. TestAs('contains',ssContains);
  122. TestAs('unevaluatedItems',ssUnevaluatedItems);
  123. TestAs('unevaluatedProperties',ssUnevaluatedProperties);
  124. TestAs('propertyNames',ssPropertyNames);
  125. end;
  126. procedure TTestSimpleTypes.TestStringFormatValidatorToString;
  127. Procedure TestAs(S : String; sfv : TStringFormatValidator);
  128. var
  129. tmp : TStringFormatValidator;
  130. begin
  131. // validator -> string
  132. tmp:=sfv;
  133. AssertEquals(S,S,tmp.AsString);
  134. // string -> validator
  135. tmp.AsString:=S;
  136. AssertEquals(S,sfv,tmp);
  137. end;
  138. begin
  139. TestAs('date-time',sfvDatetime);
  140. TestAs('date',sfvDate);
  141. TestAs('time',sfvTime);
  142. TestAs('duration',sfvDuration);
  143. TestAs('email',sfvEmail);
  144. TestAs('idn-email',sfvIdnEmail);
  145. TestAs('hostname',sfvHostname);
  146. TestAs('idn-hostname',sfvIdnHostname);
  147. TestAs('ipv4',sfvIPV4);
  148. TestAs('ipv6',sfvIPV6);
  149. TestAs('uri',sfvURI);
  150. TestAs('uri-reference',sfvURIReference);
  151. TestAs('iri',sfvIRI);
  152. TestAs('iri-reference',sfvIRIReference);
  153. TestAs('uuid',sfvUUID);
  154. TestAs('uri-template',sfvURITemplate);
  155. TestAs('json-pointer',sfvJSONPointer);
  156. TestAs('relative-json-pointer',sfvRelativeJSONPointer);
  157. TestAs('regex',sfvRegex);
  158. end;
  159. procedure TTestSimpleTypes.TestSchemaSimpleTypeToString;
  160. Procedure TestAs(S : String; sst : TSchemaSimpleType);
  161. var
  162. tmp : TSchemaSimpleType;
  163. begin
  164. // simpletype -> string
  165. tmp:=sst;
  166. AssertEquals(S,S,tmp.AsString);
  167. // string -> simpletype
  168. tmp.AsString:=S;
  169. AssertEquals(S,sst,tmp);
  170. end;
  171. begin
  172. TestAs('',sstNone);
  173. TestAs('null',sstNull);
  174. TestAs('boolean',sstBoolean);
  175. TestAs('integer',sstInteger);
  176. TestAs('number',sstNumber);
  177. TestAs('string',sstString);
  178. TestAs('array',sstArray);
  179. TestAs('object',sstObject);
  180. TestAs('any',sstAny);
  181. end;
  182. initialization
  183. RegisterTest(TTestSimpleTypes);
  184. end.