Fix macOS build warning.
[libdcp.git] / test / language_tag_test.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "exceptions.h"
36 #include "language_tag.h"
37 #include <boost/test/unit_test.hpp>
38
39
40 using std::vector;
41 using std::string;
42
43
44 BOOST_AUTO_TEST_CASE (language_tag_create_test)
45 {
46         /* Bad subtags raise errors */
47
48         {
49                 dcp::LanguageTag t;
50
51                 BOOST_CHECK_THROW (t.to_string(), dcp::LanguageTagError);
52
53                 BOOST_CHECK_THROW (t.set_language("sheila"), dcp::LanguageTagError);
54                 BOOST_CHECK_THROW (t.set_script("frobozz"), dcp::LanguageTagError);
55                 BOOST_CHECK_THROW (t.set_region("ostrabaglous"), dcp::LanguageTagError);
56                 BOOST_CHECK_THROW (dcp::LanguageTag::VariantSubtag("universe"), dcp::LanguageTagError);
57                 BOOST_CHECK_THROW (dcp::LanguageTag::ExtlangSubtag("universe"), dcp::LanguageTagError);
58         }
59
60         /* Duplicate subtags raise errors */
61
62         {
63                 dcp::LanguageTag t;
64
65                 BOOST_CHECK_NO_THROW (t.add_variant("rozaj"));
66                 BOOST_CHECK_THROW (t.add_variant("rozaj"), dcp::LanguageTagError);
67
68                 BOOST_CHECK_NO_THROW (t.add_extlang("ltg"));
69                 BOOST_CHECK_THROW (t.add_extlang("ltg"), dcp::LanguageTagError);
70         }
71
72         /* Language */
73
74         {
75                 dcp::LanguageTag t;
76                 BOOST_CHECK_NO_THROW (t.set_language("de"));
77                 BOOST_CHECK_EQUAL (t.to_string(), "de");
78                 BOOST_CHECK_EQUAL (t.description(), "German");
79         }
80
81         /* Case is ignored */
82
83         {
84                 dcp::LanguageTag t;
85                 BOOST_CHECK_NO_THROW (t.set_language("dE"));
86                 BOOST_CHECK_EQUAL (t.to_string(), "dE");
87         }
88
89         /* Language + script */
90
91         {
92                 dcp::LanguageTag t;
93                 BOOST_CHECK_NO_THROW (t.set_language("zh"));
94                 BOOST_CHECK_NO_THROW (t.set_script("Hant"));
95                 BOOST_CHECK_EQUAL (t.to_string(), "zh-Hant");
96                 BOOST_CHECK_EQUAL (t.description(), "Chinese written using the Han (Traditional variant) script");
97         }
98
99         /* Language + region */
100
101         {
102                 dcp::LanguageTag t;
103                 BOOST_CHECK_NO_THROW (t.set_language("de"));
104                 BOOST_CHECK_NO_THROW (t.set_region("DE"));
105                 BOOST_CHECK_EQUAL (t.to_string(), "de-DE");
106                 BOOST_CHECK_EQUAL (t.description(), "German for Germany");
107         }
108
109         /* Language + variant */
110
111         {
112                 dcp::LanguageTag t;
113                 BOOST_CHECK_NO_THROW (t.set_language("sl"));
114                 BOOST_CHECK_NO_THROW (t.add_variant("rozaj"));
115                 BOOST_CHECK_EQUAL (t.to_string(), "sl-rozaj");
116                 BOOST_CHECK_EQUAL (t.description(), "Rezijan dialect of Slovenian");
117         }
118
119         /* Language + 2 variants */
120
121         {
122                 dcp::LanguageTag t;
123                 BOOST_CHECK_NO_THROW (t.set_language("sl"));
124                 BOOST_CHECK_NO_THROW (t.add_variant("biske"));
125                 BOOST_CHECK_NO_THROW (t.add_variant("rozaj"));
126                 BOOST_CHECK_EQUAL (t.to_string(), "sl-biske-rozaj");
127                 BOOST_CHECK_EQUAL (t.description(), "The Bila dialect of Resian dialect of Rezijan dialect of Slovenian");
128         }
129
130         /* Language + extlang */
131
132         {
133                 dcp::LanguageTag t;
134                 BOOST_CHECK_NO_THROW (t.set_language("sl"));
135                 BOOST_CHECK_NO_THROW (t.add_extlang("afb"));
136                 BOOST_CHECK_EQUAL (t.to_string(), "sl-afb");
137                 BOOST_CHECK_EQUAL (t.description(), "Slovenian, Gulf Arabic");
138         }
139
140         /* Language + 2 extlangs */
141
142         {
143                 dcp::LanguageTag t;
144                 BOOST_CHECK_NO_THROW (t.set_language("sl"));
145                 BOOST_CHECK_NO_THROW (t.add_extlang("afb"));
146                 BOOST_CHECK_NO_THROW (t.add_extlang("ltg"));
147                 BOOST_CHECK_EQUAL (t.to_string(), "sl-afb-ltg");
148                 BOOST_CHECK_EQUAL (t.description(), "Slovenian, Gulf Arabic, Latgalian");
149         }
150
151         /* Language + script + region */
152
153         {
154                 dcp::LanguageTag t;
155                 BOOST_CHECK_NO_THROW (t.set_language("zh"));
156                 BOOST_CHECK_NO_THROW (t.set_script("Hant"));
157                 BOOST_CHECK_NO_THROW (t.set_region("DE"));
158                 BOOST_CHECK_EQUAL (t.to_string(), "zh-Hant-DE");
159                 BOOST_CHECK_EQUAL (t.description(), "Chinese written using the Han (Traditional variant) script for Germany");
160         }
161
162         /* Language + script + region + variant */
163
164         {
165                 dcp::LanguageTag t;
166                 BOOST_CHECK_NO_THROW (t.set_language("hy"));
167                 BOOST_CHECK_NO_THROW (t.set_script("Latn"));
168                 BOOST_CHECK_NO_THROW (t.set_region("IT"));
169                 BOOST_CHECK_NO_THROW (t.add_variant("arevela"));
170                 BOOST_CHECK_EQUAL (t.to_string(), "hy-Latn-IT-arevela");
171                 BOOST_CHECK_EQUAL (t.description(), "Eastern Armenian dialect of Armenian written using the Latin script for Italy");
172         }
173
174         /* Langauge + script + region + variant + extlang */
175
176         {
177                 dcp::LanguageTag t;
178                 BOOST_CHECK_NO_THROW (t.set_language("hy"));
179                 BOOST_CHECK_NO_THROW (t.set_script("Latn"));
180                 BOOST_CHECK_NO_THROW (t.set_region("IT"));
181                 BOOST_CHECK_NO_THROW (t.add_variant("arevela"));
182                 BOOST_CHECK_NO_THROW (t.add_extlang("ltg"));
183                 BOOST_CHECK_EQUAL (t.to_string(), "hy-Latn-IT-arevela-ltg");
184                 BOOST_CHECK_EQUAL (t.description(), "Eastern Armenian dialect of Armenian written using the Latin script for Italy, Latgalian");
185         }
186
187 }
188
189
190 BOOST_AUTO_TEST_CASE (language_tag_parse_test)
191 {
192         BOOST_CHECK_THROW (dcp::LanguageTag(""), dcp::LanguageTagError);
193         BOOST_CHECK_THROW (dcp::LanguageTag("...Aw498012351!"), dcp::LanguageTagError);
194         BOOST_CHECK_THROW (dcp::LanguageTag("fish"), dcp::LanguageTagError);
195         BOOST_CHECK_THROW (dcp::LanguageTag("de-Dogr-fish"), dcp::LanguageTagError);
196         BOOST_CHECK_THROW (dcp::LanguageTag("de-Dogr-DE-aranes-fish"), dcp::LanguageTagError);
197
198         BOOST_CHECK_EQUAL (dcp::LanguageTag("de").to_string(), "de");
199         BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr").to_string(), "de-Dogr");
200         BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE").to_string(), "de-Dogr-DE");
201         BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE-aranes").to_string(), "de-Dogr-DE-aranes");
202         BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE-aranes-lemosin").to_string(), "de-Dogr-DE-aranes-lemosin");
203         BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE-aranes-lemosin-abv").to_string(), "de-Dogr-DE-aranes-lemosin-abv");
204         BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE-aranes-lemosin-abv-zsm").to_string(), "de-Dogr-DE-aranes-lemosin-abv-zsm");
205 }
206