stringex 1.5.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +15 -0
  2. data/Gemfile +16 -0
  3. data/Gemfile.lock +74 -0
  4. data/README.rdoc +22 -1
  5. data/Rakefile +46 -223
  6. data/VERSION +1 -0
  7. data/init.rb +1 -0
  8. data/lib/stringex.rb +11 -3
  9. data/lib/stringex/acts_as_url.rb +49 -97
  10. data/lib/stringex/acts_as_url/adapter.rb +26 -0
  11. data/lib/stringex/acts_as_url/adapter/active_record.rb +23 -0
  12. data/lib/stringex/acts_as_url/adapter/base.rb +188 -0
  13. data/lib/stringex/acts_as_url/adapter/data_mapper.rb +67 -0
  14. data/lib/stringex/acts_as_url/adapter/mongoid.rb +36 -0
  15. data/lib/stringex/configuration.rb +4 -0
  16. data/lib/stringex/configuration/acts_as_url.rb +44 -0
  17. data/lib/stringex/configuration/base.rb +58 -0
  18. data/lib/stringex/configuration/configurator.rb +25 -0
  19. data/lib/stringex/configuration/string_extensions.rb +19 -0
  20. data/lib/stringex/localization.rb +98 -0
  21. data/lib/stringex/localization/backend/i18n.rb +53 -0
  22. data/lib/stringex/localization/backend/internal.rb +51 -0
  23. data/lib/stringex/localization/conversion_expressions.rb +148 -0
  24. data/lib/stringex/localization/converter.rb +121 -0
  25. data/lib/stringex/localization/default_conversions.rb +88 -0
  26. data/lib/stringex/rails/railtie.rb +10 -0
  27. data/lib/stringex/string_extensions.rb +153 -208
  28. data/lib/stringex/unidecoder.rb +6 -101
  29. data/lib/stringex/unidecoder_data/x00.yml +1 -1
  30. data/lib/stringex/unidecoder_data/x02.yml +5 -5
  31. data/lib/stringex/unidecoder_data/x05.yml +1 -1
  32. data/lib/stringex/unidecoder_data/x06.yml +1 -1
  33. data/lib/stringex/unidecoder_data/x07.yml +3 -3
  34. data/lib/stringex/unidecoder_data/x09.yml +1 -1
  35. data/lib/stringex/unidecoder_data/x0e.yml +2 -2
  36. data/lib/stringex/unidecoder_data/x1f.yml +2 -2
  37. data/lib/stringex/unidecoder_data/x20.yml +1 -1
  38. data/lib/stringex/unidecoder_data/xfb.yml +1 -1
  39. data/lib/stringex/unidecoder_data/xff.yml +1 -1
  40. data/lib/stringex/version.rb +8 -0
  41. data/locales/da.yml +73 -0
  42. data/locales/en.yml +66 -0
  43. data/stringex.gemspec +77 -18
  44. data/test/acts_as_url/adapter/active_record.rb +72 -0
  45. data/test/acts_as_url/adapter/data_mapper.rb +82 -0
  46. data/test/acts_as_url/adapter/mongoid.rb +73 -0
  47. data/test/acts_as_url_configuration_test.rb +51 -0
  48. data/test/acts_as_url_integration_test.rb +271 -0
  49. data/test/localization/da_test.rb +117 -0
  50. data/test/localization/default_test.rb +113 -0
  51. data/test/localization/en_test.rb +117 -0
  52. data/test/localization_test.rb +123 -0
  53. data/test/redcloth_to_html_test.rb +37 -0
  54. data/test/string_extensions_test.rb +59 -91
  55. data/test/test_helper.rb +2 -0
  56. data/test/unicode_point_suite/basic_greek_test.rb +113 -0
  57. data/test/unicode_point_suite/basic_latin_test.rb +142 -0
  58. data/test/unicode_point_suite/codepoint_test_helper.rb +32 -0
  59. data/test/unidecoder/bad_localization.yml +1 -0
  60. data/test/unidecoder/localization.yml +4 -0
  61. data/test/unidecoder_test.rb +3 -5
  62. metadata +145 -37
  63. data/test/acts_as_url_test.rb +0 -272
@@ -0,0 +1,117 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test_helper'
4
+ require 'i18n'
5
+ require 'stringex'
6
+
7
+ class DanishYAMLLocalizationTest < Test::Unit::TestCase
8
+ def setup
9
+ Stringex::Localization.reset!
10
+ Stringex::Localization.backend = :i18n
11
+ Stringex::Localization.backend.load_translations :da
12
+ Stringex::Localization.locale = :da
13
+ end
14
+
15
+ {
16
+ "foo & bar" => "foo og bar",
17
+ "AT&T" => "AT og T",
18
+ "99° is normal" => "99 grader is normal",
19
+ "4 ÷ 2 is 2" => "4 divideret med 2 is 2",
20
+ "webcrawler.com" => "webcrawler punktum com",
21
+ "Well..." => "Well prik prik prik",
22
+ "x=1" => "x lig med 1",
23
+ "a #2 pencil" => "a nummer 2 pencil",
24
+ "100%" => "100 procent",
25
+ "cost+tax" => "cost plus tax",
26
+ "batman/robin fan fiction" => "batman skråstreg robin fan fiction",
27
+ "dial *69" => "dial stjerne 69",
28
+ # " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
29
+ }.each do |original, converted|
30
+ define_method "test_character_conversion: '#{original}'" do
31
+ assert_equal converted, original.convert_miscellaneous_characters
32
+ end
33
+ end
34
+
35
+ {
36
+ "$100" => "100 dollars",
37
+ "$19.99" => "19 dollars 99 cents",
38
+ "£100" => "100 pund",
39
+ "£19.99" => "19 pund 99 pence",
40
+ "€100" => "100 euro",
41
+ "€19.99" => "19 euro 99 cent",
42
+ "¥1000" => "1000 yen"
43
+ }.each do |original, converted|
44
+ define_method "test_currency_conversion: '#{original}'" do
45
+ assert_equal converted, original.convert_miscellaneous_characters
46
+ end
47
+ end
48
+
49
+ {
50
+ "Tea &amp; Sympathy" => "Tea and Sympathy",
51
+ "10&cent;" => "10 cents",
52
+ "&copy;2000" => "(c)2000",
53
+ "98&deg; is fine" => "98 grader is fine",
54
+ "10&divide;5" => "10 divideret med 5",
55
+ "&quot;quoted&quot;" => '"quoted"',
56
+ "to be continued&hellip;" => "to be continued...",
57
+ "2000&ndash;2004" => "2000-2004",
58
+ "I wish&mdash;oh, never mind" => "I wish--oh, never mind",
59
+ "&frac12; ounce of gold" => "halv ounce of gold",
60
+ "1 and &frac14; ounces of silver" => "1 and en fjerdedel ounces of silver",
61
+ "9 and &frac34; ounces of platinum" => "9 and tre fjerdedele ounces of platinum",
62
+ "3&gt;2" => "3>2",
63
+ "2&lt;3" => "2<3",
64
+ "two&nbsp;words" => "two words",
65
+ "&pound;100" => "pund 100",
66
+ "Walmart&reg;" => "Walmart(r)",
67
+ "&apos;single quoted&apos;" => "'single quoted'",
68
+ "2&times;4" => "2x4",
69
+ "Programming&trade;" => "Programming(tm)",
70
+ "&yen;20000" => "yen 20000",
71
+ " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
72
+ }.each do |original, converted|
73
+ define_method "test_html_entity_conversion: '#{original}'" do
74
+ assert_equal converted, original.convert_miscellaneous_html_entities
75
+ end
76
+ end
77
+
78
+ {
79
+ "&frac12;" => "halv",
80
+ "½" => "halv",
81
+ "&#189;" => "halv",
82
+ "⅓" => "en tredjedel",
83
+ "&#8531;" => "en tredjedel",
84
+ "⅔" => "to tredjedele",
85
+ "&#8532;" => "to tredjedele",
86
+ "&frac14;" => "en fjerdedel",
87
+ "¼" => "en fjerdedel",
88
+ "&#188;" => "en fjerdedel",
89
+ "&frac34;" => "tre fjerdedele",
90
+ "¾" => "tre fjerdedele",
91
+ "&#190;" => "tre fjerdedele",
92
+ "⅕" => "en femtedel",
93
+ "&#8533;" => "en femtedel",
94
+ "⅖" => "to femtedele",
95
+ "&#8534;" => "to femtedele",
96
+ "⅗" => "tre femtedele",
97
+ "&#8535;" => "tre femtedele",
98
+ "⅘" => "fire femtedele",
99
+ "&#8536;" => "fire femtedele",
100
+ "⅙" => "en sjettedel",
101
+ "&#8537;" => "en sjettedel",
102
+ "⅚" => "fem sjettedele",
103
+ "&#8538;" => "fem sjettedele",
104
+ "⅛" => "en ottendedel",
105
+ "&#8539;" => "en ottendedel",
106
+ "⅜" => "tre ottendedele",
107
+ "&#8540;" => "tre ottendedele",
108
+ "⅝" => "fem ottendedele",
109
+ "&#8541;" => "fem ottendedele",
110
+ "⅞" => "syv ottendedele",
111
+ "&#8542;" => "syv ottendedele"
112
+ }.each do |original, converted|
113
+ define_method "test_vulgar_fractions_conversion: #{original}" do
114
+ assert_equal converted, original.convert_vulgar_fractions
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,113 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test_helper'
4
+ require 'stringex'
5
+
6
+ class DefaultLocalizationTest < Test::Unit::TestCase
7
+ def setup
8
+ Stringex::Localization.reset!
9
+ Stringex::Localization.backend = :internal
10
+ end
11
+ {
12
+ "foo & bar" => "foo and bar",
13
+ "AT&T" => "AT and T",
14
+ "99° is normal" => "99 degrees is normal",
15
+ "4 ÷ 2 is 2" => "4 divided by 2 is 2",
16
+ "webcrawler.com" => "webcrawler dot com",
17
+ "Well..." => "Well dot dot dot",
18
+ "x=1" => "x equals 1",
19
+ "a #2 pencil" => "a number 2 pencil",
20
+ "100%" => "100 percent",
21
+ "cost+tax" => "cost plus tax",
22
+ "batman/robin fan fiction" => "batman slash robin fan fiction",
23
+ "dial *69" => "dial star 69",
24
+ " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
25
+ }.each do |original, converted|
26
+ define_method "test_character_conversion: '#{original}'" do
27
+ assert_equal converted, original.convert_miscellaneous_characters
28
+ end
29
+ end
30
+
31
+ {
32
+ "$100" => "100 dollars",
33
+ "$19.99" => "19 dollars 99 cents",
34
+ "£100" => "100 pounds",
35
+ "£19.99" => "19 pounds 99 pence",
36
+ "€100" => "100 euros",
37
+ "€19.99" => "19 euros 99 cents",
38
+ "¥1000" => "1000 yen"
39
+ }.each do |original, converted|
40
+ define_method "test_currency_conversion: '#{original}'" do
41
+ assert_equal converted, original.convert_miscellaneous_characters
42
+ end
43
+ end
44
+
45
+ {
46
+ "Tea &amp; Sympathy" => "Tea and Sympathy",
47
+ "10&cent;" => "10 cents",
48
+ "&copy;2000" => "(c)2000",
49
+ "98&deg; is fine" => "98 degrees is fine",
50
+ "10&divide;5" => "10 divided by 5",
51
+ "&quot;quoted&quot;" => '"quoted"',
52
+ "to be continued&hellip;" => "to be continued...",
53
+ "2000&ndash;2004" => "2000-2004",
54
+ "I wish&mdash;oh, never mind" => "I wish--oh, never mind",
55
+ "&frac12; ounce of gold" => "half ounce of gold",
56
+ "1 and &frac14; ounces of silver" => "1 and one fourth ounces of silver",
57
+ "9 and &frac34; ounces of platinum" => "9 and three fourths ounces of platinum",
58
+ "3&gt;2" => "3>2",
59
+ "2&lt;3" => "2<3",
60
+ "two&nbsp;words" => "two words",
61
+ "&pound;100" => "pounds 100",
62
+ "Walmart&reg;" => "Walmart(r)",
63
+ "&apos;single quoted&apos;" => "'single quoted'",
64
+ "2&times;4" => "2x4",
65
+ "Programming&trade;" => "Programming(tm)",
66
+ "&yen;20000" => "yen 20000",
67
+ " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
68
+ }.each do |original, converted|
69
+ define_method "test_html_entity_conversion: '#{original}'" do
70
+ assert_equal converted, original.convert_miscellaneous_html_entities
71
+ end
72
+ end
73
+
74
+ {
75
+ "&frac12;" => "half",
76
+ "½" => "half",
77
+ "&#189;" => "half",
78
+ "⅓" => "one third",
79
+ "&#8531;" => "one third",
80
+ "⅔" => "two thirds",
81
+ "&#8532;" => "two thirds",
82
+ "&frac14;" => "one fourth",
83
+ "¼" => "one fourth",
84
+ "&#188;" => "one fourth",
85
+ "&frac34;" => "three fourths",
86
+ "¾" => "three fourths",
87
+ "&#190;" => "three fourths",
88
+ "⅕" => "one fifth",
89
+ "&#8533;" => "one fifth",
90
+ "⅖" => "two fifths",
91
+ "&#8534;" => "two fifths",
92
+ "⅗" => "three fifths",
93
+ "&#8535;" => "three fifths",
94
+ "⅘" => "four fifths",
95
+ "&#8536;" => "four fifths",
96
+ "⅙" => "one sixth",
97
+ "&#8537;" => "one sixth",
98
+ "⅚" => "five sixths",
99
+ "&#8538;" => "five sixths",
100
+ "⅛" => "one eighth",
101
+ "&#8539;" => "one eighth",
102
+ "⅜" => "three eighths",
103
+ "&#8540;" => "three eighths",
104
+ "⅝" => "five eighths",
105
+ "&#8541;" => "five eighths",
106
+ "⅞" => "seven eighths",
107
+ "&#8542;" => "seven eighths"
108
+ }.each do |original, converted|
109
+ define_method "test_vulgar_fractions_conversion: #{original}" do
110
+ assert_equal converted, original.convert_vulgar_fractions
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,117 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test_helper'
4
+ require 'i18n'
5
+ require 'stringex'
6
+
7
+ class EnglishYAMLLocalizationTest < Test::Unit::TestCase
8
+ def setup
9
+ Stringex::Localization.reset!
10
+ Stringex::Localization.backend = :i18n
11
+ Stringex::Localization.backend.load_translations :en
12
+ Stringex::Localization.locale = :en
13
+ end
14
+
15
+ {
16
+ "foo & bar" => "foo and bar",
17
+ "AT&T" => "AT and T",
18
+ "99° is normal" => "99 degrees is normal",
19
+ "4 ÷ 2 is 2" => "4 divided by 2 is 2",
20
+ "webcrawler.com" => "webcrawler dot com",
21
+ "Well..." => "Well dot dot dot",
22
+ "x=1" => "x equals 1",
23
+ "a #2 pencil" => "a number 2 pencil",
24
+ "100%" => "100 percent",
25
+ "cost+tax" => "cost plus tax",
26
+ "batman/robin fan fiction" => "batman slash robin fan fiction",
27
+ "dial *69" => "dial star 69",
28
+ " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
29
+ }.each do |original, converted|
30
+ define_method "test_character_conversion: '#{original}'" do
31
+ assert_equal converted, original.convert_miscellaneous_characters
32
+ end
33
+ end
34
+
35
+ {
36
+ "$100" => "100 dollars",
37
+ "$19.99" => "19 dollars 99 cents",
38
+ "£100" => "100 pounds",
39
+ "£19.99" => "19 pounds 99 pence",
40
+ "€100" => "100 euros",
41
+ "€19.99" => "19 euros 99 cents",
42
+ "¥1000" => "1000 yen"
43
+ }.each do |original, converted|
44
+ define_method "test_currency_conversion: '#{original}'" do
45
+ assert_equal converted, original.convert_miscellaneous_characters
46
+ end
47
+ end
48
+
49
+ {
50
+ "Tea &amp; Sympathy" => "Tea and Sympathy",
51
+ "10&cent;" => "10 cents",
52
+ "&copy;2000" => "(c)2000",
53
+ "98&deg; is fine" => "98 degrees is fine",
54
+ "10&divide;5" => "10 divided by 5",
55
+ "&quot;quoted&quot;" => '"quoted"',
56
+ "to be continued&hellip;" => "to be continued...",
57
+ "2000&ndash;2004" => "2000-2004",
58
+ "I wish&mdash;oh, never mind" => "I wish--oh, never mind",
59
+ "&frac12; ounce of gold" => "half ounce of gold",
60
+ "1 and &frac14; ounces of silver" => "1 and one fourth ounces of silver",
61
+ "9 and &frac34; ounces of platinum" => "9 and three fourths ounces of platinum",
62
+ "3&gt;2" => "3>2",
63
+ "2&lt;3" => "2<3",
64
+ "two&nbsp;words" => "two words",
65
+ "&pound;100" => "pounds 100",
66
+ "Walmart&reg;" => "Walmart(r)",
67
+ "&apos;single quoted&apos;" => "'single quoted'",
68
+ "2&times;4" => "2x4",
69
+ "Programming&trade;" => "Programming(tm)",
70
+ "&yen;20000" => "yen 20000",
71
+ " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
72
+ }.each do |original, converted|
73
+ define_method "test_html_entity_conversion: '#{original}'" do
74
+ assert_equal converted, original.convert_miscellaneous_html_entities
75
+ end
76
+ end
77
+
78
+ {
79
+ "&frac12;" => "half",
80
+ "½" => "half",
81
+ "&#189;" => "half",
82
+ "⅓" => "one third",
83
+ "&#8531;" => "one third",
84
+ "⅔" => "two thirds",
85
+ "&#8532;" => "two thirds",
86
+ "&frac14;" => "one fourth",
87
+ "¼" => "one fourth",
88
+ "&#188;" => "one fourth",
89
+ "&frac34;" => "three fourths",
90
+ "¾" => "three fourths",
91
+ "&#190;" => "three fourths",
92
+ "⅕" => "one fifth",
93
+ "&#8533;" => "one fifth",
94
+ "⅖" => "two fifths",
95
+ "&#8534;" => "two fifths",
96
+ "⅗" => "three fifths",
97
+ "&#8535;" => "three fifths",
98
+ "⅘" => "four fifths",
99
+ "&#8536;" => "four fifths",
100
+ "⅙" => "one sixth",
101
+ "&#8537;" => "one sixth",
102
+ "⅚" => "five sixths",
103
+ "&#8538;" => "five sixths",
104
+ "⅛" => "one eighth",
105
+ "&#8539;" => "one eighth",
106
+ "⅜" => "three eighths",
107
+ "&#8540;" => "three eighths",
108
+ "⅝" => "five eighths",
109
+ "&#8541;" => "five eighths",
110
+ "⅞" => "seven eighths",
111
+ "&#8542;" => "seven eighths"
112
+ }.each do |original, converted|
113
+ define_method "test_vulgar_fractions_conversion: #{original}" do
114
+ assert_equal converted, original.convert_vulgar_fractions
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,123 @@
1
+ require "test_helper"
2
+ require 'stringex'
3
+ require 'i18n'
4
+
5
+ class LocalizationTest < Test::Unit::TestCase
6
+ def setup
7
+ Stringex::Localization.reset!
8
+ end
9
+
10
+ def test_stores_translations
11
+ Stringex::Localization.backend = :internal
12
+
13
+ data = { :one => "number one", :two => "number two" }
14
+ Stringex::Localization.store_translations :en, :test_store, data
15
+
16
+ data.each do |key, value|
17
+ assert_equal value, Stringex::Localization.translate(:test_store, key)
18
+ end
19
+ end
20
+
21
+ def test_converts_translation_keys_to_symbols
22
+ Stringex::Localization.backend = :internal
23
+
24
+ data = { "one" => "number one", "two" => "number two" }
25
+ Stringex::Localization.store_translations :en, :test_convert, data
26
+
27
+ data.each do |key, value|
28
+ assert_equal value, Stringex::Localization.translate(:test_convert, key)
29
+ assert_equal value, Stringex::Localization.translate(:test_convert, key.to_sym)
30
+ end
31
+ end
32
+
33
+ def test_can_translate
34
+ Stringex::Localization.backend = :internal
35
+
36
+ data = { :one => "number one", :two => "number two" }
37
+ Stringex::Localization.store_translations :en, :test_translate, data
38
+
39
+ data.each do |key, value|
40
+ assert_equal value, Stringex::Localization.translate(:test_translate, key)
41
+ end
42
+ end
43
+
44
+ def test_can_translate_when_given_string_as_key
45
+ Stringex::Localization.backend = :internal
46
+
47
+ data = { :one => "number one", :two => "number two" }
48
+ Stringex::Localization.store_translations :en, :test_translate, data
49
+
50
+ data.each do |key, value|
51
+ assert_equal value, Stringex::Localization.translate(:test_translate, key.to_s)
52
+ end
53
+ end
54
+
55
+ def test_returns_default_if_none_found
56
+ Stringex::Localization.backend = :internal
57
+ assert_equal "my default", Stringex::Localization.translate(:test_default, :nonexistent, :default => "my default")
58
+ end
59
+
60
+ def test_returns_nil_if_no_default
61
+ Stringex::Localization.backend = :internal
62
+ assert_nil Stringex::Localization.translate(:test_no_default, :nonexistent)
63
+ end
64
+
65
+ def test_falls_back_to_default_locale
66
+ Stringex::Localization.backend = :internal
67
+ Stringex::Localization.default_locale = :es
68
+ Stringex::Localization.locale = :da
69
+
70
+ data = { "one" => "number one", "two" => "number two" }
71
+ Stringex::Localization.store_translations :es, :test_default_locale, data
72
+
73
+ data.each do |key, value|
74
+ assert_equal value, Stringex::Localization.translate(:test_default_locale, key)
75
+ end
76
+ end
77
+
78
+ def test_with_locale
79
+ Stringex::Localization.backend = :internal
80
+ Stringex::Localization.locale = :fr
81
+ assert_equal :fr, Stringex::Localization.locale
82
+ locale_set_in_block = nil
83
+ Stringex::Localization.with_locale :da do
84
+ locale_set_in_block = Stringex::Localization.locale
85
+ end
86
+ assert_equal :da, locale_set_in_block
87
+ assert_equal :fr, Stringex::Localization.locale
88
+ end
89
+
90
+ def test_stores_translations_in_i18n
91
+ Stringex::Localization.backend = :i18n
92
+
93
+ data = { :one => "number one", :two => "number two" }
94
+ Stringex::Localization.store_translations :en, :test_i18n_store, data
95
+
96
+ data.each do |key, value|
97
+ assert_equal value, I18n.translate("stringex.test_i18n_store.#{key}")
98
+ end
99
+ end
100
+
101
+ def test_can_translate_using_i18n
102
+ Stringex::Localization.backend = :i18n
103
+
104
+ data = { :one => "number one", :two => "number two" }
105
+
106
+ I18n.backend.store_translations :en, { :stringex => { :test_i18n_translation => data } }
107
+
108
+ data.each do |key, value|
109
+ assert_equal value, Stringex::Localization.translate(:test_i18n_translation, key)
110
+ end
111
+ end
112
+
113
+ def test_allows_blank_translations
114
+ [:internal, :i18n].each do |backend|
115
+ Stringex::Localization.backend = backend
116
+
117
+ assert_equal "Test blank", "Test&nbsp;blank".convert_miscellaneous_html_entities
118
+
119
+ Stringex::Localization.store_translations :en, :html_entities, { :nbsp => "" }
120
+ assert_equal "Testblank", "Test&nbsp;blank".convert_miscellaneous_html_entities
121
+ end
122
+ end
123
+ end