libept  0.5.25
vocabulary.test.h
Go to the documentation of this file.
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>
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 
42 {
44  Vocabulary& tags() { return m_tags; }
45 
46  Test _1()
47 {
48  tags(); // this will throw if the open above didn't work
49 }
50 
51  Test _2()
52 {
53  assert( tags().hasFacet( "works-with" ) );
54  assert( !tags().hasFacet( "blah" ) );
55 }
56 
57  Test _3()
58 {
59  assert( tags().hasTag( "works-with::people" ) );
60  assert( !tags().hasTag( "works-with::midgets" ) );
61 }
62 
63  Test _4()
64 {
65  Tag people = tags().tagByName( "works-with::people" ),
66  midgets = tags().tagByName( "works-with::midgets" ),
67  blahg = tags().tagByName( "works-with::blahg" ),
68  text = tags().tagByName( "works-with::text" ),
69  people2 = tags().tagByName( "works-with::people" );
70  assert( people != midgets );
71  assert( people != text );
72  assert( people != blahg );
73  assert( midgets == blahg );
74  assert( midgets == midgets );
75  assert( people == people2 );
76  assert( people == people );
77 }
78 
79  Test _5()
80 {
81  Tag a = tags().tagByName( "works-with::people" ),
82  b = tags().tagByName( "works-with::midgets" );
83  std::set< Tag > s = tags().tags(),
84  f = tags().tags( "works-with" ),
85  n = tags().tags( "nonsense" );
86  assert( set_contains(s, a) );
87  assert( set_contains(f, a) );
88  assert( set_contains(s, f) );
89  assert( !set_contains(s, b) );
90  assert( !set_contains(f, b) );
91  assert( n.empty() );
92 }
93 
94  Test _6()
95 {
96  Facet f = tags().facetByName( "works-with" );
97  Tag t = tags().tagByName( "works-with::people" );
98  assert_eq(f.name(), "works-with");
99  assert_eq(t.name(), "people");
100  assert_eq(t.fullname(), "works-with::people");
101 }
102 
103  Test _7()
104 {
105  Facet f = tags().facetByName( "works-with" );
106  std::set< Tag > x = tags().tags( "works-with" );
107  assert( x == f.tags() );
108 }
109 
110  Test _8()
111 {
112  Facet f = tags().facetByName( "does-not-work-with" );
113  int x = 1;
114  try {
115  f.tags();
116  x = 2;
117  } catch (...) {
118  x = 3;
119  }
120  assert_eq( x, 3 );
121 }
122 
123  Test _9()
124 {
125  Facet f = tags().facetByName( "legacy" );
126  assert_eq(f.shortDescription(), "");
127  assert_eq(f.longDescription(), "");
128  //assert_eq(f.shortDescription( "weehee" ), "weehee");
129 }
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  Test _11()
138 {
139  // assert that all tags are somehow working
140  std::set<Facet> facets = tags().facets();
141 
142  for (std::set<Facet>::const_iterator i = facets.begin();
143  i != facets.end(); i++)
144  {
145  i->name(string("foo"));
146  i->shortDescription(string("foo"));
147  i->longDescription(string("foo"));
148  i->tags();
149  }
150 }
151 
152  Test _12()
153 {
154  // assert that all tags are somehow working
155  std::set<Tag> tags = this->tags().tags();
156 
157  for (std::set<Tag>::const_iterator i = tags.begin();
158  i != tags.end(); i++)
159  {
160  i->name(string("foo"));
161  i->fullname(string("foo"));
162  i->shortDescription(string("foo"));
163  i->longDescription(string("foo"));
164  }
165 }
166 
167 // Check for correctness of the first and last tag in the vocabulary
168  Test _13()
169 {
170  Vocabulary& tags = this->tags();
171 
172  Tag first = tags.tagByName("accessibility::TODO");
173  assert(first != Tag());
174  assert_eq(first.fullname(), string("accessibility::TODO"));
175  assert_eq(first.name(), string("TODO"));
176  assert_eq(first.shortDescription(), string("Need an extra tag"));
177 
178  Tag last = tags.tagByName("x11::xserver");
179  assert(last != Tag());
180  assert_eq(last.fullname(), string("x11::xserver"));
181  assert_eq(last.name(), string("xserver"));
182  assert_eq(last.shortDescription(), string("X Server"));
183 }
184 
185  Test _14()
186 {
187  // assert that it's possible to go from facet to ID and back
188  std::set<Facet> facets = tags().facets();
189 
190  for (std::set<Facet>::const_iterator i = facets.begin();
191  i != facets.end(); i++)
192  {
193  Facet f = tags().facetByID(i->id());
194  assert_eq(*i, f);
195  assert_eq(i->name(), f.name());
196  assert_eq(i->shortDescription(), f.shortDescription());
197  assert_eq(i->longDescription(), f.longDescription());
198  assert_eq(i->tags().size(), f.tags().size());
199  }
200 }
201 
202  Test _15()
203 {
204  // assert that it's possible to go from tag to ID and back
205  std::set<Tag> tags = this->tags().tags();
206 
207  for (std::set<Tag>::const_iterator i = tags.begin();
208  i != tags.end(); i++)
209  {
210  Tag t = this->tags().tagByID(i->id());
211  assert_eq(*i, t);
212  assert_eq(i->name(), t.name());
213  assert_eq(i->fullname(), t.fullname());
214  assert_eq(i->shortDescription(), t.shortDescription());
215  assert_eq(i->longDescription(), t.longDescription());
216  }
217 }
218 
219  Test _16()
220 {
221  // assert that facet IDs are distinct
222  std::set<Facet> facets = tags().facets();
223  std::set<int> ids;
224  for (std::set<Facet>::const_iterator i = facets.begin();
225  i != facets.end(); i++)
226  ids.insert(i->id());
227 
228  assert_eq(facets.size(), ids.size());
229 }
230 
231  Test _17()
232 {
233  // assert that tag IDs are distinct
234  std::set<Tag> tags = this->tags().tags();
235  std::set<int> ids;
236  for (std::set<Tag>::const_iterator i = tags.begin();
237  i != tags.end(); i++)
238  ids.insert(i->id());
239 
240  assert_eq(tags.size(), ids.size());
241 }
242 
243  Test _18()
244 {
245  // assert that all the tags are indexed
247  tagcoll::input::Stdio in(ept::debtags::Path::vocabulary());
248  voc.read(in);
249  std::set<std::string> all = voc.tagNames();
250  for (std::set<std::string>::const_iterator i = all.begin();
251  i != all.end(); ++i)
252  assert(this->tags().hasTag(*i));
253 
254  // There should be the same amount of tags in both
255  std::set<Tag> allTags = this->tags().tags();
256  assert_eq(all.size(), allTags.size());
257 }
258 
259  Test _19()
260 {
261  // test the tagcmp function
262 
263  // If unfaceted, same as strcmp
264  assert(ept::debtags::tagcmp("antani", "blinda") < 0);
265  assert(ept::debtags::tagcmp("blinda", "antani") > 0);
266  assert_eq(ept::debtags::tagcmp("antani", "antani"), 0);
267 
268  // If the same and faceted, should work
269  assert_eq(ept::debtags::tagcmp("antani::blinda", "antani::blinda"), 0);
270 
271  // With different facet names, work just as strcmp
272  assert(ept::debtags::tagcmp("antani::blinda", "blinda::blinda") < 0);
273  assert(ept::debtags::tagcmp("blinda::blinda", "antani::blinda") > 0);
274  assert(ept::debtags::tagcmp("anta::blinda", "antani::blinda") < 0);
275  assert(ept::debtags::tagcmp("antani::blinda", "anta::blinda") > 0);
276  assert(ept::debtags::tagcmp("anta::blinda", "anta-ni::blinda") < 0);
277  assert(ept::debtags::tagcmp("anta-ni::blinda", "anta::blinda") > 0);
278 
279  // With same facet names, work just as strcmp on the tags
280  assert(ept::debtags::tagcmp("a::antani", "a::blinda") < 0);
281  assert(ept::debtags::tagcmp("a::blinda", "a::antani") > 0);
282  assert(ept::debtags::tagcmp("a::anta", "a::antani") < 0);
283  assert(ept::debtags::tagcmp("a::antani", "a::anta") > 0);
284  assert(ept::debtags::tagcmp("a::anta", "a::anta-ni") < 0);
285  assert(ept::debtags::tagcmp("a::anta-ni", "a::anta") > 0);
286 }
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  Test _21()
300 {
301  Path::OverrideDebtagsSourceDir odsd("./empty");
302  Path::OverrideDebtagsIndexDir odid("./empty");
303  Path::OverrideDebtagsUserSourceDir odusd("./empty");
304  Path::OverrideDebtagsUserIndexDir oduid("./empty");
305  Vocabulary empty;
306 
307  assert(!empty.hasData());
308 
309  set<Facet> facets = empty.facets();
310  assert_eq(facets.size(), 0u);
311 
312  set<Tag> tags = empty.tags();
313  assert_eq(tags.size(), 0u);
314 }
315 
316 };
317 
318 // vim:set ts=4 sw=4:
Test _6()
Definition: vocabulary.test.h:94
Definition: debtags/maint/path.h:82
Definition: test.h:24
std::string name() const
Return the name of the facet.
Definition: tag.cc:36
std::string longDescription() const
Return the long description of the tag.
Definition: tag.cc:124
Vocabulary & tags()
Definition: vocabulary.test.h:44
Test _16()
Definition: vocabulary.test.h:219
Vocabulary m_tags
Definition: vocabulary.test.h:43
Test _4()
Definition: vocabulary.test.h:63
Test _19()
Definition: vocabulary.test.h:259
Test _14()
Definition: vocabulary.test.h:185
void read(tagcoll::input::Input &input)
Parse and import the vocabulary from `input', merging the data with the previously imported ones...
Definition: vocabularymerger.cc:115
Test _3()
Definition: vocabulary.test.h:57
Definition: packagerecord.test.h:22
debtags paths
Definition: debtags/maint/path.h:72
Test _12()
Definition: vocabulary.test.h:152
-*- C++ -*- (c) 2006, 2007 Petr Rockai me@mornfall.net
Definition: apt.cc:43
std::string shortDescription() const
Return the short description of the tag.
Definition: tag.cc:113
Test _8()
Definition: vocabulary.test.h:110
Test _18()
Definition: vocabulary.test.h:243
Test _10()
Definition: vocabulary.test.h:131
std::set< Tag > tags() const
Return all the tags in the vocabulary.
Definition: vocabulary.h:219
Definition: debtags.cc:53
std::set< Facet > facets() const
Return all the facets in the vocabulary.
Definition: vocabulary.h:208
std::set< Tag > tags() const
Return the list of tags in this facet.
Definition: tag.cc:76
Definition: vocabulary.test.h:41
Representation of a tag.
Definition: tag.h:163
Definition: debtags/maint/path.h:92
Representation of a facet.
Definition: tag.h:60
Test _13()
Definition: vocabulary.test.h:168
int tagcmp(const char *tag1, const char *tag2)
Definition: vocabulary.cc:65
Test _7()
Definition: vocabulary.test.h:103
Test _11()
Definition: vocabulary.test.h:137
std::string longDescription() const
Return the long description of the facet.
Definition: tag.cc:58
std::string fullname() const
Return the name of the tag, with the facet:: prefix.
Definition: tag.cc:102
Definition: vocabulary.h:37
Test _17()
Definition: vocabulary.test.h:231
Test _21()
Definition: vocabulary.test.h:299
std::string shortDescription() const
Return the short description of the facet.
Definition: tag.cc:47
std::string name() const
Return the name of the tag, without the facet:: prefix.
Definition: tag.cc:91
Test _1()
Definition: vocabulary.test.h:46
Definition: debtags/maint/path.h:102
Definition: vocabularymerger.h:33
Test _2()
Definition: vocabulary.test.h:51
Tag tagByName(const std::string &fullname) const
Return the tag with the given full name.
Definition: vocabulary.h:203
bool hasData() const
Return true if this data source has data, false if it's empty.
Definition: vocabulary.h:149
Test _5()
Definition: vocabulary.test.h:79
Test _20()
Definition: vocabulary.test.h:288
std::set< std::string > tagNames() const
Return a set with all tag names.
Definition: vocabularymerger.cc:201
Test _15()
Definition: vocabulary.test.h:202
Test _9()
Definition: vocabulary.test.h:123
static std::string vocabulary()
Definition: debtags/maint/path.cc:94