2 Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 #include "exceptions.h"
36 #include "language_tag.h"
37 #include <boost/test/unit_test.hpp>
44 BOOST_AUTO_TEST_CASE (language_tag_create_test)
46 /* Bad subtags raise errors */
51 BOOST_CHECK_THROW (t.to_string(), dcp::LanguageTagError);
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);
60 /* Duplicate subtags raise errors */
65 BOOST_CHECK_NO_THROW (t.add_variant("rozaj"));
66 BOOST_CHECK_THROW (t.add_variant("rozaj"), dcp::LanguageTagError);
68 BOOST_CHECK_NO_THROW (t.add_extlang("ltg"));
69 BOOST_CHECK_THROW (t.add_extlang("ltg"), dcp::LanguageTagError);
76 BOOST_CHECK_NO_THROW (t.set_language("de"));
77 BOOST_CHECK_EQUAL (t.to_string(), "de");
78 BOOST_CHECK_EQUAL (t.description(), "German");
81 /* Language + script */
85 BOOST_CHECK_NO_THROW (t.set_language("zh"));
86 BOOST_CHECK_NO_THROW (t.set_script("Hant"));
87 BOOST_CHECK_EQUAL (t.to_string(), "zh-Hant");
88 BOOST_CHECK_EQUAL (t.description(), "Chinese written using the Han (Traditional variant) script");
91 /* Language + region */
95 BOOST_CHECK_NO_THROW (t.set_language("de"));
96 BOOST_CHECK_NO_THROW (t.set_region("DE"));
97 BOOST_CHECK_EQUAL (t.to_string(), "de-DE");
98 BOOST_CHECK_EQUAL (t.description(), "German for Germany");
101 /* Language + variant */
105 BOOST_CHECK_NO_THROW (t.set_language("sl"));
106 BOOST_CHECK_NO_THROW (t.add_variant("rozaj"));
107 BOOST_CHECK_EQUAL (t.to_string(), "sl-rozaj");
108 BOOST_CHECK_EQUAL (t.description(), "Rezijan dialect of Slovenian");
111 /* Language + 2 variants */
115 BOOST_CHECK_NO_THROW (t.set_language("sl"));
116 BOOST_CHECK_NO_THROW (t.add_variant("biske"));
117 BOOST_CHECK_NO_THROW (t.add_variant("rozaj"));
118 BOOST_CHECK_EQUAL (t.to_string(), "sl-biske-rozaj");
119 BOOST_CHECK_EQUAL (t.description(), "The Bila dialect of Resian dialect of Rezijan dialect of Slovenian");
122 /* Language + extlang */
126 BOOST_CHECK_NO_THROW (t.set_language("sl"));
127 BOOST_CHECK_NO_THROW (t.add_extlang("afb"));
128 BOOST_CHECK_EQUAL (t.to_string(), "sl-afb");
129 BOOST_CHECK_EQUAL (t.description(), "Slovenian, Gulf Arabic");
132 /* Language + 2 extlangs */
136 BOOST_CHECK_NO_THROW (t.set_language("sl"));
137 BOOST_CHECK_NO_THROW (t.add_extlang("afb"));
138 BOOST_CHECK_NO_THROW (t.add_extlang("ltg"));
139 BOOST_CHECK_EQUAL (t.to_string(), "sl-afb-ltg");
140 BOOST_CHECK_EQUAL (t.description(), "Slovenian, Gulf Arabic, Latgalian");
143 /* Language + script + region */
147 BOOST_CHECK_NO_THROW (t.set_language("zh"));
148 BOOST_CHECK_NO_THROW (t.set_script("Hant"));
149 BOOST_CHECK_NO_THROW (t.set_region("DE"));
150 BOOST_CHECK_EQUAL (t.to_string(), "zh-Hant-DE");
151 BOOST_CHECK_EQUAL (t.description(), "Chinese written using the Han (Traditional variant) script for Germany");
154 /* Language + script + region + variant */
158 BOOST_CHECK_NO_THROW (t.set_language("hy"));
159 BOOST_CHECK_NO_THROW (t.set_script("Latn"));
160 BOOST_CHECK_NO_THROW (t.set_region("IT"));
161 BOOST_CHECK_NO_THROW (t.add_variant("arevela"));
162 BOOST_CHECK_EQUAL (t.to_string(), "hy-Latn-IT-arevela");
163 BOOST_CHECK_EQUAL (t.description(), "Eastern Armenian dialect of Armenian written using the Latin script for Italy");
166 /* Langauge + script + region + variant + extlang */
170 BOOST_CHECK_NO_THROW (t.set_language("hy"));
171 BOOST_CHECK_NO_THROW (t.set_script("Latn"));
172 BOOST_CHECK_NO_THROW (t.set_region("IT"));
173 BOOST_CHECK_NO_THROW (t.add_variant("arevela"));
174 BOOST_CHECK_NO_THROW (t.add_extlang("ltg"));
175 BOOST_CHECK_EQUAL (t.to_string(), "hy-Latn-IT-arevela-ltg");
176 BOOST_CHECK_EQUAL (t.description(), "Eastern Armenian dialect of Armenian written using the Latin script for Italy, Latgalian");
182 BOOST_AUTO_TEST_CASE (language_tag_parse_test)
184 BOOST_CHECK_THROW (dcp::LanguageTag(""), dcp::LanguageTagError);
185 BOOST_CHECK_THROW (dcp::LanguageTag("...Aw498012351!"), dcp::LanguageTagError);
186 BOOST_CHECK_THROW (dcp::LanguageTag("fish"), dcp::LanguageTagError);
187 BOOST_CHECK_THROW (dcp::LanguageTag("de-Dogr-fish"), dcp::LanguageTagError);
188 BOOST_CHECK_THROW (dcp::LanguageTag("de-Dogr-DE-aranes-fish"), dcp::LanguageTagError);
190 BOOST_CHECK_EQUAL (dcp::LanguageTag("de").to_string(), "de");
191 BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr").to_string(), "de-Dogr");
192 BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE").to_string(), "de-Dogr-DE");
193 BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE-aranes").to_string(), "de-Dogr-DE-aranes");
194 BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE-aranes-lemosin").to_string(), "de-Dogr-DE-aranes-lemosin");
195 BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE-aranes-lemosin-abv").to_string(), "de-Dogr-DE-aranes-lemosin-abv");
196 BOOST_CHECK_EQUAL (dcp::LanguageTag("de-Dogr-DE-aranes-lemosin-abv-zsm").to_string(), "de-Dogr-DE-aranes-lemosin-abv-zsm");