duck_puncher 4.4.2 → 5.0.1

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.
@@ -1,64 +0,0 @@
1
- require_relative '../../test_helper'
2
-
3
- DuckPuncher.punch! Object
4
-
5
- class EnumerableTest < MiniTest::Test
6
- attr_reader :subject
7
-
8
- def setup
9
- @subject = ('a'..'m').to_a
10
- end
11
-
12
- def test_m
13
- subject.punch!
14
- assert_equal subject.map(&:upcase), subject.m(:upcase)
15
- refute_equal subject.object_id, subject.m(:upcase).object_id
16
- assert_equal subject.map!(&:upcase), subject.m!(:upcase)
17
- assert_equal subject.object_id, subject.m!(:upcase).object_id
18
- end
19
-
20
- def test_m_with_range
21
- @subject = ('a'..'f')
22
- assert_equal %w[A B C D E F], @subject.punch.m(:upcase)
23
- assert_equal %w[A B C D E F], @subject.punch!.m(:upcase)
24
- end
25
-
26
- def test_m_with_enum
27
- @subject = ('A'..'F').to_enum
28
- assert_equal %w[B C D E F G], @subject.punch.m(:next)
29
- assert_equal %w[B C D E F G], @subject.punch!.m(:next)
30
- end
31
-
32
- def test_mm_with_set
33
- @subject = Set.new %w[a b c d e f]
34
- assert_equal %w[A B C D E F], @subject.punch.m(:upcase)
35
- assert_equal %w[A B C D E F], @subject.punch!.m(:upcase)
36
- assert_equal %w[B C D E F G], @subject.punch!.m!(:upcase).m(:next)
37
- end
38
-
39
- def test_mm_with_two_args
40
- subject.punch!
41
- assert_equal subject.map { |x| x.prepend('btn-') }, subject.mm(:prepend, 'btn-')
42
- refute_equal subject.object_id, subject.mm(:prepend, 'btn-')
43
- assert_equal subject.map! { |x| x.prepend('btn-') }, subject.mm!(:prepend, 'btn-')
44
- assert_equal subject.object_id, subject.mm!(:prepend, 'btn-').object_id
45
- end
46
-
47
- def test_mm_with_three_args
48
- @subject = @subject.punch
49
- assert_equal subject.map { |x| x.sub(/[aeiou]/, '*') }, subject.mm(:sub, /[aeiou]/, '*')
50
- end
51
-
52
- def test_except
53
- @subject = @subject.punch
54
- assert_equal subject.except('a'), %w[b c d e f g h i j k l m]
55
- assert_equal subject.except('a', 'b', 'c'), %w[d e f g h i j k l m]
56
- assert_equal subject.except, subject
57
- end
58
-
59
- def test_map_keys
60
- @subject = [{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { name: 'c' }]
61
- assert_respond_to @subject.punch, :map_keys
62
- assert_equal %w[a b c], @subject.punch.map_keys(:name)
63
- end
64
- end
@@ -1,23 +0,0 @@
1
- require_relative '../../test_helper'
2
-
3
- DuckPuncher.punch! Hash
4
-
5
- class HashTest < MiniTest::Test
6
- def setup
7
- @subject = { a: 1, b: { c: 2 } }
8
- end
9
-
10
- def test_dig
11
- assert_equal @subject.dig(:a), 1
12
- assert_equal @subject.dig(:b, :a), nil
13
- assert_equal @subject.dig(:b, :c), 2
14
- assert_equal @subject.dig(:b), { c: 2 }
15
- end
16
-
17
- def test_compact
18
- assert_equal @subject.compact, { a: 1, b: { c: 2 } }
19
- @subject[:b] = nil
20
- assert_equal @subject, { a: 1, b: nil }
21
- assert_equal @subject.compact, { a: 1 }
22
- end
23
- end
@@ -1,32 +0,0 @@
1
- require_relative '../../test_helper'
2
- require_relative '../../fixtures/wut'
3
- DuckPuncher.punch! Method
4
-
5
- class MethodTest < MiniTest::Test
6
-
7
- # Called before every test method runs. Can be used
8
- # to set up fixture information.
9
- def setup
10
- @subject = Wut.new
11
- end
12
-
13
- def test_to_instruct
14
- assert_match /:to_a/, @subject.method(:to_a).to_instruct
15
- assert_match /newarray/, @subject.method(:to_a).to_instruct
16
- assert_match /opt_plus/, @subject.method(:to_a).to_instruct
17
- assert_equal nil, [].method(:to_s).to_instruct
18
- end
19
-
20
- def test_to_instruct_single_line
21
- assert_match /:to_f/, @subject.method(:to_f).to_instruct
22
- assert_match /getconstant\s*:INFINITY/, @subject.method(:to_f).to_instruct
23
- end
24
-
25
- def test_to_source
26
- assert_equal "def to_a\n ['w'] + ['u'] + ['t']\nend\n", @subject.method(:to_a).to_source
27
- end
28
-
29
- def test_to_source_with_no_source
30
- assert_equal '', @subject.method(:object_id).to_source
31
- end
32
- end
@@ -1,9 +0,0 @@
1
- require_relative '../../test_helper'
2
-
3
- DuckPuncher.punch! Module
4
-
5
- class ModuleTest < MiniTest::Test
6
- def test_local_methods
7
- assert_equal ModWithNestedMod.local_methods, [:instance_method_1, :class_method_1]
8
- end
9
- end
@@ -1,48 +0,0 @@
1
- require_relative '../../test_helper'
2
- DuckPuncher.(Numeric, String)
3
-
4
- class NumericTest < MiniTest::Test
5
-
6
- def test_to_currency
7
- assert_equal '0.00', 0.to_currency
8
- assert_equal '25.00', 25.to_currency
9
- assert_equal '25.20', 25.2.to_currency
10
- assert_equal '25.25', 25.245.to_currency
11
- assert_equal '$25.25', 25.245.to_currency('$')
12
- end
13
-
14
- def test_to_duration
15
- assert_equal '', 10.to_duration
16
- assert_equal '1 min', 100.to_duration
17
- assert_equal '16 min', 1_000.to_duration
18
- assert_equal '2 h 46 min', 10_000.to_duration
19
- assert_equal '27 h 46 min', 100_000.to_duration
20
- end
21
-
22
- def test_to_duration_with_seconds
23
- assert_equal '10 s', 10.to_duration(true)
24
- assert_equal '1 min 40 s', 100.to_duration(true)
25
- assert_equal '16 min 40 s', 1_000.to_duration(true)
26
- assert_equal '2 h 46 min', 10_000.to_duration(true)
27
- end
28
-
29
- def test_to_rad
30
- assert_equal 0.0, 0.to_rad
31
- assert_equal 0.17715091907742445, 10.15.to_rad
32
- assert_equal 0.36035409894869713, 20.646769.to_rad
33
- assert_equal -2.730392366234936, -156.439959.to_rad
34
- end
35
-
36
- def test_to_time_ago
37
- assert_equal 'less than a minute ago', 10.to_time_ago
38
- assert_equal '1 minute ago', 100.to_time_ago
39
- assert_equal '2 minutes ago', 130.to_time_ago
40
- assert_equal '16 minutes ago', 1_000.to_time_ago
41
- assert_equal '1 hour ago', 3_600.to_time_ago
42
- assert_equal '1 hour ago', 4_600.to_time_ago
43
- assert_equal '2 hours ago', 7_300.to_time_ago
44
- assert_equal '1 day ago', 86_400.to_time_ago
45
- assert_equal '1 day ago', 100_000.to_time_ago
46
- assert_equal '2 days ago', 180_000.to_time_ago
47
- end
48
- end
@@ -1,78 +0,0 @@
1
- require_relative '../../test_helper'
2
- DuckPuncher.punch! Object
3
-
4
- class ObjectTest < MiniTest::Test
5
- def setup
6
- @animal = Animal.new
7
- @dog = Dog.new
8
- @kaia = Kaia.new
9
- end
10
-
11
- def teardown
12
- DuckPuncher.deregister Animal, Dog, Kaia
13
- end
14
-
15
- def test_clone!
16
- cloned = @dog.clone!
17
- assert_equal cloned.class, @dog.class
18
- refute_equal cloned, @dog
19
- end
20
-
21
- def test_punch_with_a_core_duck
22
- assert [].punch.respond_to?(:m)
23
- end
24
-
25
- def test_punch_on_a_custom_duck
26
- DuckPuncher.register Animal, CustomPunch2
27
- assert @animal.punch.respond_to?(:quack)
28
- end
29
-
30
- def test_punch_with_multiple_custom_duck
31
- DuckPuncher.register Animal, CustomPunch2
32
- DuckPuncher.register Animal, CustomPunch3
33
- assert @animal.punch.respond_to?(:wobble)
34
- end
35
-
36
- def test_punch_call_stack
37
- Animal.send(:define_method, :foo) { quack }
38
- Animal.send(:define_method, :quack) { 'foo' }
39
- assert_equal 'foo', @animal.foo
40
- DuckPuncher.register Animal, CustomPunch2
41
- @animal.punch!
42
- assert_equal 'quack', @animal.foo
43
- end
44
-
45
- def test_punch_on_ancestor_only
46
- DuckPuncher.register Dog, CustomPunch2
47
- assert_respond_to @dog.punch, :quack
48
- end
49
-
50
- def test_punch_includes_all_ancestors
51
- DuckPuncher.register Animal, CustomPunch2
52
- DuckPuncher.register Dog, CustomPunch
53
- DuckPuncher.register Kaia, CustomPunch3
54
- @kaia = Kaia.new
55
- @kaia.punch!
56
- assert_respond_to @kaia, :wobble
57
- assert_respond_to @kaia, :talk
58
- assert_respond_to @kaia, :quack
59
- end
60
-
61
- def test_soft_punch_includes_all_ancestors
62
- DuckPuncher.register Animal, CustomPunch2
63
- DuckPuncher.register Dog, CustomPunch
64
- DuckPuncher.register Kaia, CustomPunch3
65
- @kaia = Kaia.new.punch
66
- assert_respond_to @kaia, :wobble
67
- assert_respond_to @kaia, :talk
68
- assert_respond_to @kaia, :quack
69
- end
70
-
71
- def test_soft_punch_with_parent_override
72
- DuckPuncher.register Animal, CustomPunch
73
- DuckPuncher.register Kaia, ModWithOverride
74
- @kaia = Kaia.new.punch
75
- assert_respond_to @kaia, :talk
76
- assert_equal @kaia.talk, 'talk is cheap'
77
- end
78
- end
@@ -1,39 +0,0 @@
1
- require_relative '../../test_helper'
2
-
3
- DuckPuncher.(String)
4
-
5
- class StringTest < MiniTest::Test
6
- def test_pluralize
7
- assert_equal 'hour', 'hour'.pluralize(1)
8
- assert_equal 'hours', 'hour'.pluralize(0)
9
- assert_equal 'hours', 'hour'.pluralize(2)
10
- end
11
-
12
- def test_underscore
13
- assert_equal 'mini_test', 'MiniTest'.underscore
14
- assert_equal 'mini_test_do_it_to_it', 'MiniTestDoItToIt'.underscore
15
- assert_equal 'mini_test/helper', 'MiniTest::Helper'.underscore
16
- assert_equal 'mini_test/helper/expectations', 'MiniTest::Helper::Expectations'.underscore
17
- assert_equal 'mini_test.rb', 'mini_test.rb'.underscore
18
- assert_equal 'duck_puncher/json_storage', 'DuckPuncher::JSONStorage'.underscore
19
- end
20
-
21
- def test_to_boolean
22
- assert 'true'.to_boolean
23
- assert '1'.to_boolean
24
- assert 'y'.to_boolean
25
- assert 'on'.to_boolean
26
- assert 'yes'.to_boolean
27
- refute 'false'.to_boolean
28
- refute '0'.to_boolean
29
- refute 'no'.to_boolean
30
- refute 'off'.to_boolean
31
- refute ''.to_boolean
32
- refute 'f'.to_boolean
33
- end
34
-
35
- def test_constantize
36
- assert_equal MiniTest, 'MiniTest'.constantize
37
- assert_equal MiniTest::Test, 'MiniTest::Test'.constantize
38
- end
39
- end
@@ -1,71 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- DuckPuncher.punch! Object, only: :punch
4
-
5
- class DuckPuncherTest < MiniTest::Test
6
- def setup
7
- @subject = Animal.new
8
- @kaia = Kaia.new
9
- end
10
-
11
- def teardown
12
- DuckPuncher.deregister Animal, Kaia, Dog
13
- end
14
-
15
- def test_punch!
16
- refute_respond_to @kaia, :talk
17
- refute_respond_to @kaia.punch, :talk
18
- DuckPuncher.register Kaia, CustomPunch
19
- DuckPuncher.punch! Kaia, only: :talk
20
- assert_respond_to @kaia, :talk
21
- assert_respond_to @kaia.punch, :talk
22
- end
23
-
24
- def test_punch_all!
25
- DuckPuncher.()
26
- expected_methods = DuckPuncher::Ducks.list.values.m(:to_a).flatten.m(:mod).m(:local_methods).flatten
27
- assert expected_methods.size > 1
28
- good_ducks = DuckPuncher::Ducks.list.select { |_, ducks|
29
- ducks.all? { |duck| (duck.mod.local_methods - duck.target.instance_methods(:false)).size.zero? }
30
- }
31
- assert good_ducks.size > 5
32
- end
33
-
34
- def test_call
35
- DuckPuncher.()
36
- expected_methods = DuckPuncher::Ducks.list.values.m(:to_a).flatten.m(:mod).m(:local_methods).flatten
37
- assert expected_methods.size > 1
38
- # Find all ducks that have copied all their methods to the target class (e.g. String)
39
- good_ducks = DuckPuncher::Ducks.list.select { |_, ducks|
40
- ducks.all? { |duck| (duck.mod.local_methods - duck.target.instance_methods(:false)).size.zero? }
41
- }
42
- assert good_ducks.size == 6, "Good ducks should be equal to 6 but are #{good_ducks.size}"
43
- end
44
-
45
- def test_call_with_target
46
- DuckPuncher.(String, only: [:to_boolean])
47
- refute_respond_to @subject, :to_boolean
48
- DuckPuncher.(String, target: @subject.class)
49
- assert_respond_to @subject, :to_boolean
50
- end
51
-
52
- def test_register_with_multiple_mods
53
- refute_respond_to @subject, :talk
54
- refute_respond_to @subject, :wobble
55
- refute_respond_to @subject.punch, :talk
56
- refute_respond_to @subject.punch, :wobble
57
- DuckPuncher.register Animal, CustomPunch, CustomPunch3
58
- assert_respond_to @subject.punch, :talk
59
- assert_respond_to @subject.punch, :wobble
60
- end
61
-
62
- def test_deregister
63
- refute_respond_to @subject, :talk
64
- refute_respond_to @subject.punch, :talk
65
- DuckPuncher.register Animal, CustomPunch
66
- assert_respond_to @subject.punch, :talk
67
- refute_respond_to @subject, :talk
68
- DuckPuncher.deregister Animal
69
- refute_respond_to @subject.punch, :talk
70
- end
71
- end
data/test/test_helper.rb DELETED
@@ -1,9 +0,0 @@
1
- require 'minitest'
2
- require 'minitest/autorun'
3
- require 'minitest/reporters'
4
- require 'duck_puncher'
5
- require_relative 'fixtures/test_classes'
6
-
7
- Minitest::Reporters.use!
8
-
9
- DuckPuncher.logger.level = Logger::DEBUG