Fixes broken export to PDF with links.
[inkscape.git] / testfiles / src / stream-test.cpp
blobd9c7c5e929b8c335e330a160b5351f05b5c823bf
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** @file
3 * Stream IO tests
4 *//*
5 * Authors: see git history
7 * Copyright (C) 2015-2023 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
11 #include <cstdio>
12 #include <gtest/gtest.h>
13 #include <string>
15 #include "io/stream/gzipstream.h"
16 #include "io/stream/inkscapestream.h"
17 #include "io/stream/stringstream.h"
18 #include "io/stream/uristream.h"
19 #include "io/stream/xsltstream.h"
20 #include "util/delete-with.h"
22 // names and path storage for other tests
23 auto const xmlpath = INKSCAPE_TESTS_DIR "/data/crystalegg.xml";
24 auto const xslpath = INKSCAPE_TESTS_DIR "/data/doc2html.xsl";
26 class MyFile
28 protected:
29 std::string _filename;
30 std::string _mode;
32 public:
33 MyFile(std::string filename, char const *mode = "rb")
34 : _filename(std::move(filename))
35 , _mode(mode)
38 FILE *open(char const *mode) const { return std::fopen(_filename.c_str(), mode); }
40 operator FILE *() const { return open(_mode.c_str()); }
42 std::string getContents() const
44 std::string buf;
45 auto fp = Inkscape::Util::delete_with<std::fclose>(open("rb"));
47 if (!fp) {
48 ADD_FAILURE() << "failed to open " << _filename;
49 exit(1);
52 for (int c; (c = std::fgetc(fp.get())) != EOF;) {
53 buf.push_back(c);
56 return buf;
60 class MyOutFile : public MyFile
62 public:
63 MyOutFile(std::string filename)
64 : MyFile("test_stream-out-" + filename, "wb")
67 ~MyOutFile() { std::remove(_filename.c_str()); }
70 TEST(StreamTest, FileStreamCopy)
72 auto inFile = MyFile(xmlpath);
73 auto outFile = MyOutFile("streamtest.copy");
75 auto ins = Inkscape::IO::FileInputStream(inFile);
76 auto outs = Inkscape::IO::FileOutputStream(outFile);
77 pipeStream(ins, outs);
79 ASSERT_EQ(inFile.getContents(), outFile.getContents());
82 TEST(StreamTest, OutputStreamWriter)
84 Inkscape::IO::StdOutputStream outs;
85 Inkscape::IO::OutputStreamWriter writer(outs);
86 writer << "Hello, world! " << 123.45 << " times\n";
87 writer.printf("There are %f quick brown foxes in %d states\n", 123.45, 88);
90 TEST(StreamTest, StdWriter)
92 Inkscape::IO::StdWriter writer;
93 writer << "Hello, world! " << 123.45 << " times\n";
94 writer.printf("There are %f quick brown foxes in %d states\n", 123.45, 88);
97 TEST(StreamTest, Xslt)
99 // ######### XSLT Sheet ############
100 auto xsltSheetFile = MyFile(xslpath);
101 auto xsltSheetIns = Inkscape::IO::FileInputStream(xsltSheetFile);
102 auto stylesheet = Inkscape::IO::XsltStyleSheet(xsltSheetIns);
103 xsltSheetIns.close();
104 auto sourceFile = MyFile(xmlpath);
105 auto xmlIns = Inkscape::IO::FileInputStream(sourceFile);
107 // ######### XSLT Input ############
108 auto destFile = MyOutFile("test.html");
109 auto xmlOuts = Inkscape::IO::FileOutputStream(destFile);
110 auto xsltIns = Inkscape::IO::XsltInputStream(xmlIns, stylesheet);
111 pipeStream(xsltIns, xmlOuts);
112 xsltIns.close();
113 xmlOuts.close();
115 // ######### XSLT Output ############
116 auto xmlIns2 = Inkscape::IO::FileInputStream(sourceFile);
117 auto destFile2 = MyOutFile("test2.html");
118 auto xmlOuts2 = Inkscape::IO::FileOutputStream(destFile2);
119 auto xsltOuts = Inkscape::IO::XsltOutputStream(xmlOuts2, stylesheet);
120 pipeStream(xmlIns2, xsltOuts);
121 xmlIns2.close();
122 xsltOuts.close();
124 auto htmlContent = destFile.getContents();
125 ASSERT_NE(htmlContent.find("<html"), std::string::npos);
126 ASSERT_EQ(htmlContent, destFile2.getContents());
129 TEST(StreamTest, Gzip)
131 auto sourceFile = MyFile(xmlpath);
132 auto gzFile = MyOutFile("test.gz");
133 auto destFile = MyOutFile("crystalegg2.xml");
135 // ######### Gzip Output ############
137 auto sourceIns = Inkscape::IO::FileInputStream(sourceFile);
138 auto gzOuts = Inkscape::IO::FileOutputStream(gzFile);
139 auto gzipOuts = Inkscape::IO::GzipOutputStream(gzOuts);
140 pipeStream(sourceIns, gzipOuts);
143 // ######### Gzip Input ############
145 auto gzIns = Inkscape::IO::FileInputStream(gzFile.open("rb"));
146 auto destOuts = Inkscape::IO::FileOutputStream(destFile);
147 auto gzipIns = Inkscape::IO::GzipInputStream(gzIns);
148 pipeStream(gzipIns, destOuts);
151 ASSERT_EQ(sourceFile.getContents(), destFile.getContents());
154 TEST(StreamTest, GzipFExtraFComment)
156 auto inFile = MyFile(INKSCAPE_TESTS_DIR "/data/example-FEXTRA-FCOMMENT.gz");
157 auto inStream = Inkscape::IO::FileInputStream(inFile);
158 auto inStreamGzip = Inkscape::IO::GzipInputStream(inStream);
159 auto outStreamString = Inkscape::IO::StringOutputStream();
160 pipeStream(inStreamGzip, outStreamString);
161 ASSERT_EQ(outStreamString.getString(), "the content");