2008-03-10 17:04:46 +01:00
|
|
|
unit ObjectMappingTests;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
2008-03-12 07:31:09 +01:00
|
|
|
TestFramework,
|
|
|
|
|
|
|
|
DataBindingResultXML,
|
|
|
|
XMLDataBindingGenerator;
|
2008-03-10 17:04:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
type
|
2008-03-12 07:31:09 +01:00
|
|
|
TTestXMLDataBindingGenerator = class(TXMLDataBindingGenerator)
|
|
|
|
protected
|
|
|
|
procedure GenerateDataBinding(); override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-03-10 17:04:46 +01:00
|
|
|
TObjectMappingTests = class(TAbstractTest, ITest)
|
2008-03-12 07:31:09 +01:00
|
|
|
private
|
|
|
|
FFileName: String;
|
2008-03-10 17:04:46 +01:00
|
|
|
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);
|
2008-03-14 16:35:07 +01:00
|
|
|
procedure CompareCollection(ATestResult: TTestResult; AGeneratorSchema: TXMLDataBindingSchema; AGeneratorItem: TXMLDataBindingCollection; AResultItem: IXMLItem);
|
2008-03-12 07:31:09 +01:00
|
|
|
|
|
|
|
property FileName: String read FFileName;
|
2008-03-10 17:04:46 +01:00
|
|
|
public
|
2008-03-12 07:31:09 +01:00
|
|
|
constructor Create(const AFileName: String);
|
|
|
|
|
|
|
|
class function Suite(): ITestSuite;
|
2008-03-10 17:04:46 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
2008-03-12 07:31:09 +01:00
|
|
|
uses
|
|
|
|
Contnrs,
|
|
|
|
SysUtils,
|
|
|
|
|
|
|
|
X2UtApp;
|
|
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
ExpectedExtension = '_expected.xml';
|
|
|
|
|
2008-03-10 17:04:46 +01:00
|
|
|
|
|
|
|
{ TObjectMappingTests }
|
2008-03-12 07:31:09 +01:00
|
|
|
class function TObjectMappingTests.Suite(): ITestSuite;
|
|
|
|
var
|
|
|
|
basePath: String;
|
|
|
|
fileInfo: TSearchRec;
|
|
|
|
|
2008-03-10 17:04:46 +01:00
|
|
|
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;
|
2008-03-10 17:04:46 +01:00
|
|
|
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;
|
|
|
|
|
2008-03-10 17:04:46 +01:00
|
|
|
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;
|
2008-03-10 17:04:46 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-03-12 07:31:09 +01:00
|
|
|
procedure TObjectMappingTests.CompareItems(ATestResult: TTestResult; AGeneratorSchema: TXMLDataBindingSchema; AResultSchema: IXMLSchema);
|
|
|
|
|
2008-03-14 16:35:07 +01:00
|
|
|
function FindItem(const AResultItem: IXMLItem): TXMLDataBindingItem;
|
2008-03-12 07:31:09 +01:00
|
|
|
var
|
2008-03-14 16:35:07 +01:00
|
|
|
itemType: TXMLDataBindingItemType;
|
|
|
|
itemIndex: Integer;
|
|
|
|
item: TXMLDataBindingItem;
|
2008-03-12 07:31:09 +01:00
|
|
|
|
|
|
|
begin
|
2008-03-14 16:35:07 +01:00
|
|
|
Result := nil;
|
|
|
|
itemType := itInterface;
|
|
|
|
|
|
|
|
if AResultItem.ItemType = 'Collection' then
|
|
|
|
itemType := itCollection
|
|
|
|
else if AResultItem.ItemType = 'Enumeration' then
|
|
|
|
itemType := itEnumeration;
|
2008-03-12 07:31:09 +01:00
|
|
|
|
|
|
|
for itemIndex := 0 to Pred(AGeneratorSchema.ItemCount) do
|
2008-03-14 16:35:07 +01:00
|
|
|
begin
|
|
|
|
item := AGeneratorSchema.Items[itemIndex];
|
|
|
|
|
|
|
|
if (item.ItemType = itemType) and
|
|
|
|
(item.Name = AResultItem.Name) then
|
2008-03-12 07:31:09 +01:00
|
|
|
begin
|
2008-03-14 16:35:07 +01:00
|
|
|
Result := item;
|
2008-03-12 07:31:09 +01:00
|
|
|
break;
|
|
|
|
end;
|
2008-03-14 16:35:07 +01:00
|
|
|
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
|
2008-03-14 16:35:07 +01:00
|
|
|
{ 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];
|
2008-03-14 16:35:07 +01:00
|
|
|
bindingItem := FindItem(resultItem);
|
2008-03-12 07:31:09 +01:00
|
|
|
|
|
|
|
if Assigned(bindingItem) then
|
|
|
|
begin
|
|
|
|
handled.Add(bindingItem);
|
2008-03-14 16:35:07 +01:00
|
|
|
|
|
|
|
case bindingItem.ItemType of
|
|
|
|
// itInterface: CompareProperties;
|
|
|
|
itCollection: CompareCollection(ATestResult, AGeneratorSchema, TXMLDataBindingCollection(bindingItem), resultItem);
|
|
|
|
end;
|
2008-03-12 07:31:09 +01:00
|
|
|
end else
|
2008-03-14 16:35:07 +01:00
|
|
|
ATestResult.AddFailure(Self, nil, Format('Item "%s.%s" expected',
|
2008-03-12 07:31:09 +01:00
|
|
|
[AGeneratorSchema.SchemaName, resultItem.Name]));
|
|
|
|
end;
|
|
|
|
|
2008-03-14 16:35:07 +01:00
|
|
|
{ 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
|
2008-03-14 16:35:07 +01:00
|
|
|
ATestResult.AddFailure(Self, nil, Format('Item "%s.%s" not expected',
|
2008-03-12 07:31:09 +01:00
|
|
|
[AGeneratorSchema.SchemaName,
|
|
|
|
AGeneratorSchema.Items[itemIndex].Name]));
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
FreeAndNil(handled);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-03-14 16:35:07 +01:00
|
|
|
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;
|
|
|
|
|
2008-03-10 17:04:46 +01:00
|
|
|
|
|
|
|
initialization
|
|
|
|
RegisterTest(TObjectMappingTests.Suite);
|
|
|
|
|
|
|
|
end.
|
|
|
|
|