1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 
8 #include "orcus/orcus_json.hpp"
9 #include "orcus/stream.hpp"
10 #include "orcus/spreadsheet/document.hpp"
11 #include "orcus/spreadsheet/factory.hpp"
12 #include "orcus/exception.hpp"
13 #include "orcus/global.hpp"
14 
15 #include <iostream>
16 #include <vector>
17 #include <cassert>
18 #include <sstream>
19 #include <boost/filesystem.hpp>
20 
21 using namespace std;
22 using namespace orcus;
23 namespace fs = boost::filesystem;
24 
25 namespace {
26 
27 const std::vector<const char*> tests =
28 {
29     SRCDIR"/test/json-mapped/array-of-arrays-basic",
30     SRCDIR"/test/json-mapped/array-of-arrays-header",
31     SRCDIR"/test/json-mapped/array-of-objects-basic",
32     SRCDIR"/test/json-mapped/array-of-objects-header",
33     SRCDIR"/test/json-mapped/nested-repeats",
34     SRCDIR"/test/json-mapped/nested-repeats-2",
35 };
36 
37 } // anonymous namespace
38 
test_mapped_json_import()39 void test_mapped_json_import()
40 {
41     for (fs::path base_dir : tests)
42     {
43         fs::path data_file = base_dir / "input.json";
44         fs::path map_file = base_dir / "map.json";
45         fs::path check_file = base_dir / "check.txt";
46 
47         cout << "reading " << data_file.string() << endl;
48         file_content content(data_file.string().data());
49         file_content map_content(map_file.string().data());
50         file_content check_content(check_file.string().data());
51 
52         spreadsheet::range_size_t ss{1048576, 16384};
53         spreadsheet::document doc{ss};
54         spreadsheet::import_factory import_fact(doc);
55 
56         orcus_json app(&import_fact);
57         app.read_map_definition(map_content.data(), map_content.size());
58         app.read_stream(content.data(), content.size());
59 
60         std::ostringstream os;
61         doc.dump_check(os);
62 
63         std::string actual_strm = os.str();
64         pstring actual(actual_strm);
65         pstring expected = check_content.str();
66         actual = actual.trim();
67         expected = expected.trim();
68         assert(actual == expected);
69     }
70 }
71 
test_invalid_map_definition()72 void test_invalid_map_definition()
73 {
74     spreadsheet::range_size_t ss{1048576, 16384};
75     spreadsheet::document doc{ss};
76     spreadsheet::import_factory import_fact(doc);
77 
78     orcus_json app(&import_fact);
79     try
80     {
81         app.read_map_definition(ORCUS_ASCII("asdfdasf"));
82         assert(false); // We were expecting an exception, but didn't get one.
83     }
84     catch (const invalid_map_error&)
85     {
86         // Success!
87     }
88 }
89 
main(int argc,char ** argv)90 int main(int argc, char** argv)
91 {
92     test_mapped_json_import();
93     test_invalid_map_definition();
94 
95     return EXIT_SUCCESS;
96 }
97 
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
99 
100