lazier 3.5.7 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis-gemfile +5 -4
- data/.travis.yml +2 -2
- data/CHANGELOG.md +65 -8
- data/Gemfile +9 -8
- data/README.md +4 -0
- data/doc/Lazier.html +178 -110
- data/doc/Lazier/Boolean.html +7 -7
- data/doc/Lazier/Configuration.html +24 -50
- data/doc/Lazier/DateTime.html +50 -305
- data/doc/Lazier/DateTime/ClassMethods.html +116 -806
- data/doc/Lazier/Exceptions.html +2 -2
- data/doc/Lazier/Exceptions/Debug.html +1 -1
- data/doc/Lazier/Exceptions/MissingTranslation.html +18 -14
- data/doc/Lazier/Exceptions/TranslationExceptionHandler.html +213 -0
- data/doc/Lazier/Hash.html +19 -155
- data/doc/Lazier/I18n.html +1735 -230
- data/doc/Lazier/Math.html +1 -1
- data/doc/Lazier/Math/ClassMethods.html +13 -13
- data/doc/Lazier/Object.html +353 -339
- data/doc/Lazier/Pathname.html +4 -4
- data/doc/Lazier/Settings.html +150 -304
- data/doc/Lazier/String.html +39 -199
- data/doc/Lazier/TimeZone.html +244 -746
- data/doc/Lazier/TimeZone/ClassMethods.html +109 -127
- data/doc/Lazier/Version.html +4 -4
- data/doc/_index.html +15 -15
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +5 -1
- data/doc/index.html +5 -1
- data/doc/method_list.html +97 -169
- data/doc/top-level-namespace.html +1 -1
- data/lazier.gemspec +9 -6
- data/lib/lazier.rb +41 -50
- data/lib/lazier/boolean.rb +0 -1
- data/lib/lazier/configuration.rb +26 -28
- data/lib/lazier/datetime.rb +33 -127
- data/lib/lazier/exceptions.rb +14 -6
- data/lib/lazier/hash.rb +7 -15
- data/lib/lazier/i18n.rb +130 -48
- data/lib/lazier/math.rb +6 -7
- data/lib/lazier/object.rb +79 -97
- data/lib/lazier/pathname.rb +0 -1
- data/lib/lazier/settings.rb +12 -25
- data/lib/lazier/string.rb +17 -38
- data/lib/lazier/timezone.rb +168 -164
- data/lib/lazier/version.rb +3 -4
- data/locales/en.yml +52 -51
- data/locales/it.yml +51 -50
- data/spec/coverage_helper.rb +0 -1
- data/spec/lazier/boolean_spec.rb +2 -3
- data/spec/lazier/configuration_spec.rb +3 -5
- data/spec/lazier/datetime_spec.rb +34 -95
- data/spec/lazier/exceptions_spec.rb +25 -0
- data/spec/lazier/hash_spec.rb +0 -21
- data/spec/lazier/i18n_spec.rb +135 -51
- data/spec/lazier/math_spec.rb +0 -1
- data/spec/lazier/object_spec.rb +105 -100
- data/spec/lazier/pathname_spec.rb +0 -1
- data/spec/lazier/settings_spec.rb +25 -28
- data/spec/lazier/string_spec.rb +7 -20
- data/spec/lazier/timezone_spec.rb +101 -87
- data/spec/lazier_spec.rb +25 -8
- data/spec/spec_helper.rb +3 -2
- metadata +15 -44
- data/doc/Lazier/Localizer.html +0 -545
- data/lib/lazier/localizer.rb +0 -41
- data/spec/lazier/localizer_spec.rb +0 -45
@@ -0,0 +1,25 @@
|
|
1
|
+
#
|
2
|
+
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
3
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
4
|
+
#
|
5
|
+
|
6
|
+
require "spec_helper"
|
7
|
+
|
8
|
+
describe Lazier::Exceptions::TranslationExceptionHandler do
|
9
|
+
describe "#call" do
|
10
|
+
it "should correctly raise exception" do
|
11
|
+
subject = Lazier::Exceptions::TranslationExceptionHandler.new
|
12
|
+
expect { subject.call(::I18n::MissingTranslation.new(1, 2, {}), 1, 2, 3) }.to raise_error(::I18n::MissingTranslationData)
|
13
|
+
expect { subject.call(RuntimeError.new, 1, 2, 3) }.to raise_error(RuntimeError)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe Lazier::Exceptions::MissingTranslation do
|
19
|
+
describe "#initialize" do
|
20
|
+
it "should initialize a good exception" do
|
21
|
+
subject = Lazier::Exceptions::MissingTranslation.new("LOCALE", "MESSAGE")
|
22
|
+
expect(subject.message).to eq("Unable to load the translation \"MESSAGE\" for the locale \"LOCALE\".")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/lazier/hash_spec.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
#
|
3
2
|
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
3
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -17,26 +16,6 @@ describe Object do
|
|
17
16
|
::Lazier.load!
|
18
17
|
end
|
19
18
|
|
20
|
-
describe "method access" do
|
21
|
-
it "it is not enabled by default" do
|
22
|
-
expect { subject.b }.to raise_error(NoMethodError)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "allows access to keys using dotted notation" do
|
27
|
-
before(:each) do
|
28
|
-
::Lazier.load!(:hash_method_access)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should allow method subject for symbol key" do
|
32
|
-
subject.b.f = 4
|
33
|
-
|
34
|
-
expect(subject.a).to eq(1)
|
35
|
-
expect(subject.b.c).to eq(2)
|
36
|
-
expect(subject["b"]["f"]).to eq(4)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
19
|
describe "#compact" do
|
41
20
|
it "should remove blank keys" do
|
42
21
|
expect({a: 1, b: nil}.compact).to eq({a: 1})
|
data/spec/lazier/i18n_spec.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
#
|
3
2
|
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
3
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -7,76 +6,161 @@
|
|
7
6
|
require "spec_helper"
|
8
7
|
|
9
8
|
describe Lazier::I18n do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
c
|
14
|
-
}
|
9
|
+
before(:each) do
|
10
|
+
Lazier::I18n.default_locale = :en
|
11
|
+
end
|
15
12
|
|
16
|
-
|
13
|
+
subject! { Lazier::I18n.instance(force: true) }
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
describe ".instance" do
|
16
|
+
it "should return the singleton instance" do
|
17
|
+
expect(subject).to be_a(Lazier::I18n)
|
18
|
+
expect(Lazier::I18n.instance).to be(subject)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return a new instance" do
|
22
|
+
another = Lazier::I18n.instance(force: true)
|
23
|
+
expect(another).not_to be(subject)
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
|
-
describe "#
|
24
|
-
it "should
|
25
|
-
subject.
|
26
|
-
expect(subject.
|
27
|
-
expect(subject.
|
27
|
+
describe "#initialize" do
|
28
|
+
it "should save attributes" do
|
29
|
+
subject = Lazier::I18n.new("it", root: "foo", path: "/dev/abc/..")
|
30
|
+
expect(subject.locale).to eq(:it)
|
31
|
+
expect(subject.root).to eq(:foo)
|
32
|
+
expect(subject.path).to eq("/dev")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should fallback to the default locale" do
|
36
|
+
allow(::I18n).to receive("locale=")
|
37
|
+
Lazier::I18n.default_locale = "pt"
|
38
|
+
expect(Lazier::I18n.new.locale).to eq(:pt)
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "should fallback to the system locale" do
|
42
|
+
before(:each) do
|
43
|
+
allow(::I18n).to receive("locale=")
|
44
|
+
Lazier::I18n.default_locale = nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it "in JRuby" do
|
48
|
+
expect(Lazier).to receive(:platform).exactly(2).and_return(:java)
|
49
|
+
|
50
|
+
if RUBY_PLATFORM !~ /java/
|
51
|
+
stub_const("Java", Object.new)
|
52
|
+
allow(Java).to receive(:java).and_return(Object.new)
|
53
|
+
allow(Java.java).to receive(:util).and_return(Object.new)
|
54
|
+
allow(Java.java.util).to receive(:Locale).and_return(Object.new)
|
55
|
+
allow(Java.java.util.Locale).to receive(:getDefault).and_return(Object.new)
|
56
|
+
end
|
57
|
+
|
58
|
+
allow(Java.java.util.Locale.getDefault).to receive(:toString).and_return("jp")
|
59
|
+
|
60
|
+
expect(Lazier::I18n.new.locale).to eq(:jp)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "in OSX" do
|
64
|
+
allow_any_instance_of(Lazier::I18n).to receive(:`).with("defaults read .GlobalPreferences AppleLanguages | awk 'NR==2{gsub(/[ ,]/, \"\");print}'").and_return("in")
|
65
|
+
|
66
|
+
expect(Lazier).to receive(:platform).exactly(2).and_return(:osx)
|
67
|
+
expect(Lazier::I18n.new.locale).to eq(:in)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "in UNIX" do
|
71
|
+
old_env = ENV["LANG"]
|
72
|
+
|
73
|
+
ENV["LANG"] = "it"
|
74
|
+
expect(Lazier).to receive(:platform).exactly(2).and_return(:posix)
|
75
|
+
expect(Lazier::I18n.new.locale).to eq(:it)
|
76
|
+
ENV["LANG"] = old_env
|
77
|
+
end
|
78
|
+
|
79
|
+
it "falling back to English" do
|
80
|
+
expect(Lazier).to receive(:platform).and_return(:other)
|
81
|
+
|
82
|
+
expect(Lazier::I18n.new.locale).to eq(:en)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should setup the backend" do
|
87
|
+
open("/tmp/it.yml", "w") {|f| f.write("---\nit:\n lazier:") }
|
88
|
+
subject = Lazier::I18n.new("it", path: "/tmp")
|
89
|
+
|
90
|
+
expect(::I18n.enforce_available_locales).to be_truthy
|
91
|
+
expect(::I18n.load_path).to include("/tmp/it.yml")
|
92
|
+
expect(::I18n.exception_handler).to be_a(::Lazier::Exceptions::TranslationExceptionHandler)
|
93
|
+
expect(subject.backend).to be_a(::I18n::Backend::Simple)
|
94
|
+
File.unlink("/tmp/it.yml")
|
95
|
+
::I18n.load_path.delete("/tmp/it.yml")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "#reload" do
|
100
|
+
it "should reload translations" do
|
101
|
+
expect_any_instance_of(::I18n::Backend::Simple).to receive(:load_translations).and_call_original
|
102
|
+
subject.reload
|
28
103
|
end
|
29
104
|
end
|
30
105
|
|
31
|
-
describe "#
|
32
|
-
it "should
|
33
|
-
subject.
|
34
|
-
expect(subject).to receive(:i18n_load_locale)
|
35
|
-
subject.i18n
|
106
|
+
describe "#translations" do
|
107
|
+
it "should return the list of translations" do
|
108
|
+
expect(subject.translations.keys).to eq([:date, :time, :support, :number, :lazier])
|
36
109
|
end
|
37
110
|
end
|
38
111
|
|
39
|
-
describe "#
|
40
|
-
it "should
|
41
|
-
expect(
|
42
|
-
subject.
|
43
|
-
expect(subject.
|
112
|
+
describe "#locale=" do
|
113
|
+
it "should assign the new locale" do
|
114
|
+
expect(::I18n).to receive("locale=").with(:it)
|
115
|
+
subject.locale = "it"
|
116
|
+
expect(subject.locale).to eq(:it)
|
44
117
|
end
|
45
118
|
end
|
46
119
|
|
47
|
-
describe "#
|
48
|
-
it "should
|
49
|
-
subject.
|
50
|
-
expect(::R18n::I18n).to receive(:new).with([ENV["LANG"]].compact.uniq, root_path).and_call_original
|
51
|
-
subject.i18n = nil
|
120
|
+
describe "#locales" do
|
121
|
+
it "should return the list of locales" do
|
122
|
+
expect(subject.locales).to eq([:en, :it])
|
52
123
|
end
|
124
|
+
end
|
53
125
|
|
54
|
-
|
55
|
-
|
56
|
-
expect(
|
57
|
-
subject.
|
126
|
+
describe "#translate" do
|
127
|
+
it "should return the translation" do
|
128
|
+
expect(subject.translate("configuration.not_defined", name: "foo", class: "bar")).to eq("Property foo is not defined for bar.")
|
129
|
+
expect(subject.translate(".date.formats")).to eq({:default=>"%Y-%m-%d", :short=>"%b %d", :long=>"%B %d, %Y"})
|
130
|
+
expect { subject.translate("::configuration.not_defined", property_name: "foo", class: "bar") }.to raise_error(Lazier::Exceptions::MissingTranslation)
|
58
131
|
end
|
132
|
+
end
|
59
133
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
expect_any_instance_of(::R18n::I18n).to receive(:t).and_return(t)
|
64
|
-
expect(t).to receive("ROOT")
|
65
|
-
subject.i18n_setup("ROOT", root_path)
|
66
|
-
subject.i18n = :it
|
134
|
+
describe "#translate_in_locale" do
|
135
|
+
it "should return the translation in the desired locale" do
|
136
|
+
expect(subject.translate_in_locale(:it, "configuration.not_defined", name: "foo", class: "bar")).to eq("La proprietà foo non è definita per bar.")
|
67
137
|
end
|
138
|
+
end
|
68
139
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
140
|
+
describe "#with_locale" do
|
141
|
+
it "should execute a block with the new locale and then set the old locale back" do
|
142
|
+
new_locale = nil
|
143
|
+
|
144
|
+
subject.with_locale(:it) do
|
145
|
+
new_locale = subject.locale
|
146
|
+
end
|
147
|
+
|
148
|
+
expect(new_locale).to eq(:it)
|
149
|
+
expect(subject.locale).to eq(:en)
|
73
150
|
end
|
74
151
|
|
75
|
-
it "should raise
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
152
|
+
it "should raise exception after having restored the old locale" do
|
153
|
+
new_locale = nil
|
154
|
+
|
155
|
+
expect {
|
156
|
+
subject.with_locale(:it) do
|
157
|
+
new_locale = subject.locale
|
158
|
+
raise RuntimeError
|
159
|
+
end
|
160
|
+
}.to raise_error(RuntimeError)
|
161
|
+
|
162
|
+
expect(new_locale).to eq(:it)
|
163
|
+
expect(subject.locale).to eq(:en)
|
80
164
|
end
|
81
165
|
end
|
82
|
-
end
|
166
|
+
end
|
data/spec/lazier/math_spec.rb
CHANGED
data/spec/lazier/object_spec.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
#
|
3
2
|
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
3
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -25,90 +24,78 @@ describe Lazier::Object do
|
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
28
|
-
describe "#
|
27
|
+
describe "#number?" do
|
29
28
|
it "should return true for a valid number" do
|
30
|
-
expect("123.45".
|
31
|
-
expect("1,23.45".
|
32
|
-
expect("-1.23.45".
|
33
|
-
expect("+123.45".
|
34
|
-
expect("+1.231,45".
|
29
|
+
expect("123.45".number?).to be_truthy
|
30
|
+
expect("1,23.45".number?).to be_truthy
|
31
|
+
expect("-1.23.45".number?).to be_truthy
|
32
|
+
expect("+123.45".number?).to be_truthy
|
33
|
+
expect("+1.231,45".number?).to be_truthy
|
34
|
+
expect(true.number?).to be_truthy
|
35
|
+
expect(false.number?).to be_truthy
|
36
|
+
expect(nil.number?).to be_truthy
|
37
|
+
expect("a".number?(Float, /[a-z]/)).to be_truthy
|
35
38
|
end
|
36
39
|
|
37
40
|
it "should return false for a invalid number" do
|
38
|
-
expect("s213".
|
41
|
+
expect("s213".number?).to be_falsey
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
42
|
-
describe "#
|
45
|
+
describe "#integer?" do
|
43
46
|
it "should return true for a valid number" do
|
44
|
-
expect(true.
|
45
|
-
expect(false.
|
46
|
-
expect(nil.
|
47
|
-
expect("123".
|
48
|
-
expect("-123".
|
49
|
-
expect("+123
|
47
|
+
expect(true.integer?).to be_truthy
|
48
|
+
expect(false.integer?).to be_truthy
|
49
|
+
expect(nil.integer?).to be_truthy
|
50
|
+
expect("123".integer?).to be_truthy
|
51
|
+
expect("-123".integer?).to be_truthy
|
52
|
+
expect("+123".integer?).to be_truthy
|
50
53
|
end
|
51
54
|
|
52
55
|
it "should return false for a invalid number" do
|
53
|
-
expect("s123".
|
54
|
-
expect("123.12".
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe "#is_integer?" do
|
59
|
-
it "should return true for a valid number" do
|
60
|
-
expect(true.is_integer?).to be_true
|
61
|
-
expect(false.is_integer?).to be_true
|
62
|
-
expect(nil.is_integer?).to be_true
|
63
|
-
expect("123".is_integer?).to be_true
|
64
|
-
expect("-123".is_integer?).to be_true
|
65
|
-
expect("+123".is_integer?).to be_true
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should return false for a invalid number" do
|
69
|
-
expect("s123".is_integer?).to be_false
|
70
|
-
expect("123.12".is_integer?).to be_false
|
56
|
+
expect("s123".integer?).to be_falsey
|
57
|
+
expect("123.12".integer?).to be_falsey
|
71
58
|
end
|
72
59
|
end
|
73
60
|
|
74
|
-
describe "#
|
61
|
+
describe "#float?" do
|
75
62
|
it "should return true for a valid number" do
|
76
|
-
expect(true.
|
77
|
-
expect(false.
|
78
|
-
expect(nil.
|
79
|
-
expect("123.45".
|
80
|
-
expect("1,23.45".
|
81
|
-
expect("-1.23.45".
|
82
|
-
expect("+123.45".
|
83
|
-
expect("+1.231,45".
|
63
|
+
expect(true.float?).to be_truthy
|
64
|
+
expect(false.float?).to be_truthy
|
65
|
+
expect(nil.float?).to be_truthy
|
66
|
+
expect("123.45".float?).to be_truthy
|
67
|
+
expect("1,23.45".float?).to be_truthy
|
68
|
+
expect("-1.23.45".float?).to be_truthy
|
69
|
+
expect("+123.45".float?).to be_truthy
|
70
|
+
expect("+1.231,45".float?).to be_truthy
|
84
71
|
end
|
85
72
|
|
86
73
|
it "should return false for a invalid number" do
|
87
|
-
expect("s213".
|
74
|
+
expect("s213".float?).to be_falsey
|
88
75
|
end
|
89
76
|
end
|
90
77
|
|
91
|
-
describe "#
|
78
|
+
describe "#boolean?" do
|
92
79
|
it "should return true for a valid boolean" do
|
93
|
-
expect(true.
|
94
|
-
expect(false.
|
95
|
-
expect(nil.
|
96
|
-
expect("y".
|
97
|
-
expect("n".
|
98
|
-
expect("yes".
|
99
|
-
expect("no".
|
100
|
-
expect("1".
|
101
|
-
expect("0".
|
102
|
-
expect("true".
|
103
|
-
expect("false".
|
104
|
-
expect("t".
|
105
|
-
expect("f".
|
106
|
-
expect(1.
|
107
|
-
expect(0.
|
80
|
+
expect(true.boolean?).to be_truthy
|
81
|
+
expect(false.boolean?).to be_truthy
|
82
|
+
expect(nil.boolean?).to be_truthy
|
83
|
+
expect("y".boolean?).to be_truthy
|
84
|
+
expect("n".boolean?).to be_truthy
|
85
|
+
expect("yes".boolean?).to be_truthy
|
86
|
+
expect("no".boolean?).to be_truthy
|
87
|
+
expect("1".boolean?).to be_truthy
|
88
|
+
expect("0".boolean?).to be_truthy
|
89
|
+
expect("true".boolean?).to be_truthy
|
90
|
+
expect("false".boolean?).to be_truthy
|
91
|
+
expect("t".boolean?).to be_truthy
|
92
|
+
expect("f".boolean?).to be_truthy
|
93
|
+
expect(1.boolean?).to be_truthy
|
94
|
+
expect(0.boolean?).to be_truthy
|
108
95
|
end
|
109
96
|
|
110
97
|
it "should return false for a invalid boolean" do
|
111
|
-
expect("11".
|
98
|
+
expect("11".boolean?).to be_falsey
|
112
99
|
end
|
113
100
|
end
|
114
101
|
|
@@ -167,46 +154,46 @@ describe Lazier::Object do
|
|
167
154
|
expect(nil.ensure_array).to eq([])
|
168
155
|
expect("A".ensure_array).to eq(["A"])
|
169
156
|
expect({a: "b"}.ensure_array).to eq([{a: "b"}])
|
170
|
-
expect(nil.ensure_array(["1"])).to eq(["1"])
|
171
|
-
expect("A".ensure_array(["2"])).to eq(["2"])
|
172
|
-
expect({a: "b"}.ensure_array(["3"])).to eq(["3"])
|
157
|
+
expect(nil.ensure_array(default: ["1"])).to eq(["1"])
|
158
|
+
expect("A".ensure_array(default: ["2"])).to eq(["2"])
|
159
|
+
expect({a: "b"}.ensure_array(default: ["3"])).to eq(["3"])
|
173
160
|
end
|
174
161
|
|
175
162
|
it "should sanitize elements of the array using a method or a block" do
|
176
163
|
expect(" 123 ".ensure_array).to eq([" 123 "])
|
177
|
-
expect(" 123 ".ensure_array(
|
178
|
-
expect(" 123 ".ensure_array
|
164
|
+
expect(" 123 ".ensure_array(sanitizer: :strip)).to eq(["123"])
|
165
|
+
expect(" 123 ".ensure_array { |e| e.reverse }).to eq([" 321 "])
|
179
166
|
end
|
180
167
|
|
181
168
|
it "should unicize, compact and flatten, array if requested to" do
|
182
|
-
expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(
|
183
|
-
expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(
|
184
|
-
expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(
|
185
|
-
expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(
|
169
|
+
expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(no_duplicates: true)).to eq([1, 2, 3, nil, [4]])
|
170
|
+
expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(compact: true)).to eq([1, 2, 3, 3, 2, 1, [4]])
|
171
|
+
expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(no_duplicates: true, compact: true)).to eq([1, 2, 3, [4]])
|
172
|
+
expect([1, 2, 3, nil, 3, 2, 1, [4]].ensure_array(no_duplicates: true, compact: true, flatten: true)).to eq([1, 2, 3, 4])
|
186
173
|
end
|
187
174
|
end
|
188
175
|
|
189
176
|
describe "#ensure_hash" do
|
190
177
|
it "should return an hash" do
|
191
178
|
expect({a: "b"}.ensure_hash).to eq({a: "b"})
|
192
|
-
expect(nil.ensure_hash(
|
179
|
+
expect(nil.ensure_hash(default: {a: "b"})).to eq({a: "b"})
|
193
180
|
|
194
181
|
expect(1.ensure_hash).to eq({})
|
195
|
-
expect(1.ensure_hash(
|
196
|
-
expect(1.ensure_hash(
|
197
|
-
expect(1.ensure_hash(
|
182
|
+
expect(1.ensure_hash(default: :test)).to eq({test: 1})
|
183
|
+
expect(1.ensure_hash(default: "test")).to eq({"test" => 1})
|
184
|
+
expect(1.ensure_hash(default: 2)).to eq({key: 1})
|
198
185
|
end
|
199
186
|
|
200
187
|
it "should sanitize values" do
|
201
|
-
expect(" 1 ".ensure_hash(nil, :
|
202
|
-
expect(1.ensure_hash(nil
|
188
|
+
expect(" 1 ".ensure_hash(default: nil, sanitizer: :strip)).to eq({key: "1"})
|
189
|
+
expect(1.ensure_hash(default: nil) { |v| v * 2 }).to eq({key: 2})
|
203
190
|
end
|
204
191
|
|
205
192
|
it "should grant access" do
|
206
193
|
subject = {a: "b"}
|
207
194
|
|
208
|
-
expect(subject ).to receive(:ensure_access).with("ACCESS")
|
209
|
-
subject
|
195
|
+
expect(subject ).to receive(:ensure_access).with(["ACCESS"])
|
196
|
+
subject.ensure_hash(accesses: "ACCESS")
|
210
197
|
end
|
211
198
|
end
|
212
199
|
|
@@ -224,7 +211,7 @@ describe Lazier::Object do
|
|
224
211
|
expect("+1.231,45".to_float).to eq(1231.45)
|
225
212
|
end
|
226
213
|
|
227
|
-
it "should return 0.0 for a invalid number without a default" do
|
214
|
+
it "should return 0.0 for a invalid number? without a default" do
|
228
215
|
expect("s213".to_float).to eq(0.0)
|
229
216
|
end
|
230
217
|
|
@@ -244,7 +231,7 @@ describe Lazier::Object do
|
|
244
231
|
expect("-123".to_integer).to eq(-123)
|
245
232
|
end
|
246
233
|
|
247
|
-
it "should return 0 for a invalid number without a default" do
|
234
|
+
it "should return 0 for a invalid number? without a default" do
|
248
235
|
expect("s213".to_integer).to eq(0)
|
249
236
|
end
|
250
237
|
|
@@ -292,25 +279,25 @@ describe Lazier::Object do
|
|
292
279
|
|
293
280
|
describe "#format_number" do
|
294
281
|
it "should format number" do
|
295
|
-
expect(123.format_number(0)).to eq("123")
|
282
|
+
expect(123.format_number(precision: 0)).to eq("123")
|
296
283
|
expect(123.456789.format_number).to eq("123.46")
|
297
284
|
expect(12312.456789.format_number).to eq("12,312.46")
|
298
285
|
expect(123123.456789.format_number).to eq("123,123.46")
|
299
286
|
expect(1123123.456789.format_number).to eq("1,123,123.46")
|
300
|
-
expect(123123.456789.format_number(2)).to eq("123,123.46")
|
301
|
-
expect(123123.456789.format_number(3, "@")).to eq("123,123@457")
|
302
|
-
expect(123123.456789.format_number(3, "@", "$")).to eq("123,123@457 $")
|
303
|
-
expect("123123.456789".format_number(3, "@", "$", "!")).to eq("123!123@457 $")
|
287
|
+
expect(123123.456789.format_number(precision: 2)).to eq("123,123.46")
|
288
|
+
expect(123123.456789.format_number(precision: 3, decimal_separator: "@")).to eq("123,123@457")
|
289
|
+
expect(123123.456789.format_number(precision: 3, decimal_separator: "@", add_string: "$")).to eq("123,123@457 $")
|
290
|
+
expect("123123.456789".format_number(precision: 3, decimal_separator: "@", add_string: "$", k_separator: "!")).to eq("123!123@457 $")
|
304
291
|
|
305
|
-
Lazier.settings.setup_format_number(5, ",", "£", ".")
|
292
|
+
Lazier.settings.setup_format_number(precision: 5, decimal_separator: ",", add_string: "£", k_separator:".")
|
306
293
|
expect(123123.456789.format_number).to eq("123.123,45679 £")
|
307
294
|
|
308
|
-
Lazier.settings.setup_format_number(2)
|
309
|
-
expect(123.456789.format_number(-1)).to eq("123")
|
295
|
+
Lazier.settings.setup_format_number(precision: 2)
|
296
|
+
expect(123.456789.format_number(precision: -1)).to eq("123")
|
310
297
|
end
|
311
298
|
|
312
299
|
it "should return nil for non numeric values" do
|
313
|
-
expect("abc".format_number(-1)).to eq(nil)
|
300
|
+
expect("abc".format_number(precision: -1)).to eq(nil)
|
314
301
|
end
|
315
302
|
end
|
316
303
|
|
@@ -321,38 +308,56 @@ describe Lazier::Object do
|
|
321
308
|
end
|
322
309
|
|
323
310
|
it "should support localization" do
|
324
|
-
Lazier.settings.setup_boolean_names("YYY", "NNN")
|
311
|
+
Lazier.settings.setup_boolean_names(true_name: "YYY", false_name: "NNN")
|
325
312
|
expect("yes".format_boolean).to eq("YYY")
|
326
313
|
expect("abc".format_boolean).to eq("NNN")
|
327
314
|
end
|
328
315
|
|
329
316
|
it "should support overrides" do
|
330
317
|
Lazier.settings.setup_boolean_names
|
331
|
-
expect("yes".format_boolean("TTT")).to eq("TTT")
|
332
|
-
expect("yes".format_boolean(
|
333
|
-
expect("abc".format_boolean("TTT")).to eq("No")
|
334
|
-
expect("abc".format_boolean(
|
318
|
+
expect("yes".format_boolean(true_name: "TTT")).to eq("TTT")
|
319
|
+
expect("yes".format_boolean(false_name: "FFF")).to eq("Yes")
|
320
|
+
expect("abc".format_boolean(true_name: "TTT")).to eq("No")
|
321
|
+
expect("abc".format_boolean(false_name: "FFF")).to eq("FFF")
|
335
322
|
end
|
336
323
|
end
|
337
324
|
|
338
325
|
describe "#indexize" do
|
339
326
|
it "should format for printing" do
|
340
327
|
expect(1.indexize).to eq("01")
|
341
|
-
expect(21.indexize(3, "A")).to eq("A21")
|
342
|
-
expect(21.indexize(3, "A", :ljust)).to eq("21A")
|
328
|
+
expect(21.indexize(length: 3, filler: "A")).to eq("A21")
|
329
|
+
expect(21.indexize(length: 3, filler: "A", formatter: :ljust)).to eq("21A")
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
describe "#to_pretty_json" do
|
334
|
+
subject { {a: "b", c: [1, 2, "3"]} }
|
335
|
+
|
336
|
+
it "should use json gem for JRuby" do
|
337
|
+
allow(Lazier).to receive(:platform).and_return(:java)
|
338
|
+
stub_const("JSON", "JSON")
|
339
|
+
expect(JSON).to receive(:pretty_generate).with(subject).and_return("JSON")
|
340
|
+
expect(subject.to_pretty_json).to eq("JSON")
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should use Oj gem for other implementations" do
|
344
|
+
allow(Lazier).to receive(:platform).and_return(:posix)
|
345
|
+
stub_const("Oj", "OJ")
|
346
|
+
expect(Oj).to receive(:dump).with(subject, mode: :compat, indent: 2).and_return("JSON")
|
347
|
+
expect(subject.to_pretty_json).to eq("JSON")
|
343
348
|
end
|
344
349
|
end
|
345
350
|
|
346
|
-
describe "#
|
351
|
+
describe "#to_debug" do
|
347
352
|
it "should return the correct representation for an object" do
|
348
353
|
subject = {a: "b"}
|
349
|
-
expect(subject.
|
350
|
-
expect(subject.
|
351
|
-
expect(subject.
|
354
|
+
expect(subject.to_debug(format: :json, as_exception: false)).to eq(subject.to_json)
|
355
|
+
expect(subject.to_debug(format: :pretty_json, as_exception: false)).to eq(subject.to_pretty_json)
|
356
|
+
expect(subject.to_debug(format: :yaml, as_exception: false)).to eq(subject.to_yaml)
|
352
357
|
end
|
353
358
|
|
354
359
|
it "should raise an exception if requested" do
|
355
|
-
expect { {a: "b"}.
|
360
|
+
expect { {a: "b"}.to_debug }.to raise_error(::Lazier::Exceptions::Debug)
|
356
361
|
end
|
357
362
|
end
|
358
363
|
end
|