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