~ubuntu-branches/ubuntu/trusty/tagcoll2/trusty-proposed

« back to all changes in this revision

Viewing changes to tagcoll/TextFormat-tut.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Fontaine
  • Date: 2006-11-18 12:15:11 UTC
  • Revision ID: james.westby@ubuntu.com-20061118121511-nic9no49s64crb7i
Tags: upstream-2.0.4
ImportĀ upstreamĀ versionĀ 2.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Serialize a tagged collection to a text file
 
3
 *
 
4
 * Copyright (C) 2003,2004,2005  Enrico Zini <enrico@debian.org>
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library 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 GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
19
 */
 
20
 
 
21
#include <tests/test-utils.h>
 
22
#include <tagcoll/TextFormat.h>
 
23
#include <tagcoll/input/string.h>
 
24
#include <tagcoll/stream/sink.h>
 
25
#include <tagcoll/patch.h>
 
26
 
 
27
namespace tut {
 
28
using namespace std;
 
29
using namespace tagcoll;
 
30
using namespace tagcoll::tests;
 
31
 
 
32
struct tagcoll_textformat_shar {
 
33
};
 
34
TESTGRP(tagcoll_textformat);
 
35
 
 
36
template<> template<>
 
37
void to::test<1>()
 
38
{
 
39
        input::String coll(
 
40
                        "a: b, c\n"
 
41
                        "b:\n"
 
42
                        "c: \n"
 
43
                        "d:  c::D, e::F,    f::g\n"
 
44
        );
 
45
        
 
46
        int countItems = 0, countTags = 0;
 
47
        textformat::parse(coll, stream::countingSink(countItems, countTags));
 
48
 
 
49
        ensure_equals(countItems, 4);
 
50
        ensure_equals(countTags, 5);
 
51
}
 
52
 
 
53
template<> template<>
 
54
void to::test<2>()
 
55
{
 
56
        input::String coll(
 
57
                        "a: +b, +c\n"
 
58
                        "b:\n"
 
59
                        "c: foo\n"
 
60
                        "d:  +c::D, -e::F,    -f::g\n"
 
61
        );
 
62
        
 
63
        PatchList<string, string> plist = textformat::parsePatch(coll);
 
64
 
 
65
        /*
 
66
        cerr << "Patchlist[" << plist.size() << "]:" << endl;
 
67
        for (PatchList<string, string>::const_iterator i = plist.begin();
 
68
                        i != plist.end(); i++)
 
69
                cerr << " " << i->first << ": " << 
 
70
                        i->second.added.size()  << "x" <<
 
71
                        i->second.removed.size()  << endl;
 
72
        */
 
73
 
 
74
        ensure_equals(plist.size(), 2u);
 
75
        ensure(plist.find("a") != plist.end());
 
76
        ensure(plist.find("b") == plist.end());
 
77
        ensure(plist.find("c") == plist.end());
 
78
        ensure(plist.find("d") != plist.end());
 
79
 
 
80
        PatchList<string, string>::const_iterator i = plist.find("a");
 
81
        ensure_equals(i->second.added.size(), 2u);
 
82
        ensure_equals(i->second.removed.size(), 0u);
 
83
 
 
84
        i = plist.find("d");
 
85
        ensure_equals(i->second.added.size(), 1u);
 
86
        ensure_equals(i->second.removed.size(), 2u);
 
87
}
 
88
 
 
89
template<> template<>
 
90
void to::test<3>()
 
91
{
 
92
        string reference =
 
93
                "foo\n"
 
94
                "foo: antani\n"
 
95
                "foo: antani, blinda\n"
 
96
                "bar, foo: antani, blinda\n";
 
97
        char buf[1000];
 
98
        FILE* out = fmemopen(buf, 1000, "w");
 
99
        ensure(out != 0);
 
100
 
 
101
        textformat::StdioWriter writer(out);
 
102
 
 
103
        set<string> s1;
 
104
        s1.insert("foo");
 
105
        *writer = make_pair(s1, wibble::Empty<string>());
 
106
        ++writer;
 
107
 
 
108
        set<string> s2;
 
109
        s2.insert("antani");
 
110
        *writer = make_pair(s1, s2);
 
111
        ++writer;
 
112
 
 
113
        s2.insert("blinda");
 
114
        *writer = make_pair(s1, s2);
 
115
        ++writer;
 
116
 
 
117
        s1.clear();
 
118
        s1.insert("foo"); s1.insert("bar");
 
119
        *writer = make_pair(s1, s2);
 
120
        ++writer;
 
121
 
 
122
        fclose(out);
 
123
 
 
124
        string outstr(buf);
 
125
        ensure_equals(reference, outstr);
 
126
}
 
127
 
 
128
template<> template<>
 
129
void to::test<4>()
 
130
{
 
131
        string reference =
 
132
                "foo\n"
 
133
                "foo: antani\n"
 
134
                "foo: antani, blinda\n"
 
135
                "bar, foo: antani, blinda\n";
 
136
        stringstream out;
 
137
 
 
138
        textformat::OstreamWriter writer(out);
 
139
 
 
140
        set<string> s1;
 
141
        s1.insert("foo");
 
142
        *writer = make_pair(s1, wibble::Empty<string>());
 
143
        ++writer;
 
144
 
 
145
        set<string> s2;
 
146
        s2.insert("antani");
 
147
        *writer = make_pair(s1, s2);
 
148
        ++writer;
 
149
 
 
150
        s2.insert("blinda");
 
151
        *writer = make_pair(s1, s2);
 
152
        ++writer;
 
153
 
 
154
        s1.clear();
 
155
        s1.insert("foo"); s1.insert("bar");
 
156
        *writer = make_pair(s1, s2);
 
157
        ++writer;
 
158
 
 
159
        ensure_equals(out.str(), reference);
 
160
}
 
161
 
 
162
template<> template<>
 
163
void to::test<5>()
 
164
{
 
165
        char buf[1000];
 
166
 
 
167
        PatchList<string, string> patchlist;
 
168
        std::set<string> added; added.insert("add");
 
169
        std::set<string> removed; removed.insert("rm");
 
170
        patchlist.addPatch(Patch<string, string>("test", added, removed));
 
171
        
 
172
        FILE* out = fmemopen(buf, 999, "w");
 
173
        ensure(out != 0);
 
174
 
 
175
        textformat::outputPatch(patchlist, out);
 
176
        fclose(out);
 
177
 
 
178
        ensure(strcmp(buf, "test: +add, -rm\n") == 0);
 
179
 
 
180
        struct {
 
181
                std::string operator()(const std::string& str) { return str + "o"; }
 
182
        } oer;
 
183
 
 
184
        out = fmemopen(buf, 999, "w");
 
185
        ensure(out != 0);
 
186
 
 
187
#if 0
 
188
#warning Enable this test please
 
189
        textformat::outputPatch(oer, oer, patchlist, out);
 
190
        fclose(out);
 
191
 
 
192
        ensure(strcmp(buf, "test: +addo, -rmo\n") == 0);
 
193
#endif
 
194
}
 
195
 
 
196
}
 
197
 
 
198
#include <tagcoll/TextFormat.tcc>
 
199
#include <tagcoll/stream/filters.tcc>
 
200
 
 
201
// vim:set ts=4 sw=4: