]> git.lyx.org Git - lyx.git/blob - src/tests/check_layout.cpp
Provide proper fallback if a bibliography processor is not found
[lyx.git] / src / tests / check_layout.cpp
1 #include <config.h>
2
3 #include "../support/FileName.h"
4 #include "../support/filetools.h"
5 #include "../Format.h"
6 #include "../LayoutFile.h"
7 #include "../LaTeXFeatures.h"
8 #include "../Lexer.h"
9 #include "../support/Messages.h"
10 #include "../support/os.h"
11 #include "../support/Package.h"
12
13 #include <cstdlib>
14 #include <fstream>
15 #include <iostream>
16 #include <sstream>
17 using namespace std;
18
19
20 namespace lyx {
21 namespace frontend {
22 namespace Alert {
23 void warning(docstring const & title, docstring const & message, bool const &)
24 {
25         LYXERR0(title);
26         LYXERR0(message);
27 }
28 } // namespace Alert
29 } // namespace frontend
30
31 bool LaTeXFeatures::isAvailable(std::string const &)
32 {
33         return false;
34 }
35
36 Formats formats;
37 bool Formats::isZippedFile(support::FileName const &) const
38 {
39         return false;
40 }
41 } // namespace lyx
42
43
44 using namespace lyx::support;
45 using namespace lyx;
46
47 using namespace std;
48
49
50 bool test_Layout(string const & input, string const & output)
51 {
52         FileName const ifn(makeAbsPath(input));
53         LayoutFileList & l = LayoutFileList::get();
54         LayoutFileIndex i = onlyFileName(l.addLocalLayout(ifn.onlyFileName(), ifn.onlyPath().absFileName()));
55         if (i.empty()) {
56                 cerr << "Could not read layout file " << input << ".layout.\n";
57                 return false;
58         }
59         LayoutFile const & f = l[i];
60         ostream * os = NULL;
61         if (output == "-")
62                 os = &cout;
63         else if (!output.empty())
64                 os = new ofstream(output.c_str());
65         bool success = true;
66         for (TextClass::const_iterator it = f.begin(); it != f.end(); ++it) {
67                 if (os)
68                         it->write(*os);
69                 ostringstream oss;
70                 it->write(oss);
71                 istringstream iss(oss.str());
72                 // swallow "Style Standard" line
73                 string line;
74                 if (getline(iss, line)) {
75                         Lexer lex;
76                         lex.setStream(iss);
77                         Layout test;
78                         test.setName(it->name());
79                         if (test.read(lex, f)) {
80                                 // Caution: operator==() is incomplete!
81                                 // Testing test == *it does not make much sense
82                                 // therefore.
83                                 // It does not work either for styles with
84                                 // non-empty obsoletedby if the obsoletedby
85                                 // style was modified after the obsolete style
86                                 // was defined: Layout::write() writes only the
87                                 // final version, but the obsolete style was
88                                 // set to the version at the time it was
89                                 // defined, and therefore they differ. See e.g.
90                                 // the obsolete style AMS which is replaced by
91                                 // Subjectclass in amsdefs.inc, and
92                                 // Subjectclass is modified later in siamltex.layout.
93                                 ostringstream osstest;
94                                 test.write(osstest);
95                                 if (osstest.str() != oss.str()) {
96                                         cerr << "Round trip for style "
97                                              << to_utf8(it->name())
98                                              << " failed:\n"
99                                              << osstest.str() << oss.str();
100                                         success = false;
101                                 }
102                         } else {
103                                 cerr << "Could not read style "
104                                      << to_utf8(it->name()) << ".\n";
105                                 success = false;
106                         }
107                 } else {
108                         cerr << "Could not read first line for style "
109                              << to_utf8(it->name()) << ".\n";
110                         success = false;
111                 }
112         }
113         if (output != "-")
114                 delete os;
115         return success;
116 }
117
118
119 int main(int argc, char * argv[])
120 {
121         os::init(argc, &argv);
122         lyxerr.setStream(cerr);
123         if (argc < 2 || argc > 3) {
124                 cerr << "Usage: " << argv[0] << " <input layout file> [<output layout file>]\n";
125                 return EXIT_FAILURE;
126         }
127         FileName const exe(makeAbsPath(os::internal_path(os::utf8_argv(0))));
128         string const lyxexe = addName(exe.onlyPath().absFileName(), "lyx");
129         init_package(lyxexe, string(), string());
130         if (test_Layout(argv[1], argc > 2 ? argv[2] : ""))
131                 return EXIT_SUCCESS;
132         return EXIT_FAILURE;
133 }