readerunit.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. {**********************************************************************
  2. This file is part of the Free Component Library (FCL)
  3. fpcunit extensions for testing TXmlReader class
  4. Copyright (c) 2008 by Sergei Gorelkin, [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit readerunit;
  12. {$mode objfpc}{$h+}
  13. interface
  14. uses Classes, SysUtils, fpcunit, xmlutils, XmlReader;
  15. type
  16. TTestMethod = procedure(reader: TXMLReader) of object;
  17. TXMLReaderTestBase = class(TTestCase)
  18. protected
  19. procedure AssertStartDocument(r: TXMLReader);
  20. procedure AssertEndDocument(r: TXMLReader);
  21. procedure AssertNode(const id: string;
  22. r: TXMLReader; nodeType: TXMLNodeType;
  23. depth: Integer;
  24. const Name, Prefix, LocalName, nsURI, Value: XMLString;
  25. attrCount: Integer);
  26. procedure AssertNodeValues(id: string;
  27. r: TXmlReader;
  28. nodeType: TXmlNodeType;
  29. depth: Integer;
  30. const name, prefix, localName, nsURI, value: XMLString;
  31. hasValue: Boolean;
  32. attrCount: Integer;
  33. hasAttributes: Boolean);
  34. procedure AssertAttribute(r: TXMLReader;
  35. const Name, Prefix, LocalName, nsURI, Value: XMLString);
  36. procedure AssertEquals(const id: string; exp, act: TXMLReadState); overload;
  37. procedure AssertEquals(exp, act: TXMLReadState); overload;
  38. procedure AssertEquals(const id: string; exp, act: TXMLNodeType); overload;
  39. procedure AssertEqualsW(const id: string; const exp, act: XMLString); overload;
  40. procedure AssertEqualsW(const exp, act: XMLString); overload;
  41. procedure AssertEquals(exp, act: TXMLNodeType); overload;
  42. procedure AssertNull(const id: string; const ws: XMLString); overload;
  43. procedure DoTest(const XmlData: string; method: TTestMethod);
  44. end;
  45. implementation
  46. uses
  47. xmltextreader;
  48. procedure TXMLReaderTestBase.AssertStartDocument(r: TXMLReader);
  49. begin
  50. AssertEquals(r.ReadState, rsInitial);
  51. AssertEquals(r.NodeType, ntNone);
  52. AssertEquals(r.Depth, 0);
  53. AssertFalse(r.EOF);
  54. end;
  55. procedure TXMLReaderTestBase.AssertEndDocument(r: TXMLReader);
  56. begin
  57. AssertFalse ('could read', r.Read);
  58. AssertEquals('NodeType is not ntNone', ntNone, r.NodeType);
  59. AssertEquals('Depth is not 0', 0, r.Depth);
  60. AssertEquals('ReadState is not rsEndOfFile', rsEndOfFile, r.ReadState);
  61. AssertTrue('not EOF', r.EOF);
  62. r.Close;
  63. AssertEquals('ReadState is not rsClosed', rsClosed, r.ReadState);
  64. end;
  65. procedure TXMLReaderTestBase.AssertNode(const id: string;
  66. r: TXMLReader; nodeType: TXMLNodeType;
  67. depth: Integer;
  68. const Name, Prefix, LocalName, nsURI, Value: XMLString;
  69. attrCount: Integer);
  70. begin
  71. AssertTrue(id+' Read() return value', r.Read);
  72. AssertEquals(id+' ReadState', r.ReadState, rsInteractive);
  73. AssertFalse(id+' not EOF', r.EOF);
  74. AssertNodeValues(id, r, nodeType, depth,
  75. Name, Prefix, localName,
  76. nsURI, Value, r.HasValue,
  77. attrCount, attrCount > 0);
  78. end;
  79. procedure TXMLReaderTestBase.AssertNodeValues(id: string;
  80. r: TXmlReader;
  81. nodeType: TXmlNodeType;
  82. depth: Integer;
  83. const name, prefix, localName, nsURI, value: XMLString;
  84. hasValue: Boolean;
  85. attrCount: Integer;
  86. hasAttributes: Boolean);
  87. begin
  88. id := id + '(' + r.ClassName + ')';
  89. AssertEquals(id+': NodeType', nodeType, r.NodeType);
  90. AssertEqualsW(id+': name', name, r.Name);
  91. AssertEqualsW(id+': prefix', prefix, r.Prefix);
  92. AssertEqualsW(id+': localName', localName, r.LocalName);
  93. AssertEqualsW(id+': namespaceURI', nsURI, r.NamespaceURI);
  94. AssertEquals(id+': Depth', depth, r.Depth);
  95. AssertEquals(id+': hasValue', hasValue, r.HasValue);
  96. AssertEqualsW(id+': Value', value, r.Value);
  97. // TODO: AssertEquals(id+': hasAttributes', hasAttributes, r.HasAttributes);
  98. AssertEquals(id+': attributeCount', attrCount, r.AttributeCount);
  99. end;
  100. procedure TXMLReaderTestBase.AssertAttribute(r: TXMLReader; const Name, Prefix,
  101. LocalName, nsURI, Value: XMLString);
  102. begin
  103. AssertEqualsW('value2', value, r.GetAttribute(name));
  104. if nsURI <> '' then
  105. begin
  106. AssertEqualsW('value3', value, r.GetAttribute(LocalName, nsURI));
  107. end;
  108. end;
  109. procedure TXMLReaderTestBase.DoTest(const XmlData: string; method: TTestMethod);
  110. var
  111. xtr: TXmlReader;
  112. settings: TXMLReaderSettings;
  113. inp: TXMLInputSource;
  114. begin
  115. settings := TXMLReaderSettings.Create;
  116. try
  117. settings.PreserveWhiteSpace := True;
  118. settings.Namespaces := True;
  119. inp := TXMLInputSource.Create(XmlData);
  120. try
  121. xtr := TXmlTextReader.Create(inp,settings);
  122. try
  123. method(xtr);
  124. finally
  125. xtr.Free;
  126. end;
  127. finally
  128. inp.Free;
  129. end;
  130. finally
  131. settings.Free;
  132. end;
  133. // here other TXMLReader descendants may be tested the same way...
  134. end;
  135. procedure TXMLReaderTestBase.AssertEquals(const id: string; exp, act: TXMLReadState);
  136. begin
  137. if exp <> act then
  138. Fail(id);
  139. end;
  140. procedure TXMLReaderTestBase.AssertEquals(exp, act: TXMLReadState);
  141. begin
  142. AssertEquals('', exp, act);
  143. end;
  144. procedure TXMLReaderTestBase.AssertEqualsW(const id: string; const exp, act: XMLString);
  145. begin
  146. AssertTrue(id + ComparisonMsg(exp, act), exp = act);
  147. end;
  148. procedure TXMLReaderTestBase.AssertEqualsW(const exp, act: XMLString);
  149. begin
  150. AssertEqualsW('', exp, act);
  151. end;
  152. procedure TXMLReaderTestBase.AssertEquals(const id: string; exp, act: TXMLNodeType);
  153. var
  154. exps,acts: string;
  155. begin
  156. if exp <> act then
  157. begin
  158. Str(exp, exps);
  159. Str(act, acts);
  160. Fail(id+ComparisonMsg(exps,acts));
  161. end;
  162. end;
  163. procedure TXMLReaderTestBase.AssertEquals(exp, act: TXMLNodeType);
  164. begin
  165. AssertEquals('', exp, act);
  166. end;
  167. procedure TXMLReaderTestBase.assertNull(const id: string; const ws: XMLString);
  168. begin
  169. if ws <> '' then
  170. Fail(id);
  171. end;
  172. end.