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