libept
|
00001 /* 00002 * Copyright (C) 2007 Enrico Zini <enrico@enricozini.org> 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2.1 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public 00015 * License along with this library; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #include <ept/test.h> 00020 #include <ept/apt/recordparser.h> 00021 00022 //#include <iostream> 00023 00024 using namespace std; 00025 using namespace ept; 00026 using namespace ept::apt; 00027 00028 struct TestAptRecordparser { 00029 std::string record; 00030 TestAptRecordparser() 00031 { 00032 record = 00033 "A:\n" 00034 "D: da de di do du\n" 00035 "B: b\n" 00036 "C: c \n" 00037 "Desc: this is the beginning\n" 00038 " this is the continuation\n" 00039 " this is the end\n"; 00040 } 00041 00042 // Check that the fields are identified and broken up correctly 00043 Test parsing() 00044 { 00045 RecordParser p(record); 00046 00047 assert_eq(p.record(), record); 00048 assert_eq(p.size(), 5u); 00049 } 00050 00051 Test fieldTuples() 00052 { 00053 RecordParser p(record); 00054 assert_eq(p.field(0), "A:\n"); 00055 assert_eq(p.field(1), "D: da de di do du\n"); 00056 assert_eq(p.field(2), "B: b\n"); 00057 assert_eq(p.field(3), "C: c \n"); 00058 assert_eq(p.field(4), "Desc: this is the beginning\n this is the continuation\n this is the end\n"); 00059 } 00060 00061 Test fieldKeys() 00062 { 00063 RecordParser p(record); 00064 assert_eq(p.name(0), "A"); 00065 assert_eq(p.name(1), "D"); 00066 assert_eq(p.name(2), "B"); 00067 assert_eq(p.name(3), "C"); 00068 assert_eq(p.name(4), "Desc"); 00069 } 00070 00071 Test fieldValues() 00072 { 00073 RecordParser p(record); 00074 assert_eq(p[0], ""); 00075 assert_eq(p[1], "da de di do du"); 00076 assert_eq(p[2], "b"); 00077 assert_eq(p[3], "c"); 00078 assert_eq(p[4], "this is the beginning\n this is the continuation\n this is the end"); 00079 } 00080 00081 // Check that the field search by name finds all the fields 00082 Test findByName() 00083 { 00084 RecordParser p(record); 00085 00086 assert_eq(p.index("A"), 0u); 00087 assert_eq(p.index("D"), 1u); 00088 assert_eq(p.index("B"), 2u); 00089 assert_eq(p.index("C"), 3u); 00090 assert_eq(p.index("Desc"), 4u); 00091 00092 assert_eq(p.name(p.index("A")), "A"); 00093 assert_eq(p.name(p.index("B")), "B"); 00094 assert_eq(p.name(p.index("C")), "C"); 00095 assert_eq(p.name(p.index("D")), "D"); 00096 assert_eq(p.name(p.index("Desc")), "Desc"); 00097 } 00098 00099 Test indexing() 00100 { 00101 RecordParser p(record); 00102 assert_eq(p["A"], ""); 00103 assert_eq(p["B"], "b"); 00104 assert_eq(p["C"], "c"); 00105 assert_eq(p["D"], "da de di do du"); 00106 assert_eq(p["Desc"], "this is the beginning\n this is the continuation\n this is the end"); 00107 } 00108 00109 Test missingBehaviour() 00110 { 00111 RecordParser p(record); 00112 // Missing fields give empty strings 00113 assert_eq(p.field(100), ""); 00114 assert_eq(p.name(100), ""); 00115 assert_eq(p[100], ""); 00116 assert_eq(p["Missing"], ""); 00117 } 00118 00119 // Check that scanning twice replaces the old fields 00120 Test rescan() 00121 { 00122 std::string record = 00123 "A: a\n" 00124 "B: b\n" 00125 "C: c\n"; 00126 00127 RecordParser p(record); 00128 assert_eq(p.size(), 3u); 00129 assert_eq(p["A"], "a"); 00130 assert_eq(p["B"], "b"); 00131 assert_eq(p["C"], "c"); 00132 00133 std::string record1 = 00134 "Foo: bar\n" 00135 "A: different\n"; 00136 00137 p.scan(record1); 00138 00139 //for (size_t i = 0; i < p.size(); ++i) 00140 // cerr << ">> " << i << "==" << p.index(p.name(i)) << " " << p.name(i) << " " << p[i] << endl; 00141 00142 assert_eq(p.size(), 2u); 00143 assert_eq(p["A"], "different"); 00144 assert_eq(p["B"], ""); 00145 assert_eq(p["C"], ""); 00146 assert_eq(p["Foo"], "bar"); 00147 } 00148 00149 // Real-life example 00150 Test realLife() 00151 { 00152 string record = 00153 "Package: apt\n" 00154 "Priority: important\n" 00155 "Section: admin\n" 00156 "Installed-Size: 4368\n" 00157 "Maintainer: APT Development Team <deity@lists.debian.org>\n" 00158 "Architecture: amd64\n" 00159 "Version: 0.6.46.4-0.1\n" 00160 "Replaces: libapt-pkg-doc (<< 0.3.7), libapt-pkg-dev (<< 0.3.7)\n" 00161 "Provides: libapt-pkg-libc6.3-6-3.11\n" 00162 "Depends: libc6 (>= 2.3.5-1), libgcc1 (>= 1:4.1.1-12), libstdc++6 (>= 4.1.1-12), debian-archive-keyring\n" 00163 "Suggests: aptitude | synaptic | gnome-apt | wajig, dpkg-dev, apt-doc, bzip2\n" 00164 "Filename: pool/main/a/apt/apt_0.6.46.4-0.1_amd64.deb\n" 00165 "Size: 1436478\n" 00166 "MD5sum: 1776421f80d6300c77a608e77a9f4a15\n" 00167 "SHA1: 1bd7337d2df56d267632cf72ac930c0a4895898f\n" 00168 "SHA256: b92442ab60046b4d0728245f39cc932f26e17db9f7933a5ec9aaa63172f51fda\n" 00169 "Description: Advanced front-end for dpkg\n" 00170 " This is Debian's next generation front-end for the dpkg package manager.\n" 00171 " It provides the apt-get utility and APT dselect method that provides a\n" 00172 " simpler, safer way to install and upgrade packages.\n" 00173 " .\n" 00174 " APT features complete installation ordering, multiple source capability\n" 00175 " and several other unique features, see the Users Guide in apt-doc.\n" 00176 "Build-Essential: yes\n" 00177 "Tag: admin::package-management, filetransfer::ftp, filetransfer::http, hardware::storage:cd, interface::commandline, network::client, protocol::{ftp,http,ipv6}, role::program, suite::debian, use::downloading, use::searching, works-with::software:package\n"; 00178 RecordParser p(record); 00179 00180 assert_eq(p.size(), 19u); 00181 00182 string rec1; 00183 for (size_t i = 0; i < p.size(); ++i) 00184 rec1 += p.field(i); 00185 assert_eq(record, rec1); 00186 } 00187 00188 // Various buffer termination patterns 00189 Test bufferTermination() 00190 { 00191 std::string record = 00192 "A: a\n" 00193 "B: b"; 00194 00195 RecordParser p(record); 00196 assert_eq(p.size(), 2u); 00197 assert_eq(p["A"], "a"); 00198 assert_eq(p["B"], "b"); 00199 } 00200 00201 Test bufferTermination2() 00202 { 00203 std::string record = 00204 "A: a\n" 00205 "B: b\n\n"; 00206 00207 RecordParser p(record); 00208 assert_eq(p.size(), 2u); 00209 assert_eq(p["A"], "a"); 00210 assert_eq(p["B"], "b"); 00211 } 00212 00213 Test bufferTermination3() 00214 { 00215 std::string record = 00216 "A: a\n" 00217 "B: b\n\n" 00218 "C: c\n"; 00219 00220 RecordParser p(record); 00221 assert_eq(p.size(), 2u); 00222 assert_eq(p["A"], "a"); 00223 assert_eq(p["B"], "b"); 00224 } 00225 00226 }; 00227 00228 // vim:set ts=4 sw=4: