1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
{
Test all with:
./runtests --format=plain --suite=TTestLazXML
Test specific with:
./runtests --format=plain --suite=TestStrToXMLValue
./runtests --format=plain --suite=TestXMLValueToStr
./runtests --format=plain --suite=TestTranslateUTF8Chars
./runtests --format=plain --suite=TestXPath
}
unit TestLazXML;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testglobals, laz2_DOM, laz2_xmlutils, laz2_xpath,
laz2_XMLRead, LazLogger;
type
{ TTestLazXML }
TTestLazXML = class(TTestCase)
public
published
procedure TestStrToXMLValue;
procedure TestXMLValueToStr;
procedure TestTranslateUTF8Chars;
procedure TestXPath;
end;
implementation
{ TTestLazXML }
procedure TTestLazXML.TestStrToXMLValue;
begin
AssertEquals('Empty string','',StrToXMLValue(''));
AssertEquals('Short string','a',StrToXMLValue('a'));
AssertEquals('String with #0','',StrToXMLValue(#0));
AssertEquals('String with &','&',StrToXMLValue('&'));
AssertEquals('String with <','<',StrToXMLValue('<'));
AssertEquals('String with >','>',StrToXMLValue('>'));
AssertEquals('String with ''',''',StrToXMLValue(''''));
AssertEquals('String with "','"',StrToXMLValue('"'));
AssertEquals('String mix 1','<a>"',StrToXMLValue('<a>'#0'"'));
AssertEquals('String mix 2','abc',StrToXMLValue('abc'));
end;
procedure TTestLazXML.TestXMLValueToStr;
begin
AssertEquals('Empty string','',XMLValueToStr(''));
AssertEquals('Short string a','a',XMLValueToStr('a'));
AssertEquals('Short string #0',#0,XMLValueToStr(#0));
AssertEquals('Short string abc','abc',XMLValueToStr('abc'));
AssertEquals('String with &','&',XMLValueToStr('&'));
AssertEquals('String with <','<',XMLValueToStr('<'));
AssertEquals('String with >','>',XMLValueToStr('>'));
AssertEquals('String with ''','''',XMLValueToStr('''));
AssertEquals('String with "','"',XMLValueToStr('"'));
AssertEquals('String mix <a>"','<a>"',XMLValueToStr('<a>"'));
end;
procedure TTestLazXML.TestTranslateUTF8Chars;
procedure T(Title, s, SrcChars, DstChars, Expected: string);
var
h: String;
begin
h:=s;
TranslateUTF8Chars(h,SrcChars,DstChars);
if h=Expected then exit;
AssertEquals(Title+': s="'+s+'" SrcChars="'+SrcChars+'" DstChars="'+DstChars+'"',Expected,h);
end;
begin
T('empty','','','','');
T('nop','a','b','b','a');
T('a to b','a','a','b','b');
T('switch a,b','abaa','ab','ba','babb');
T('delete a','a','a','','');
T('delete a','aba','a','','b');
T('replace ä with ö','bä','ä','ö','bö');
T('replace ä with ö','äbä','ä','ö','öbö');
T('switch ä,ö','äbö','äö','öä','öbä');
T('delete ä','äbö','ä','','bö');
T('replace ä with a','äbö','ä','a','abö');
end;
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
xml: String;
ss: TStringStream;
Doc: TXMLDocument;
BookStoreNode: TDOMElement;
V: TXPathVariable;
NodeSet: TNodeSet;
Node: TDOMElement;
begin
xml:='<?xml version="1.0"?>'+LineEnding
+'<bookstore>'+LineEnding
+' <book>'+LineEnding
+' <title lang="en">Lazarus</title>'+LineEnding
+' <author forename="Michael" surname="Van Canneyt"/>'+LineEnding
+' <author forename="Mattias" surname="Gaertner"/>'+LineEnding
+' <author forename="Felipe" surname="de Carvalho">Felipe Monteiro de Carvalho</author>'+LineEnding
+' <author forename="Swen" surname="Heinig"/>'+LineEnding
+' <year>2011</year>'+LineEnding
+' <price>37,50</price>'+LineEnding
+' </book>'+LineEnding
+'</bookstore>'+LineEnding;
Doc:=nil;
V:=nil;
ss:=TStringStream.Create(xml);
try
ReadXMLFile(Doc,ss);
BookStoreNode:=Doc.DocumentElement;
// CheckNode return type
V:=EvaluateXPathExpression('/bookstore',BookStoreNode);
//debugln(['TTestLazXML.TestXPath ',dbgsname(V)]);
AssertEquals('/bookstore returns class',TXPathNodeSetVariable,V.ClassType);
NodeSet:=V.AsNodeSet;
AssertEquals('/bookstore AsNodeSet',True,NodeSet<>nil);
AssertEquals('/bookstore AsNodeSet.Count',1,NodeSet.Count);
Node:=TDOMElement(NodeSet[0]);
AssertEquals('/bookstore AsNodeSet[0] class',TDOMElement,Node.ClassType);
AssertEquals('/bookstore node',True,Node=BookStoreNode);
FreeAndNil(V);
// CheckNode //
CheckNode(BookStoreNode,'book',1,'book');
CheckNode(BookStoreNode,'//book',1,'book');
CheckNode(BookStoreNode,'book/title',1,'title');
CheckNode(BookStoreNode,'book/author',4,'author');
CheckNode(BookStoreNode,'book//title',1,'title');
CheckNode(BookStoreNode,'book/author[@forename=''Mattias'']',1,'author');
CheckNode(BookStoreNode,'book/author[@surname=''Gaertner'']',1,'author');
CheckNode(BookStoreNode,'book/author[@forename=''Mattias'' and @surname=''Gaertner'']',1,'author');
CheckNode(BookStoreNode,'book/author[2]',1,'author');
CheckNode(BookStoreNode,'book/author[2][1]',1,'author');
CheckNode(BookStoreNode,'book/author[2][@forename=''Mattias'']',1,'author');
CheckNode(BookStoreNode,'book/author[last()]',1,'author');
CheckNode(BookStoreNode,'book/author[last()-1]',1,'author');
CheckNode(BookStoreNode,'book/author[last()-1][@forename=''Felipe'']',1,'author');
CheckNode(BookStoreNode,'book[year>2000]',1,'book');
CheckNode(BookStoreNode,'book/*[@forename]',4,'author');
CheckNode(BookStoreNode,'book/title|book/price',2,'title');
finally
V.Free;
Doc.Free;
ss.Free;
end;
end;
initialization
AddToLazUtilsTestSuite(TTestLazXML);
end.
|