2 Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "ssa_reader.h"
24 #include <boost/test/unit_test.hpp>
25 #include <boost/filesystem.hpp>
30 BOOST_AUTO_TEST_CASE (ssa_reader_test)
32 boost::filesystem::path p = private_test / "example.ssa";
33 FILE* f = fopen (p.string().c_str(), "r");
34 sub::SSAReader reader (f);
36 list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
38 list<sub::Subtitle>::iterator i = subs.begin ();
40 BOOST_REQUIRE (i != subs.end ());
41 BOOST_CHECK_EQUAL (i->from, sub::Time::from_hms (0, 2, 40, 650));
42 BOOST_CHECK_EQUAL (i->to, sub::Time::from_hms (0, 2, 41, 790));
43 list<sub::Line>::iterator j = i->lines.begin();
44 BOOST_REQUIRE (j != i->lines.end ());
45 BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
46 sub::Block b = j->blocks.front ();
47 BOOST_CHECK_EQUAL (b.text, "Et les enregistrements de ses ondes delta ?");
48 BOOST_CHECK_EQUAL (b.font.get(), "Wolf_Rain");
49 BOOST_CHECK_EQUAL (b.font_size.points().get(), 56);
50 BOOST_CHECK_EQUAL (b.bold, false);
51 BOOST_CHECK_EQUAL (b.italic, false);
52 BOOST_CHECK_EQUAL (b.underline, false);
55 BOOST_REQUIRE (i != subs.end ());
56 BOOST_CHECK_EQUAL (i->from, sub::Time::from_hms (0, 2, 42, 420));
57 BOOST_CHECK_EQUAL (i->to, sub::Time::from_hms (0, 2, 44, 150));
59 BOOST_REQUIRE (j != i->lines.end ());
60 BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
61 b = j->blocks.front ();
62 BOOST_CHECK_EQUAL (b.text, "Toujours rien.");
63 BOOST_CHECK_EQUAL (b.font.get(), "Wolf_Rain");
64 BOOST_CHECK_EQUAL (b.font_size.points().get(), 56);
65 BOOST_CHECK_EQUAL (b.bold, false);
66 BOOST_CHECK_EQUAL (b.italic, false);
67 BOOST_CHECK_EQUAL (b.underline, false);
70 BOOST_CHECK (i == subs.end());
73 BOOST_AUTO_TEST_CASE (ssa_reader_line_test1)
75 sub::RawSubtitle base;
76 list<sub::RawSubtitle> r = sub::SSAReader::parse_line (base, "This is a line with some {i1}italics{i0} and then\\nthere is a new line.");
78 list<sub::RawSubtitle>::const_iterator i = r.begin ();
79 BOOST_CHECK_EQUAL (i->text, "This is a line with some ");
80 BOOST_CHECK_EQUAL (i->italic, false);
82 BOOST_REQUIRE (i != r.end ());
84 BOOST_CHECK_EQUAL (i->text, "italics");
85 BOOST_CHECK_EQUAL (i->italic, true);
87 BOOST_REQUIRE (i != r.end ());
89 BOOST_CHECK_EQUAL (i->text, " and then");
90 BOOST_CHECK_EQUAL (i->italic, false);
92 BOOST_REQUIRE (i != r.end ());
94 BOOST_CHECK_EQUAL (i->text, "there is a new line.");
96 BOOST_REQUIRE (i == r.end ());
99 BOOST_AUTO_TEST_CASE (ssa_reader_line_test2)
101 sub::RawSubtitle base;
102 list<sub::RawSubtitle> r = sub::SSAReader::parse_line (base, "{i1}It's all just italics{i0}");
104 list<sub::RawSubtitle>::const_iterator i = r.begin ();
105 BOOST_CHECK_EQUAL (i->text, "It's all just italics");
106 BOOST_CHECK_EQUAL (i->italic, true);
108 BOOST_REQUIRE (i == r.end ());
112 test (boost::filesystem::path p)
114 p = private_test / p;
115 FILE* f = fopen (p.string().c_str(), "r");
117 sub::SSAReader r (f);
121 /** Test of reading some typical .ssa files */
122 BOOST_AUTO_TEST_CASE (ssa_reader_test2)
124 test ("DKH_UT_EN20160601def.ssa");
127 /** Test reading of a file within the libsub tree which exercises the parser */
128 BOOST_AUTO_TEST_CASE (ssa_reader_test3)
130 boost::filesystem::path p = "test/data/test.ssa";
131 FILE* f = fopen (p.string().c_str(), "r");
132 sub::SSAReader reader (f);
134 list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
136 list<sub::Subtitle>::iterator i = subs.begin ();
138 BOOST_REQUIRE (i != subs.end ());
139 BOOST_CHECK_EQUAL (i->from, sub::Time::from_hms (0, 0, 1, 230));
140 BOOST_CHECK_EQUAL (i->to, sub::Time::from_hms (0, 0, 4, 550));
141 list<sub::Line>::iterator j = i->lines.begin();
142 BOOST_REQUIRE (j != i->lines.end ());
143 BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
144 sub::Block b = j->blocks.front ();
145 BOOST_CHECK_EQUAL (b.text, "Hello world");
146 BOOST_CHECK_EQUAL (b.font.get(), "Arial");
147 BOOST_CHECK_EQUAL (b.font_size.points().get(), 20);
148 BOOST_CHECK_EQUAL (b.bold, false);
149 BOOST_CHECK_EQUAL (b.italic, false);
150 BOOST_CHECK_EQUAL (b.underline, false);
153 BOOST_REQUIRE (i != subs.end ());
154 BOOST_CHECK_EQUAL (i->from, sub::Time::from_hms (0, 0, 5, 740));
155 BOOST_CHECK_EQUAL (i->to, sub::Time::from_hms (0, 0, 11, 0));
156 j = i->lines.begin();
157 BOOST_REQUIRE (j != i->lines.end ());
158 BOOST_REQUIRE_EQUAL (j->blocks.size(), 1);
159 b = j->blocks.front ();
160 BOOST_CHECK_EQUAL (b.text, "This is vertically moved");
161 BOOST_CHECK_EQUAL (b.font.get(), "Arial");
162 BOOST_CHECK_EQUAL (b.font_size.points().get(), 20);
163 BOOST_CHECK_EQUAL (b.bold, false);
164 BOOST_CHECK_EQUAL (b.italic, false);
165 BOOST_CHECK_EQUAL (b.underline, false);
168 BOOST_REQUIRE (i == subs.end ());