/************************************************************************* ** DvisvgmSpecialTest.cpp ** ** ** ** This file is part of dvisvgm -- a fast DVI to SVG converter ** ** Copyright (C) 2005-2022 Martin Gieseking ** ** ** ** This program is free software; you can redistribute it and/or ** ** modify it under the terms of the GNU General Public License as ** ** published by the Free Software Foundation; either version 3 of ** ** the License, or (at your option) any later version. ** ** ** ** This program is distributed in the hope that it will be useful, but ** ** WITHOUT ANY WARRANTY; without even the implied warranty of ** ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** ** GNU General Public License for more details. ** ** ** ** You should have received a copy of the GNU General Public License ** ** along with this program; if not, see . ** *************************************************************************/ #include #include #include #include "DvisvgmSpecialHandler.hpp" #include "SpecialActions.hpp" #include "XMLNode.hpp" using namespace std; class MyDvisvgmSpecialHandler : public DvisvgmSpecialHandler { public: void finishPreprocessing () {dviPreprocessingFinished();} void finishPage () {dviEndPage(0, _actions);} protected: EmptySpecialActions _actions; }; class DvisvgmSpecialTest : public ::testing::Test { protected: class ActionsRecorder : public EmptySpecialActions { public: void embed (const BoundingBox &bb) override {bbox.embed(bb);} double getX () const override {return -42;} double getY () const override {return 14;} bool defsEquals (const string &str) const {return defsString() == str;} bool pageEquals (const string &str) const {return pageString() == str;} bool bboxEquals (const string &str) const {return bbox.toSVGViewBox() == str;} string bboxString () const {return bbox.toSVGViewBox();} string defsString () const {return toString(svgTree().defsNode());} string pageString () const {return toString(svgTree().pageNode());} void clear () { SpecialActions::svgTree().reset(); SpecialActions::svgTree().newPage(1); bbox = BoundingBox(0, 0, 0, 0); } protected: string toString (const XMLNode *node) const { ostringstream oss; if (node) node->write(oss); return oss.str(); } private: BoundingBox bbox; }; void SetUp () override { recorder.clear(); XMLElement::WRITE_NEWLINES = false; } protected: MyDvisvgmSpecialHandler handler; ActionsRecorder recorder; }; TEST_F(DvisvgmSpecialTest, basic) { EXPECT_EQ(handler.name(), "dvisvgm"); } TEST_F(DvisvgmSpecialTest, rawText) { istringstream iss("raw first{?nl}{?x},{?y}"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("first\n-42,14")) << recorder.pageString(); iss.clear(); iss.str("raw \t ;{?(-x+2*y-5)}{?(-y+2*x-5)}second {?bbox dummy} \t"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("first\n-42,14;65-103second 0 0 0 0")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, rawPage1) { istringstream iss("raw text1<text2text3"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("text1<text2text3")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, rawPage2) { istringstream iss("raw text1"); handler.process("", iss, recorder); iss.clear(); iss.str("raw text2text3text4"); handler.process("", iss, recorder); iss.clear(); iss.str("raw "); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("text1text2text3text4")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, rawPage3) { istringstream iss("raw text2text3text4"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("text2text3text4")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, rawDefs1) { istringstream iss("rawdef text1<text2text3"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("text1<text2text3")) << recorder.defsString(); EXPECT_TRUE(recorder.pageEquals("")); } TEST_F(DvisvgmSpecialTest, rawDefs2) { istringstream iss("rawdef text1"); handler.process("", iss, recorder); iss.clear(); iss.str("rawdef text2text3text4"); handler.process("", iss, recorder); iss.clear(); iss.str("rawdef "); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("text1text2text3text4")); EXPECT_TRUE(recorder.pageEquals("")); } TEST_F(DvisvgmSpecialTest, rawDefs3) { istringstream iss("rawdef text2text3text4"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("text2text3text4")); EXPECT_TRUE(recorder.pageEquals("")); } TEST_F(DvisvgmSpecialTest, rawCDATA) { istringstream iss("raw text1]]>text2"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("text1]]>text2")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, rawComments) { istringstream iss("raw text"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("text")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, rawPI) { istringstream iss("raw "); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, rawPageFail) { istringstream iss("raw "); EXPECT_THROW(handler.process("", iss, recorder), SpecialException); // spurious closing tag iss.clear(); iss.str("raw text"); EXPECT_THROW(handler.process("", iss, recorder), SpecialException); // mismatching tags } TEST_F(DvisvgmSpecialTest, rawDefsFail) { istringstream iss("rawdef "); EXPECT_THROW(handler.process("", iss, recorder), SpecialException); // spurious closing tag iss.clear(); iss.str("rawdef text"); EXPECT_THROW(handler.process("", iss, recorder), SpecialException); // mismatching tags } TEST_F(DvisvgmSpecialTest, rawdef) { std::istringstream iss("rawdef first"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("first")) << recorder.defsString(); EXPECT_TRUE(recorder.pageEquals("")); iss.clear(); iss.str("rawdef \t \t"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("first")); EXPECT_TRUE(recorder.pageEquals("")); } TEST_F(DvisvgmSpecialTest, pattern1) { const auto cmds = { "rawset pat1", "raw text1", "raw text2", "endrawset", "raw first", "rawput pat1", "rawput pat1", }; for (const char *cmd : cmds) { std::istringstream iss(cmd); handler.preprocess("", iss, recorder); } handler.finishPreprocessing(); for (const char *cmd : cmds) { std::istringstream iss(cmd); handler.process("", iss, recorder); } handler.finishPage(); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("firsttext1text2text1text2")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, pattern2) { const auto cmds = { "rawset pat2", "rawdef text1", "rawdef text2", "endrawset", "rawdef first", "rawput pat2", "rawput pat2", }; for (const char *cmd : cmds) { std::istringstream iss(cmd); handler.preprocess("", iss, recorder); } handler.finishPreprocessing(); for (const char *cmd : cmds) { std::istringstream iss(cmd); handler.process("", iss, recorder); } handler.finishPage(); EXPECT_TRUE(recorder.defsEquals("firsttext1text2")) << recorder.defsString(); EXPECT_TRUE(recorder.pageEquals("")); } TEST_F(DvisvgmSpecialTest, pattern3) { const auto cmds = { "rawset pat3", "raw text", "rawdef text2", "endrawset", "rawdef first", "raw second", "rawput pat3", "rawput pat3", }; for (const char *cmd : cmds) { std::istringstream iss(cmd); handler.preprocess("", iss, recorder); } handler.finishPreprocessing(); for (const char *cmd : cmds) { std::istringstream iss(cmd); handler.process("", iss, recorder); } EXPECT_TRUE(recorder.defsEquals("firsttext2")); EXPECT_TRUE(recorder.pageEquals("secondtexttext")) << recorder.pageString(); handler.finishPage(); } TEST_F(DvisvgmSpecialTest, fail1) { std::istringstream iss("rawset"); // pattern name missing EXPECT_THROW(handler.preprocess("", iss, recorder), SpecialException); handler.finishPreprocessing(); } TEST_F(DvisvgmSpecialTest, fail2) { std::istringstream iss("rawset pat"); // endrawset missing handler.preprocess("", iss, recorder); EXPECT_THROW(handler.finishPreprocessing(), SpecialException); } TEST_F(DvisvgmSpecialTest, processImg) { std::istringstream iss("img 72.27 72.27 test.png"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("")) << recorder.pageString(); recorder.clear(); iss.clear(); iss.str("img 10bp 20bp test2.png"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.pageEquals("")) << recorder.pageString(); } TEST_F(DvisvgmSpecialTest, fail3) { std::istringstream iss("img 10 20xy test.png"); // unknown unit EXPECT_THROW(handler.process("", iss, recorder), SpecialException); } TEST_F(DvisvgmSpecialTest, processBBox) { std::istringstream iss("bbox abs 0 0 72.27 72.27"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.defsEquals("")); EXPECT_TRUE(recorder.pageEquals("")); EXPECT_TRUE(recorder.bboxEquals("0 0 72 72")); recorder.clear(); iss.clear(); iss.str("bbox 72.27 72.27"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.bboxEquals("-42 -58 72 72")); recorder.clear(); iss.clear(); iss.str("bbox 72bp 72bp"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.bboxEquals("-42 -58 72 72")); recorder.clear(); iss.clear(); iss.str("bbox rel 72.27 72.27"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.bboxEquals("-42 -58 72 72")); recorder.clear(); iss.clear(); iss.str("bbox new name"); handler.process("", iss, recorder); EXPECT_TRUE(recorder.bboxEquals("0 0 0 0")); } TEST_F(DvisvgmSpecialTest, fail4) { std::istringstream iss("bbox abs 0 0 72.27xx 72.27"); // unknown unit EXPECT_THROW(handler.process("", iss, recorder), SpecialException); }