libept
0.5.25
|
00001 /* 00002 * Tag vocabulary access 00003 * 00004 * Copyright (C) 2003--2007 Enrico Zini <enrico@debian.org> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 #include <wibble/test.h> 00022 #include <ept/debtags/vocabulary.h> 00023 #include <ept/debtags/maint/vocabularymerger.h> 00024 #include <ept/debtags/maint/path.h> 00025 #include <tagcoll/utils/set.h> 00026 #include <tagcoll/input/stdio.h> 00027 00028 #include "debtags.test.h" 00029 00030 // This is not exported by default 00031 namespace ept { 00032 namespace debtags { 00033 int tagcmp(const char* tag1, const char* tag2); 00034 } 00035 } 00036 00037 using namespace std; 00038 using namespace tagcoll::utils; 00039 using namespace ept::debtags; 00040 00041 struct TestVocabulary : DebtagsTestEnvironment 00042 { 00043 Vocabulary m_tags; 00044 Vocabulary& tags() { return m_tags; } 00045 00046 Test _1() 00047 { 00048 tags(); // this will throw if the open above didn't work 00049 } 00050 00051 Test _2() 00052 { 00053 assert( tags().hasFacet( "works-with" ) ); 00054 assert( !tags().hasFacet( "blah" ) ); 00055 } 00056 00057 Test _3() 00058 { 00059 assert( tags().hasTag( "works-with::people" ) ); 00060 assert( !tags().hasTag( "works-with::midgets" ) ); 00061 } 00062 00063 Test _4() 00064 { 00065 Tag people = tags().tagByName( "works-with::people" ), 00066 midgets = tags().tagByName( "works-with::midgets" ), 00067 blahg = tags().tagByName( "works-with::blahg" ), 00068 text = tags().tagByName( "works-with::text" ), 00069 people2 = tags().tagByName( "works-with::people" ); 00070 assert( people != midgets ); 00071 assert( people != text ); 00072 assert( people != blahg ); 00073 assert( midgets == blahg ); 00074 assert( midgets == midgets ); 00075 assert( people == people2 ); 00076 assert( people == people ); 00077 } 00078 00079 Test _5() 00080 { 00081 Tag a = tags().tagByName( "works-with::people" ), 00082 b = tags().tagByName( "works-with::midgets" ); 00083 std::set< Tag > s = tags().tags(), 00084 f = tags().tags( "works-with" ), 00085 n = tags().tags( "nonsense" ); 00086 assert( set_contains(s, a) ); 00087 assert( set_contains(f, a) ); 00088 assert( set_contains(s, f) ); 00089 assert( !set_contains(s, b) ); 00090 assert( !set_contains(f, b) ); 00091 assert( n.empty() ); 00092 } 00093 00094 Test _6() 00095 { 00096 Facet f = tags().facetByName( "works-with" ); 00097 Tag t = tags().tagByName( "works-with::people" ); 00098 assert_eq(f.name(), "works-with"); 00099 assert_eq(t.name(), "people"); 00100 assert_eq(t.fullname(), "works-with::people"); 00101 } 00102 00103 Test _7() 00104 { 00105 Facet f = tags().facetByName( "works-with" ); 00106 std::set< Tag > x = tags().tags( "works-with" ); 00107 assert( x == f.tags() ); 00108 } 00109 00110 Test _8() 00111 { 00112 Facet f = tags().facetByName( "does-not-work-with" ); 00113 int x = 1; 00114 try { 00115 f.tags(); 00116 x = 2; 00117 } catch (...) { 00118 x = 3; 00119 } 00120 assert_eq( x, 3 ); 00121 } 00122 00123 Test _9() 00124 { 00125 Facet f = tags().facetByName( "legacy" ); 00126 assert_eq(f.shortDescription(), ""); 00127 assert_eq(f.longDescription(), ""); 00128 //assert_eq(f.shortDescription( "weehee" ), "weehee"); 00129 } 00130 00131 Test _10() 00132 { 00133 // assert that one-character tag names are parsed correctly 00134 assert( tags().hasTag( "implemented-in::c" ) ); 00135 } 00136 00137 Test _11() 00138 { 00139 // assert that all tags are somehow working 00140 std::set<Facet> facets = tags().facets(); 00141 00142 for (std::set<Facet>::const_iterator i = facets.begin(); 00143 i != facets.end(); i++) 00144 { 00145 i->name(string("foo")); 00146 i->shortDescription(string("foo")); 00147 i->longDescription(string("foo")); 00148 i->tags(); 00149 } 00150 } 00151 00152 Test _12() 00153 { 00154 // assert that all tags are somehow working 00155 std::set<Tag> tags = this->tags().tags(); 00156 00157 for (std::set<Tag>::const_iterator i = tags.begin(); 00158 i != tags.end(); i++) 00159 { 00160 i->name(string("foo")); 00161 i->fullname(string("foo")); 00162 i->shortDescription(string("foo")); 00163 i->longDescription(string("foo")); 00164 } 00165 } 00166 00167 // Check for correctness of the first and last tag in the vocabulary 00168 Test _13() 00169 { 00170 Vocabulary& tags = this->tags(); 00171 00172 Tag first = tags.tagByName("accessibility::TODO"); 00173 assert(first != Tag()); 00174 assert_eq(first.fullname(), string("accessibility::TODO")); 00175 assert_eq(first.name(), string("TODO")); 00176 assert_eq(first.shortDescription(), string("Need an extra tag")); 00177 00178 Tag last = tags.tagByName("x11::xserver"); 00179 assert(last != Tag()); 00180 assert_eq(last.fullname(), string("x11::xserver")); 00181 assert_eq(last.name(), string("xserver")); 00182 assert_eq(last.shortDescription(), string("X Server")); 00183 } 00184 00185 Test _14() 00186 { 00187 // assert that it's possible to go from facet to ID and back 00188 std::set<Facet> facets = tags().facets(); 00189 00190 for (std::set<Facet>::const_iterator i = facets.begin(); 00191 i != facets.end(); i++) 00192 { 00193 Facet f = tags().facetByID(i->id()); 00194 assert_eq(*i, f); 00195 assert_eq(i->name(), f.name()); 00196 assert_eq(i->shortDescription(), f.shortDescription()); 00197 assert_eq(i->longDescription(), f.longDescription()); 00198 assert_eq(i->tags().size(), f.tags().size()); 00199 } 00200 } 00201 00202 Test _15() 00203 { 00204 // assert that it's possible to go from tag to ID and back 00205 std::set<Tag> tags = this->tags().tags(); 00206 00207 for (std::set<Tag>::const_iterator i = tags.begin(); 00208 i != tags.end(); i++) 00209 { 00210 Tag t = this->tags().tagByID(i->id()); 00211 assert_eq(*i, t); 00212 assert_eq(i->name(), t.name()); 00213 assert_eq(i->fullname(), t.fullname()); 00214 assert_eq(i->shortDescription(), t.shortDescription()); 00215 assert_eq(i->longDescription(), t.longDescription()); 00216 } 00217 } 00218 00219 Test _16() 00220 { 00221 // assert that facet IDs are distinct 00222 std::set<Facet> facets = tags().facets(); 00223 std::set<int> ids; 00224 for (std::set<Facet>::const_iterator i = facets.begin(); 00225 i != facets.end(); i++) 00226 ids.insert(i->id()); 00227 00228 assert_eq(facets.size(), ids.size()); 00229 } 00230 00231 Test _17() 00232 { 00233 // assert that tag IDs are distinct 00234 std::set<Tag> tags = this->tags().tags(); 00235 std::set<int> ids; 00236 for (std::set<Tag>::const_iterator i = tags.begin(); 00237 i != tags.end(); i++) 00238 ids.insert(i->id()); 00239 00240 assert_eq(tags.size(), ids.size()); 00241 } 00242 00243 Test _18() 00244 { 00245 // assert that all the tags are indexed 00246 ept::debtags::VocabularyMerger voc; 00247 tagcoll::input::Stdio in(ept::debtags::Path::vocabulary()); 00248 voc.read(in); 00249 std::set<std::string> all = voc.tagNames(); 00250 for (std::set<std::string>::const_iterator i = all.begin(); 00251 i != all.end(); ++i) 00252 assert(this->tags().hasTag(*i)); 00253 00254 // There should be the same amount of tags in both 00255 std::set<Tag> allTags = this->tags().tags(); 00256 assert_eq(all.size(), allTags.size()); 00257 } 00258 00259 Test _19() 00260 { 00261 // test the tagcmp function 00262 00263 // If unfaceted, same as strcmp 00264 assert(ept::debtags::tagcmp("antani", "blinda") < 0); 00265 assert(ept::debtags::tagcmp("blinda", "antani") > 0); 00266 assert_eq(ept::debtags::tagcmp("antani", "antani"), 0); 00267 00268 // If the same and faceted, should work 00269 assert_eq(ept::debtags::tagcmp("antani::blinda", "antani::blinda"), 0); 00270 00271 // With different facet names, work just as strcmp 00272 assert(ept::debtags::tagcmp("antani::blinda", "blinda::blinda") < 0); 00273 assert(ept::debtags::tagcmp("blinda::blinda", "antani::blinda") > 0); 00274 assert(ept::debtags::tagcmp("anta::blinda", "antani::blinda") < 0); 00275 assert(ept::debtags::tagcmp("antani::blinda", "anta::blinda") > 0); 00276 assert(ept::debtags::tagcmp("anta::blinda", "anta-ni::blinda") < 0); 00277 assert(ept::debtags::tagcmp("anta-ni::blinda", "anta::blinda") > 0); 00278 00279 // With same facet names, work just as strcmp on the tags 00280 assert(ept::debtags::tagcmp("a::antani", "a::blinda") < 0); 00281 assert(ept::debtags::tagcmp("a::blinda", "a::antani") > 0); 00282 assert(ept::debtags::tagcmp("a::anta", "a::antani") < 0); 00283 assert(ept::debtags::tagcmp("a::antani", "a::anta") > 0); 00284 assert(ept::debtags::tagcmp("a::anta", "a::anta-ni") < 0); 00285 assert(ept::debtags::tagcmp("a::anta-ni", "a::anta") > 0); 00286 } 00287 00288 Test _20() 00289 { 00290 // check that we're seeing all the tags for a facet 00291 std::set<Tag> t = tags().tags("accessibility"); 00292 assert_eq(t.size(), 10u); 00293 00294 t = tags().tags("works-with-format"); 00295 assert_eq(t.size(), 33u); 00296 } 00297 00298 // If there is no data, Vocabulary should work as an empty vocabulary 00299 Test _21() 00300 { 00301 Path::OverrideDebtagsSourceDir odsd("./empty"); 00302 Path::OverrideDebtagsIndexDir odid("./empty"); 00303 Path::OverrideDebtagsUserSourceDir odusd("./empty"); 00304 Path::OverrideDebtagsUserIndexDir oduid("./empty"); 00305 Vocabulary empty; 00306 00307 assert(!empty.hasData()); 00308 00309 set<Facet> facets = empty.facets(); 00310 assert_eq(facets.size(), 0u); 00311 00312 set<Tag> tags = empty.tags(); 00313 assert_eq(tags.size(), 0u); 00314 } 00315 00316 }; 00317 00318 // vim:set ts=4 sw=4: