File: testctxmlfixfragments.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (130 lines) | stat: -rw-r--r-- 4,262 bytes parent folder | download | duplicates (6)
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
{
 Test all with:
     ./runtests --format=plain --suite=TTestCTXMLFixFragment

 Test individually:
     ./runtests --format=plain --suite=TestFixXMLFragmentComment
     ./runtests --format=plain --suite=TestFixXMLValue
}
unit TestCTXMLFixFragments;

{$mode objfpc}{$H+}

interface

uses
  fpcunit, testregistry, Classes, SysUtils, FileProcs,
  CTXMLFixFragment;

type

  { TTestCTXMLFixFragment }

  TTestCTXMLFixFragment = class(TTestCase)
  protected
    function TestFrag(Title, Fragment, FixedFragment: string): boolean;
    function TestAttr(Title, Value, FixedValue: string): boolean;
  published
    procedure TestFixXMLFragmentComment;
    procedure TestFixXMLFragmentInvalidCharacters;
    procedure TestFixXMLFragmentOpenTag;
    procedure TestFixXMLFragmentAttribute;
    procedure TestFixXMLFragmentCloseTag;
    procedure TestFixXMLFragmentBugReports;
    // attribute value
    procedure TestFixXMLValue;
  end;

implementation

{ TTestCTXMLFixFragment }

function TTestCTXMLFixFragment.TestFrag(Title, Fragment, FixedFragment: string
  ): boolean;
var
  s: String;
begin
  Result:=true;
  s:=Fragment;
  FixFPDocFragment(s,true,true,nil,false);
  AssertEquals(Title+' fragment: '+DbgStr(Fragment),dbgstr(FixedFragment),dbgstr(s));
end;

function TTestCTXMLFixFragment.TestAttr(Title, Value, FixedValue: string
  ): boolean;
var
  s: String;
begin
  Result:=true;
  s:=Value;
  FixFPDocAttributeValue(s);
  AssertEquals(Title+' value: '+DbgStr(Value),dbgstr(FixedValue),dbgstr(s));
end;

procedure TTestCTXMLFixFragment.TestFixXMLFragmentComment;
begin
  TestFrag('close comment','<!--','<!---->');
  TestFrag('close comment and delete invalid char','<!--null'#0#1#2'comment','<!--nullcomment-->');
end;

procedure TTestCTXMLFixFragment.TestFixXMLFragmentInvalidCharacters;
begin
  TestFrag('delete special characters','A'#0'B'#1#127,'AB');
  TestFrag('replace tag characters','LT< GT>AMP&','LT&lt; GT&gt;AMP&amp;');
  TestFrag('lower case special characters','&LT;','&lt;');
end;

procedure TTestCTXMLFixFragment.TestFixXMLFragmentOpenTag;
begin
  TestFrag('valid short tag','<link/>','<link/>');
  TestFrag('valid short with empty attribute tag','<link id=""/>','<link id=""/>');
  TestFrag('missing tag name','<>','&lt;&gt;');
  TestFrag('lower case tag name','<A></a>','<a></a>');
  TestFrag('invalid character in tag','<a "></a>','<a >"&gt;</a>');
end;

procedure TTestCTXMLFixFragment.TestFixXMLFragmentAttribute;
begin
  TestFrag('lower case attribute name','<a Name=""></a>','<a name=""></a>');
  TestFrag('missing attribute equal','<a name ""></a>','<a name =""></a>');
  TestFrag('missing attribute value','<a name=></a>','<a name=""></a>');
  TestFrag('missing attribute quotes','<a name=1></a>','<a name="1"></a>');
  TestFrag('missing attribute ending quote','<a name="1></a>','<a name="1"></a>');
  TestFrag('invalid character in xml fragment attribute value','<a name="&"></a>','<a name="&amp;"></a>');
  TestFrag('amp attribute value','<a name="&amp;"></a>','<a name="&amp;"></a>');
  TestFrag('lt attribute value','<a name="operator&lt;"></a>','<a name="operator&lt;"></a>');
end;

procedure TTestCTXMLFixFragment.TestFixXMLFragmentCloseTag;
begin
  TestFrag('lower case close tag name','<a></A>','<a></a>');
  TestFrag('close open tag','<a>','<a/>');
  TestFrag('close open sub tag','<p><a></p>','<p><a/></p>');
  TestFrag('disable invalid close tag','</p>','&lt;/p&gt;');
end;

procedure TTestCTXMLFixFragment.TestFixXMLFragmentBugReports;
begin
  TestFrag('15120','operator <(TPoint, TPoint): Boolean',
                 'operator &lt;(TPoint, TPoint): Boolean');
  TestFrag('16671','<br>',
               '<br/>');
  TestFrag('18800','<link id="foo"/>','<link id="foo"/>');
end;

procedure TTestCTXMLFixFragment.TestFixXMLValue;
begin
  TestAttr('invalid character in xml attribute value','operator<','operator&lt;');
  TestAttr('correct character in xml attribute value','&amp;','&amp;');
  TestAttr('lower case character name in attribute value','&AMP;','&amp;');
  TestAttr('" in attribute value','"','&quot;');
  TestAttr(''' in attribute value','''','&apos;');
  TestAttr('< in attribute value','<','&lt;');
  TestAttr('> in attribute value','>','&gt;');
end;

initialization
  RegisterTest(TTestCTXMLFixFragment);

end.