golly-utils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.gitignore +19 -0
  2. data/.rspec +8 -0
  3. data/.simplecov +14 -0
  4. data/.travis.yml +5 -0
  5. data/.yardopts +6 -0
  6. data/CHANGELOG.md +10 -0
  7. data/Gemfile +12 -0
  8. data/Gemfile.corvid +27 -0
  9. data/Gemfile.lock +86 -0
  10. data/Guardfile +44 -0
  11. data/README.md +3 -0
  12. data/RELEASE.md +13 -0
  13. data/Rakefile +6 -0
  14. data/bin/guard +16 -0
  15. data/bin/rake +16 -0
  16. data/bin/rspec +16 -0
  17. data/bin/yard +16 -0
  18. data/bin/yardoc +16 -0
  19. data/bin/yri +16 -0
  20. data/golly-utils.gemspec +17 -0
  21. data/lib/golly-utils/attr_declarative.rb +74 -0
  22. data/lib/golly-utils/callbacks.rb +92 -0
  23. data/lib/golly-utils/child_process.rb +124 -0
  24. data/lib/golly-utils/colourer.rb +50 -0
  25. data/lib/golly-utils/delegator.rb +81 -0
  26. data/lib/golly-utils/multi_io.rb +13 -0
  27. data/lib/golly-utils/ruby_ext.rb +2 -0
  28. data/lib/golly-utils/ruby_ext/array_to_hash.rb +29 -0
  29. data/lib/golly-utils/ruby_ext/deep_dup.rb +33 -0
  30. data/lib/golly-utils/ruby_ext/env_helpers.rb +27 -0
  31. data/lib/golly-utils/ruby_ext/hash_combinations.rb +49 -0
  32. data/lib/golly-utils/ruby_ext/pretty_error_messages.rb +9 -0
  33. data/lib/golly-utils/ruby_ext/subclasses.rb +17 -0
  34. data/lib/golly-utils/test/spec/deferrable_specs.rb +85 -0
  35. data/lib/golly-utils/test/spec/within_time.rb +56 -0
  36. data/lib/golly-utils/version.rb +3 -0
  37. data/test/bootstrap/all.rb +4 -0
  38. data/test/bootstrap/spec.rb +5 -0
  39. data/test/bootstrap/unit.rb +4 -0
  40. data/test/spec/child_process_mock_target.rb +28 -0
  41. data/test/spec/child_process_spec.rb +41 -0
  42. data/test/unit/attr_declarative_test.rb +54 -0
  43. data/test/unit/callbacks_test.rb +76 -0
  44. data/test/unit/delegator_test.rb +99 -0
  45. data/test/unit/multi_io_test.rb +18 -0
  46. data/test/unit/ruby_ext/env_helpers_test.rb +48 -0
  47. data/test/unit/ruby_ext/hash_combinations_test.rb +31 -0
  48. data/test/unit/ruby_ext/subclasses_test.rb +24 -0
  49. metadata +107 -0
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+ require_relative '../bootstrap/unit'
3
+ require 'golly-utils/delegator'
4
+
5
+ class DelegatorTest < MiniTest::Unit::TestCase
6
+
7
+ class A
8
+ def a; 1; end
9
+ def c; 10; end
10
+ def add(a,b,c) a+b+c end
11
+ end
12
+
13
+ class B
14
+ def b; 2; end
15
+ def c; 20; end
16
+ end
17
+
18
+ def na; A.new end
19
+ def nb; B.new end
20
+ def nd(*args) GollyUtils::Delegator.new(*args) end
21
+
22
+ def test_simple_delegation
23
+ d= nd(na,nb)
24
+ assert_equal 1, d.a
25
+ assert_equal 2, d.b
26
+ end
27
+
28
+ def test_delegation_with_multiple_args
29
+ assert_equal 15, nd(na).add(3,5,7)
30
+ end
31
+
32
+ def test_conflict_calls_first_by_default
33
+ assert_equal 10, nd(na,nb).c
34
+ end
35
+
36
+ def test_conflict_calls_all
37
+ assert_equal [10,20], nd(na,nb,delegate_to: :all).c
38
+ end
39
+
40
+ def test_respond_to
41
+ d= nd(na,nb)
42
+ assert d.respond_to?(:a)
43
+ assert d.respond_to?(:b)
44
+ assert d.respond_to?(:c)
45
+ assert !d.respond_to?(:x)
46
+ end
47
+
48
+ def test_caching
49
+ [true,false].each do |cache|
50
+ a= na
51
+ d= nd(a, cache: cache)
52
+ assert_raises(NoMethodError){ d.x }
53
+ def a.x; 123; end
54
+ if cache
55
+ assert_raises(NoMethodError){ d.x }
56
+ else
57
+ assert_equal 123, d.x
58
+ end
59
+ end
60
+ end
61
+
62
+ def test_method_whitelist__fixed
63
+ d= nd(na,nb, method_whitelist: [:a, 'b'])
64
+
65
+ assert d.respond_to?(:a)
66
+ assert d.respond_to?(:b)
67
+ assert !d.respond_to?(:c)
68
+
69
+ assert_equal 1, d.a
70
+ assert_equal 2, d.b
71
+ assert_raises(NoMethodError){ d.c }
72
+ end
73
+
74
+ def test_method_whitelist__regex
75
+ d= nd(na,nb, method_whitelist: /^[ac]$/)
76
+
77
+ assert d.respond_to?(:a)
78
+ assert !d.respond_to?(:b)
79
+ assert d.respond_to?(:c)
80
+
81
+ assert_equal 1, d.a
82
+ assert_equal 10, d.c
83
+ assert_raises(NoMethodError){ d.b }
84
+ end
85
+
86
+ def test_method_blacklist
87
+ d= nd(na,nb, method_blacklist: [:a, 'b', /^ad{2}$/])
88
+
89
+ assert !d.respond_to?(:a)
90
+ assert !d.respond_to?(:b)
91
+ assert d.respond_to?(:c)
92
+ assert !d.respond_to?(:add)
93
+
94
+ assert_equal 10, d.c
95
+ assert_raises(NoMethodError){ d.a }
96
+ assert_raises(NoMethodError){ d.b }
97
+ assert_raises(NoMethodError){ d.add }
98
+ end
99
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ require_relative '../bootstrap/unit'
3
+ require 'golly-utils/multi_io'
4
+ require 'stringio'
5
+
6
+ class MultiIOTest < MiniTest::Unit::TestCase
7
+
8
+ def test_writing
9
+ a= StringIO.new
10
+ b= StringIO.new
11
+ a.write 'a_'
12
+ m= GollyUtils::MultiIO.new(a,b)
13
+ m.write 'hehe'
14
+ [a,b].each &:rewind
15
+ assert_equal 'a_hehe', a.read
16
+ assert_equal 'hehe', b.read
17
+ end
18
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+ require_relative '../../bootstrap/unit'
3
+ require 'golly-utils/ruby_ext/env_helpers'
4
+
5
+ class EnvTest < MiniTest::Unit::TestCase
6
+ KEY= 'golly_test_env'
7
+
8
+ def test_boolean_on
9
+ %w[1 yes on true y].each do |v|
10
+ ENV[KEY]= v
11
+ assert ENV.on?(KEY)
12
+ assert ENV.yes?(KEY)
13
+ assert ENV.enabled?(KEY)
14
+ refute ENV.off?(KEY)
15
+ refute ENV.no?(KEY)
16
+ refute ENV.disabled?(KEY)
17
+ end
18
+ end
19
+
20
+ def test_boolean_off
21
+ %w[0 no off false n].each do |v|
22
+ ENV[KEY]= v
23
+ refute ENV.on?(KEY)
24
+ refute ENV.yes?(KEY)
25
+ refute ENV.enabled?(KEY)
26
+ assert ENV.off?(KEY)
27
+ assert ENV.no?(KEY)
28
+ assert ENV.disabled?(KEY)
29
+ end
30
+ end
31
+
32
+ def test_boolean_default
33
+ ENV.delete KEY
34
+ assert_nil ENV.boolean(KEY)
35
+ assert_equal true, ENV.boolean(KEY,true)
36
+ assert_equal false, ENV.boolean(KEY,false)
37
+ end
38
+
39
+ def test_boolean_defaults_to_off
40
+ ENV.delete KEY
41
+ refute ENV.on?(KEY)
42
+ refute ENV.yes?(KEY)
43
+ refute ENV.enabled?(KEY)
44
+ assert ENV.off?(KEY)
45
+ assert ENV.no?(KEY)
46
+ assert ENV.disabled?(KEY)
47
+ end
48
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require_relative '../../bootstrap/unit'
3
+ require 'golly-utils/ruby_ext/hash_combinations'
4
+
5
+ class HashCombinationsTest < MiniTest::Unit::TestCase
6
+
7
+ def test_empty
8
+ assert_equal [{}], {}.value_combinations
9
+ end
10
+
11
+ def test_array1_single2
12
+ x= {a:false, b:[1,2], c:'true'}
13
+ assert_arrays [{a:false,b:1,c:'true'},{a:false,b:2,c:'true'}], x.value_combinations
14
+ end
15
+
16
+ def test_array2_single1
17
+ x= {a:%w[a b c], b:[1,2], c:'true', d:nil}
18
+ assert_arrays [
19
+ {a:'a', b:1, c:'true', d:nil},
20
+ {a:'b', b:1, c:'true', d:nil},
21
+ {a:'c', b:1, c:'true', d:nil},
22
+ {a:'a', b:2, c:'true', d:nil},
23
+ {a:'b', b:2, c:'true', d:nil},
24
+ {a:'c', b:2, c:'true', d:nil},
25
+ ], x.value_combinations
26
+ end
27
+
28
+ def assert_arrays(exp, act)
29
+ assert_equal exp.sort_by(&:to_s), act.sort_by(&:to_s)
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require_relative '../../bootstrap/unit'
3
+ require 'golly-utils/ruby_ext/subclasses'
4
+
5
+ class SubclassesTest < MiniTest::Unit::TestCase
6
+ class A; end
7
+ class B < A; end
8
+ class C < A; end
9
+ class B1 < B; end
10
+ class B2 < B; end
11
+
12
+ def assert_arrays(exp,act)
13
+ v= [exp,act].map{|a| a.sort_by(&:to_s) }
14
+ assert_equal *v
15
+ end
16
+
17
+ def test_all_subclasses
18
+ assert_arrays [B,C,B1,B2], A.subclasses(true)
19
+ end
20
+
21
+ def test_leaf_nodes_only
22
+ assert_arrays [C,B1,B2], A.subclasses
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: golly-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Barri
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This contains a bunch of shared, utility code that has no external dependencies
15
+ apart from Ruby stdlib.
16
+ email:
17
+ - japgolly@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - .rspec
24
+ - .simplecov
25
+ - .travis.yml
26
+ - .yardopts
27
+ - CHANGELOG.md
28
+ - Gemfile
29
+ - Gemfile.corvid
30
+ - Gemfile.lock
31
+ - Guardfile
32
+ - README.md
33
+ - RELEASE.md
34
+ - Rakefile
35
+ - bin/guard
36
+ - bin/rake
37
+ - bin/rspec
38
+ - bin/yard
39
+ - bin/yardoc
40
+ - bin/yri
41
+ - golly-utils.gemspec
42
+ - lib/golly-utils/attr_declarative.rb
43
+ - lib/golly-utils/callbacks.rb
44
+ - lib/golly-utils/child_process.rb
45
+ - lib/golly-utils/colourer.rb
46
+ - lib/golly-utils/delegator.rb
47
+ - lib/golly-utils/multi_io.rb
48
+ - lib/golly-utils/ruby_ext.rb
49
+ - lib/golly-utils/ruby_ext/array_to_hash.rb
50
+ - lib/golly-utils/ruby_ext/deep_dup.rb
51
+ - lib/golly-utils/ruby_ext/env_helpers.rb
52
+ - lib/golly-utils/ruby_ext/hash_combinations.rb
53
+ - lib/golly-utils/ruby_ext/pretty_error_messages.rb
54
+ - lib/golly-utils/ruby_ext/subclasses.rb
55
+ - lib/golly-utils/test/spec/deferrable_specs.rb
56
+ - lib/golly-utils/test/spec/within_time.rb
57
+ - lib/golly-utils/version.rb
58
+ - test/bootstrap/all.rb
59
+ - test/bootstrap/spec.rb
60
+ - test/bootstrap/unit.rb
61
+ - test/spec/child_process_mock_target.rb
62
+ - test/spec/child_process_spec.rb
63
+ - test/unit/attr_declarative_test.rb
64
+ - test/unit/callbacks_test.rb
65
+ - test/unit/delegator_test.rb
66
+ - test/unit/multi_io_test.rb
67
+ - test/unit/ruby_ext/env_helpers_test.rb
68
+ - test/unit/ruby_ext/hash_combinations_test.rb
69
+ - test/unit/ruby_ext/subclasses_test.rb
70
+ homepage: https://github.com/japgolly/golly-utils
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.24
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: ! 'Golly''s Utils: Reusable Ruby utility code.'
94
+ test_files:
95
+ - test/bootstrap/all.rb
96
+ - test/bootstrap/spec.rb
97
+ - test/bootstrap/unit.rb
98
+ - test/spec/child_process_mock_target.rb
99
+ - test/spec/child_process_spec.rb
100
+ - test/unit/attr_declarative_test.rb
101
+ - test/unit/callbacks_test.rb
102
+ - test/unit/delegator_test.rb
103
+ - test/unit/multi_io_test.rb
104
+ - test/unit/ruby_ext/env_helpers_test.rb
105
+ - test/unit/ruby_ext/hash_combinations_test.rb
106
+ - test/unit/ruby_ext/subclasses_test.rb
107
+ has_rdoc: