1 # frozen_string_literal: true
5 class TagParserTest < ActiveSupport::TestCase
7 links = TagParser.wiki_link("key", "highway")
8 assert_equal [{ :text => "highway", :url => "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", :type => :"wiki_link.key", :key => "highway" }], links
10 links = TagParser.wiki_link("tag", "highway", "primary")
11 assert_equal [{ :text => "primary", :url => "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", :type => :"wiki_link.tag", :key => "highway", :value => "primary" }], links
13 I18n.with_locale "de" do
14 links = TagParser.wiki_link("key", "highway")
15 assert_equal [{ :text => "highway", :url => "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", :type => :"wiki_link.key", :key => "highway" }], links
17 links = TagParser.wiki_link("tag", "highway", "primary")
18 assert_equal [{ :text => "primary", :url => "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", :type => :"wiki_link.tag", :key => "highway", :value => "primary" }], links
21 I18n.with_locale "tr" do
22 links = TagParser.wiki_link("key", "highway")
23 assert_equal [{ :text => "highway", :url => "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", :type => :"wiki_link.key", :key => "highway" }], links
25 links = TagParser.wiki_link("tag", "highway", "path")
26 assert_equal [{ :text => "path", :url => "https://wiki.openstreetmap.org/wiki/Tag:highway=path?uselang=tr", :type => :"wiki_link.tag", :key => "highway", :value => "path" }], links
30 def test_wikidata_links
31 ### Non-prefixed wikidata-tag (only one value allowed)
34 links = TagParser.wikidata_links("foo", "Test")
38 links = TagParser.wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
41 # No language-prefixes (as wikidata is multilanguage)
42 links = TagParser.wikidata_links("wikidata", "en:Q1")
46 links = TagParser.wikidata_links("wikidata", "1")
49 # No leading zeros allowed
50 links = TagParser.wikidata_links("wikidata", "Q0123")
54 links = TagParser.wikidata_links("wikidata", "Q42")
55 assert_equal 2, links.length
56 assert_equal({ :type => :html, :widget => :wikidata_preview, :qids => ["Q42"], :count => 1 }, links[0])
57 assert_equal({ :text => "Q42", :url => "https://www.wikidata.org/entity/Q42?uselang=en", :type => :wikidata_link, :page => "Q42" }, links[1])
59 # the language of the wikidata-page should match the current locale
60 I18n.with_locale "zh-CN" do
61 links = TagParser.wikidata_links("wikidata", "Q1234")
62 assert_equal 2, links.length
63 assert_equal({ :type => :html, :widget => :wikidata_preview, :qids => ["Q1234"], :count => 1 }, links[0])
64 assert_equal({ :text => "Q1234", :url => "https://www.wikidata.org/entity/Q1234?uselang=zh-CN", :type => :wikidata_link, :page => "Q1234" }, links[1])
67 ### Prefixed wikidata-tags
69 # examples for secondary wikidata keys
70 links = TagParser.wikidata_links("operator:wikidata", "Q24")
71 assert_equal 2, links.length
72 assert_equal({ :type => :html, :widget => :wikidata_preview, :qids => ["Q24"], :count => 1 }, links[0])
73 assert_equal({ :text => "Q24", :url => "https://www.wikidata.org/entity/Q24?uselang=en", :type => :wikidata_link, :page => "Q24" }, links[1])
75 links = TagParser.wikidata_links("buried:wikidata", "Q24")
76 assert_equal 2, links.length
77 assert_equal({ :type => :html, :widget => :wikidata_preview, :qids => ["Q24"], :count => 1 }, links[0])
78 assert_equal({ :text => "Q24", :url => "https://www.wikidata.org/entity/Q24?uselang=en", :type => :wikidata_link, :page => "Q24" }, links[1])
80 links = TagParser.wikidata_links("species:wikidata", "Q26899")
81 assert_equal 2, links.length
82 assert_equal({ :type => :html, :widget => :wikidata_preview, :qids => ["Q26899"], :count => 1 }, links[0])
83 assert_equal({ :text => "Q26899", :url => "https://www.wikidata.org/entity/Q26899?uselang=en", :type => :wikidata_link, :page => "Q26899" }, links[1])
85 links = TagParser.wikidata_links("name:etymology:wikidata", "Q121745508")
86 assert_equal 2, links.length
87 assert_equal({ :type => :html, :widget => :wikidata_preview, :qids => ["Q121745508"], :count => 1 }, links[0])
88 assert_equal({ :text => "Q121745508", :url => "https://www.wikidata.org/entity/Q121745508?uselang=en", :type => :wikidata_link, :page => "Q121745508" }, links[1])
90 # Another one with multiple values and I18n
91 I18n.with_locale "dsb" do
92 links = TagParser.wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
93 assert_equal 4, links.length
94 assert_equal({ :type => :html, :widget => :wikidata_preview, :qids => %w[Q936 Q2013 Q1568346], :count => 3 }, links[0])
95 assert_equal({ :text => "Q936", :url => "https://www.wikidata.org/entity/Q936?uselang=dsb", :type => :wikidata_link, :page => "Q936" }, links[1])
96 assert_equal({ :text => "Q2013", :url => "https://www.wikidata.org/entity/Q2013?uselang=dsb", :type => :wikidata_link, :page => "Q2013" }, links[2])
97 assert_equal({ :text => "Q1568346", :url => "https://www.wikidata.org/entity/Q1568346?uselang=dsb", :type => :wikidata_link, :page => "Q1568346" }, links[3])
100 # and now with whitespaces...
101 links = TagParser.wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
102 assert_equal 5, links.length
103 assert_equal({ :type => :html, :widget => :wikidata_preview, :qids => %w[Q6542248 Q180 Q364 Q4006], :count => 4 }, links[0])
104 assert_equal({ :text => "Q6542248 ", :url => "https://www.wikidata.org/entity/Q6542248?uselang=en", :type => :wikidata_link, :page => "Q6542248" }, links[1])
105 assert_equal({ :text => "\tQ180\n ", :url => "https://www.wikidata.org/entity/Q180?uselang=en", :type => :wikidata_link, :page => "Q180" }, links[2])
106 assert_equal({ :text => "\rQ364\t\n\r ", :url => "https://www.wikidata.org/entity/Q364?uselang=en", :type => :wikidata_link, :page => "Q364" }, links[3])
107 assert_equal({ :text => "\nQ4006", :url => "https://www.wikidata.org/entity/Q4006?uselang=en", :type => :wikidata_link, :page => "Q4006" }, links[4])
109 links = TagParser.wikidata_links("source:species:wikidata", "PlantNet.org AI")
113 def test_wikipedia_links
114 links = TagParser.wikipedia_links("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
117 links = TagParser.wikipedia_links("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
120 links = TagParser.wikipedia_links("wikipedia", "Test")
123 links = TagParser.wikipedia_links("wikipedia", "de:Test")
124 assert_equal 1, links.length
125 assert_equal({ :text => "de:Test", :url => "https://de.wikipedia.org/wiki/Test?uselang=en", :type => :wikipedia_link, :page => "de:Test" }, links[0])
127 links = TagParser.wikipedia_links("wikipedia:fr", "Portsea")
128 assert_equal 1, links.length
129 assert_equal({ :text => "Portsea", :url => "https://fr.wikipedia.org/wiki/Portsea?uselang=en", :type => :wikipedia_link, :page => "Portsea" }, links[0])
131 links = TagParser.wikipedia_links("wikipedia:fr", "de:Test")
132 assert_equal 1, links.length
133 assert_equal({ :text => "de:Test", :url => "https://de.wikipedia.org/wiki/Test?uselang=en", :type => :wikipedia_link, :page => "de:Test" }, links[0])
135 links = TagParser.wikipedia_links("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
136 assert_equal 1, links.length
138 :text => "de:Englischer Garten (München)#Japanisches Teehaus",
139 :url => "https://de.wikipedia.org/wiki/Englischer_Garten_%28M%C3%BCnchen%29?uselang=en#Japanisches_Teehaus",
140 :type => :wikipedia_link,
141 :page => "de:Englischer Garten (München)#Japanisches Teehaus"
144 links = TagParser.wikipedia_links("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
145 assert_equal 1, links.length
147 :text => "de:Alte Brücke (Heidelberg)#Brückenaffe",
148 :url => "https://de.wikipedia.org/wiki/Alte_Br%C3%BCcke_%28Heidelberg%29?uselang=en#Br%C3%BCckenaffe",
149 :type => :wikipedia_link,
150 :page => "de:Alte Brücke (Heidelberg)#Brückenaffe"
153 links = TagParser.wikipedia_links("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
154 assert_equal 1, links.length
156 :text => "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus",
157 :url => "https://de.wikipedia.org/wiki/Liste_der_Baudenkm%C3%A4ler_in_Eichst%C3%A4tt?uselang=en#Br%C3%BCckenstra%C3%9Fe_1%2C_Ehemaliges_Bauernhaus",
158 :type => :wikipedia_link,
159 :page => "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus"
162 links = TagParser.wikipedia_links("wikipedia", "en:Are Years What? (for Marianne Moore)")
163 assert_equal 1, links.length
165 :text => "en:Are Years What? (for Marianne Moore)",
166 :url => "https://en.wikipedia.org/wiki/Are_Years_What%3F_%28for_Marianne_Moore%29?uselang=en",
167 :type => :wikipedia_link,
168 :page => "en:Are Years What? (for Marianne Moore)"
171 I18n.with_locale "pt-BR" do
172 links = TagParser.wikipedia_links("wikipedia", "zh-classical:Test#Section")
173 assert_equal 1, links.length
174 assert_equal({ :text => "zh-classical:Test#Section", :url => "https://zh-classical.wikipedia.org/wiki/Test?uselang=pt-BR#Section", :type => :wikipedia_link, :page => "zh-classical:Test#Section" }, links[0])
177 links = TagParser.wikipedia_links("subject:wikipedia", "en:Catherine McAuley")
178 assert_equal 1, links.length
179 assert_equal({ :text => "en:Catherine McAuley", :url => "https://en.wikipedia.org/wiki/Catherine_McAuley?uselang=en", :type => :wikipedia_link, :page => "en:Catherine McAuley" }, links[0])
181 links = TagParser.wikipedia_links("artist:wikipedia", "en:Pablo Picasso")
182 assert_equal 1, links.length
183 assert_equal({ :text => "en:Pablo Picasso", :url => "https://en.wikipedia.org/wiki/Pablo_Picasso?uselang=en", :type => :wikipedia_link, :page => "en:Pablo Picasso" }, links[0])
185 links = TagParser.wikipedia_links("architect:wikipedia", "en:Frank Lloyd Wright")
186 assert_equal 1, links.length
187 assert_equal({ :text => "en:Frank Lloyd Wright", :url => "https://en.wikipedia.org/wiki/Frank_Lloyd_Wright?uselang=en", :type => :wikipedia_link, :page => "en:Frank Lloyd Wright" }, links[0])
189 links = TagParser.wikipedia_links("buried:wikipedia", "en:Stephen Hawking")
190 assert_equal 1, links.length
191 assert_equal({ :text => "en:Stephen Hawking", :url => "https://en.wikipedia.org/wiki/Stephen_Hawking?uselang=en", :type => :wikipedia_link, :page => "en:Stephen Hawking" }, links[0])
193 links = TagParser.wikipedia_links("foo", "Test")
196 # Multiple values separated by ;
197 links = TagParser.wikipedia_links("wikipedia", "Test;Hello")
200 links = TagParser.wikipedia_links("wikipedia", "de:Berlin;en:London;fr:Paris")
201 assert_equal 3, links.length
202 assert_equal({ :text => "de:Berlin", :url => "https://de.wikipedia.org/wiki/Berlin?uselang=en", :type => :wikipedia_link, :page => "de:Berlin" }, links[0])
203 assert_equal({ :text => "en:London", :url => "https://en.wikipedia.org/wiki/London?uselang=en", :type => :wikipedia_link, :page => "en:London" }, links[1])
204 assert_equal({ :text => "fr:Paris", :url => "https://fr.wikipedia.org/wiki/Paris?uselang=en", :type => :wikipedia_link, :page => "fr:Paris" }, links[2])
206 links = TagParser.wikipedia_links("fixme:wikipedia", "The wikipedia tag links to a list article, not an article about this specific feature.")
210 def test_wikimedia_commons_link
211 links = TagParser.wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
214 links = TagParser.wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
217 links = TagParser.wikimedia_commons_link("wikimedia_commons", "Test.jpg")
220 links = TagParser.wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
221 assert_equal 1, links.length
222 assert_equal({ :text => "File:Test.jpg", :url => "https://commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", :type => :wikimedia_commons_link, :page => "File:Test.jpg" }, links[0])
224 links = TagParser.wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
225 assert_equal 1, links.length
226 assert_equal({ :text => "Category:Test_Category", :url => "https://commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", :type => :wikimedia_commons_link, :page => "Category:Test_Category" }, links[0])
228 links = TagParser.wikimedia_commons_link("wikimedia_commons", "Category:What If? (Bonn)")
229 assert_equal 1, links.length
230 assert_equal({ :text => "Category:What If? (Bonn)", :url => "https://commons.wikimedia.org/wiki/Category:What%20If%3F%20%28Bonn%29?uselang=en", :type => :wikimedia_commons_link, :page => "Category:What If? (Bonn)" }, links[0])
232 links = TagParser.wikimedia_commons_link("wikimedia_commons", "File:Corsica-vizzavona-abri-southwell.jpg#mediaviewer/File:Corsica-vizzavona-abri-southwell.jpg")
233 assert_equal 1, links.length
235 :text => "File:Corsica-vizzavona-abri-southwell.jpg#mediaviewer/File:Corsica-vizzavona-abri-southwell.jpg",
236 :url => "https://commons.wikimedia.org/wiki/File:Corsica-vizzavona-abri-southwell.jpg?uselang=en",
237 :type => :wikimedia_commons_link,
238 :page => "File:Corsica-vizzavona-abri-southwell.jpg#mediaviewer/File:Corsica-vizzavona-abri-southwell.jpg"
241 # Reject namespaces other than File and Category
242 links = TagParser.wikimedia_commons_link("wikimedia_commons", "Commons:Featured pictures")
245 # Secondary Wikimedia Commons links
246 links = TagParser.wikimedia_commons_link("artist:wikimedia_commons", "File:Unknown Artist Portrait.jpg")
247 assert_equal 1, links.length
248 assert_equal({ :text => "File:Unknown Artist Portrait.jpg", :url => "https://commons.wikimedia.org/wiki/File:Unknown%20Artist%20Portrait.jpg?uselang=en", :type => :wikimedia_commons_link, :page => "File:Unknown Artist Portrait.jpg" }, links[0])
250 I18n.with_locale "pt-BR" do
251 links = TagParser.wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
252 assert_equal 1, links.length
253 assert_equal({ :text => "File:Test.jpg", :url => "https://commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", :type => :wikimedia_commons_link, :page => "File:Test.jpg" }, links[0])
256 links = TagParser.wikimedia_commons_link("foo", "Test")
261 links = TagParser.email_link("foo", "Test")
264 links = TagParser.email_link("email", "123")
267 links = TagParser.email_link("email", "Abc.example.com")
270 links = TagParser.email_link("email", "a@b@c.com")
273 links = TagParser.email_link("email", "just\"not\"right@example.com")
276 links = TagParser.email_link("email", "123 abcdefg@space.com")
279 links = TagParser.email_link("email", "test@ abc")
282 links = TagParser.email_link("email", "using;semicolon@test.com")
285 links = TagParser.email_link("email", "x@example.com")
286 assert_equal 1, links.length
287 assert_equal({ :text => "x@example.com", :url => "x@example.com", :type => :email_link, :email => "x@example.com" }, links[0])
289 links = TagParser.email_link("email", "other.email-with-hyphen@example.com")
290 assert_equal 1, links.length
291 assert_equal({ :text => "other.email-with-hyphen@example.com", :url => "other.email-with-hyphen@example.com", :type => :email_link, :email => "other.email-with-hyphen@example.com" }, links[0])
293 links = TagParser.email_link("email", "user.name+tag+sorting@example.com")
294 assert_equal 1, links.length
295 assert_equal({ :text => "user.name+tag+sorting@example.com", :url => "user.name+tag+sorting@example.com", :type => :email_link, :email => "user.name+tag+sorting@example.com" }, links[0])
297 links = TagParser.email_link("email", "dash-in@both-parts.com")
298 assert_equal 1, links.length
299 assert_equal({ :text => "dash-in@both-parts.com", :url => "dash-in@both-parts.com", :type => :email_link, :email => "dash-in@both-parts.com" }, links[0])
301 links = TagParser.email_link("email", "example@s.example")
302 assert_equal 1, links.length
303 assert_equal({ :text => "example@s.example", :url => "example@s.example", :type => :email_link, :email => "example@s.example" }, links[0])
305 # Strips whitespace at ends
306 links = TagParser.email_link("email", " test@email.com ")
307 assert_equal 1, links.length
308 assert_equal({ :text => "test@email.com", :url => "test@email.com", :type => :email_link, :email => "test@email.com" }, links[0])
310 links = TagParser.email_link("contact:email", "example@example.com")
311 assert_equal 1, links.length
312 assert_equal({ :text => "example@example.com", :url => "example@example.com", :type => :email_link, :email => "example@example.com" }, links[0])
314 links = TagParser.email_link("maxweight:conditional", "none@agricultural")
318 def test_telephone_links
319 links = TagParser.telephone_links("foo", "Test")
322 links = TagParser.telephone_links("phone", "+123")
325 links = TagParser.telephone_links("phone", "123")
328 links = TagParser.telephone_links("phone", "123 abcdefg")
331 links = TagParser.telephone_links("phone", "+1234567890 abc")
334 # If multiple numbers are listed, all must be valid
335 links = TagParser.telephone_links("phone", "+1234567890; +223")
338 links = TagParser.telephone_links("phone", "1234567890")
341 links = TagParser.telephone_links("phone", "+1234567890")
342 assert_equal 1, links.length
343 assert_equal({ :text => "+1234567890", :url => "tel:+1234567890", :type => :telephone_link, :phone_number => "+1234567890" }, links[0])
345 links = TagParser.telephone_links("phone", "+1234-567-890")
346 assert_equal 1, links.length
347 assert_equal({ :text => "+1234-567-890", :url => "tel:+1234-567-890", :type => :telephone_link, :phone_number => "+1234-567-890" }, links[0])
349 links = TagParser.telephone_links("phone", "+1234/567/890")
350 assert_equal 1, links.length
351 assert_equal({ :text => "+1234/567/890", :url => "tel:+1234/567/890", :type => :telephone_link, :phone_number => "+1234/567/890" }, links[0])
353 links = TagParser.telephone_links("phone", "+1234.567.890")
354 assert_equal 1, links.length
355 assert_equal({ :text => "+1234.567.890", :url => "tel:+1234.567.890", :type => :telephone_link, :phone_number => "+1234.567.890" }, links[0])
357 links = TagParser.telephone_links("phone", " +1234 567-890 ")
358 assert_equal 1, links.length
359 assert_equal({ :text => "+1234 567-890", :url => "tel:+1234567-890", :type => :telephone_link, :phone_number => "+1234 567-890" }, links[0])
361 links = TagParser.telephone_links("phone", "+1 234-567-890")
362 assert_equal 1, links.length
363 assert_equal({ :text => "+1 234-567-890", :url => "tel:+1234-567-890", :type => :telephone_link, :phone_number => "+1 234-567-890" }, links[0])
365 links = TagParser.telephone_links("phone", "+1 (234) 567-890")
366 assert_equal 1, links.length
367 assert_equal({ :text => "+1 (234) 567-890", :url => "tel:+1(234)567-890", :type => :telephone_link, :phone_number => "+1 (234) 567-890" }, links[0])
369 # Multiple valid phone numbers separated by ;
370 links = TagParser.telephone_links("phone", "+1234567890; +22334455667788")
371 assert_equal 2, links.length
372 assert_equal({ :text => "+1234567890", :url => "tel:+1234567890", :type => :telephone_link, :phone_number => "+1234567890" }, links[0])
373 assert_equal({ :text => "+22334455667788", :url => "tel:+22334455667788", :type => :telephone_link, :phone_number => "+22334455667788" }, links[1])
375 links = TagParser.telephone_links("phone", "+1 (234) 567-890 ; +22(33)4455.66.7788 ")
376 assert_equal 2, links.length
377 assert_equal({ :text => "+1 (234) 567-890", :url => "tel:+1(234)567-890", :type => :telephone_link, :phone_number => "+1 (234) 567-890" }, links[0])
378 assert_equal({ :text => "+22(33)4455.66.7788", :url => "tel:+22(33)4455.66.7788", :type => :telephone_link, :phone_number => "+22(33)4455.66.7788" }, links[1])
381 def test_colour_preview
382 # basic positive tests
383 links = TagParser.colour_preview("colour", "red")
384 assert_equal 2, links.length
385 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "red" }, links[0])
386 assert_equal({ :text => "red" }, links[1])
388 links = TagParser.colour_preview("colour", "Red")
389 assert_equal 2, links.length
390 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "Red" }, links[0])
391 assert_equal({ :text => "Red" }, links[1])
393 links = TagParser.colour_preview("colour", "darkRed")
394 assert_equal 2, links.length
395 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "darkRed" }, links[0])
396 assert_equal({ :text => "darkRed" }, links[1])
398 links = TagParser.colour_preview("colour", "#f00")
399 assert_equal 2, links.length
400 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "#f00" }, links[0])
401 assert_equal({ :text => "#f00" }, links[1])
403 links = TagParser.colour_preview("colour", "#fF0000")
404 assert_equal 2, links.length
405 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "#fF0000" }, links[0])
406 assert_equal({ :text => "#fF0000" }, links[1])
408 # other tag variants:
409 links = TagParser.colour_preview("building:colour", "#f00")
410 assert_equal 2, links.length
411 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "#f00" }, links[0])
412 assert_equal({ :text => "#f00" }, links[1])
414 links = TagParser.colour_preview("ref:colour", "#f00")
415 assert_equal 2, links.length
416 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "#f00" }, links[0])
417 assert_equal({ :text => "#f00" }, links[1])
419 links = TagParser.colour_preview("int_ref:colour", "green")
420 assert_equal 2, links.length
421 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "green" }, links[0])
422 assert_equal({ :text => "green" }, links[1])
424 links = TagParser.colour_preview("roof:colour", "#f00")
425 assert_equal 2, links.length
426 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "#f00" }, links[0])
427 assert_equal({ :text => "#f00" }, links[1])
429 links = TagParser.colour_preview("seamark:beacon_lateral:colour", "#f00")
430 assert_equal 2, links.length
431 assert_equal({ :type => :html, :widget => :colour_preview, :colour_value => "#f00" }, links[0])
432 assert_equal({ :text => "#f00" }, links[1])
435 links = TagParser.colour_preview("colour", "")
438 links = TagParser.colour_preview("colour", " ")
441 links = TagParser.colour_preview("colour", nil)
444 # ignore US spelling variant
445 links = TagParser.colour_preview("color", "red")
448 # irrelevant tag names
449 links = TagParser.colour_preview("building", "red")
452 links = TagParser.colour_preview("ref:colour_no", "red")
455 links = TagParser.colour_preview("ref:colour-bg", "red")
458 links = TagParser.colour_preview("int_ref", "red")
462 links = TagParser.colour_preview("colour", "#")
465 links = TagParser.colour_preview("colour", "#ff")
468 links = TagParser.colour_preview("colour", "#ffff")
471 links = TagParser.colour_preview("colour", "#fffffff")
474 links = TagParser.colour_preview("colour", "#ggg")
477 links = TagParser.colour_preview("colour", "#ff 00 00")
480 # invalid w3c color names:
481 links = TagParser.colour_preview("colour", "r")
484 links = TagParser.colour_preview("colour", "ffffff")
487 links = TagParser.colour_preview("colour", "f00")
490 links = TagParser.colour_preview("colour", "xxxred")
493 links = TagParser.colour_preview("colour", "dark red")
496 links = TagParser.colour_preview("colour", "dark_red")
499 links = TagParser.colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")
504 assert_nil TagParser.basic_link("website", "https://example.com/page")[:url]
506 assert_nil TagParser.basic_link("nonexistent_key", "SomeValue")[:url]
508 url = TagParser.basic_link("wikidata", "Q936")[:url]
509 assert_equal "https://www.wikidata.org/entity/Q936", url
511 url = TagParser.basic_link("hashtags", "#maproulette")[:url]
512 assert_equal "https://resultmaps.neis-one.org/osm-changesets?comment=maproulette", url
515 def test_build_tag2link_dict_rejects_deprecated_and_third_party
517 { "key" => "Key:example", "url" => "http://example.com/$1", "rank" => "deprecated", "source" => "osmwiki:P8" },
518 { "key" => "Key:example2", "url" => "http://example2.com/$1", "rank" => "preferred", "source" => "wikidata:P3303" },
519 { "key" => "Key:example3", "url" => "http://example3.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" }
521 dict = TagParser.build_tag2link_dict(data)
522 assert_not_includes dict, "example"
523 assert_not_includes dict, "example2"
524 assert_includes dict, "example3"
527 def test_build_tag2link_dict_chooses_single_preferred_item
529 { "key" => "Key:example", "url" => "http://example2.com/$1", "rank" => "normal", "source" => "osmwiki:P8" },
530 { "key" => "Key:example", "url" => "http://example.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" }
532 dict = TagParser.build_tag2link_dict(data)
533 assert_equal "http://example.com/$1", dict["example"]
536 { "key" => "Key:example", "url" => "http://example2.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" },
537 { "key" => "Key:example", "url" => "http://example.com/$1", "rank" => "preferred", "source" => "wikidata:P1630" },
538 { "key" => "Key:example", "url" => "http://example3.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" }
540 dict = TagParser.build_tag2link_dict(data)
541 assert_equal "http://example.com/$1", dict["example"]
544 { "key" => "Key:example", "url" => "http://example2.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" }
546 dict = TagParser.build_tag2link_dict(data)
547 assert_equal "http://example2.com/$1", dict["example"]
550 def test_build_tag2link_dict_deduplicates_urls
552 { "key" => "Key:example", "url" => "http://example.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" },
553 { "key" => "Key:example", "url" => "http://example.com/$1", "rank" => "normal", "source" => "wikidata:P1630" }
555 dict = TagParser.build_tag2link_dict(data)
556 assert_equal "http://example.com/$1", dict["example"]
559 def test_build_tag2link_dict_rejects_multiple_equally_preferred_items
561 { "key" => "Key:example", "url" => "http://example1.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" },
562 { "key" => "Key:example", "url" => "http://example2.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" }
564 dict = TagParser.build_tag2link_dict(data)
565 assert_not_includes dict, "example"
568 { "key" => "Key:example", "url" => "http://example1.com/$1", "rank" => "normal", "source" => "osmwiki:P8" },
569 { "key" => "Key:example", "url" => "http://example2.com/$1", "rank" => "normal", "source" => "osmwiki:P8" }
571 dict = TagParser.build_tag2link_dict(data)
572 assert_not_includes dict, "example"
575 def test_build_tag2link_dict_chooses_osmwiki_when_both_have_single_preferred
577 { "key" => "Key:example", "url" => "http://example1.com/$1", "rank" => "preferred", "source" => "osmwiki:P8" },
578 { "key" => "Key:example", "url" => "http://example2.com/$1", "rank" => "preferred", "source" => "wikidata:P1630" }
580 dict = TagParser.build_tag2link_dict(data)
581 assert_equal "http://example1.com/$1", dict["example"]
584 { "key" => "Key:example", "url" => "http://example1.com/$1", "rank" => "normal", "source" => "osmwiki:P8" },
585 { "key" => "Key:example", "url" => "http://example2.com/$1", "rank" => "normal", "source" => "wikidata:P1630" }
587 dict = TagParser.build_tag2link_dict(data)
588 assert_equal "http://example1.com/$1", dict["example"]
591 def test_build_tag2link_dict_multiple_sources_more_than_two
593 { "key" => "Key:example", "url" => "http://example1.com/$1", "rank" => "normal", "source" => "osmwiki:P8" },
594 { "key" => "Key:example", "url" => "http://example2.com/$1", "rank" => "normal", "source" => "wikidata:P1630" },
595 { "key" => "Key:example", "url" => "http://example3.com/$1", "rank" => "normal", "source" => "other:source" }
597 dict = TagParser.build_tag2link_dict(data)
598 # Should not happen with current tag2link schema, but ensure we handle it gracefully
599 assert_not_includes dict, "example"