1 : /*
2 : * Tag vocabulary access
3 : *
4 : * Copyright (C) 2003--2007 Enrico Zini <enrico@debian.org>
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 2 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program; if not, write to the Free Software
18 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 : */
20 :
21 : #include <wibble/test.h>
22 : #include <ept/debtags/vocabulary.h>
23 : #include <ept/debtags/maint/vocabularymerger.h>
24 : #include <ept/debtags/maint/path.h>
25 : #include <tagcoll/utils/set.h>
26 : #include <tagcoll/input/stdio.h>
27 :
28 : #include "debtags.test.h"
29 :
30 : // This is not exported by default
31 : namespace ept {
32 : namespace debtags {
33 : int tagcmp(const char* tag1, const char* tag2);
34 : }
35 : }
36 :
37 : using namespace std;
38 : using namespace tagcoll::utils;
39 : using namespace ept::debtags;
40 :
41 : struct TestVocabulary : DebtagsTestEnvironment
42 38 : {
43 : Vocabulary m_tags;
44 1298 : Vocabulary& tags() { return m_tags; }
45 :
46 1 : Test _1()
47 : {
48 1 : tags(); // this will throw if the open above didn't work
49 1 : }
50 :
51 1 : Test _2()
52 : {
53 1 : assert( tags().hasFacet( "works-with" ) );
54 2 : assert( !tags().hasFacet( "blah" ) );
55 1 : }
56 :
57 1 : Test _3()
58 : {
59 1 : assert( tags().hasTag( "works-with::people" ) );
60 2 : assert( !tags().hasTag( "works-with::midgets" ) );
61 1 : }
62 :
63 1 : Test _4()
64 : {
65 1 : Tag people = tags().tagByName( "works-with::people" ),
66 2 : midgets = tags().tagByName( "works-with::midgets" ),
67 2 : blahg = tags().tagByName( "works-with::blahg" ),
68 2 : text = tags().tagByName( "works-with::text" ),
69 2 : people2 = tags().tagByName( "works-with::people" );
70 2 : assert( people != midgets );
71 2 : assert( people != text );
72 2 : assert( people != blahg );
73 2 : assert( midgets == blahg );
74 2 : assert( midgets == midgets );
75 2 : assert( people == people2 );
76 2 : assert( people == people );
77 1 : }
78 :
79 1 : Test _5()
80 : {
81 1 : Tag a = tags().tagByName( "works-with::people" ),
82 2 : b = tags().tagByName( "works-with::midgets" );
83 2 : std::set< Tag > s = tags().tags(),
84 1 : f = tags().tags( "works-with" ),
85 2 : n = tags().tags( "nonsense" );
86 2 : assert( set_contains(s, a) );
87 2 : assert( set_contains(f, a) );
88 2 : assert( set_contains(s, f) );
89 2 : assert( !set_contains(s, b) );
90 2 : assert( !set_contains(f, b) );
91 2 : assert( n.empty() );
92 1 : }
93 :
94 1 : Test _6()
95 : {
96 1 : Facet f = tags().facetByName( "works-with" );
97 2 : Tag t = tags().tagByName( "works-with::people" );
98 2 : assert_eq(f.name(), "works-with");
99 1 : assert_eq(t.name(), "people");
100 1 : assert_eq(t.fullname(), "works-with::people");
101 1 : }
102 :
103 1 : Test _7()
104 : {
105 1 : Facet f = tags().facetByName( "works-with" );
106 2 : std::set< Tag > x = tags().tags( "works-with" );
107 2 : assert( x == f.tags() );
108 1 : }
109 :
110 1 : Test _8()
111 : {
112 1 : Facet f = tags().facetByName( "does-not-work-with" );
113 1 : int x = 1;
114 : try {
115 1 : f.tags();
116 0 : x = 2;
117 2 : } catch (...) {
118 1 : x = 3;
119 : }
120 1 : assert_eq( x, 3 );
121 1 : }
122 :
123 1 : Test _9()
124 : {
125 1 : Facet f = tags().facetByName( "legacy" );
126 2 : assert_eq(f.shortDescription(), "");
127 1 : assert_eq(f.longDescription(), "");
128 : //assert_eq(f.shortDescription( "weehee" ), "weehee");
129 1 : }
130 :
131 : Test _10()
132 : {
133 : // assert that one-character tag names are parsed correctly
134 : assert( tags().hasTag( "implemented-in::c" ) );
135 : }
136 :
137 1 : Test _11()
138 : {
139 : // assert that all tags are somehow working
140 1 : std::set<Facet> facets = tags().facets();
141 :
142 30 : for (std::set<Facet>::const_iterator i = facets.begin();
143 : i != facets.end(); i++)
144 : {
145 29 : i->name(string("foo"));
146 58 : i->shortDescription(string("foo"));
147 58 : i->longDescription(string("foo"));
148 58 : i->tags();
149 1 : }
150 1 : }
151 :
152 1 : Test _12()
153 : {
154 : // assert that all tags are somehow working
155 1 : std::set<Tag> tags = this->tags().tags();
156 :
157 1242 : for (std::set<Tag>::const_iterator i = tags.begin();
158 : i != tags.end(); i++)
159 : {
160 620 : i->name(string("foo"));
161 1240 : i->fullname(string("foo"));
162 1240 : i->shortDescription(string("foo"));
163 1240 : i->longDescription(string("foo"));
164 1 : }
165 1 : }
166 :
167 : // Check for correctness of the first and last tag in the vocabulary
168 1 : Test _13()
169 : {
170 1 : Vocabulary& tags = this->tags();
171 :
172 1 : Tag first = tags.tagByName("accessibility::TODO");
173 2 : assert(first != Tag());
174 1 : assert_eq(first.fullname(), string("accessibility::TODO"));
175 2 : assert_eq(first.name(), string("TODO"));
176 2 : assert_eq(first.shortDescription(), string("Need an extra tag"));
177 :
178 2 : Tag last = tags.tagByName("x11::xserver");
179 2 : assert(last != Tag());
180 1 : assert_eq(last.fullname(), string("x11::xserver"));
181 2 : assert_eq(last.name(), string("xserver"));
182 2 : assert_eq(last.shortDescription(), string("X Server"));
183 1 : }
184 :
185 1 : Test _14()
186 : {
187 : // assert that it's possible to go from facet to ID and back
188 1 : std::set<Facet> facets = tags().facets();
189 :
190 30 : for (std::set<Facet>::const_iterator i = facets.begin();
191 : i != facets.end(); i++)
192 : {
193 29 : Facet f = tags().facetByID(i->id());
194 29 : assert_eq(*i, f);
195 29 : assert_eq(i->name(), f.name());
196 29 : assert_eq(i->shortDescription(), f.shortDescription());
197 29 : assert_eq(i->longDescription(), f.longDescription());
198 29 : assert_eq(i->tags().size(), f.tags().size());
199 1 : }
200 1 : }
201 :
202 1 : Test _15()
203 : {
204 : // assert that it's possible to go from tag to ID and back
205 1 : std::set<Tag> tags = this->tags().tags();
206 :
207 621 : for (std::set<Tag>::const_iterator i = tags.begin();
208 : i != tags.end(); i++)
209 : {
210 620 : Tag t = this->tags().tagByID(i->id());
211 620 : assert_eq(*i, t);
212 620 : assert_eq(i->name(), t.name());
213 620 : assert_eq(i->fullname(), t.fullname());
214 620 : assert_eq(i->shortDescription(), t.shortDescription());
215 620 : assert_eq(i->longDescription(), t.longDescription());
216 1 : }
217 1 : }
218 :
219 1 : Test _16()
220 : {
221 : // assert that facet IDs are distinct
222 1 : std::set<Facet> facets = tags().facets();
223 1 : std::set<int> ids;
224 30 : for (std::set<Facet>::const_iterator i = facets.begin();
225 : i != facets.end(); i++)
226 29 : ids.insert(i->id());
227 :
228 1 : assert_eq(facets.size(), ids.size());
229 1 : }
230 :
231 1 : Test _17()
232 : {
233 : // assert that tag IDs are distinct
234 1 : std::set<Tag> tags = this->tags().tags();
235 1 : std::set<int> ids;
236 621 : for (std::set<Tag>::const_iterator i = tags.begin();
237 : i != tags.end(); i++)
238 620 : ids.insert(i->id());
239 :
240 1 : assert_eq(tags.size(), ids.size());
241 1 : }
242 :
243 1 : Test _18()
244 : {
245 : // assert that all the tags are indexed
246 1 : ept::debtags::VocabularyMerger voc;
247 1 : tagcoll::input::Stdio in(ept::debtags::Path::vocabulary());
248 1 : voc.read(in);
249 1 : std::set<std::string> all = voc.tagNames();
250 1242 : for (std::set<std::string>::const_iterator i = all.begin();
251 : i != all.end(); ++i)
252 620 : assert(this->tags().hasTag(*i));
253 :
254 : // There should be the same amount of tags in both
255 1 : std::set<Tag> allTags = this->tags().tags();
256 1 : assert_eq(all.size(), allTags.size());
257 1 : }
258 :
259 1 : Test _19()
260 : {
261 : // test the tagcmp function
262 :
263 : // If unfaceted, same as strcmp
264 1 : assert(ept::debtags::tagcmp("antani", "blinda") < 0);
265 2 : assert(ept::debtags::tagcmp("blinda", "antani") > 0);
266 2 : assert_eq(ept::debtags::tagcmp("antani", "antani"), 0);
267 :
268 : // If the same and faceted, should work
269 2 : assert_eq(ept::debtags::tagcmp("antani::blinda", "antani::blinda"), 0);
270 :
271 : // With different facet names, work just as strcmp
272 2 : assert(ept::debtags::tagcmp("antani::blinda", "blinda::blinda") < 0);
273 2 : assert(ept::debtags::tagcmp("blinda::blinda", "antani::blinda") > 0);
274 2 : assert(ept::debtags::tagcmp("anta::blinda", "antani::blinda") < 0);
275 2 : assert(ept::debtags::tagcmp("antani::blinda", "anta::blinda") > 0);
276 2 : assert(ept::debtags::tagcmp("anta::blinda", "anta-ni::blinda") < 0);
277 2 : assert(ept::debtags::tagcmp("anta-ni::blinda", "anta::blinda") > 0);
278 :
279 : // With same facet names, work just as strcmp on the tags
280 2 : assert(ept::debtags::tagcmp("a::antani", "a::blinda") < 0);
281 2 : assert(ept::debtags::tagcmp("a::blinda", "a::antani") > 0);
282 2 : assert(ept::debtags::tagcmp("a::anta", "a::antani") < 0);
283 2 : assert(ept::debtags::tagcmp("a::antani", "a::anta") > 0);
284 2 : assert(ept::debtags::tagcmp("a::anta", "a::anta-ni") < 0);
285 2 : assert(ept::debtags::tagcmp("a::anta-ni", "a::anta") > 0);
286 1 : }
287 :
288 : Test _20()
289 : {
290 : // check that we're seeing all the tags for a facet
291 : std::set<Tag> t = tags().tags("accessibility");
292 : assert_eq(t.size(), 10u);
293 :
294 : t = tags().tags("works-with-format");
295 : assert_eq(t.size(), 33u);
296 : }
297 :
298 : // If there is no data, Vocabulary should work as an empty vocabulary
299 1 : Test _21()
300 : {
301 1 : Path::OverrideDebtagsSourceDir odsd("./empty");
302 2 : Path::OverrideDebtagsIndexDir odid("./empty");
303 2 : Path::OverrideDebtagsUserSourceDir odusd("./empty");
304 2 : Path::OverrideDebtagsUserIndexDir oduid("./empty");
305 1 : Vocabulary empty;
306 :
307 1 : assert(!empty.hasData());
308 :
309 1 : set<Facet> facets = empty.facets();
310 1 : assert_eq(facets.size(), 0u);
311 :
312 1 : set<Tag> tags = empty.tags();
313 1 : assert_eq(tags.size(), 0u);
314 1 : }
315 :
316 : };
317 :
318 : // vim:set ts=4 sw=4:
|