More testing / SubRip parse fixes.
[dcpomatic.git] / test / subrip_test.cc
1 /*
2     Copyright (C) 2014 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 <boost/test/unit_test.hpp>
21 #include "lib/subrip.h"
22 #include "lib/subrip_content.h"
23
24 using std::list;
25 using std::string;
26 using boost::shared_ptr;
27
28 /** Test SubRip::convert_time */
29 BOOST_AUTO_TEST_CASE (subrip_time_test)
30 {
31         BOOST_CHECK_EQUAL (SubRip::convert_time ("00:03:10,500"), rint (((3 * 60) + 10 + 0.5) * TIME_HZ));
32         BOOST_CHECK_EQUAL (SubRip::convert_time ("04:19:51,782"), rint (((4 * 3600) + (19 * 60) + 51 + 0.782) * TIME_HZ));
33 }
34
35 /** Test SubRip::convert_coordinate */
36 BOOST_AUTO_TEST_CASE (subrip_coordinate_test)
37 {
38         BOOST_CHECK_EQUAL (SubRip::convert_coordinate ("foo:42"), 42);
39         BOOST_CHECK_EQUAL (SubRip::convert_coordinate ("X1:999"), 999);
40 }
41
42 /** Test SubRip::convert_content */
43 BOOST_AUTO_TEST_CASE (subrip_content_test)
44 {
45         list<string> c;
46         list<SubRipSubtitlePiece> p;
47         
48         c.push_back ("Hello world");
49         p = SubRip::convert_content (c);
50         BOOST_CHECK_EQUAL (p.size(), 1);
51         BOOST_CHECK_EQUAL (p.front().text, "Hello world");
52         c.clear ();
53
54         c.push_back ("<b>Hello world</b>");
55         p = SubRip::convert_content (c);
56         BOOST_CHECK_EQUAL (p.size(), 1);
57         BOOST_CHECK_EQUAL (p.front().text, "Hello world");
58         BOOST_CHECK_EQUAL (p.front().bold, true);
59         c.clear ();
60
61         c.push_back ("<i>Hello world</i>");
62         p = SubRip::convert_content (c);
63         BOOST_CHECK_EQUAL (p.size(), 1);
64         BOOST_CHECK_EQUAL (p.front().text, "Hello world");
65         BOOST_CHECK_EQUAL (p.front().italic, true);
66         c.clear ();
67
68         c.push_back ("<u>Hello world</u>");
69         p = SubRip::convert_content (c);
70         BOOST_CHECK_EQUAL (p.size(), 1);
71         BOOST_CHECK_EQUAL (p.front().text, "Hello world");
72         BOOST_CHECK_EQUAL (p.front().underline, true);
73         c.clear ();
74
75         c.push_back ("{b}Hello world{/b}");
76         p = SubRip::convert_content (c);
77         BOOST_CHECK_EQUAL (p.size(), 1);
78         BOOST_CHECK_EQUAL (p.front().text, "Hello world");
79         BOOST_CHECK_EQUAL (p.front().bold, true);
80         c.clear ();
81
82         c.push_back ("{i}Hello world{/i}");
83         p = SubRip::convert_content (c);
84         BOOST_CHECK_EQUAL (p.size(), 1);
85         BOOST_CHECK_EQUAL (p.front().text, "Hello world");
86         BOOST_CHECK_EQUAL (p.front().italic, true);
87         c.clear ();
88
89         c.push_back ("{u}Hello world{/u}");
90         p = SubRip::convert_content (c);
91         BOOST_CHECK_EQUAL (p.size(), 1);
92         BOOST_CHECK_EQUAL (p.front().text, "Hello world");
93         BOOST_CHECK_EQUAL (p.front().underline, true);
94         c.clear ();
95
96         c.push_back ("<b>This is <i>nesting</i> of subtitles</b>");
97         p = SubRip::convert_content (c);
98         BOOST_CHECK_EQUAL (p.size(), 3);
99         list<SubRipSubtitlePiece>::iterator i = p.begin ();     
100         BOOST_CHECK_EQUAL (i->text, "This is ");
101         BOOST_CHECK_EQUAL (i->bold, true);
102         BOOST_CHECK_EQUAL (i->italic, false);
103         ++i;
104         BOOST_CHECK_EQUAL (i->text, "nesting");
105         BOOST_CHECK_EQUAL (i->bold, true);
106         BOOST_CHECK_EQUAL (i->italic, true);
107         ++i;
108         BOOST_CHECK_EQUAL (i->text, " of subtitles");
109         BOOST_CHECK_EQUAL (i->bold, true);
110         BOOST_CHECK_EQUAL (i->italic, false);
111         ++i;
112         c.clear ();
113 }
114
115 /** Test parsing of full SubRip file content */
116 BOOST_AUTO_TEST_CASE (subrip_parse_test)
117 {
118         shared_ptr<SubRipContent> content (new SubRipContent (shared_ptr<Film> (), "test/data/subrip.srt"));
119         content->examine (shared_ptr<Job> ());
120         BOOST_CHECK_EQUAL (content->full_length(), ((3 * 60) + 56.471) * TIME_HZ);
121
122         SubRip s (content);
123
124         list<SubRipSubtitle>::const_iterator i = s._subtitles.begin();
125
126         BOOST_CHECK (i != s._subtitles.end ());
127         BOOST_CHECK_EQUAL (i->from, ((1 * 60) + 49.200) * TIME_HZ);
128         BOOST_CHECK_EQUAL (i->to, ((1 * 60) + 52.351) * TIME_HZ);
129         BOOST_CHECK_EQUAL (i->pieces.size(), 1);
130         BOOST_CHECK_EQUAL (i->pieces.front().text, "This is a subtitle, and it goes over two lines.");
131
132         ++i;
133         BOOST_CHECK (i != s._subtitles.end ());
134         BOOST_CHECK_EQUAL (i->from, ((1 * 60) + 52.440) * TIME_HZ);
135         BOOST_CHECK_EQUAL (i->to, ((1 * 60) + 54.351) * TIME_HZ);
136         BOOST_CHECK_EQUAL (i->pieces.size(), 1);
137         BOOST_CHECK_EQUAL (i->pieces.front().text, "We have emboldened this");
138         BOOST_CHECK_EQUAL (i->pieces.front().bold, true);
139
140         ++i;
141         BOOST_CHECK (i != s._subtitles.end ());
142         BOOST_CHECK_EQUAL (i->from, ((1 * 60) + 54.440) * TIME_HZ);
143         BOOST_CHECK_EQUAL (i->to, ((1 * 60) + 56.590) * TIME_HZ);
144         BOOST_CHECK_EQUAL (i->pieces.size(), 1);
145         BOOST_CHECK_EQUAL (i->pieces.front().text, "And italicised this.");
146         BOOST_CHECK_EQUAL (i->pieces.front().italic, true);
147
148         ++i;
149         BOOST_CHECK (i != s._subtitles.end ());
150         BOOST_CHECK_EQUAL (i->from, ((1 * 60) + 56.680) * TIME_HZ);
151         BOOST_CHECK_EQUAL (i->to, ((1 * 60) + 58.955) * TIME_HZ);
152         BOOST_CHECK_EQUAL (i->pieces.size(), 1);
153         BOOST_CHECK_EQUAL (i->pieces.front().text, "Shall I compare thee to a summers' day?");
154
155         ++i;
156         BOOST_CHECK (i != s._subtitles.end ());
157         BOOST_CHECK_EQUAL (i->from, ((2 * 60) + 0.840) * TIME_HZ);
158         BOOST_CHECK_EQUAL (i->to, ((2 * 60) + 3.400) * TIME_HZ);
159         BOOST_CHECK_EQUAL (i->pieces.size(), 1);
160         BOOST_CHECK_EQUAL (i->pieces.front().text, "Is this a dagger I see before me?");
161
162         ++i;
163         BOOST_CHECK (i != s._subtitles.end ());
164         BOOST_CHECK_EQUAL (i->from, ((3 * 60) + 54.560) * TIME_HZ);
165         BOOST_CHECK_EQUAL (i->to, ((3 * 60) + 56.471) * TIME_HZ);
166         BOOST_CHECK_EQUAL (i->pieces.size(), 1);
167         BOOST_CHECK_EQUAL (i->pieces.front().text, "Hello world.");
168
169         ++i;
170         BOOST_CHECK (i == s._subtitles.end ());
171 }