override_kernel 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -23,11 +23,11 @@ Currently, only the exit function is dealt with in such a manner.
23
23
  Simply use the following call to start brushing the exit function off like it's
24
24
  a harmless pest:
25
25
 
26
- start_override_exit
26
+ OverrideKernel.start_override_exit
27
27
 
28
28
  This line will gladly put it back in your way:
29
29
 
30
- stop_override_exit
30
+ OverrideKernel.stop_override_exit
31
31
 
32
32
  You can check the state of the application by
33
33
  checking OverrideKernel.override_exit.
@@ -38,8 +38,8 @@ module Kernel
38
38
  # (A call to exit will cause testing to stop otherwise).
39
39
  alias :old_exit :exit
40
40
  def exit(status = true)
41
- old_exit(status) unless OverrideKernel.override_exit
42
41
  OverrideKernel::AppState.state = :dead
42
+ old_exit(status) unless OverrideKernel.override_exit
43
43
  return status
44
44
  end
45
45
  end
@@ -35,26 +35,12 @@
35
35
 
36
36
  module OverrideKernel
37
37
 
38
- # Contains a list of methods that will be dynamically handled by the module.
39
- # The following methods will be created:
40
- #
41
- # :start_#{method_name} #=> @method_name = true
42
- # :stop_#{method_name} #=> @method_name = false
43
- # :#{method_name} #=> Returns the value of @method_name
44
- METHOD_LIST = [
45
- 'override_exit',
46
- ]
47
-
48
38
  # This constant is used to indicate whether the application would have
49
39
  # been halted by Kernel functions.
50
40
  AppState = StateManager.new(:alive, [:alive, :dead])
51
41
 
52
- ############################################################################
53
- module_function
54
- ############################################################################
55
-
56
- def method_missing(method, *args, &block)
57
- if METHOD_LIST.include?(method.to_s.sub(/^(start|stop)_/, ''))
42
+ def self.method_missing(method, *args, &block)
43
+ if method_list.include?(method.to_s.sub(/^(start|stop)_/, '').to_sym)
58
44
  var = '@' + method.to_s.sub(/^(start|stop)_/, '')
59
45
  case method.to_s
60
46
  when /^start/
@@ -62,6 +48,7 @@ module OverrideKernel
62
48
  when /^stop/
63
49
  send :instance_variable_set, var.to_sym, false
64
50
  when var[1..-1]
51
+ return nil unless instance_variables.include?(var.to_sym)
65
52
  send :instance_variable_get, var.to_sym
66
53
  end
67
54
  else
@@ -69,11 +56,28 @@ module OverrideKernel
69
56
  end
70
57
  end
71
58
 
72
- def respond_to_missing?(method, include_private)
73
- if METHOD_LIST.include?(method.to_s.sub(/^(start|stop)_/, ''))
59
+ def self.respond_to_missing?(method, include_private)
60
+ if method_list.include?(method.to_s.sub(/^(start|stop)_/, '').to_sym)
74
61
  return true
75
62
  else
76
63
  super
77
64
  end
78
65
  end
66
+
67
+ class << self
68
+ private
69
+ # Returns a list of methods that will be dynamically handled by the module.
70
+ # The following methods will be created:
71
+ #
72
+ # :start_#{method_name} #=> @method_name = true
73
+ # :stop_#{method_name} #=> @method_name = false
74
+ # :#{method_name} #=> Returns the value of @method_name
75
+ # ==== Output
76
+ # [Array] An array of base method names.
77
+ def method_list
78
+ [
79
+ :override_exit,
80
+ ]
81
+ end
82
+ end
79
83
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'override_kernel'
3
- s.version = '0.0.1'
3
+ s.version = '1.0.0'
4
4
 
5
5
  s.summary = 'Overrides specific Kernel functions.'
6
6
  s.description = %Q{
data/test/exit_test.rb CHANGED
@@ -46,6 +46,7 @@ class ExitTest < Test::Unit::TestCase
46
46
  def test_without_override
47
47
  override_exit(false)
48
48
  assert_raise(SystemExit) { exit }
49
+ assert_dead
49
50
  end
50
51
 
51
52
  def test_basic_exit_call
@@ -0,0 +1,3 @@
1
+ class TestClass
2
+ include OverrideKernel
3
+ end
@@ -0,0 +1,3 @@
1
+ module TestModule
2
+ include OverrideKernel
3
+ end
@@ -33,21 +33,110 @@
33
33
  require_relative 'require'
34
34
 
35
35
  class OverrideKernelTest < Test::Unit::TestCase
36
- METHOD_LIST = [
37
- :override_exit,
38
- ]
36
+ ITEMS = {
37
+ :method_list => [:override_exit,],
38
+ :constants => {
39
+ :AppState => StateManager.new(:alive, [:alive, :dead]),
40
+ },
41
+ }
42
+
43
+ def setup
44
+ @module = OverrideKernel
45
+ @module.instance_eval do
46
+ instance_variables.each do |var|
47
+ remove_instance_variable var
48
+ end
49
+ end
50
+ super
51
+ end
52
+
53
+ def test_constants
54
+ ITEMS[:constants].each_key do |constant|
55
+ assert_equal strip_id(ITEMS[:constants][constant]),
56
+ strip_id(@module.const_get(constant))
57
+ end
58
+ end
59
+
60
+ def test_method_list
61
+ assert_equal ITEMS[:method_list], @module.send(:method_list)
62
+ end
39
63
 
40
64
  def test_all_methods
41
- METHOD_LIST.each do |method|
65
+ ITEMS[:method_list].each do |method|
66
+ assert_equal nil, @module.send(method)
67
+
42
68
  {
43
69
  "start_#{method}".to_sym => true,
44
70
  "stop_#{method}".to_sym => false,
45
71
  }.each do |method_name, value|
46
- assert_respond_to OverrideKernel, method_name
47
- assert_nothing_raised { OverrideKernel.send method_name }
48
- assert_respond_to OverrideKernel, method
49
- assert_equal value, OverrideKernel.send(method)
72
+ assert_respond_to @module, method_name
73
+ assert_nothing_raised { @module.send method_name }
74
+ assert_respond_to @module, method
75
+ assert_equal value, @module.send(method)
76
+ end
77
+ end
78
+
79
+ assert_no_response @module, :method_list, false
80
+ end
81
+
82
+ def test_module_include
83
+ ITEMS[:method_list].each do |method|
84
+ assert_no_response TestModule, method
85
+
86
+ [
87
+ "start_#{method}".to_sym,
88
+ "stop_#{method}".to_sym,
89
+ ].each do |meth|
90
+ assert_no_response TestModule, meth
50
91
  end
51
92
  end
93
+
94
+ ITEMS[:constants].each_key do |constant|
95
+ assert_equal strip_id(ITEMS[:constants][constant]),
96
+ strip_id(TestModule.const_get(constant))
97
+ end
98
+
99
+ assert_no_response TestModule, :method_list
100
+ end
101
+
102
+ def test_class_include
103
+ obj = TestClass.new
104
+ ITEMS[:method_list].each do |method|
105
+ assert_no_response TestClass, method
106
+ assert_no_response obj, method
107
+
108
+ [
109
+ "start_#{method}".to_sym,
110
+ "stop_#{method}".to_sym,
111
+ ].each do |meth|
112
+ assert_no_response TestClass, meth
113
+ assert_no_response obj, meth
114
+ end
115
+ end
116
+
117
+ ITEMS[:constants].each_key do |constant|
118
+ assert_equal strip_id(ITEMS[:constants][constant]),
119
+ strip_id(TestClass.const_get(constant))
120
+ end
121
+
122
+ assert_no_response TestClass, :method_list
123
+ assert_no_response obj, :method_list
124
+ end
125
+
126
+ ############################################################################
127
+ private
128
+ ############################################################################
129
+
130
+ def strip_id(object)
131
+ return object unless object.respond_to?(:pretty_inspect) &&
132
+ object.pretty_inspect.match(/^#</)
133
+ object.pretty_inspect.sub(/:\w+? /, ' ')
134
+ end
135
+
136
+ def assert_no_response(obj, method, include_private = true, msg = nil)
137
+ msg = message(msg) {
138
+ "Did not expect #{mu_pp(obj)} (#{obj.class}) to respond to ##{method}"
139
+ }
140
+ assert !obj.respond_to?(method, include_private), msg
52
141
  end
53
142
  end
data/test/require.rb CHANGED
@@ -3,3 +3,6 @@ Bundler.require :test
3
3
  require 'test/unit'
4
4
 
5
5
  require_relative '../lib/override_kernel'
6
+
7
+ require_relative File.join('lib', 'test_module')
8
+ require_relative File.join('lib', 'test_class')
metadata CHANGED
@@ -3,10 +3,10 @@ name: override_kernel
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
+ - 1
6
7
  - 0
7
8
  - 0
8
- - 1
9
- version: 0.0.1
9
+ version: 1.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis Herrick
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-08-10 00:00:00 -04:00
17
+ date: 2011-08-15 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -68,6 +68,8 @@ files:
68
68
  - rakefile
69
69
  - README
70
70
  - test/override_kernel_test.rb
71
+ - test/lib/test_module.rb
72
+ - test/lib/test_class.rb
71
73
  - test/require.rb
72
74
  - test/exit_test.rb
73
75
  has_rdoc: true
@@ -84,7 +86,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
86
  requirements:
85
87
  - - ">="
86
88
  - !ruby/object:Gem::Version
87
- hash: -3169159202290470423
89
+ hash: -1613111938212231117
88
90
  segments:
89
91
  - 0
90
92
  version: "0"
@@ -105,5 +107,7 @@ specification_version: 3
105
107
  summary: Overrides specific Kernel functions.
106
108
  test_files:
107
109
  - test/override_kernel_test.rb
110
+ - test/lib/test_module.rb
111
+ - test/lib/test_class.rb
108
112
  - test/require.rb
109
113
  - test/exit_test.rb