libept
recordparser.test.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Enrico Zini <enrico@enricozini.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include <ept/test.h>
20 #include <ept/apt/recordparser.h>
21 
22 //#include <iostream>
23 
24 using namespace std;
25 using namespace ept;
26 using namespace ept::apt;
27 
29  std::string record;
31  {
32  record =
33  "A:\n"
34  "D: da de di do du\n"
35  "B: b\n"
36  "C: c \n"
37  "Desc: this is the beginning\n"
38  " this is the continuation\n"
39  " this is the end\n";
40  }
41 
42  // Check that the fields are identified and broken up correctly
43  Test parsing()
44  {
45  RecordParser p(record);
46 
47  assert_eq(p.record(), record);
48  assert_eq(p.size(), 5u);
49  }
50 
51  Test fieldTuples()
52  {
53  RecordParser p(record);
54  assert_eq(p.field(0), "A:\n");
55  assert_eq(p.field(1), "D: da de di do du\n");
56  assert_eq(p.field(2), "B: b\n");
57  assert_eq(p.field(3), "C: c \n");
58  assert_eq(p.field(4), "Desc: this is the beginning\n this is the continuation\n this is the end\n");
59  }
60 
61  Test fieldKeys()
62  {
63  RecordParser p(record);
64  assert_eq(p.name(0), "A");
65  assert_eq(p.name(1), "D");
66  assert_eq(p.name(2), "B");
67  assert_eq(p.name(3), "C");
68  assert_eq(p.name(4), "Desc");
69  }
70 
71  Test fieldValues()
72  {
73  RecordParser p(record);
74  assert_eq(p[0], "");
75  assert_eq(p[1], "da de di do du");
76  assert_eq(p[2], "b");
77  assert_eq(p[3], "c");
78  assert_eq(p[4], "this is the beginning\n this is the continuation\n this is the end");
79  }
80 
81  // Check that the field search by name finds all the fields
82  Test findByName()
83  {
84  RecordParser p(record);
85 
86  assert_eq(p.index("A"), 0u);
87  assert_eq(p.index("D"), 1u);
88  assert_eq(p.index("B"), 2u);
89  assert_eq(p.index("C"), 3u);
90  assert_eq(p.index("Desc"), 4u);
91 
92  assert_eq(p.name(p.index("A")), "A");
93  assert_eq(p.name(p.index("B")), "B");
94  assert_eq(p.name(p.index("C")), "C");
95  assert_eq(p.name(p.index("D")), "D");
96  assert_eq(p.name(p.index("Desc")), "Desc");
97  }
98 
99  Test indexing()
100  {
101  RecordParser p(record);
102  assert_eq(p["A"], "");
103  assert_eq(p["B"], "b");
104  assert_eq(p["C"], "c");
105  assert_eq(p["D"], "da de di do du");
106  assert_eq(p["Desc"], "this is the beginning\n this is the continuation\n this is the end");
107  }
108 
109  Test missingBehaviour()
110  {
111  RecordParser p(record);
112  // Missing fields give empty strings
113  assert_eq(p.field(100), "");
114  assert_eq(p.name(100), "");
115  assert_eq(p[100], "");
116  assert_eq(p["Missing"], "");
117  }
118 
119  // Check that scanning twice replaces the old fields
120  Test rescan()
121  {
122  std::string record =
123  "A: a\n"
124  "B: b\n"
125  "C: c\n";
126 
127  RecordParser p(record);
128  assert_eq(p.size(), 3u);
129  assert_eq(p["A"], "a");
130  assert_eq(p["B"], "b");
131  assert_eq(p["C"], "c");
132 
133  std::string record1 =
134  "Foo: bar\n"
135  "A: different\n";
136 
137  p.scan(record1);
138 
139  //for (size_t i = 0; i < p.size(); ++i)
140  // cerr << ">> " << i << "==" << p.index(p.name(i)) << " " << p.name(i) << " " << p[i] << endl;
141 
142  assert_eq(p.size(), 2u);
143  assert_eq(p["A"], "different");
144  assert_eq(p["B"], "");
145  assert_eq(p["C"], "");
146  assert_eq(p["Foo"], "bar");
147  }
148 
149  // Real-life example
150  Test realLife()
151  {
152  string record =
153  "Package: apt\n"
154  "Priority: important\n"
155  "Section: admin\n"
156  "Installed-Size: 4368\n"
157  "Maintainer: APT Development Team <deity@lists.debian.org>\n"
158  "Architecture: amd64\n"
159  "Version: 0.6.46.4-0.1\n"
160  "Replaces: libapt-pkg-doc (<< 0.3.7), libapt-pkg-dev (<< 0.3.7)\n"
161  "Provides: libapt-pkg-libc6.3-6-3.11\n"
162  "Depends: libc6 (>= 2.3.5-1), libgcc1 (>= 1:4.1.1-12), libstdc++6 (>= 4.1.1-12), debian-archive-keyring\n"
163  "Suggests: aptitude | synaptic | gnome-apt | wajig, dpkg-dev, apt-doc, bzip2\n"
164  "Filename: pool/main/a/apt/apt_0.6.46.4-0.1_amd64.deb\n"
165  "Size: 1436478\n"
166  "MD5sum: 1776421f80d6300c77a608e77a9f4a15\n"
167  "SHA1: 1bd7337d2df56d267632cf72ac930c0a4895898f\n"
168  "SHA256: b92442ab60046b4d0728245f39cc932f26e17db9f7933a5ec9aaa63172f51fda\n"
169  "Description: Advanced front-end for dpkg\n"
170  " This is Debian's next generation front-end for the dpkg package manager.\n"
171  " It provides the apt-get utility and APT dselect method that provides a\n"
172  " simpler, safer way to install and upgrade packages.\n"
173  " .\n"
174  " APT features complete installation ordering, multiple source capability\n"
175  " and several other unique features, see the Users Guide in apt-doc.\n"
176  "Build-Essential: yes\n"
177  "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";
178  RecordParser p(record);
179 
180  assert_eq(p.size(), 19u);
181 
182  string rec1;
183  for (size_t i = 0; i < p.size(); ++i)
184  rec1 += p.field(i);
185  assert_eq(record, rec1);
186  }
187 
188  // Various buffer termination patterns
189  Test bufferTermination()
190  {
191  std::string record =
192  "A: a\n"
193  "B: b";
194 
195  RecordParser p(record);
196  assert_eq(p.size(), 2u);
197  assert_eq(p["A"], "a");
198  assert_eq(p["B"], "b");
199  }
200 
201  Test bufferTermination2()
202  {
203  std::string record =
204  "A: a\n"
205  "B: b\n\n";
206 
207  RecordParser p(record);
208  assert_eq(p.size(), 2u);
209  assert_eq(p["A"], "a");
210  assert_eq(p["B"], "b");
211  }
212 
213  Test bufferTermination3()
214  {
215  std::string record =
216  "A: a\n"
217  "B: b\n\n"
218  "C: c\n";
219 
220  RecordParser p(record);
221  assert_eq(p.size(), 2u);
222  assert_eq(p["A"], "a");
223  assert_eq(p["B"], "b");
224  }
225 
226 };
227 
228 // vim:set ts=4 sw=4: