Add another test stub.
[libsub.git] / test / ssa_reader_test.cc
1 /*
2     Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "test.h"
21 #include "ssa_reader.h"
22 #include "collect.h"
23 #include "subtitle.h"
24 #include <boost/test/unit_test.hpp>
25 #include <boost/filesystem.hpp>
26 #include <cstdio>
27
28 using std::list;
29
30 BOOST_AUTO_TEST_CASE (ssa_reader_test)
31 {
32         boost::filesystem::path p = private_test / "example.ssa";
33         FILE* f = fopen (p.string().c_str(), "r");
34         sub::SSAReader reader (f);
35         fclose (f);
36         list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
37
38         list<sub::Subtitle>::iterator i = subs.begin ();
39
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);
53         ++i;
54
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));
58         j = i->lines.begin();
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);
68         ++i;
69
70         BOOST_CHECK (i == subs.end());
71 }
72
73 BOOST_AUTO_TEST_CASE (ssa_reader_line_test1)
74 {
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.");
77
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);
81         ++i;
82         BOOST_REQUIRE (i != r.end ());
83
84         BOOST_CHECK_EQUAL (i->text, "italics");
85         BOOST_CHECK_EQUAL (i->italic, true);
86         ++i;
87         BOOST_REQUIRE (i != r.end ());
88
89         BOOST_CHECK_EQUAL (i->text, " and then");
90         BOOST_CHECK_EQUAL (i->italic, false);
91         ++i;
92         BOOST_REQUIRE (i != r.end ());
93
94         BOOST_CHECK_EQUAL (i->text, "there is a new line.");
95         ++i;
96         BOOST_REQUIRE (i == r.end ());
97 }
98
99 BOOST_AUTO_TEST_CASE (ssa_reader_line_test2)
100 {
101         sub::RawSubtitle base;
102         list<sub::RawSubtitle> r = sub::SSAReader::parse_line (base, "{i1}It's all just italics{i0}");
103
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);
107         ++i;
108         BOOST_REQUIRE (i == r.end ());
109 }
110
111 static void
112 test (boost::filesystem::path p)
113 {
114         p = private_test / p;
115         FILE* f = fopen (p.string().c_str(), "r");
116         BOOST_REQUIRE (f);
117         sub::SSAReader r (f);
118         fclose (f);
119 }
120
121 /** Test of reading some typical .ssa files */
122 BOOST_AUTO_TEST_CASE (ssa_reader_test2)
123 {
124         test ("DKH_UT_EN20160601def.ssa");
125 }
126
127 /** Test reading of a file within the libsub tree which exercises the parser */
128 BOOST_AUTO_TEST_CASE (ssa_reader_test3)
129 {
130         boost::filesystem::path p = "test/data/test.ssa";
131         FILE* f = fopen (p.string().c_str(), "r");
132         sub::SSAReader reader (f);
133         fclose (f);
134         list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> > (reader.subtitles ());
135
136         list<sub::Subtitle>::iterator i = subs.begin ();
137
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);
151         ++i;
152
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);
166         ++i;
167
168         BOOST_REQUIRE (i == subs.end ());
169 }