]> git.lyx.org Git - lyx.git/blob - src/tests/check_layout.cpp
Introduce new function copyInsetToTemp
[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 "../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)
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 } // namespace lyx
37
38
39 using namespace lyx::support;
40 using namespace lyx;
41
42 using namespace std;
43
44
45 bool test_Layout(string const & input, string const & output)
46 {
47         FileName const ifn(makeAbsPath(input));
48         LayoutFileList & l = LayoutFileList::get();
49         LayoutFileIndex i = onlyFileName(l.addLocalLayout(ifn.onlyFileName(), ifn.onlyPath().absFileName()));
50         if (i.empty()) {
51                 cerr << "Could not read layout file " << input << ".layout.\n";
52                 return false;
53         }
54         LayoutFile const & f = l[i];
55         ostream * os = nullptr;
56         if (output == "-")
57                 os = &cout;
58         else if (!output.empty())
59                 os = new ofstream(output.c_str());
60         bool success = true;
61         for (TextClass::const_iterator it = f.begin(); it != f.end(); ++it) {
62                 if (os)
63                         it->write(*os);
64                 ostringstream oss;
65                 it->write(oss);
66                 istringstream iss(oss.str());
67                 // swallow "Style Standard" line
68                 string line;
69                 if (getline(iss, line)) {
70                         Lexer lex;
71                         lex.setStream(iss);
72                         Layout test;
73                         test.setName(it->name());
74                         if (test.read(lex, f)) {
75                                 // Caution: operator==() is incomplete!
76                                 // Testing test == *it does not make much sense
77                                 // therefore.
78                                 // It does not work either for styles with
79                                 // non-empty obsoletedby if the obsoletedby
80                                 // style was modified after the obsolete style
81                                 // was defined: Layout::write() writes only the
82                                 // final version, but the obsolete style was
83                                 // set to the version at the time it was
84                                 // defined, and therefore they differ. See e.g.
85                                 // the obsolete style AMS which is replaced by
86                                 // Subjectclass in amsdefs.inc, and
87                                 // Subjectclass is modified later in siamltex.layout.
88                                 ostringstream osstest;
89                                 test.write(osstest);
90                                 if (osstest.str() != oss.str()) {
91                                         cerr << "Round trip for style "
92                                              << to_utf8(it->name())
93                                              << " failed:\n"
94                                              << osstest.str() << oss.str();
95                                         success = false;
96                                 }
97                         } else {
98                                 cerr << "Could not read style "
99                                      << to_utf8(it->name()) << ".\n";
100                                 success = false;
101                         }
102                 } else {
103                         cerr << "Could not read first line for style "
104                              << to_utf8(it->name()) << ".\n";
105                         success = false;
106                 }
107         }
108         if (output != "-")
109                 delete os;
110         return success;
111 }
112
113
114 int main(int argc, char * argv[])
115 {
116         os::init(argc, &argv);
117         lyxerr.setStream(cerr);
118         if (argc < 2 || argc > 3) {
119                 cerr << "Usage: " << argv[0] << " <input layout file> [<output layout file>]\n";
120                 return EXIT_FAILURE;
121         }
122         FileName const exe(makeAbsPath(os::internal_path(os::utf8_argv(0))));
123         string const lyxexe = addName(exe.onlyPath().absFileName(), "lyx");
124         init_package(lyxexe, string(), string());
125         if (test_Layout(argv[1], argc > 2 ? argv[2] : ""))
126                 return EXIT_SUCCESS;
127         return EXIT_FAILURE;
128 }