| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- unit testbrookutils;
- {$mode objfpc}{$H+}
- interface
- uses
- BrookUtils, BrookConsts, fpcunit, testregistry, sysutils, Classes, dateutils,
- typinfo;
- type
- TMyEnum = (enum1, enum2, enum3);
- TMySet = set of TMyEnum;
- { TMyType }
- TMyType = class
- private
- FMyBoolean: Boolean;
- FMyChar: Char;
- FMyCurrency: Currency;
- FMyDateTime: TDateTime;
- FMyEnum: TMyEnum;
- FMyFloat: Double;
- FMyInt64: Int64;
- FMyInteger: Integer;
- FMySet: TMySet;
- FMyString: string;
- published
- property MyChar: Char read FMyChar write FMyChar;
- property MyString: string read FMyString write FMyString;
- property MyInteger: Integer read FMyInteger write FMyInteger;
- property MyInt64: Int64 read FMyInt64 write FMyInt64;
- property MyFloat: Double read FMyFloat write FMyFloat;
- property MyCurrency: Currency read FMyCurrency write FMyCurrency;
- property MyBoolean: Boolean read FMyBoolean write FMyBoolean;
- property MyDateTime: TDateTime read FMyDateTime write FMyDateTime;
- property MyEnum: TMyEnum read FMyEnum write FMyEnum;
- property MySet: TMySet read FMySet write FMySet;
- end;
- { TTestBrookUtils }
- TTestBrookUtils = class(TTestCase)
- published
- procedure TestStartsChar;
- procedure TestEndsChar;
- procedure TestExtractPathLevels;
- procedure TestGetPathLevel;
- procedure TestGetPathLevels;
- procedure TestMacthMethod;
- procedure TestFileDate;
- procedure TestExcludeTrailingUrlDelimiter;
- procedure TestIncludeTrailingUrlDelimiter;
- procedure TestExists;
- procedure TestStringToObject;
- procedure TestStringsToObject;
- procedure TestObjectToString;
- procedure TestObjectToStrings;
- procedure TestCopyObject;
- end;
- implementation
- { TTestBrookUtils }
- procedure TTestBrookUtils.TestStartsChar;
- begin
- AssertTrue(BrookStartsChar('a', 'abc'));
- end;
- procedure TTestBrookUtils.TestEndsChar;
- begin
- AssertTrue(BrookEndsChar('c', 'abc'));
- end;
- procedure TTestBrookUtils.TestExtractPathLevels;
- var
- s, r, lv: string;
- ed: Boolean = False;
- begin
- s := '/a/b/c/';
- r := '';
- lv := '';
- BrookExtractPathLevels(s, r, lv, ed);
- AssertEquals('a', r);
- AssertEquals('a', lv);
- AssertTrue(ed);
- BrookExtractPathLevels(s, r, lv, ed);
- AssertEquals('a/b', r);
- AssertEquals('b', lv);
- BrookExtractPathLevels(s, r, lv, ed);
- AssertEquals('a/b/c', r);
- AssertEquals('c', lv);
- end;
- procedure TTestBrookUtils.TestGetPathLevel;
- begin
- AssertEquals('b', BrookGetPathLevel('/a/b/c/', 1));
- end;
- procedure TTestBrookUtils.TestGetPathLevels;
- begin
- AssertEquals('b/c/', BrookGetPathLevels('/a/b/c/', 1));
- end;
- procedure TTestBrookUtils.TestMacthMethod;
- begin
- AssertTrue(BrookMatchMethod(rmPost, 'POST'));
- end;
- procedure TTestBrookUtils.TestFileDate;
- var
- f: TStringList;
- fn, VPath: string;
- begin
- f := TStringList.Create;
- try
- VPath := ExtractFilePath(ParamStr(0));
- fn := VPath + 'file';
- f.SaveToFile(fn);
- AssertEquals(Trunc(Now), Trunc(BrookFileDate(fn)));
- DeleteFile(fn);
- finally
- f.Free;
- end;
- end;
- procedure TTestBrookUtils.TestExcludeTrailingUrlDelimiter;
- begin
- AssertEquals('', BrookExcludeTrailingUrlDelimiter(''));
- AssertEquals('', BrookExcludeTrailingUrlDelimiter('/'));
- AssertEquals('http://localhost',
- BrookExcludeTrailingUrlDelimiter('http://localhost/'));
- end;
- procedure TTestBrookUtils.TestIncludeTrailingUrlDelimiter;
- begin
- AssertEquals('/', BrookIncludeTrailingUrlDelimiter(''));
- AssertEquals('/', BrookIncludeTrailingUrlDelimiter('/'));
- AssertEquals('http://localhost/',
- BrookIncludeTrailingUrlDelimiter('http://localhost'));
- end;
- procedure TTestBrookUtils.TestExists;
- begin
- AssertTrue(BrookExists('123', ['abc', '123', 'abc123']));
- AssertTrue(BrookExists('Abc', ['ABC', '123', 'abc123'], True));
- end;
- procedure TTestBrookUtils.TestStringToObject;
- var
- i: Integer;
- n, v: string;
- o: TMyType;
- dt: TDateTime;
- st: TStringList;
- begin
- o := TMyType.Create;
- st := TStringList.Create;
- try
- dt := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
- st.Add('MyChar=A');
- st.Add('MyString=ABC123');
- st.Add('MyInteger=123');
- st.Add('MyInt64=456');
- st.Add('MyFloat=' + FloatToStr(123.456));
- st.Add('MyCurrency=' + CurrToStr(456.789));
- st.Add('MyBoolean=on');
- st.Add('MyDateTime=' + DateTimeToStr(dt));
- st.Add('MyEnum=enum2');
- st.Add('MySet=[enum1,enum3]');
- for i := 0 to Pred(st.Count) do
- begin
- st.GetNameValue(i, n, v);
- BrookStringToObject(o, n, v);
- end;
- AssertEquals(o.MyChar, 'A');
- AssertEquals(o.MyString, 'ABC123');
- AssertEquals(o.MyInteger, 123);
- AssertEquals(o.MyInt64, 456);
- AssertEquals(o.MyFloat, 123.456);
- AssertEquals(o.MyCurrency, 456.789);
- AssertEquals(o.MyBoolean, True);
- AssertEquals(o.MyDateTime, dt);
- AssertTrue(o.MyEnum = enum2);
- AssertTrue(o.MySet = [enum1, enum3]);
- finally
- o.Free;
- st.Free;
- end;
- end;
- procedure TTestBrookUtils.TestStringsToObject;
- var
- o: TMyType;
- dt: TDateTime;
- st: TStringList;
- begin
- o := TMyType.Create;
- st := TStringList.Create;
- try
- dt := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
- st.Add('MyChar=A');
- st.Add('MyString=ABC123');
- st.Add('MyInteger=123');
- st.Add('MyInt64=456');
- st.Add('MyFloat=' + FloatToStr(123.456));
- st.Add('MyCurrency=' + CurrToStr(456.789));
- st.Add('MyBoolean=on');
- st.Add('MyDateTime=' + DateTimeToStr(dt));
- st.Add('MyEnum=enum2');
- st.Add('MySet=[enum1,enum3]');
- BrookStringsToObject(o, st);
- AssertEquals(o.MyChar, 'A');
- AssertEquals(o.MyString, 'ABC123');
- AssertEquals(o.MyInteger, 123);
- AssertEquals(o.MyInt64, 456);
- AssertEquals(o.MyFloat, 123.456);
- AssertEquals(o.MyCurrency, 456.789);
- AssertEquals(o.MyBoolean, True);
- AssertEquals(o.MyDateTime, dt);
- AssertTrue(o.MyEnum = enum2);
- AssertTrue(o.MySet = [enum1, enum3]);
- st.Values['MyChar'] := 'B';
- st.Values['MyInteger'] := '456';
- st.Values['MyInt64'] := '789';
- o.MyInt64 := 0;
- BrookStringsToObject(o, st, ['myint64']);
- AssertEquals(o.MyChar, 'B');
- AssertEquals(o.MyInteger, 456);
- AssertEquals(o.MyInt64, 0);
- finally
- o.Free;
- st.Free;
- end;
- end;
- procedure TTestBrookUtils.TestObjectToString;
- var
- v: string;
- dt: TDateTime;
- o: TMyType;
- st: TStringList;
- i, c: Integer;
- pi: PPropInfo;
- pl: PPropList = nil;
- begin
- o := TMyType.Create;
- st := TStringList.Create;
- c := GetPropList(o, pl);
- try
- dt := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
- o.MyChar := 'A';
- o.MyString := 'ABC123';
- o.MyInteger :=123;
- o.MyInt64 := 456;
- o.MyFloat := 123.456;
- o.MyCurrency := 456.789;
- o.MyBoolean := True;
- o.MyDateTime := dt;
- o.MyEnum := enum2;
- o.MySet := [enum1, enum3];
- if Assigned(pl) then
- try
- for i := 0 to Pred(c) do
- begin
- pi := pl^[i];
- BrookObjectToString(o, pi, v);
- st.Add(pi^.Name + '=' + v);
- end;
- finally
- FreeMem(pl);
- end;
- AssertEquals(st.ValueFromIndex[0], 'A');
- AssertEquals(st.ValueFromIndex[1], 'ABC123');
- AssertEquals(st.ValueFromIndex[2], '123');
- AssertEquals(st.ValueFromIndex[3], '456');
- AssertEquals(st.ValueFromIndex[4], FloatToStr(123.456));
- AssertEquals(st.ValueFromIndex[5], CurrToStr(456.789));
- AssertEquals(st.ValueFromIndex[6], 'True');
- AssertEquals(st.ValueFromIndex[7], DateTimeToStr(dt));
- AssertEquals(st.ValueFromIndex[8], 'enum2');
- AssertEquals(st.ValueFromIndex[9], 'enum1,enum3');
- finally
- o.Free;
- st.Free;
- end;
- end;
- procedure TTestBrookUtils.TestObjectToStrings;
- var
- dt: TDateTime;
- o: TMyType;
- st: TStringList;
- begin
- o := TMyType.Create;
- st := TStringList.Create;
- try
- dt := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
- o.MyChar := 'A';
- o.MyString := 'ABC123';
- o.MyInteger :=123;
- o.MyInt64 := 456;
- o.MyFloat := 123.456;
- o.MyCurrency := 456.789;
- o.MyBoolean := True;
- o.MyDateTime := dt;
- o.MyEnum := enum2;
- o.MySet := [enum1, enum3];
- BrookObjectToStrings(o, st);
- AssertEquals(st.ValueFromIndex[0], 'A');
- AssertEquals(st.ValueFromIndex[1], 'ABC123');
- AssertEquals(st.ValueFromIndex[2], '123');
- AssertEquals(st.ValueFromIndex[3], '456');
- AssertEquals(st.ValueFromIndex[4], FloatToStr(123.456));
- AssertEquals(st.ValueFromIndex[5], CurrToStr(456.789));
- AssertEquals(st.ValueFromIndex[6], 'True');
- AssertEquals(st.ValueFromIndex[7], DateTimeToStr(dt));
- AssertEquals(st.ValueFromIndex[8], 'enum2');
- AssertEquals(st.ValueFromIndex[9], 'enum1,enum3');
- st.Clear;
- o.MyChar := 'B';
- o.MyInteger := 456;
- o.MyInt64 := 789;
- st.Values['MyInt64'] := '0';
- BrookObjectToStrings(o, st, ['myint64']);
- AssertEquals(st.Values['MyChar'], 'B');
- AssertEquals(st.Values['MyInteger'], '456');
- AssertEquals(st.Values['MyInt64'], '0');
- finally
- o.Free;
- st.Free;
- end;
- end;
- procedure TTestBrookUtils.TestCopyObject;
- var
- o1, o2: TMyType;
- begin
- o1 := TMyType.Create;
- o2 := TMyType.Create;
- try
- o1.MyChar := 'A';
- o1.MyString := 'ABC123';
- o1.MyInteger :=123;
- o1.MyInt64 := 456;
- o1.MyFloat := 123.456;
- o1.MyCurrency := 456.789;
- o1.MyBoolean := True;
- o1.MyDateTime := EncodeDateTime(2000, 12, 31, 23, 59, 59, 999);
- o1.MyEnum := enum2;
- o1.MySet := [enum1, enum3];
- BrookCopyObject(o1, o2);
- AssertEquals(o1.MyChar, o2.MyChar);
- AssertEquals(o1.MyString, o2.MyString);
- AssertEquals(o1.MyInteger, o2.MyInteger);
- AssertEquals(o1.MyInt64, o2.MyInt64);
- AssertEquals(o1.MyFloat, o2.MyFloat);
- AssertEquals(o1.MyCurrency, o2.MyCurrency);
- AssertEquals(o1.MyBoolean, o2.MyBoolean);
- AssertEquals(o1.MyDateTime, o2.MyDateTime);
- AssertTrue(o1.MyEnum = o2.MyEnum);
- AssertTrue(o1.MySet = o2.MySet);
- o1.MyChar := 'B';
- o1.MyInteger := 456;
- o1.MyInt64 := 789;
- o2.MyInt64 := 0;
- BrookCopyObject(o1, o2, ['myint64']);
- AssertEquals(o2.MyChar, 'B');
- AssertEquals(o2.MyInteger, 456);
- AssertEquals(o2.MyInt64, 0);
- finally
- o1.Free;
- o2.Free;
- end;
- end;
- initialization
- RegisterTest(TTestBrookUtils);
- end.
|