elements, then take their siblings which are
elements and select them.");
96 | }
97 |
98 | [Test]
99 | public void Empty()
100 | {
101 | Run("*:empty", "Take all elements where the element is empty and select them.");
102 | }
103 |
104 | [Test]
105 | public void FirstChild()
106 | {
107 | Run("*:first-child", "Take all elements which are the first child of their parent and select them.");
108 | }
109 |
110 | [Test]
111 | public void Child()
112 | {
113 | Run("* > p", "Take all elements, then take their immediate children which are
elements and select them.");
114 | }
115 |
116 | [Test] public void Class()
117 | {
118 | Run(".myclass", "Take all elements with a class of 'myclass' and select them.");
119 | }
120 |
121 | [Test] public void LastChild()
122 | {
123 | Run("*:last-child", "Take all elements which are the last child of their parent and select them.");
124 | }
125 |
126 | [Test]
127 | public void NthChild()
128 | {
129 | Run("*:nth-child(2)", "Take all elements where the element has 1n+2-1 sibling before it and select them.");
130 | }
131 |
132 | [Test]
133 | public void OnlyChild()
134 | {
135 | Run("*:only-child", "Take all elements where the element is the only child and select them.");
136 | }
137 |
138 | [Test] public void AttributeDashMatch()
139 | {
140 | Run("*[lang|='en']", "Take all elements which have attribute lang with a hyphen separated value matching 'en' and select them.");
141 | }
142 | [Test] public void AttributeExact()
143 | {
144 | Run("*[title='hithere']", "Take all elements which have attribute title with a value of 'hithere' and select them.");
145 | }
146 | [Test] public void AttributeIncludes()
147 | {
148 | Run("*[title~='hithere']", "Take all elements which have attribute title that includes the word 'hithere' and select them.");
149 | }
150 | [Test] public void AttributeSubstring()
151 | {
152 | Run("*[title*='hithere']", "Take all elements which have attribute title whose value contains 'hithere' and select them.");
153 | }
154 | [Test] public void AttributeSuffixMatch()
155 | {
156 | Run("*[title$='hithere']", "Take all elements which have attribute title whose value ends with 'hithere' and select them.");
157 | }
158 |
159 | [Test]
160 | public void AttributePrefixMatch()
161 | {
162 | Run("*[title^='hithere']", "Take all elements which have attribute title whose value begins with 'hithere' and select them.");
163 | }
164 |
165 | static void Run(string selector, string message)
166 | {
167 | var generator = Parser.Parse(selector, new HumanReadableSelectorGenerator());
168 | Assert.That(generator.Text, Is.EqualTo(message));
169 | }
170 | }
171 | }
172 |
--------------------------------------------------------------------------------
/tests/NamespacePrefixTests.cs:
--------------------------------------------------------------------------------
1 | #region Copyright and License
2 | //
3 | // Fizzler - CSS Selector Engine for Microsoft .NET Framework
4 | // Copyright (c) 2009 Atif Aziz, Colin Ramsay. All rights reserved.
5 | //
6 | // This library is free software; you can redistribute it and/or modify it under
7 | // the terms of the GNU Lesser General Public License as published by the Free
8 | // Software Foundation; either version 3 of the License, or (at your option)
9 | // any later version.
10 | //
11 | // This library is distributed in the hope that it will be useful, but WITHOUT
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 | // details.
15 | //
16 | // You should have received a copy of the GNU Lesser General Public License
17 | // along with this library; if not, write to the Free Software Foundation, Inc.,
18 | // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 | //
20 | #endregion
21 |
22 | namespace Fizzler.Tests
23 | {
24 | using NUnit.Framework;
25 |
26 | [TestFixture]
27 | public class NamespacePrefixTests
28 | {
29 | [Test]
30 | public void Initialization()
31 | {
32 | Assert.That(new NamespacePrefix("foo").Text, Is.EqualTo("foo"));
33 | }
34 |
35 | [Test]
36 | public void NoneText()
37 | {
38 | Assert.That(NamespacePrefix.None.Text, Is.Null);
39 | }
40 |
41 | [Test]
42 | public void NoneIsNone()
43 | {
44 | Assert.That(NamespacePrefix.None.IsNone, Is.True);
45 | }
46 |
47 | [Test]
48 | public void NoneIsNotAny()
49 | {
50 | Assert.That(NamespacePrefix.None.IsAny, Is.False);
51 | }
52 |
53 | [Test]
54 | public void NoneIsNotEmpty()
55 | {
56 | Assert.That(NamespacePrefix.None.IsEmpty, Is.False);
57 | }
58 |
59 | [Test]
60 | public void NoneIsNotSpecific()
61 | {
62 | Assert.That(NamespacePrefix.None.IsSpecific, Is.False);
63 | }
64 |
65 | [Test]
66 | public void AnyText()
67 | {
68 | Assert.That(NamespacePrefix.Any.Text, Is.EqualTo("*"));
69 | }
70 |
71 | [Test]
72 | public void AnyIsNotNone()
73 | {
74 | Assert.That(NamespacePrefix.Any.IsNone, Is.False);
75 | }
76 |
77 | [Test]
78 | public void AnyIsAny()
79 | {
80 | Assert.That(NamespacePrefix.Any.IsAny, Is.True);
81 | }
82 |
83 | [Test]
84 | public void AnyIsNotEmpty()
85 | {
86 | Assert.That(NamespacePrefix.Any.IsEmpty, Is.False);
87 | }
88 |
89 | [Test]
90 | public void AnyIsNotSpecific()
91 | {
92 | Assert.That(NamespacePrefix.Any.IsSpecific, Is.False);
93 | }
94 |
95 | [Test]
96 | public void EmptyText()
97 | {
98 | Assert.That(NamespacePrefix.Empty.Text, Is.EqualTo(string.Empty));
99 | }
100 |
101 | [Test]
102 | public void EmptyIsNotNone()
103 | {
104 | Assert.That(NamespacePrefix.Empty.IsNone, Is.False);
105 | }
106 |
107 | [Test]
108 | public void EmptyIsNotAny()
109 | {
110 | Assert.That(NamespacePrefix.Empty.IsAny, Is.False);
111 | }
112 |
113 | [Test]
114 | public void EmptyIsEmpty()
115 | {
116 | Assert.That(NamespacePrefix.Empty.IsEmpty, Is.True);
117 | }
118 |
119 | [Test]
120 | public void EmptyIsSpecific()
121 | {
122 | Assert.That(NamespacePrefix.Empty.IsSpecific, Is.True);
123 | }
124 |
125 | [Test]
126 | public void Equality()
127 | {
128 | Assert.That(NamespacePrefix.None.Equals(NamespacePrefix.None), Is.True);
129 | Assert.That(NamespacePrefix.Any.Equals(NamespacePrefix.Any), Is.True);
130 | Assert.That(NamespacePrefix.Empty.Equals(NamespacePrefix.Empty), Is.True);
131 | var foo = new NamespacePrefix("foo");
132 | Assert.That(foo.Equals(foo), Is.True);
133 | Assert.That(foo.Equals((object)foo), Is.True);
134 | }
135 |
136 | [Test]
137 | public void Inequality()
138 | {
139 | var foo = new NamespacePrefix("foo");
140 | var bar = new NamespacePrefix("bar");
141 | Assert.That(foo.Equals(bar), Is.False);
142 | Assert.That(foo.Equals((object)bar), Is.False);
143 | }
144 |
145 | [Test]
146 | public void TypeEquality()
147 | {
148 | var foo = new NamespacePrefix("foo");
149 | Assert.That(foo, Is.Not.EqualTo(null));
150 | Assert.That(foo, Is.Not.EqualTo(123));
151 | }
152 |
153 | [Test]
154 | public void NoneHashCode()
155 | {
156 | Assert.That(0, Is.EqualTo(NamespacePrefix.None.GetHashCode()));
157 | }
158 |
159 | [Test]
160 | public void HashCode()
161 | {
162 | var foo = new NamespacePrefix("foo");
163 | var bar = new NamespacePrefix("bar");
164 | Assert.That(foo.GetHashCode() == bar.GetHashCode(), Is.False);
165 | }
166 |
167 | [Test]
168 | public void NoneStringRepresentation()
169 | {
170 | Assert.That(NamespacePrefix.None.ToString(), Is.EqualTo("(none)"));
171 | }
172 |
173 | [Test]
174 | public void EmptyStringRepresentation()
175 | {
176 | Assert.That(NamespacePrefix.Empty.ToString(), Is.EqualTo(string.Empty));
177 | }
178 |
179 | [Test]
180 | public void AnyStringRepresentation()
181 | {
182 | Assert.That(NamespacePrefix.Any.ToString(), Is.EqualTo("*"));
183 | }
184 |
185 | [Test]
186 | public void StringRepresentation()
187 | {
188 | Assert.That(new NamespacePrefix("foo").ToString(), Is.EqualTo("foo"));
189 | }
190 |
191 | [Test]
192 | public void FormatNone()
193 | {
194 | Assert.That(NamespacePrefix.None.Format("name"), Is.EqualTo("name"));
195 | }
196 |
197 | [Test]
198 | public void FormatAny()
199 | {
200 | Assert.That(NamespacePrefix.Any.Format("name"), Is.EqualTo("*|name"));
201 | }
202 |
203 | [Test]
204 | public void FormatEmpty()
205 | {
206 | Assert.That(NamespacePrefix.Empty.Format("name"), Is.EqualTo("|name"));
207 | }
208 |
209 | [Test]
210 | public void Format()
211 | {
212 | Assert.That(new NamespacePrefix("foo").Format("bar"), Is.EqualTo("foo|bar"));
213 | }
214 | }
215 | }
216 |
--------------------------------------------------------------------------------
/tests/ReaderTests.cs:
--------------------------------------------------------------------------------
1 | #region Copyright and License
2 | //
3 | // Fizzler - CSS Selector Engine for Microsoft .NET Framework
4 | // Copyright (c) 2009 Atif Aziz, Colin Ramsay. All rights reserved.
5 | //
6 | // This library is free software; you can redistribute it and/or modify it under
7 | // the terms of the GNU Lesser General Public License as published by the Free
8 | // Software Foundation; either version 3 of the License, or (at your option)
9 | // any later version.
10 | //
11 | // This library is distributed in the hope that it will be useful, but WITHOUT
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 | // details.
15 | //
16 | // You should have received a copy of the GNU Lesser General Public License
17 | // along with this library; if not, write to the Free Software Foundation, Inc.,
18 | // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 | //
20 | #endregion
21 |
22 | namespace Fizzler.Tests
23 | {
24 | #region Imports
25 |
26 | using System;
27 | using System.Collections;
28 | using System.Collections.Generic;
29 | using NUnit.Framework;
30 |
31 | #endregion
32 |
33 | [TestFixture]
34 | public class ReaderTests
35 | {
36 | [Test]
37 | public void NullEnumeratorInitialization()
38 | {
39 | Assert.That(() => new Reader((IEnumerator)null!),
40 | Throws.ArgumentNullException("e"));
41 | }
42 |
43 | [Test]
44 | public void NullEnumerableInitialization()
45 | {
46 | Assert.That(() => new Reader((IEnumerable)null!),
47 | Throws.ArgumentNullException("e"));
48 | }
49 |
50 | [Test]
51 | public void HasMoreWhenEmpty()
52 | {
53 | using var reader = new Reader([]);
54 | Assert.That(reader.HasMore, Is.False);
55 | }
56 |
57 | [Test]
58 | public void HasMoreWhenNotEmpty()
59 | {
60 | using var reader = new Reader(new int[1]);
61 | Assert.That(reader.HasMore, Is.True);
62 | }
63 |
64 | [Test]
65 | public void ReadEmpty()
66 | {
67 | using var reader = new Reader([]);
68 | Assert.That(reader.Read, Throws.InvalidOperationException);
69 | }
70 |
71 | [Test]
72 | public void Unreading()
73 | {
74 | using var reader = new Reader([78, 910]);
75 | reader.Unread(56);
76 | reader.Unread(34);
77 | reader.Unread(12);
78 | Assert.That(reader.Read(), Is.EqualTo(12));
79 | Assert.That(reader.Read(), Is.EqualTo(34));
80 | Assert.That(reader.Read(), Is.EqualTo(56));
81 | Assert.That(reader.Read(), Is.EqualTo(78));
82 | Assert.That(reader.Read(), Is.EqualTo(910));
83 | }
84 |
85 | [Test]
86 | public void PeekEmpty()
87 | {
88 | using var reader = new Reader([]);
89 | Assert.That(reader.Peek, Throws.InvalidOperationException);
90 | }
91 |
92 | [Test]
93 | public void PeekNonEmpty()
94 | {
95 | using var reader = new Reader([12, 34, 56]);
96 | Assert.That(reader.Peek(), Is.EqualTo(12));
97 | Assert.That(reader.Read(), Is.EqualTo(12));
98 | Assert.That(reader.Peek(), Is.EqualTo(34));
99 | Assert.That(reader.Read(), Is.EqualTo(34));
100 | Assert.That(reader.Peek(), Is.EqualTo(56));
101 | Assert.That(reader.Read(), Is.EqualTo(56));
102 | }
103 |
104 | [Test]
105 | public void Enumeration()
106 | {
107 | using var reader = new Reader([12, 34, 56]);
108 | using var e = reader.GetEnumerator();
109 | Assert.That(e.MoveNext(), Is.True);
110 | Assert.That(e.Current, Is.EqualTo(12));
111 | Assert.That(e.MoveNext(), Is.True);
112 | Assert.That(e.Current, Is.EqualTo(34));
113 | Assert.That(e.MoveNext(), Is.True);
114 | Assert.That(e.Current, Is.EqualTo(56));
115 | Assert.That(e.MoveNext(), Is.False);
116 | }
117 |
118 | [Test]
119 | public void EnumerationNonGeneric()
120 | {
121 | using var reader = new Reader([12, 34, 56]);
122 | var e = ((IEnumerable)reader).GetEnumerator();
123 | Assert.That(e.MoveNext(), Is.True);
124 | Assert.That(e.Current, Is.EqualTo(12));
125 | Assert.That(e.MoveNext(), Is.True);
126 | Assert.That(e.Current, Is.EqualTo(34));
127 | Assert.That(e.MoveNext(), Is.True);
128 | Assert.That(e.Current, Is.EqualTo(56));
129 | Assert.That(e.MoveNext(), Is.False);
130 | }
131 |
132 | [Test]
133 | public void CloseDisposes()
134 | {
135 | using var e = new TestEnumerator