Commits

Anonymous committed 5b9c15d

Formatting

Comments (0)

Files changed (2)

dbasetests/dbftoolsunit.pas

 
 uses
   Classes, SysUtils, toolsunit,
-  db, Dbf, dbf_common;
+  DB, Dbf, dbf_common;
 
 type
-{ TDBFDBConnector }
+  { TDBFDBConnector }
 
   TDBFDBConnector = class(TDBConnector)
   protected
     procedure CreateFieldDataset; override;
     procedure DropNDatasets; override;
     procedure DropFieldDataset; override;
-    Function InternalGetNDataset(n : integer) : TDataset; override;
-    Function InternalGetFieldDataset : TDataSet; override;
+    function InternalGetNDataset(n: integer): TDataset; override;
+    function InternalGetFieldDataset: TDataSet; override;
   public
-    function GetTraceDataset(AChange : Boolean) : TDataset; override;
+    function GetTraceDataset(AChange: boolean): TDataset; override;
   end;
 
   { TDbfTraceDataset }
 
   TDbfTraceDataset = class(Tdbf)
   protected
-    procedure SetCurrentRecord(Index: Longint); override;
+    procedure SetCurrentRecord(Index: longint); override;
     procedure RefreshInternalCalcFields(Buffer: PChar); override;
     procedure InternalInitFieldDefs; override;
     procedure CalculateFields(Buffer: PChar); override;
 
   { TDBFAutoClean }
   // DBF descendant that saves to a temp file and removes file when closed
-  TDBFAutoClean=class(TDBF)
+  TDBFAutoClean = class(TDBF)
+  private
+    function GetUserRequestedTableLevel: integer;
   public
     constructor Create;
     constructor Create(AOwner: TComponent); override;
 
 { TDBFAutoClean }
 
+function TDBFAutoClean.GetUserRequestedTableLevel: integer;
+  // User can specify table level as a connector param, e.g.:
+  // connectorparams=4
+  // If none given, default to DBase IV
+var
+  TableLevelProvided: integer;
+begin
+  TableLevelProvided := StrToIntDef(dbconnectorparams, 4);
+  if not (TableLevelProvided in [3, 4, 5, 7, TDBF_TABLELEVEL_FOXPRO,
+    TDBF_TABLELEVEL_VISUALFOXPRO]) then
+  begin
+    Result := -1; // hope this crashes the tests so user is alerted.
+    //Invalid tablelevel specified in connectorparams= field. Aborting
+    exit;
+  end;
+  Result := TableLevelProvided;
+end;
+
 constructor TDBFAutoClean.Create;
 var
   DBFFileName: string;
   TableLevelProvided: integer;
 begin
-  DBFFileName:=GetTempFileName;
-  FilePathFull:=ExtractFilePath(DBFFileName);
+  DBFFileName := GetTempFileName;
+  FilePathFull := ExtractFilePath(DBFFileName);
   TableName := ExtractFileName(DBFFileName);
-  // User can specify table level as a connector param, e.g.:
-  // connectorparams=4
-  // If none given, default to DBase IV
-  TableLevelProvided:=StrToIntDef(dbconnectorparams,4);
-  if not (TableLevelProvided in [3,4,5,7,TDBF_TABLELEVEL_FOXPRO,TDBF_TABLELEVEL_VISUALFOXPRO]) then
-  begin
-    writeln('Invalid tablelevel specified in connectorparams= field. Aborting');
-    exit;
-  end;
+  TableLevelProvided := GetUserRequestedTableLevel;
   TableLevel := TableLevelProvided;
   CreateTable; //write out header to disk
 end;
 var
   FileName: string;
 begin
-  FileName:=AbsolutePath+TableName;
+  FileName := AbsolutePath + TableName;
   inherited Destroy;
   deletefile(FileName);
 end;
 var
   countID: integer;
 begin
-  result:=(TDBFAutoClean.Create(nil) as TDataSet);
-  with (result as TDBFAutoclean) do
-    begin
-    FieldDefs.Add('ID',ftInteger);
-    FieldDefs.Add('NAME',ftString,50);
+  Result := (TDBFAutoClean.Create(nil) as TDataSet);
+  with (Result as TDBFAutoclean) do
+  begin
+    FieldDefs.Add('ID', ftInteger);
+    FieldDefs.Add('NAME', ftString, 50);
     CreateTable;
     Open;
-    if n > 0 then for countId := 1 to n do
+    if n > 0 then
+      for countId := 1 to n do
       begin
-      Append;
-      FieldByName('ID').AsInteger := countID;
-      FieldByName('NAME').AsString := 'TestName'+inttostr(countID);
-      // Explicitly call .post, since there could be a bug which disturbs
-      // the automatic call to post. (example: when TDataset.DataEvent doesn't
-      // work properly)
-      Post;
+        Append;
+        FieldByName('ID').AsInteger := countID;
+        FieldByName('NAME').AsString := 'TestName' + IntToStr(countID);
+        // Explicitly call .post, since there could be a bug which disturbs
+        // the automatic call to post. (example: when TDataset.DataEvent doesn't
+        // work properly)
+        Post;
       end;
     if state = dsinsert then
       Post;
     Close;
-    end;
+  end;
 end;
 
 function TDBFDBConnector.InternalGetFieldDataset: TDataSet;
 var
-  i : integer;
+  i: integer;
 begin
-  result:=(TDbfAutoClean.Create(nil) as TDataSet);
-  with (result as TDBFAutoClean) do
-    begin
-    FieldDefs.Add('ID',ftInteger);
-    FieldDefs.Add('FSTRING',ftString,10);
-    FieldDefs.Add('FSMALLINT',ftSmallint);
-    FieldDefs.Add('FINTEGER',ftInteger);
-    FieldDefs.Add('FWORD',ftWord);
-    FieldDefs.Add('FBOOLEAN',ftBoolean);
-    FieldDefs.Add('FFLOAT',ftFloat);
-    if (result as TDBF).TableLevel>=25 then
-      FieldDefs.Add('FCURRENCY',ftCurrency);
-    if (result as TDBF).TableLevel>=25 then
-      FieldDefs.Add('FBCD',ftBCD);
-    FieldDefs.Add('FDATE',ftDate);
-//    FieldDefs.Add('FTIME',ftTime);
-    FieldDefs.Add('FDATETIME',ftDateTime);
-    FieldDefs.Add('FLARGEINT',ftLargeint);
-    FieldDefs.Add('FMEMO',ftMemo);
+  Result := (TDbfAutoClean.Create(nil) as TDataSet);
+  with (Result as TDBFAutoClean) do
+  begin
+    FieldDefs.Add('ID', ftInteger);
+    FieldDefs.Add('FSTRING', ftString, 10);
+    FieldDefs.Add('FSMALLINT', ftSmallint);
+    FieldDefs.Add('FINTEGER', ftInteger);
+    FieldDefs.Add('FWORD', ftWord);
+    FieldDefs.Add('FBOOLEAN', ftBoolean);
+    FieldDefs.Add('FFLOAT', ftFloat);
+    if (Result as TDBF).TableLevel >= 25 then
+      FieldDefs.Add('FCURRENCY', ftCurrency);
+    if (Result as TDBF).TableLevel >= 25 then
+      FieldDefs.Add('FBCD', ftBCD);
+    FieldDefs.Add('FDATE', ftDate);
+    //    FieldDefs.Add('FTIME',ftTime);
+    FieldDefs.Add('FDATETIME', ftDateTime);
+    FieldDefs.Add('FLARGEINT', ftLargeint);
+    FieldDefs.Add('FMEMO', ftMemo);
     CreateTable;
     Open;
-    for i := 0 to testValuesCount-1 do
-      begin
+    for i := 0 to testValuesCount - 1 do
+    begin
       Append;
       FieldByName('ID').AsInteger := i;
       FieldByName('FSTRING').AsString := testStringValues[i];
       FieldByName('FDATE').AsDateTime := StrToDate(testDateValues[i], 'yyyy/mm/dd', '-');
       FieldByName('FLARGEINT').AsLargeInt := testLargeIntValues[i];
       Post;
-      end;
+    end;
     Close;
-    end;
+  end;
 end;
 
-function TDBFDBConnector.GetTraceDataset(AChange: Boolean): TDataset;
-var ADS, AResDS : TDbf;
+function TDBFDBConnector.GetTraceDataset(AChange: boolean): TDataset;
+var
+  ADS, AResDS: TDbf;
 begin
-  ADS := GetNDataset(AChange,15) as TDbf;
+  ADS := GetNDataset(AChange, 15) as TDbf;
   AResDS := TDbfTraceDataset.Create(nil);
-  AResDS.FilePath:=ADS.FilePath;
-  AResDs.TableName:=ADS.TableName;
-  Result:=AResDS;
+  AResDS.FilePath := ADS.FilePath;
+  AResDs.TableName := ADS.TableName;
+  Result := AResDS;
 end;
 
 { TDbfTraceDataset }
 
-procedure TDbfTraceDataset.SetCurrentRecord(Index: Longint);
+procedure TDbfTraceDataset.SetCurrentRecord(Index: longint);
 begin
   DataEvents := DataEvents + 'SetCurrentRecord' + ';';
   inherited SetCurrentRecord(Index);
 end;
 
 procedure TDbfTraceDataset.InternalInitFieldDefs;
-var i : integer;
-    IntCalcFieldName : String;
+var
+  i: integer;
+  IntCalcFieldName: string;
 begin
   // To fake an internal calculated field, set its fielddef InternalCalcField
   // property to true, before the dataset is opened.
   // This procedure takes care of setting the automatically created fielddef's
   // InternalCalcField property to true. (works for only one field)
-  IntCalcFieldName:='';
-  for i := 0 to FieldDefs.Count -1 do
-    if fielddefs[i].InternalCalcField then IntCalcFieldName := FieldDefs[i].Name;
+  IntCalcFieldName := '';
+  for i := 0 to FieldDefs.Count - 1 do
+    if fielddefs[i].InternalCalcField then
+      IntCalcFieldName := FieldDefs[i].Name;
   inherited InternalInitFieldDefs;
-  if IntCalcFieldName<>'' then with FieldDefs.find(IntCalcFieldName) do
+  if IntCalcFieldName <> '' then
+    with FieldDefs.find(IntCalcFieldName) do
     begin
-    InternalCalcField := True;
+      InternalCalcField := True;
     end;
 end;
 
 initialization
   RegisterClass(TDBFDBConnector);
 end.
-

dbasetests/testspecifictdbf.pas

     procedure SetUp; override;
     procedure TearDown; override;
   published
-    // Create fields the old fashioned way:
+    // Create fields using indexdefs:
     procedure TestCreateDatasetFromFielddefs;
     // Specifying fields from field objects
     procedure TestCreateDatasetFromFields;
   begin
     Ignore('Autoinc fields are only supported in tablelevel 7 and higher');
   end;
+
   F := TAutoIncField.Create(ds);
   F.FieldName:='ID';
   F.DataSet:=ds;