Add primitive WebVTT reader.
[libsub.git] / test / webvtt_reader_test.cc
1 /*
2     Copyright (C) 2022 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 "web_vtt_reader.h"
21 #include "subtitle.h"
22 #include "test.h"
23 #include "exceptions.h"
24 #include "collect.h"
25 #include <boost/test/unit_test.hpp>
26 #include <boost/filesystem.hpp>
27 #include <cmath>
28 #include <iostream>
29 #include <cstdio>
30
31
32 using std::cerr;
33 using std::vector;
34 using std::fabs;
35
36
37 /* Test reading of a VTT file */
38 BOOST_AUTO_TEST_CASE(vtt_reader_test)
39 {
40         auto f = fopen("test/data/test.vtt", "r");
41         sub::WebVTTReader reader(f);
42         fclose(f);
43         auto subs = sub::collect<std::vector<sub::Subtitle>>(reader.subtitles());
44
45         auto i = subs.begin();
46
47
48         /* First subtitle */
49
50         BOOST_REQUIRE(i != subs.end());
51         BOOST_CHECK_EQUAL(i->from, sub::Time::from_hms(0, 0, 41, 90));
52         BOOST_CHECK_EQUAL(i->to, sub::Time::from_hms(0, 0, 42, 210));
53
54         auto j = i->lines.begin();
55         BOOST_CHECK(j != i->lines.end());
56         BOOST_REQUIRE_EQUAL(j->blocks.size(), 1);
57         auto b = j->blocks[0];
58         BOOST_CHECK_EQUAL(b.text, "This is a subtitle");
59         /* No font is specified by WebVTT, so none should be seen here */
60         BOOST_CHECK(!b.font);
61         BOOST_CHECK(!b.font_size.specified());
62         BOOST_CHECK_EQUAL(b.bold, false);
63         BOOST_CHECK_EQUAL(b.italic, false);
64         BOOST_CHECK_EQUAL(b.underline, false);
65         BOOST_REQUIRE(j->vertical_position.line);
66         BOOST_CHECK_EQUAL(j->vertical_position.line.get(), 0);
67         BOOST_CHECK_EQUAL(j->vertical_position.reference.get(), sub::TOP_OF_SUBTITLE);
68         ++j;
69
70         BOOST_CHECK(j != i->lines.end());
71         BOOST_REQUIRE_EQUAL(j->blocks.size(), 1);
72         b = j->blocks[0];
73         BOOST_CHECK_EQUAL(b.text, "and that's a line break");
74         /* No font is specified by WebVTT, so none should be seen here */
75         BOOST_CHECK(!b.font);
76         BOOST_CHECK(!b.font_size.specified());
77         BOOST_CHECK_EQUAL(b.bold, false);
78         BOOST_CHECK_EQUAL(b.italic, false);
79         BOOST_CHECK_EQUAL(b.underline, false);
80         BOOST_REQUIRE(j->vertical_position.line);
81         BOOST_CHECK_EQUAL(j->vertical_position.line.get(), 1);
82         BOOST_CHECK_EQUAL(j->vertical_position.reference.get(), sub::TOP_OF_SUBTITLE);
83         ++i;
84
85
86         /* Second subtitle */
87
88         BOOST_REQUIRE(i != subs.end());
89         BOOST_CHECK_EQUAL(i->from, sub::Time::from_hms(0, 1, 1, 10));
90         BOOST_CHECK_EQUAL(i->to, sub::Time::from_hms(0, 1, 2, 100));
91
92         BOOST_CHECK_EQUAL(i->lines.size(), 1);
93         sub::Line l = i->lines[0];
94         BOOST_CHECK_EQUAL(l.blocks.size(), 1);
95         BOOST_CHECK_EQUAL(l.vertical_position.line.get(), 0);
96         BOOST_CHECK_EQUAL(l.vertical_position.reference.get(), sub::TOP_OF_SUBTITLE);
97
98         BOOST_REQUIRE_EQUAL(l.blocks.size(), 1U);
99         b = l.blocks[0];
100         BOOST_CHECK_EQUAL(b.text, "This is some stuff.");
101         /* No font is specified by WebVTT, so none should be seen here */
102         BOOST_CHECK(!b.font);
103         BOOST_CHECK(!b.font_size.specified());
104         BOOST_CHECK_EQUAL(b.bold, false);
105         BOOST_CHECK_EQUAL(b.italic, false);
106         BOOST_CHECK_EQUAL(b.underline, false);
107 }
108