Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / sc / qa / unit / helper / qahelper.hxx
blob32f7f7a59229d73943493f9343ea1be2fed82c8e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * Major Contributor(s):
16 * Copyright (C) 2012 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer)
18 * All Rights Reserved.
20 * For minor contributions see the git repository.
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 * instead of those above.
29 #ifndef SC_QA_HELPER_HXX
30 #define SC_QA_HELPER_HXX
32 #include "helper/csv_handler.hxx"
33 #include "helper/debughelper.hxx"
34 #include "orcus/csv_parser.hpp"
35 #include <fstream>
36 #include <string>
37 #include <sstream>
39 #include <osl/detail/android-bootstrap.h>
41 // Why is this here and not in osl, and using the already existing file
42 // handling APIs? Do we really want to add arbitrary new file handling
43 // wrappers here and there (and then having to handle the Android (and
44 // eventually perhaps iOS) special cases here, too)? Please move this to osl,
45 // it sure looks gemerally useful. Or am I missing something?
47 void loadFile(const rtl::OUString& aFileName, std::string& aContent)
49 rtl::OString aOFileName = rtl::OUStringToOString(aFileName, RTL_TEXTENCODING_UTF8);
51 #ifdef ANDROID
52 const char *contents;
53 size_t size;
54 if (strncmp(aOFileName.getStr(), "/assets/", sizeof("/assets/")-1) == 0) {
55 contents = (const char *) lo_apkentry(aOFileName.getStr(), &size);
56 if (contents != 0) {
57 aContent = std::string(contents, size);
58 return;
61 #endif
63 std::ifstream aFile(aOFileName.getStr());
65 rtl::OStringBuffer aErrorMsg("Could not open csv file: ");
66 aErrorMsg.append(aOFileName);
67 CPPUNIT_ASSERT_MESSAGE(aErrorMsg.getStr(), aFile);
68 std::ostringstream aOStream;
69 aOStream << aFile.rdbuf();
70 aFile.close();
71 aContent = aOStream.str();
74 void testFile(rtl::OUString& aFileName, ScDocument* pDoc, SCTAB nTab, StringType aStringFormat = StringValue)
76 csv_handler aHandler(pDoc, nTab, aStringFormat);
77 orcus::csv_parser_config aConfig;
78 aConfig.delimiters.push_back(',');
79 aConfig.delimiters.push_back(';');
80 aConfig.text_qualifier = '"';
81 aConfig.trim_cell_value = false;
84 std::string aContent;
85 loadFile(aFileName, aContent);
86 orcus::csv_parser<csv_handler> parser ( &aContent[0], aContent.size() , aHandler, aConfig);
87 try
89 parser.parse();
91 catch (const orcus::csv_parse_error& e)
93 std::cout << "reading csv content file failed: " << e.what() << std::endl;
94 rtl::OStringBuffer aErrorMsg("csv parser error: ");
95 aErrorMsg.append(e.what());
96 CPPUNIT_ASSERT_MESSAGE(aErrorMsg.getStr(), false);
100 //need own handler because conditional formatting strings must be generated
101 void testCondFile(rtl::OUString& aFileName, ScDocument* pDoc, SCTAB nTab)
103 conditional_format_handler aHandler(pDoc, nTab);
104 orcus::csv_parser_config aConfig;
105 aConfig.delimiters.push_back(',');
106 aConfig.delimiters.push_back(';');
107 aConfig.text_qualifier = '"';
108 std::string aContent;
109 loadFile(aFileName, aContent);
110 orcus::csv_parser<conditional_format_handler> parser ( &aContent[0], aContent.size() , aHandler, aConfig);
113 parser.parse();
115 catch (const orcus::csv_parse_error& e)
117 std::cout << "reading csv content file failed: " << e.what() << std::endl;
118 rtl::OStringBuffer aErrorMsg("csv parser error: ");
119 aErrorMsg.append(e.what());
120 CPPUNIT_ASSERT_MESSAGE(aErrorMsg.getStr(), false);
125 #define ASSERT_DOUBLES_EQUAL( expected, result ) \
126 CPPUNIT_ASSERT_DOUBLES_EQUAL( (expected), (result), 1e-14 )
128 #define ASSERT_DOUBLES_EQUAL_MESSAGE( message, expected, result ) \
129 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( (message), (expected), (result), 1e-14 )
131 #endif
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */