~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/UnitTests/MonoDevelop.Xml.StateEngine/ParsingTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ParsingTests.cs
 
3
// 
 
4
// Author:
 
5
//   Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Collections.Generic;
 
31
using MonoDevelop.Ide.Gui.Content;
 
32
using System.Linq;
 
33
 
 
34
using NUnit.Framework;
 
35
 
 
36
 
 
37
namespace MonoDevelop.Xml.StateEngine
 
38
{
 
39
        
 
40
        [TestFixture]
 
41
        public class ParsingTests
 
42
        {
 
43
                
 
44
                public virtual XmlFreeState CreateRootState ()
 
45
                {
 
46
                        return new XmlFreeState ();
 
47
                }
 
48
                
 
49
                [Test]
 
50
                public void AttributeName ()
 
51
                {
 
52
                        TestParser parser = new TestParser (CreateRootState ());
 
53
                        parser.Parse (@"
 
54
<doc>
 
55
        <tag.a>
 
56
                <tag.b id=""$foo"" />
 
57
        </tag.a>
 
58
</doc>
 
59
",
 
60
                                delegate {
 
61
                                        parser.AssertStateIs<XmlDoubleQuotedAttributeValueState> ();
 
62
                                        parser.AssertPath ("//doc/tag.a/tag.b/@id");
 
63
                                }
 
64
                        );
 
65
                        parser.AssertEmpty ();
 
66
                        parser.AssertErrorCount (0);
 
67
                }
 
68
                
 
69
                [Test]
 
70
                public void Attributes ()
 
71
                {
 
72
                        TestParser parser = new TestParser (CreateRootState ());
 
73
                        parser.Parse (@"
 
74
<doc>
 
75
        <tag.a name=""foo"" arg=5 wibble = 6 bar.baz = 'y.ff7]' $ />
 
76
</doc>
 
77
",
 
78
                                delegate {
 
79
                                        parser.AssertStateIs<XmlTagState> ();
 
80
                                        parser.AssertAttributes ("name", "foo", "arg", "5", "wibble", "6", "bar.baz", "y.ff7]");
 
81
                                }
 
82
                        );
 
83
                        parser.AssertEmpty ();
 
84
                        parser.AssertErrorCount (0);
 
85
                }
 
86
                
 
87
                [Test, Ignore ("Not working")]
 
88
                public void AttributeRecovery ()
 
89
                {
 
90
                        TestParser parser = new TestParser (CreateRootState ());
 
91
                        parser.Parse (@"
 
92
<doc>
 
93
        <tag.a>
 
94
                <tag.b arg='fff' sdd = sdsds= 'foo' ff $ />
 
95
        </tag.a>
 
96
<a><b valid/></a>
 
97
</doc>
 
98
",
 
99
                                delegate {
 
100
                                        parser.AssertStateIs<XmlTagState> ();
 
101
                                        parser.AssertAttributes ("arg", "fff", "sdd", "", "sdsds", "foo", "ff", "");
 
102
                                        parser.AssertErrorCount (1);
 
103
                                }
 
104
                        );
 
105
                        parser.AssertEmpty ();
 
106
                        parser.AssertErrorCount (1);
 
107
                }
 
108
                
 
109
                [Test]
 
110
                public void IncompleteTags ()
 
111
                {
 
112
                        TestParser parser = new TestParser (CreateRootState ());
 
113
                        parser.Parse (@"
 
114
<doc>
 
115
        <tag.a att1 >
 
116
                <tag.b att2="" >
 
117
                        <tag.c att3 = ' 
 
118
                                <tag.d att4 = >
 
119
                                        <tag.e att5='' att6=' att7 = >
 
120
                                                <tag.f id='$foo' />
 
121
                                        </tag.e>
 
122
                                </tag.d>
 
123
                        </tag.c>
 
124
                </tag.b>
 
125
        </tag.a>
 
126
</doc>
 
127
",
 
128
                                delegate {
 
129
                                        parser.AssertStateIs<XmlSingleQuotedAttributeValueState> ();
 
130
                                        parser.AssertNodeDepth (9);
 
131
                                        parser.AssertPath ("//doc/tag.a/tag.b/tag.c/tag.d/tag.e/tag.f/@id");
 
132
                                }
 
133
                        );
 
134
                        parser.AssertEmpty ();
 
135
                        parser.AssertErrorCount (3, x => x.ErrorType == ErrorType.Error);
 
136
                        parser.AssertErrorCount (2, x => x.ErrorType == ErrorType.Warning);
 
137
                }
 
138
                
 
139
                [Test]
 
140
                public void Unclosed ()
 
141
                {
 
142
                        TestParser parser = new TestParser (CreateRootState ());
 
143
                        parser.Parse (@"
 
144
<doc>
 
145
        <tag.a>
 
146
                <tag.b><tag.b>$
 
147
        </tag.a>$
 
148
</doc>
 
149
",
 
150
                                delegate {
 
151
                                        parser.AssertStateIs<XmlFreeState> ();
 
152
                                        parser.AssertNodeDepth (5);
 
153
                                        parser.AssertPath ("//doc/tag.a/tag.b/tag.b");
 
154
                                },
 
155
                                delegate {
 
156
                                        parser.AssertStateIs<XmlFreeState> ();
 
157
                                        parser.AssertNodeDepth (2);
 
158
                                        parser.AssertPath ("//doc");
 
159
                                }
 
160
                        );
 
161
                        parser.AssertEmpty ();
 
162
                        parser.AssertErrorCount (2);
 
163
                }
 
164
                
 
165
                [Test]
 
166
                public void Misc ()
 
167
                {
 
168
                        TestParser parser = new TestParser (CreateRootState ());
 
169
                        parser.Parse (@"
 
170
<doc>
 
171
        <!DOCTYPE $  >
 
172
        <![CDATA[ ]  $ ]  ]]>
 
173
        <!--   <foo> <bar arg=""> $  -->
 
174
</doc>
 
175
",
 
176
                                delegate {
 
177
                                        parser.AssertStateIs<XmlDocTypeState> ();
 
178
                                        parser.AssertNodeDepth (3);
 
179
                                        parser.AssertPath ("//doc/<!DOCTYPE>");
 
180
                                },
 
181
                                delegate {
 
182
                                        parser.AssertStateIs<XmlCDataState> ();
 
183
                                        parser.AssertNodeDepth (3);
 
184
                                        parser.AssertPath ("//doc/<![CDATA[ ]]>");
 
185
                                },
 
186
                                delegate {
 
187
                                        parser.AssertStateIs<XmlCommentState> ();
 
188
                                        parser.AssertNodeDepth (3);
 
189
                                        parser.AssertPath ("//doc/<!-- -->");
 
190
                                }
 
191
                        );
 
192
                        parser.AssertEmpty ();
 
193
                        parser.AssertErrorCount (0);
 
194
                }
 
195
                
 
196
                [Test]
 
197
                public void DocTypeCapture ()
 
198
                {
 
199
                        TestParser parser = new TestParser (CreateRootState (), true);
 
200
                        parser.Parse (@"
 
201
                <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN""
 
202
""DTD/xhtml1-strict.dtd""
 
203
[
 
204
<!-- foo -->
 
205
<!bar #baz>
 
206
]>
 
207
<doc><foo/></doc>");
 
208
                        parser.AssertEmpty ();
 
209
                        XDocument doc = (XDocument)parser.Nodes.Peek ();
 
210
                        Assert.IsTrue (doc.FirstChild is XDocType);
 
211
                        XDocType dt = (XDocType) doc.FirstChild;
 
212
                        Assert.AreEqual ("html", dt.RootElement.FullName);
 
213
                        Assert.AreEqual ("-//W3C//DTD XHTML 1.0 Strict//EN", dt.PublicFpi);
 
214
                        Assert.AreEqual ("DTD/xhtml1-strict.dtd", dt.Uri);
 
215
                        Assert.AreEqual (dt.InternalDeclarationRegion.Start.Line, 4);
 
216
                        Assert.AreEqual (dt.InternalDeclarationRegion.End.Line, 7);
 
217
                        parser.AssertNoErrors ();
 
218
                }
 
219
        }
 
220
        
 
221
}