mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-18 17:39:22 +02:00
lazutils: xpath: test for nodes
git-svn-id: trunk@39915 -
This commit is contained in:
parent
32fc4e1bed
commit
9ad25df1d3
@ -90,6 +90,28 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TTestLazXML.TestXPath;
|
procedure TTestLazXML.TestXPath;
|
||||||
|
|
||||||
|
procedure CheckNode(Node: TDOMElement; XPath: string;
|
||||||
|
ExpCount: integer; ExpFirstTagName: string);
|
||||||
|
var
|
||||||
|
V: TXPathVariable;
|
||||||
|
FirstNode: TDOMElement;
|
||||||
|
begin
|
||||||
|
V:=nil;
|
||||||
|
try
|
||||||
|
V:=EvaluateXPathExpression(XPath,Node);
|
||||||
|
AssertEquals('xpath="'+XPath+'": AsNodeSet',True,V.AsNodeSet<>nil);
|
||||||
|
AssertEquals('xpath="'+XPath+'": AsNodeSet.Count',ExpCount,V.ASNodeSet.Count);
|
||||||
|
if V.ASNodeSet.Count=0 then exit;
|
||||||
|
FirstNode:=TDOMElement(V.AsNodeSet[0]);
|
||||||
|
if ExpFirstTagName<>'' then begin
|
||||||
|
AssertEquals('xpath="'+XPath+'": node',ExpFirstTagName,FirstNode.TagName);
|
||||||
|
end;
|
||||||
|
finally
|
||||||
|
V.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
var
|
var
|
||||||
xml: String;
|
xml: String;
|
||||||
ss: TStringStream;
|
ss: TStringStream;
|
||||||
@ -103,10 +125,10 @@ begin
|
|||||||
+'<bookstore>'+LineEnding
|
+'<bookstore>'+LineEnding
|
||||||
+' <book>'+LineEnding
|
+' <book>'+LineEnding
|
||||||
+' <title lang="en">Lazarus</title>'+LineEnding
|
+' <title lang="en">Lazarus</title>'+LineEnding
|
||||||
+' <author>Michael Van Canneyt</author>'+LineEnding
|
+' <author forename="Michael" surname="Van Canneyt"/>'+LineEnding
|
||||||
+' <author>Mattias Gaertner</author>'+LineEnding
|
+' <author forname="Mattias" surname="Gaertner"/>'+LineEnding
|
||||||
+' <author>Felipe Monteiro de Carvalho</author>'+LineEnding
|
+' <author forename="Felipe" surname="de Carvalho">Felipe Monteiro de Carvalho</author>'+LineEnding
|
||||||
+' <author>Swen Heinig</author>'+LineEnding
|
+' <author forename="Swen" surname="Heinig"/>'+LineEnding
|
||||||
+' <year>2011</year>'+LineEnding
|
+' <year>2011</year>'+LineEnding
|
||||||
+' <price>37,50</price>'+LineEnding
|
+' <price>37,50</price>'+LineEnding
|
||||||
+' </book>'+LineEnding
|
+' </book>'+LineEnding
|
||||||
@ -118,7 +140,7 @@ begin
|
|||||||
ReadXMLFile(Doc,ss);
|
ReadXMLFile(Doc,ss);
|
||||||
BookStoreNode:=Doc.DocumentElement;
|
BookStoreNode:=Doc.DocumentElement;
|
||||||
|
|
||||||
// check return type
|
// CheckNode return type
|
||||||
V:=EvaluateXPathExpression('/bookstore',BookStoreNode);
|
V:=EvaluateXPathExpression('/bookstore',BookStoreNode);
|
||||||
debugln(['TTestLazXML.TestXPath ',dbgsname(V)]);
|
debugln(['TTestLazXML.TestXPath ',dbgsname(V)]);
|
||||||
AssertEquals('/bookstore returns class',TXPathNodeSetVariable,V.ClassType);
|
AssertEquals('/bookstore returns class',TXPathNodeSetVariable,V.ClassType);
|
||||||
@ -130,13 +152,12 @@ begin
|
|||||||
AssertEquals('/bookstore node',True,Node=BookStoreNode);
|
AssertEquals('/bookstore node',True,Node=BookStoreNode);
|
||||||
FreeAndNil(V);
|
FreeAndNil(V);
|
||||||
|
|
||||||
// check //
|
// CheckNode //
|
||||||
V:=EvaluateXPathExpression('//book',BookStoreNode);
|
CheckNode(BookStoreNode,'book',1,'book');
|
||||||
AssertEquals('//book AsNodeSet',True,V.AsNodeSet<>nil);
|
CheckNode(BookStoreNode,'//book',1,'book');
|
||||||
AssertEquals('//book AsNodeSet.Count',1,V.ASNodeSet.Count);
|
CheckNode(BookStoreNode,'book/title',1,'title');
|
||||||
Node:=TDOMElement(V.AsNodeSet[0]);
|
CheckNode(BookStoreNode,'book/author',4,'author');
|
||||||
AssertEquals('//book node','book',Node.TagName);
|
CheckNode(BookStoreNode,'book//title',1,'title');
|
||||||
|
|
||||||
finally
|
finally
|
||||||
V.Free;
|
V.Free;
|
||||||
Doc.Free;
|
Doc.Free;
|
||||||
|
Loading…
Reference in New Issue
Block a user