Allow LanguageTag to be constructed from a tag string.
[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         /* Language + script */
82
83         {
84                 dcp::LanguageTag t;
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");
89         }
90
91         /* Language + region */
92
93         {
94                 dcp::LanguageTag t;
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");
99         }
100
101         /* Language + variant */
102
103         {
104                 dcp::LanguageTag t;
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");
109         }
110
111         /* Language + 2 variants */
112
113         {
114                 dcp::LanguageTag t;
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");
120         }
121
122         /* Language + extlang */
123
124         {
125                 dcp::LanguageTag t;
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");
130         }
131
132         /* Language + 2 extlangs */
133
134         {
135                 dcp::LanguageTag t;
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");
141         }
142
143         /* Language + script + region */
144
145         {
146                 dcp::LanguageTag t;
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");
152         }
153
154         /* Language + script + region + variant */
155
156         {
157                 dcp::LanguageTag t;
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");
164         }
165
166         /* Langauge + script + region + variant + extlang */
167
168         {
169                 dcp::LanguageTag t;
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");
177         }
178
179 }
180
181
182 BOOST_AUTO_TEST_CASE (language_tag_parse_test)
183 {
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);
189
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");
197 }
198