1
0
mirror of synced 2024-11-25 04:03:07 +01:00
x2xmldatabinding/Tests/Source/ObjectMappingTests.pas

246 lines
7.2 KiB
ObjectPascal
Raw Normal View History

unit ObjectMappingTests;
interface
uses
2008-03-12 07:31:09 +01:00
TestFramework,
DataBindingResultXML,
XMLDataBindingGenerator;
type
2008-03-12 07:31:09 +01:00
TTestXMLDataBindingGenerator = class(TXMLDataBindingGenerator)
protected
procedure GenerateDataBinding(); override;
end;
TObjectMappingTests = class(TAbstractTest, ITest)
2008-03-12 07:31:09 +01:00
private
FFileName: String;
protected
procedure RunTest(testResult: TTestResult); override;
2008-03-12 07:31:09 +01:00
procedure CompareSchemas(ATestResult: TTestResult; AGenerator: TTestXMLDataBindingGenerator; AResult: IXMLDataBindingResult);
procedure CompareItems(ATestResult: TTestResult; AGeneratorSchema: TXMLDataBindingSchema; AResultSchema: IXMLSchema);
// procedure CompareCollection(ATestResult: TTestResult; AGeneratorSchema: TXMLDataBindingSchema; AGeneratorItem: TXMLDataBindingCollection; AResultItem: IXMLItem);
2008-03-12 07:31:09 +01:00
property FileName: String read FFileName;
public
2008-03-12 07:31:09 +01:00
constructor Create(const AFileName: String);
class function Suite(): ITestSuite;
end;
implementation
2008-03-12 07:31:09 +01:00
uses
Contnrs,
SysUtils,
X2UtApp;
const
ExpectedExtension = '_expected.xml';
{ TObjectMappingTests }
2008-03-12 07:31:09 +01:00
class function TObjectMappingTests.Suite(): ITestSuite;
var
basePath: String;
fileInfo: TSearchRec;
begin
Result := TTestSuite.Create(Self.ClassName);
2008-03-12 07:31:09 +01:00
{ Add tests for all .xsd files which have a corresponding .expected file }
basePath := App.Path + 'Tests\Data\';
if FindFirst(basePath + '*.xsd', faAnyFile, fileInfo) = 0 then
begin
repeat
if FileExists(basePath + ChangeFileExt(fileInfo.Name, ExpectedExtension)) then
begin
Result.AddTest(Self.Create(basePath + fileInfo.Name));
end;
until FindNext(fileInfo) <> 0;
SysUtils.FindClose(fileInfo);
end;
end;
constructor TObjectMappingTests.Create(const AFileName: String);
begin
inherited Create(ChangeFileExt(ExtractFileName(AFileName), ''));
FFileName := AFileName;
end;
procedure TObjectMappingTests.RunTest(testResult: TTestResult);
2008-03-12 07:31:09 +01:00
var
generator: TTestXMLDataBindingGenerator;
expectedResult: IXMLDataBindingResult;
begin
generator := TTestXMLDataBindingGenerator.Create();
try
generator.Execute(FileName);
expectedResult := LoadDataBindingResult(ChangeFileExt(FileName, ExpectedExtension));
CompareSchemas(testResult, generator, expectedResult);
finally
FreeAndNil(generator);
end;
end;
procedure TObjectMappingTests.CompareSchemas(ATestResult: TTestResult; AGenerator: TTestXMLDataBindingGenerator; AResult: IXMLDataBindingResult);
var
handled: TObjectList;
schemaIndex: Integer;
resultSchema: IXMLSchema;
bindingSchema: TXMLDataBindingSchema;
begin
2008-03-12 07:31:09 +01:00
handled := TObjectList.Create(False);
try
{ Iterate expected schemas }
for schemaIndex := 0 to Pred(AResult.Schemas.Count) do
begin
resultSchema := AResult.Schemas[schemaIndex];
bindingSchema := AGenerator.FindSchema(resultSchema.Name);
if Assigned(bindingSchema) then
begin
handled.Add(bindingSchema);
CompareItems(ATestResult, bindingSchema, resultSchema);
end else
ATestResult.AddFailure(Self, nil, Format('Schema "%s" expected', [resultSchema.Name]));
end;
{ Find unexpected schemas }
for schemaIndex := 0 to Pred(AGenerator.SchemaCount) do
if handled.IndexOf(AGenerator.Schemas[schemaIndex]) = -1 then
begin
ATestResult.AddFailure(Self, nil, Format('Schema "%s" not expected', [AGenerator.Schemas[schemaIndex].SchemaName]));
end;
finally
FreeAndNil(handled);
end;
end;
2008-03-12 07:31:09 +01:00
procedure TObjectMappingTests.CompareItems(ATestResult: TTestResult; AGeneratorSchema: TXMLDataBindingSchema; AResultSchema: IXMLSchema);
function FindItem(const AResultItem: IXMLItem): TXMLDataBindingItem;
2008-03-12 07:31:09 +01:00
var
itemType: TXMLDataBindingItemType;
itemIndex: Integer;
item: TXMLDataBindingItem;
2008-03-12 07:31:09 +01:00
begin
Result := nil;
itemType := itInterface;
{if AResultItem.ItemType = 'Collection' then
itemType := itInterface
else }if AResultItem.ItemType = 'Enumeration' then
itemType := itEnumeration;
2008-03-12 07:31:09 +01:00
for itemIndex := 0 to Pred(AGeneratorSchema.ItemCount) do
begin
item := AGeneratorSchema.Items[itemIndex];
if (item.ItemType = itemType) and
(item.Name = AResultItem.Name) then
2008-03-12 07:31:09 +01:00
begin
Result := item;
2008-03-12 07:31:09 +01:00
break;
end;
end;
2008-03-12 07:31:09 +01:00
end;
var
handled: TObjectList;
itemIndex: Integer;
resultItem: IXMLItem;
bindingItem: TXMLDataBindingItem;
begin
handled := TObjectList.Create(False);
try
{ Iterate expected items }
2008-03-12 07:31:09 +01:00
for itemIndex := 0 to Pred(AResultSchema.Items.Count) do
begin
resultItem := AResultSchema.Items[itemIndex];
bindingItem := FindItem(resultItem);
2008-03-12 07:31:09 +01:00
if Assigned(bindingItem) then
begin
handled.Add(bindingItem);
// case bindingItem.ItemType of
// itInterface: CompareProperties;
// itCollection: CompareCollection(ATestResult, AGeneratorSchema, TXMLDataBindingCollection(bindingItem), resultItem);
// end;
2008-03-12 07:31:09 +01:00
end else
ATestResult.AddFailure(Self, nil, Format('Item "%s.%s" expected',
2008-03-12 07:31:09 +01:00
[AGeneratorSchema.SchemaName, resultItem.Name]));
end;
{ Find unexpected items }
2008-03-12 07:31:09 +01:00
for itemIndex := 0 to Pred(AGeneratorSchema.ItemCount) do
begin
// bindingItem := AGeneratorSchema.Items[itemIndex];
// if bindingItem.ItemType <> itForward then
// begin
// if handled.IndexOf(bindingItem) = -1 then
// begin
// ATestResult.AddFailure(Self, nil, Format('Item "%s.%s" not expected',
// [AGeneratorSchema.SchemaName,
// AGeneratorSchema.Items[itemIndex].Name]));
// end;
// end;
2008-03-12 07:31:09 +01:00
end;
finally
FreeAndNil(handled);
end;
end;
{
procedure TObjectMappingTests.CompareCollection(ATestResult: TTestResult; AGeneratorSchema: TXMLDataBindingSchema; AGeneratorItem: TXMLDataBindingCollection; AResultItem: IXMLItem);
begin
if Assigned(AGeneratorItem.CollectionItem) then
begin
if AGeneratorItem.CollectionItem.Name <> AResultItem.Collection.ItemName then
ATestResult.AddFailure(Self, nil, Format('Item "%s.%s": collection item "%s" expected but "%s" found',
[AGeneratorSchema.SchemaName,
AGeneratorItem.Name,
AResultItem.Collection.ItemName,
AGeneratorItem.CollectionItem.Name]));
end else
ATestResult.AddFailure(Self, nil, Format('Item "%s.%s": collection item not Assigned',
[AGeneratorSchema.SchemaName,
AGeneratorItem.Name]));
end;
}
2008-03-12 07:31:09 +01:00
{ TTestXMLDataBindingGenerator }
procedure TTestXMLDataBindingGenerator.GenerateDataBinding();
begin
end;
initialization
// RegisterTest(TObjectMappingTests.Suite);
end.