Source

delPython / demo / delpython_tests.pas

Full commit
unit delpython_tests;

interface

uses
  delPython, Classes;

procedure TestAll;

var TestLog: TStrings;

implementation

uses
  SysUtils, Rtti;

procedure AssertExpr(const Expected, PythonExpression: string);
var actual: string;
begin
  try
    actual := python.EvalStr(PythonExpression);
    if Expected <> actual then
      if TestLog <> nil then TestLog.Append(Format('Expected "%s" but found "%s" for %s', [Expected, actual, PythonExpression]));
  except
    if TestLog <> nil then
      TestLog.Append(Format('Exception while evaluating "%s" Details: "%s"', [PythonExpression, Exception(ExceptObject).Message]));
  end;
end;

procedure AssertNoRaise(const PythonExpression: string);
begin
  try
    python.Exec(PythonExpression);
  except
    if TestLog <> nil then TestLog.Append(Format('FAIL: %s raised an exception: %s', [PythonExpression,
      Exception(ExceptObject).Message]));
  end;
end;

procedure AssertRaises(const PythonExpression: string);
begin
  try
    python.EvalStr(PythonExpression);
    if TestLog <> nil then TestLog.Append(Format('FAIL: %s should have raised an exception', [PythonExpression]));
  except
    ;
  end;
end;

procedure TestTStringList;
begin
  python.LoadDll('d:\projects\pfaide\v1.0\ide\internal_python\python26.dll');
  python.CreateModule('vcl');
  python.Wrap(TStringList);
  python.FixProp(TStringList, 'Strings', function(obj: TObject; i: integer): TValue
                                         begin
                                           Result := TStringList(obj).Strings[i];
                                         end
                                         ,
                                         procedure(obj: TObject; i: integer; value: TValue)
                                         begin
                                           TStringList(obj).Strings[i] := value.AsString;
                                         end );

  { create a TStringList }
  AssertNoRaise('import vcl; o=vcl.TStringList()');
  //todo: AssertNoRaise('import vcl; o=vcl.TStringList.Create()');

  { check initial property values }
  AssertExpr('dupIgnore', 'o.Duplicates');
  AssertExpr('False', 'o.Sorted');
  AssertExpr('False', 'o.CaseSensitive');
  AssertExpr('None', 'o.OnChange');
  AssertExpr('None', 'o.OnChanging');
  AssertExpr('False', 'o.OwnsObjects');
  AssertExpr('0', 'o.Capacity');
  AssertExpr('', 'o.CommaText');
  AssertExpr('0', 'o.Count');
  AssertExpr(',', 'o.Delimiter');
  AssertExpr('', 'o.DelimitedText');
  AssertExpr(#13#10, 'o.LineBreak');
  AssertExpr('"', 'o.QuoteChar');
  AssertExpr('=', 'o.NameValueSeparator');
  AssertExpr('False', 'o.StrictDelimiter');
  AssertExpr('', 'o.Text');
  AssertExpr('None', 'o.StringsAdapter');

  { test methods }

  // Add
  AssertExpr('0', 'o.Add("1")');
  AssertExpr('1', 'o.Add("2")');
  AssertExpr('2', 'o.Add("3")');
  AssertExpr('3', 'o.Count');
  AssertExpr('1', 'o.Strings[0]');
  AssertExpr('2', 'o.Strings[1]');
  AssertExpr('3', 'o.Strings[2]');
  AssertExpr('1', 'o[0]');
  AssertExpr('2', 'o[1]');
  AssertExpr('3', 'o[2]');
  AssertExpr('None', 'o.Objects[0]');
  AssertExpr('None', 'o.Objects[1]');
  AssertExpr('None', 'o.Objects[2]');

  // Clear
  AssertExpr('None', 'o.Clear()');
  AssertExpr('0', 'o.Count');
  AssertRaises('o[0]');
  AssertRaises('o.Strings[0]');
  AssertRaises('o.Objects[0]');

  //Delete
  AssertNoRaise('o.Clear(); o.Add("1"); o.Add("2"); o.Add("3");');
  AssertExpr('None', 'o.Delete(1)');
  AssertExpr('2', 'o.Count');
  AssertExpr('1', 'o[0]');
  AssertExpr('3', 'o[1]');
  AssertExpr('1', 'o.Strings[0]');
  AssertExpr('3', 'o.Strings[1]');
  AssertExpr('None', 'o.Objects[0]');
  AssertExpr('None', 'o.Objects[1]');
  AssertRaises('o.Delete(2)');

  // Exchange
  AssertNoRaise('o.Clear(); o.Add("1"); o.Add("2"); o.Add("3");');
  AssertExpr('None', 'o.Exchange(0, 1)');
  AssertExpr('1', 'o[0]');
  AssertExpr('0', 'o[1]');

  // Find
  AssertNoRaise('o.Clear(); o.Add("1"); o.Add("2"); o.Add("3");');
  AssertNoRaise('i=0');
  AssertExpr('True', 'o.Find("1",i)');
  AssertExpr('0', 'i');
  AssertNoRaise('i=0');
  AssertExpr('True', 'o.Find("2",i)');
  AssertExpr('1', 'i');
  AssertNoRaise('i=0');
  AssertExpr('True', 'o.Find("2",i)');
  AssertExpr('2', 'i');

  // IndexOf
  AssertNoRaise('o.Clear(); o.Add("1"); o.Add("2"); o.Add("3");');
  AssertExpr('0', 'o.IndexOf("1")');
  AssertExpr('1', 'o.IndexOf("2")');
  AssertExpr('2', 'o.IndexOf("3")');
  AssertExpr('-1', 'o.IndexOf("")');

  // Insert
  AssertNoRaise('o.Clear(); o.Add("1"); o.Add("2"); o.Add("3");');
  AssertNoRaise('o.Insert(0, "0")');
  AssertExpr('0', 'o.IndexOf("0")');
  AssertExpr('1', 'o.IndexOf("1")');
  AssertExpr('2', 'o.IndexOf("2")');
  AssertExpr('3', 'o.IndexOf("3")');

  // Sort
  AssertNoRaise('o.Clear(); o.Add("ccc"); o.Add("bbb"); o.Add("aaa");');
  AssertNoRaise('o.Sort()');
  AssertExpr('aaa', 'o[0]');
  AssertExpr('bbb', 'o[1]');
  AssertExpr('ccc', 'o[2]');

  { from TStrings }

  // Append
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertExpr('0', 'o.IndexOf("1")');
  AssertExpr('1', 'o.IndexOf("2")');
  AssertExpr('2', 'o.IndexOf("3")');

  // AddStrings
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertNoRaise('o2=vcl.TStringList(); o2.Add("4"); o2.Add("5"); o.AddStrings(o2)');
  AssertExpr('5', 'o.Count');
  AssertExpr('0', 'o.IndexOf("1")');
  AssertExpr('1', 'o.IndexOf("2")');
  AssertExpr('2', 'o.IndexOf("3")');
  AssertExpr('3', 'o.IndexOf("4")');
  AssertExpr('4', 'o.IndexOf("5")');

  // Assign
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertNoRaise('o2=vcl.TStringList(); o2.Add("4"); o2.Add("5"); o.Assign(o2)');
  AssertExpr('2', 'o.Count');
  AssertExpr('0', 'o[0]');
  AssertExpr('1', 'o[1]');

  { test properties }

  // Duplicates
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertNoRaise('o.Duplicates = dupIgnore');
  AssertNoRaise('o.Add("1")');
  AssertExpr('3', 'o.Count');
  AssertNoRaise('o.Duplicates = dupAccept');
  AssertNoRaise('o.Add("1")');
  AssertExpr('4', 'o.Count');

  // Sorted
  AssertNoRaise('o.Clear(); o.Append("2"); o.Append("1"); o.Append("3");');
  AssertNoRaise('o.Sorted = True');
  AssertExpr('True', 'o.Sorted');
  AssertExpr('1', 'o[0]');
  AssertExpr('2', 'o[1]');
  AssertExpr('3', 'o[2]');

  // Capacity
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertNoRaise('o.Capacity = 50');
  AssertExpr('50', 'o.Capacity');

  // CommaText
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertExpr('1;2;3', 'o.CommaText');
  AssertNoRaise('o.CommaText = "3,2,1"');
  AssertExpr('3', 'o[0]');
  AssertExpr('2', 'o[1]');
  AssertExpr('1', 'o[2]');

  // Count
  AssertNoRaise('o.Clear()');
  AssertExpr('0', 'o.Count');
  AssertNoRaise('o.Append("1")');
  AssertExpr('1', 'o.Count');
  AssertNoRaise('for i in range(99): o.Add("99")');
  AssertExpr('100', 'o.Count');

  // Delimiter & DelimitedText
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertNoRaise('o.Delimiter = ";"');
  AssertExpr('1;2;3', 'o.DelimitedText');
  AssertNoRaise('o.DelimitedText = "3;2;1"');
  AssertExpr('3', 'o[0]');
  AssertExpr('2', 'o[1]');
  AssertExpr('1', 'o[2]');

  // Names
  AssertNoRaise('o.Clear(); o.Append("a=1"); o.Append("b=2"); o.Append("c=3");');
  AssertExpr('a', 'o.Names[0]');
  AssertExpr('b', 'o.Names[1]');
  AssertExpr('c', 'o.Names[2]');

  // Objects
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertExpr('None', 'o.Objects[0]');
  AssertExpr('None', 'o.Objects[1]');
  AssertExpr('None', 'o.Objects[2]');
  AssertExpr('', 'o.Objects[2] = 42');
  AssertExpr('42', 'o.Objects[2]');

  // Values
  AssertNoRaise('o.Clear(); o.Append("a=1"); o.Append("b=2"); o.Append("c=3");');
  AssertExpr('1', 'o.Values["a"]');
  AssertExpr('2', 'o.Values["b"]');
  AssertExpr('3', 'o.Values["c"]');

  // ValueFromIndex
  AssertNoRaise('o.Clear(); o.Append("a=1"); o.Append("b=2"); o.Append("c=3");');
  AssertExpr('1', 'o.ValueFromIndex[0]');
  AssertExpr('2', 'o.ValueFromIndex[1]');
  AssertExpr('3', 'o.ValueFromIndex[2]');

  // Strings
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertExpr('1', 'o.Strings[0]');
  AssertExpr('2', 'o.Strings[1]');
  AssertExpr('3', 'o.Strings[2]');
  AssertExpr('3', 'o.Count');

  // Text
  AssertNoRaise('o.Clear(); o.Append("1"); o.Append("2"); o.Append("3");');
  AssertNoRaise('o.Text = "utku\r\nkarataş"');
  AssertExpr('2', 'o.Count');
  AssertExpr('utku', 'o[0]');
  AssertExpr('karataş', 'o[1]');

  { Advanced cases }

  // StringsAdaptor
  AssertExpr('None', 'o.StringsAdaptor');

  // OnChange
  AssertNoRaise('event_test = 0');
  AssertNoRaise('def _onchange(self, sender): global event_test; event_test = 1');
  AssertNoRaise('o.Clear(); o.OnChange = _onchange');
  AssertNoRaise('o.Append("1");');
  AssertExpr('1', 'event_test');

  // OnChanging
  AssertNoRaise('def _onchanging(self, sender): global event_test; event_test = 2');
  AssertNoRaise('o.Clear(); o.OnChange = _onchange');
  AssertNoRaise('o.Append("1");');
  AssertExpr('2', 'event_test');

  // CustomSort
  AssertNoRaise('o.Clear(); o.Append("b"); o.Append("c"); o.Append("a"); o.Append("d");');
  AssertNoRaise(
'def StringListCompareStrings(l, Index1, Index2):' + #13#10 +
'    if l[Index1] > l[Index2]: return 1          ' + #13#10 +
'    elif l[Index1] < l[Index2]: return -1       ' + #13#10 +
'    else: return 0                              '
);
  AssertNoRaise('o.CustomSort(StringListCompareStrings)');
  AssertExpr('a', 'o[0]');
  AssertExpr('b', 'o[1]');
  AssertExpr('c', 'o[2]');
  AssertExpr('d', 'o[3]');
  AssertExpr('4', 'o.Count');

  // todo: what to do with TObject references in Python Code?
  // OwnsObjects
  // InsertObject
  // AddObject
  // Equals
  // todo: GetEnumerator
  // todo: GetText
  // todo: LoadFromFile overloaded
  // todo: LoadFromStream overloaded
  // todo: Move
  // todo: SaveToFile overloaded
  // todo: SaveToStream overloaded
  // todo: SetText

  // Not tested as they dont convey anything testable:
  // BeginUpdate, EndUpdate, LineBreak, NameValueSeparator, StrictDelimiter, QuoteChar, CaseSensitive
end;


procedure TestAll;
begin
  TestTStringList;
  if TestLog <> nil then
    TestLog.Append(Format('DONE tests.', []));
end;

end.