Browse Source

* Patch from Graeme Geldenhuys to improve DUnit compatibility

git-svn-id: trunk@12302 -
michael 16 years ago
parent
commit
bcc1287bbc

+ 35 - 0
packages/fcl-fpcunit/src/DUnitCompatibleInterface.inc

@@ -19,9 +19,14 @@
     class procedure CheckIs(obj :TObject; pClass: TClass; msg: string = ''); overload;
     class procedure CheckSame(expected, actual: TObject; msg: string = ''); overload;
     class procedure FailNotEquals(expected, actual: string; msg: string = ''; errorAddr: Pointer = nil); virtual;
+    class procedure CheckTrue(condition: Boolean; msg: string);
+    class procedure CheckFalse(condition: Boolean; msg: string);
+    class function  EqualsErrorMessage(const expected, actual: string; const ErrorMsg: string): string;
+    class function  NotEqualsErrorMessage(const expected, actual: string; const ErrorMsg: string): string;
 
     class function Suite: TTest;
 
+
     {
     *** TODO  ***
     procedure CheckEqualsBin(expected, actual: longword; msg: string = ''; digits: integer=32); virtual;
@@ -135,6 +140,36 @@ begin
   Fail(msg + ComparisonMsg(Expected, Actual));
 end;
 
+class procedure TAssert.CheckTrue(condition: Boolean; msg: string);
+begin
+  if (not condition) then
+      FailNotEquals(BoolToStr(true, true), BoolToStr(false, true), msg, nil);
+end;
+
+class procedure TAssert.CheckFalse(condition: Boolean; msg: string);
+begin
+  if (condition) then
+      FailNotEquals(BoolToStr(false, true), BoolToStr(true, true), msg, nil);
+end;
+
+class function TAssert.EqualsErrorMessage(const expected, actual: string;
+    const ErrorMsg: string): string;
+begin
+  if (ErrorMsg <> '') then
+    Result := Format(sMsgActualEqualsExpFmt, [ErrorMsg + ', ', expected, actual])
+  else
+    Result := Format(sActualEqualsExpFmt, [expected, actual])
+end;
+
+class function TAssert.NotEqualsErrorMessage(const expected, actual: string;
+    const ErrorMsg: string): string;
+begin
+  if (ErrorMsg <> '') then
+    Result := Format(sExpectedButWasAndMessageFmt, [ErrorMsg, expected, actual])
+  else
+    Result := Format(sExpectedButWasFmt, [expected, actual]);
+end;
+
 class function TAssert.Suite: TTest;
 begin
   result := TTestSuite.Create(self);

+ 6 - 0
packages/fcl-fpcunit/src/fpcunit.pp

@@ -309,6 +309,12 @@ implementation
 uses
   testutils;
 
+Const
+  sExpectedButWasFmt = 'Expected:' + LineEnding + '"%s"' + LineEnding + 'But was:' + LineEnding + '"%s"';
+  sExpectedButWasAndMessageFmt = '%s' + LineEnding + sExpectedButWasFmt;
+  sMsgActualEqualsExpFmt = '%s' + LineEnding + 'Expected ' + LineEnding + '< %s > ' + LineEnding + 'equals actual ' + LineEnding + '< %s >';
+  sActualEqualsExpFmt = 'Expected ' + LineEnding + '< %s > ' + LineEnding + 'equals actual ' + LineEnding + '< %s >';
+
 
 { This lets us use a single include file for both the Interface and
   Implementation sections. }