File: test_ods.cpp

package info (click to toggle)
libodsstream 0.9.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 804 kB
  • sloc: cpp: 4,750; python: 150; makefile: 31; sh: 1
file content (373 lines) | stat: -rw-r--r-- 9,653 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

#include <catch2/catch_test_macros.hpp>

#include "config.h"
#include <QDebug>
#include <iostream>
#include <odsstream/calcwriterinterface.h>
#include <odsstream/odsdocwriter.h>
#include <odsstream/odsdocreader.h>
#include <odsstream/odsdocwriter.h>
#include <odsstream/odsexception.h>
#include <odsstream/writer/options/odscolorscale.h>
#include <odsstream/writer/options/odstablecellstyle.h>
#include "metadataodsreader.h"
#include <QVariant>


// make test ARGS="-V -I 1,1"

// ./test/test_ods [ods] -s

using namespace std;

class CustomHandler : public OdsDocHandlerInterface
{
  public:
  /**
   * callback that indicates the begining of a data sheet. Override it in
   * order to retrieve information about the current data sheet.
   *
   */
  virtual void startSheet([[maybe_unused]] const QString &sheet_name){};

  /**
   * callback that indicates the end of the current data sheet. Override it if
   * needed
   */
  virtual void
  endSheet() override
  {
    qDebug() << "endSheet";
  };

  /**
   * callback that indicates a new line start. Override it if needed.
   */

  virtual void startLine() override{};

  /**
   * callback that indicates a line ending. Override it if needed.
   */

  virtual void endLine() override{};

  /**
   * callback that report the content of the current cell in a dedicated Cell
   * object. Override it if you need to retrieve cell content.
   */
  virtual void
  setCell(const OdsCell &cell) override
  {
    qDebug() << cell.toString();
  };

  /**
   * callback that report the end of the ODS document. Override it if you need
   * to know that reading is finished.
   */
  virtual void endDocument() override{};
};


class CustomHandlerXic : public OdsDocHandlerInterface
{
  public:
  /**
   * callback that indicates the begining of a data sheet. Override it in
   * order to retrieve information about the current data sheet.
   *
   */
  virtual void
  startSheet([[maybe_unused]] const QString &sheet_name)
  {
    m_columnNumber = 0;
    m_rowNumber    = 0;
    if((sheet_name == "Feuille1") || (sheet_name == "metadata"))
      {
      }
    else
      {
        throw OdsException(
          QObject::tr("sheet_name %1 != Feuille1").arg(sheet_name));
      }
  };

  /**
   * callback that indicates the end of the current data sheet. Override it if
   * needed
   */
  virtual void
  endSheet() override
  {
    qDebug() << "endSheet";
  };

  /**
   * callback that indicates a new line start. Override it if needed.
   */

  virtual void
  startLine() override
  {
    m_columnNumber = 0;
  };

  /**
   * callback that indicates a line ending. Override it if needed.
   */

  virtual void
  endLine() override
  {
    m_rowNumber++;
  };

  /**
   * callback that report the content of the current cell in a dedicated Cell
   * object. Override it if you need to retrieve cell content.
   */
  virtual void
  setCell(const OdsCell &cell) override
  {
    qDebug() << cell.toString();
    if((m_columnNumber == 0) && (m_rowNumber == 0) && (cell.toString() != "rt"))
      {
        throw OdsException(
          QObject::tr("cell.toString() %1 != rt").arg(cell.toString()));
      }
    if((m_rowNumber == 0) && (m_columnNumber == 1) &&
       (cell.toString() != "intensity"))
      {
        throw OdsException(
          QObject::tr("cell.toString() %1 != intensity").arg(cell.toString()));
      }
    if((m_rowNumber > 0) && (m_columnNumber == 0) &&
       (cell.getDoubleValue() != m_rowNumber))
      {
        throw OdsException(
          QObject::tr("cell.getDoubleValue() %1 != m_rowNumber %2")
            .arg(cell.getDoubleValue())
            .arg(m_rowNumber));
      }
    if((m_rowNumber > 0) && (m_columnNumber == 1) &&
       (cell.getDoubleValue() != 0))
      {
        if((m_rowNumber == 12) && (cell.getDoubleValue() == 5))
          {
          }
        else if((m_rowNumber == 21) && (cell.getDoubleValue() == 18))
          {
          }
        else if((m_rowNumber == 22) && (cell.getDoubleValue() == 6))
          {
          }
        else if((m_rowNumber == 23) && (cell.getDoubleValue() == 3))
          {
          }
        else if((m_rowNumber == 30) && (cell.getDoubleValue() == 2))
          {
          }
        else
          {
            throw OdsException(
              QObject::tr("cell.getDoubleValue() %1 != 0 at line %2")
                .arg(cell.getDoubleValue())
                .arg(m_rowNumber));
          }
      }
    m_columnNumber++;
  };

  /**
   * callback that report the end of the ODS document. Override it if you need
   * to know that reading is finished.
   */
  virtual void endDocument() override{};

  private:
  std::size_t m_rowNumber    = 0;
  std::size_t m_columnNumber = 0;
};


TEST_CASE("Test ODS metadata bug files.", "[odsbug]")
{
  // Set the debugging message formatting pattern.
  qSetMessagePattern(QString("%{file}@%{line}, %{function}(): %{message}"));

  SECTION(
    "reading "
    "/gorgone/pappso/moulon/users/Olivier/20240131_proteobench/mcqr/"
    "metadata_template.ods",
    "[odsbug]")
  {
    cout << endl << "..:: Test ODS files ::.." << endl;

    cout << endl
         << "..:: read ODS file "
            "metadata_template.ods ::.."
         << endl;
    MetadataOdsReader handler;
    QFile xicfile(QString(CMAKE_SOURCE_DIR).append("/test/data/metadata_template.ods"));
    OdsDocReader xicfile_reader(handler);
    REQUIRE_NOTHROW(xicfile_reader.parse(&xicfile));

    xicfile.close();

    cout << endl << "..:: read ODS file xic.ods is OK ::.." << endl;
  }
}
TEST_CASE("Test ODS files.", "[ods]")
{
  // Set the debugging message formatting pattern.
  qSetMessagePattern(QString("%{file}@%{line}, %{function}(): %{message}"));

  SECTION("reading /test/data/xic.ods", "[ods]")
  {
    cout << endl << "..:: Test ODS files ::.." << endl;

    cout << endl << "..:: read ODS file xic.ods ::.." << endl;
    CustomHandlerXic handler;
    QFile xicfile(QString("../").append("/test/data/xic.ods"));
    OdsDocReader xicfile_reader(handler);
    REQUIRE_NOTHROW(xicfile_reader.parse(&xicfile));

    xicfile.close();

    cout << endl << "..:: read ODS file xic.ods is OK ::.." << endl;
  }
  SECTION("reading other ods", "[ods]")
  {

    QFile file("test.ods");
    // file.open(QIODevice::WriteOnly);
    OdsDocWriter writer(&file);

    QColor red("red");
    OdsTableCellStyle style;
    style.setBackgroundColor(QColor("yellow"));
    style.setTextColor(red);

    OdsTableCellStyleRef ref = writer.getTableCellStyleRef(style);

    QString test("truc");

    writer.writeSheet("classeur");

    OdsTableSettings settings;
    // settings.setHorizontalWindowSplit(4);
    settings.setVerticalSplit(1);
    writer.setCurrentOdsTableSettings(settings);

    writer.writeCell(test);
    // writer.clearTableCellStyleRef();
    writer.setTableCellStyleRef(ref);
    writer.writeLine();
    writer.writeEmptyCell();
    writer.setCellAnnotation("ceci est un commentaire n1");
    writer.writeCell("coucou");
    bool vf(0);
    writer.writeCell(vf);
    writer.clearTableCellStyleRef();
    writer.writeLine();
    writer.writeLine();
    writer.writeLine();
    writer.writeCell(1);
    QString start_position = writer.getOdsCellCoordinate();
    writer.writeCell(2);
    writer.writeCell(3);
    writer.writeCell(4);
    writer.writeCell(5);

    writer.writeLine();
    writer.writeCell(6);
    writer.writeCell(7);
    writer.writeCell(8);
    writer.writeCell(9);
    writer.writeCell(10);
    QString end_position;
    end_position = writer.getOdsCellCoordinate();

    qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
             << end_position;
    OdsColorScale color_scale(start_position, end_position);
    writer.addColorScale(color_scale);

    writer.writeSheet("classeur2");

    writer.setCurrentOdsTableSettings(settings);

    writer.writeCell(test);
    // writer.clearTableCellStyleRef();
    writer.setTableCellStyleRef(ref);
    writer.writeLine();
    writer.writeEmptyCell();
    writer.setCellAnnotation("ceci est un commentaire n1");
    writer.writeCell("coucou");

    QVariant var(28.753423424);
    writer.writeCellVariant(var);

    QDateTime currentdate(QDateTime::currentDateTime());

    writer.setCellAnnotation("ceci est un commentaire sur une date");
    writer.writeCell(currentdate);

    writer.clearTableCellStyleRef();
    writer.writeLine();
    writer.writeEmptyCell();
    writer.setCellAnnotation("ceci est un commentaire");
    writer.writeCell("coucou");
    writer.writeCell(vf);
    writer.writeCell(currentdate);

    writer.writeCellPercentage(0.2);

    QUrl urltest("http://pappso.inra.fr/");
    writer.writeCell(urltest, "ceci est un lien");

    writer.writeSheet("test coordinates");

    QStringList list_coordinates;
    for(int i = 0; i < 200; i++)
      {
        writer.writeCell(i + 1);
        list_coordinates << writer.getOdsCellCoordinate();
      }
    writer.writeLine();
    for(const QString &coord : list_coordinates)
      {
        writer.writeCell(coord);
      }
    settings.setVerticalSplit(1);
    writer.setCurrentOdsTableSettings(settings);
    settings.setVerticalSplit(2);
    writer.setCurrentOdsTableSettings(settings);

    writer.close();

    file.close();

    // qDebug() << "coucou";
    CustomHandler handler;
    // file.open();
    OdsDocReader reader(handler);
    reader.parse(&file);
    file.close();

    QFile file2("test2.ods");
    OdsDocReader reader2(handler);
    reader2.parse(&file2);

    file2.close();

    // SUCCESS

    OdsDocWriter writerb("testbis.ods");
    writerb.writeCell("coucou");

    writerb.writeCell(urltest, "ceci est un lien");
  }
}