Various work on SubRip.
[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         SubRipContent content (shared_ptr<Film> (), "test/data/subrip.srt");
119         content.examine (shared_ptr<Job> ());
120 }