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");
85 BOOST_CHECK_NO_THROW (t.set_language("dE"));
86 BOOST_CHECK_EQUAL (t.to_string(), "dE");
89 /* Language + script */
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");
99 /* Language + region */
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");
109 /* Language + variant */
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");
119 /* Language + 2 variants */
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");
130 /* Language + extlang */
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");
140 /* Language + 2 extlangs */
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");
151 /* Language + script + region */
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");
162 /* Language + script + region + variant */
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");
174 /* Langauge + script + region + variant + extlang */
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");
190 BOOST_AUTO_TEST_CASE (language_tag_parse_test)
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);
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");