testbrookutils.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. unit testbrookutils;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. BrookUtils, BrookConsts, fpcunit, testregistry, sysutils, Classes, dateutils,
  6. typinfo;
  7. type
  8. TMyEnum = (enum1, enum2, enum3);
  9. TMySet = set of TMyEnum;
  10. { TMyType }
  11. TMyType = class
  12. private
  13. FMyBoolean: Boolean;
  14. FMyChar: Char;
  15. FMyCurrency: Currency;
  16. FMyDateTime: TDateTime;
  17. FMyEnum: TMyEnum;
  18. FMyFloat: Double;
  19. FMyInt64: Int64;
  20. FMyInteger: Integer;
  21. FMySet: TMySet;
  22. FMyString: string;
  23. published
  24. property MyChar: Char read FMyChar write FMyChar;
  25. property MyString: string read FMyString write FMyString;
  26. property MyInteger: Integer read FMyInteger write FMyInteger;
  27. property MyInt64: Int64 read FMyInt64 write FMyInt64;
  28. property MyFloat: Double read FMyFloat write FMyFloat;
  29. property MyCurrency: Currency read FMyCurrency write FMyCurrency;
  30. property MyBoolean: Boolean read FMyBoolean write FMyBoolean;
  31. property MyDateTime: TDateTime read FMyDateTime write FMyDateTime;
  32. property MyEnum: TMyEnum read FMyEnum write FMyEnum;
  33. property MySet: TMySet read FMySet write FMySet;
  34. end;
  35. { TTestBrookUtils }
  36. TTestBrookUtils = class(TTestCase)
  37. published
  38. procedure TestStartsChar;
  39. procedure TestEndsChar;
  40. procedure TestExtractPathLevels;
  41. procedure TestGetPathLevel;
  42. procedure TestGetPathLevels;
  43. procedure TestMacthMethod;
  44. procedure TestFileDate;
  45. procedure TestExcludeTrailingUrlDelimiter;
  46. procedure TestIncludeTrailingUrlDelimiter;
  47. procedure TestExists;
  48. procedure TestStringToObject;
  49. procedure TestStringsToObject;
  50. procedure TestObjectToString;
  51. procedure TestObjectToStrings;
  52. procedure TestCopyObject;
  53. end;
  54. implementation
  55. { TTestBrookUtils }
  56. procedure TTestBrookUtils.TestStartsChar;
  57. begin
  58. AssertTrue(BrookStartsChar('a', 'abc'));
  59. end;
  60. procedure TTestBrookUtils.TestEndsChar;
  61. begin
  62. AssertTrue(BrookEndsChar('c', 'abc'));
  63. end;
  64. procedure TTestBrookUtils.TestExtractPathLevels;
  65. var
  66. s, r, lv: string;
  67. ed: Boolean = False;
  68. begin
  69. s := '/a/b/c/';
  70. r := '';
  71. lv := '';
  72. BrookExtractPathLevels(s, r, lv, ed);
  73. AssertEquals('a', r);
  74. AssertEquals('a', lv);
  75. AssertTrue(ed);
  76. BrookExtractPathLevels(s, r, lv, ed);
  77. AssertEquals('a/b', r);
  78. AssertEquals('b', lv);
  79. BrookExtractPathLevels(s, r, lv, ed);
  80. AssertEquals('a/b/c', r);
  81. AssertEquals('c', lv);
  82. end;
  83. procedure TTestBrookUtils.TestGetPathLevel;
  84. begin
  85. AssertEquals('b', BrookGetPathLevel('/a/b/c/', 1));
  86. end;
  87. procedure TTestBrookUtils.TestGetPathLevels;
  88. begin
  89. AssertEquals('b/c/', BrookGetPathLevels('/a/b/c/', 1));
  90. end;
  91. procedure TTestBrookUtils.TestMacthMethod;
  92. begin
  93. AssertTrue(BrookMatchMethod(rmPost, 'POST'));
  94. end;
  95. procedure TTestBrookUtils.TestFileDate;
  96. var
  97. f: TStringList;
  98. fn, VPath: string;
  99. begin
  100. f := TStringList.Create;
  101. try
  102. VPath := ExtractFilePath(ParamStr(0));
  103. fn := VPath + 'file';
  104. f.SaveToFile(fn);
  105. AssertEquals(Trunc(Now), Trunc(BrookFileDate(fn)));
  106. DeleteFile(fn);
  107. finally
  108. f.Free;
  109. end;
  110. end;
  111. procedure TTestBrookUtils.TestExcludeTrailingUrlDelimiter;
  112. begin
  113. AssertEquals('', BrookExcludeTrailingUrlDelimiter(''));
  114. AssertEquals('', BrookExcludeTrailingUrlDelimiter('/'));
  115. AssertEquals('http://localhost',
  116. BrookExcludeTrailingUrlDelimiter('http://localhost/'));
  117. end;
  118. procedure TTestBrookUtils.TestIncludeTrailingUrlDelimiter;
  119. begin
  120. AssertEquals('/', BrookIncludeTrailingUrlDelimiter(''));
  121. AssertEquals('/', BrookIncludeTrailingUrlDelimiter('/'));
  122. AssertEquals('http://localhost/',
  123. BrookIncludeTrailingUrlDelimiter('http://localhost'));
  124. end;
  125. procedure TTestBrookUtils.TestExists;
  126. begin
  127. AssertTrue(BrookExists('123', ['abc', '123', 'abc123']));
  128. AssertTrue(BrookExists('Abc', ['ABC', '123', 'abc123'], True));
  129. end;
  130. procedure TTestBrookUtils.TestStringToObject;
  131. var
  132. i: Integer;
  133. n, v: string;
  134. o: TMyType;
  135. dt: TDateTime;
  136. st: TStringList;
  137. begin
  138. o := TMyType.Create;
  139. st := TStringList.Create;
  140. try
  141. dt := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
  142. st.Add('MyChar=A');
  143. st.Add('MyString=ABC123');
  144. st.Add('MyInteger=123');
  145. st.Add('MyInt64=456');
  146. st.Add('MyFloat=' + FloatToStr(123.456));
  147. st.Add('MyCurrency=' + CurrToStr(456.789));
  148. st.Add('MyBoolean=on');
  149. st.Add('MyDateTime=' + DateTimeToStr(dt));
  150. st.Add('MyEnum=enum2');
  151. st.Add('MySet=[enum1,enum3]');
  152. for i := 0 to Pred(st.Count) do
  153. begin
  154. st.GetNameValue(i, n, v);
  155. BrookStringToObject(o, n, v);
  156. end;
  157. AssertEquals(o.MyChar, 'A');
  158. AssertEquals(o.MyString, 'ABC123');
  159. AssertEquals(o.MyInteger, 123);
  160. AssertEquals(o.MyInt64, 456);
  161. AssertEquals(o.MyFloat, 123.456);
  162. AssertEquals(o.MyCurrency, 456.789);
  163. AssertEquals(o.MyBoolean, True);
  164. AssertEquals(o.MyDateTime, dt);
  165. AssertTrue(o.MyEnum = enum2);
  166. AssertTrue(o.MySet = [enum1, enum3]);
  167. finally
  168. o.Free;
  169. st.Free;
  170. end;
  171. end;
  172. procedure TTestBrookUtils.TestStringsToObject;
  173. var
  174. o: TMyType;
  175. dt: TDateTime;
  176. st: TStringList;
  177. begin
  178. o := TMyType.Create;
  179. st := TStringList.Create;
  180. try
  181. dt := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
  182. st.Add('MyChar=A');
  183. st.Add('MyString=ABC123');
  184. st.Add('MyInteger=123');
  185. st.Add('MyInt64=456');
  186. st.Add('MyFloat=' + FloatToStr(123.456));
  187. st.Add('MyCurrency=' + CurrToStr(456.789));
  188. st.Add('MyBoolean=on');
  189. st.Add('MyDateTime=' + DateTimeToStr(dt));
  190. st.Add('MyEnum=enum2');
  191. st.Add('MySet=[enum1,enum3]');
  192. BrookStringsToObject(o, st);
  193. AssertEquals(o.MyChar, 'A');
  194. AssertEquals(o.MyString, 'ABC123');
  195. AssertEquals(o.MyInteger, 123);
  196. AssertEquals(o.MyInt64, 456);
  197. AssertEquals(o.MyFloat, 123.456);
  198. AssertEquals(o.MyCurrency, 456.789);
  199. AssertEquals(o.MyBoolean, True);
  200. AssertEquals(o.MyDateTime, dt);
  201. AssertTrue(o.MyEnum = enum2);
  202. AssertTrue(o.MySet = [enum1, enum3]);
  203. st.Values['MyChar'] := 'B';
  204. st.Values['MyInteger'] := '456';
  205. st.Values['MyInt64'] := '789';
  206. o.MyInt64 := 0;
  207. BrookStringsToObject(o, st, ['myint64']);
  208. AssertEquals(o.MyChar, 'B');
  209. AssertEquals(o.MyInteger, 456);
  210. AssertEquals(o.MyInt64, 0);
  211. finally
  212. o.Free;
  213. st.Free;
  214. end;
  215. end;
  216. procedure TTestBrookUtils.TestObjectToString;
  217. var
  218. v: string;
  219. dt: TDateTime;
  220. o: TMyType;
  221. st: TStringList;
  222. i, c: Integer;
  223. pi: PPropInfo;
  224. pl: PPropList = nil;
  225. begin
  226. o := TMyType.Create;
  227. st := TStringList.Create;
  228. c := GetPropList(o, pl);
  229. try
  230. dt := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
  231. o.MyChar := 'A';
  232. o.MyString := 'ABC123';
  233. o.MyInteger :=123;
  234. o.MyInt64 := 456;
  235. o.MyFloat := 123.456;
  236. o.MyCurrency := 456.789;
  237. o.MyBoolean := True;
  238. o.MyDateTime := dt;
  239. o.MyEnum := enum2;
  240. o.MySet := [enum1, enum3];
  241. if Assigned(pl) then
  242. try
  243. for i := 0 to Pred(c) do
  244. begin
  245. pi := pl^[i];
  246. BrookObjectToString(o, pi, v);
  247. st.Add(pi^.Name + '=' + v);
  248. end;
  249. finally
  250. FreeMem(pl);
  251. end;
  252. AssertEquals(st.ValueFromIndex[0], 'A');
  253. AssertEquals(st.ValueFromIndex[1], 'ABC123');
  254. AssertEquals(st.ValueFromIndex[2], '123');
  255. AssertEquals(st.ValueFromIndex[3], '456');
  256. AssertEquals(st.ValueFromIndex[4], FloatToStr(123.456));
  257. AssertEquals(st.ValueFromIndex[5], CurrToStr(456.789));
  258. AssertEquals(st.ValueFromIndex[6], 'True');
  259. AssertEquals(st.ValueFromIndex[7], DateTimeToStr(dt));
  260. AssertEquals(st.ValueFromIndex[8], 'enum2');
  261. AssertEquals(st.ValueFromIndex[9], 'enum1,enum3');
  262. finally
  263. o.Free;
  264. st.Free;
  265. end;
  266. end;
  267. procedure TTestBrookUtils.TestObjectToStrings;
  268. var
  269. dt: TDateTime;
  270. o: TMyType;
  271. st: TStringList;
  272. begin
  273. o := TMyType.Create;
  274. st := TStringList.Create;
  275. try
  276. dt := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
  277. o.MyChar := 'A';
  278. o.MyString := 'ABC123';
  279. o.MyInteger :=123;
  280. o.MyInt64 := 456;
  281. o.MyFloat := 123.456;
  282. o.MyCurrency := 456.789;
  283. o.MyBoolean := True;
  284. o.MyDateTime := dt;
  285. o.MyEnum := enum2;
  286. o.MySet := [enum1, enum3];
  287. BrookObjectToStrings(o, st);
  288. AssertEquals(st.ValueFromIndex[0], 'A');
  289. AssertEquals(st.ValueFromIndex[1], 'ABC123');
  290. AssertEquals(st.ValueFromIndex[2], '123');
  291. AssertEquals(st.ValueFromIndex[3], '456');
  292. AssertEquals(st.ValueFromIndex[4], FloatToStr(123.456));
  293. AssertEquals(st.ValueFromIndex[5], CurrToStr(456.789));
  294. AssertEquals(st.ValueFromIndex[6], 'True');
  295. AssertEquals(st.ValueFromIndex[7], DateTimeToStr(dt));
  296. AssertEquals(st.ValueFromIndex[8], 'enum2');
  297. AssertEquals(st.ValueFromIndex[9], 'enum1,enum3');
  298. st.Clear;
  299. o.MyChar := 'B';
  300. o.MyInteger := 456;
  301. o.MyInt64 := 789;
  302. st.Values['MyInt64'] := '0';
  303. BrookObjectToStrings(o, st, ['myint64']);
  304. AssertEquals(st.Values['MyChar'], 'B');
  305. AssertEquals(st.Values['MyInteger'], '456');
  306. AssertEquals(st.Values['MyInt64'], '0');
  307. finally
  308. o.Free;
  309. st.Free;
  310. end;
  311. end;
  312. procedure TTestBrookUtils.TestCopyObject;
  313. var
  314. o1, o2: TMyType;
  315. begin
  316. o1 := TMyType.Create;
  317. o2 := TMyType.Create;
  318. try
  319. o1.MyChar := 'A';
  320. o1.MyString := 'ABC123';
  321. o1.MyInteger :=123;
  322. o1.MyInt64 := 456;
  323. o1.MyFloat := 123.456;
  324. o1.MyCurrency := 456.789;
  325. o1.MyBoolean := True;
  326. o1.MyDateTime := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
  327. o1.MyEnum := enum2;
  328. o1.MySet := [enum1, enum3];
  329. BrookCopyObject(o1, o2);
  330. AssertEquals(o1.MyChar, o2.MyChar);
  331. AssertEquals(o1.MyString, o2.MyString);
  332. AssertEquals(o1.MyInteger, o2.MyInteger);
  333. AssertEquals(o1.MyInt64, o2.MyInt64);
  334. AssertEquals(o1.MyFloat, o2.MyFloat);
  335. AssertEquals(o1.MyCurrency, o2.MyCurrency);
  336. AssertEquals(o1.MyBoolean, o2.MyBoolean);
  337. AssertEquals(o1.MyDateTime, o2.MyDateTime);
  338. AssertTrue(o1.MyEnum = o2.MyEnum);
  339. AssertTrue(o1.MySet = o2.MySet);
  340. o1.MyChar := 'B';
  341. o1.MyInteger := 456;
  342. o1.MyInt64 := 789;
  343. o2.MyInt64 := 0;
  344. BrookCopyObject(o1, o2, ['myint64']);
  345. AssertEquals(o2.MyChar, 'B');
  346. AssertEquals(o2.MyInteger, 456);
  347. AssertEquals(o2.MyInt64, 0);
  348. finally
  349. o1.Free;
  350. o2.Free;
  351. end;
  352. end;
  353. initialization
  354. RegisterTest(TTestBrookUtils);
  355. end.