app_mode 0.0.4 → 1.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.
- data/README +15 -15
- data/app_mode.gemspec +2 -2
- data/lib/app_mode/app_mode.rb +12 -110
- data/lib/app_mode/state_manager.rb +147 -0
- data/lib/app_mode.rb +1 -0
- data/test/app_mode_test.rb +5 -208
- data/test/lib/{app_mode.rb → state_manager.rb} +1 -1
- data/test/lib/{app_mode_support.rb → state_manager_support.rb} +1 -1
- data/test/require.rb +3 -3
- data/test/state_manager_test.rb +265 -0
- metadata +11 -8
data/README
CHANGED
@@ -30,7 +30,7 @@ There are two ways to use this gem.
|
|
30
30
|
|
31
31
|
See notes below on the :dynamic state, which is the default.
|
32
32
|
|
33
|
-
my_mode =
|
33
|
+
my_mode = StateManager.new
|
34
34
|
|
35
35
|
my_mode.state = :test
|
36
36
|
|
@@ -44,12 +44,12 @@ There are two ways to use this gem.
|
|
44
44
|
|
45
45
|
Or specify the state you want to use when initializing the object:
|
46
46
|
|
47
|
-
my_mode =
|
47
|
+
my_mode = StateManager.new(:development)
|
48
48
|
|
49
49
|
If you would like to use states other than those provided, you are free
|
50
50
|
to do so:
|
51
51
|
|
52
|
-
my_mode =
|
52
|
+
my_mode = StateManager.new(:blue, [:red, :yellow, :green, :blue])
|
53
53
|
|
54
54
|
my_mode.state = :yellow
|
55
55
|
|
@@ -59,7 +59,7 @@ There are two ways to use this gem.
|
|
59
59
|
|
60
60
|
The :dynamic state is still valid with custom states:
|
61
61
|
|
62
|
-
my_mode =
|
62
|
+
my_mode = StateManager.new(:dynamic, [:red, :yellow, :green, :blue])
|
63
63
|
|
64
64
|
2. Use the class methods:
|
65
65
|
This method is really only recommended for the end application. The initial
|
@@ -86,18 +86,18 @@ to your gem or anyone else's.
|
|
86
86
|
The following method is recommended for use in libraries or gems:
|
87
87
|
|
88
88
|
module MyModule
|
89
|
-
MyModuleMode =
|
89
|
+
MyModuleMode = StateManager.new
|
90
90
|
end
|
91
91
|
|
92
92
|
or
|
93
93
|
|
94
94
|
class MyClass
|
95
|
-
MyClassMode =
|
95
|
+
MyClassMode = StateManager.new
|
96
96
|
end
|
97
97
|
|
98
98
|
== A Word on States
|
99
99
|
|
100
|
-
*
|
100
|
+
* StateManager assumes the following default "states":
|
101
101
|
|
102
102
|
:development
|
103
103
|
|
@@ -107,20 +107,20 @@ or
|
|
107
107
|
|
108
108
|
:production
|
109
109
|
|
110
|
-
* There is a fifth state (:dynamic), which will tell
|
110
|
+
* There is a fifth state (:dynamic), which will tell StateManager to determine
|
111
111
|
the state by examining the stack trace. It is only available when initializing
|
112
|
-
a new
|
112
|
+
a new StateManager object and assumes that the first state is 'development',
|
113
113
|
the second is 'test', the third is 'rake', and the last one (not the fourth)
|
114
114
|
is 'production'. If less than four states are specified, the last one will be
|
115
115
|
used for multiple states. For example, if the states specified are
|
116
116
|
[:orange, :apple, :grape], :grape will be used for both rake and production.
|
117
117
|
|
118
|
-
* Only states in the list when the
|
118
|
+
* Only states in the list when the StateManager object were created are valid.
|
119
119
|
For example, the following code will generate errors:
|
120
120
|
|
121
|
-
my_mode =
|
121
|
+
my_mode = StateManager.new(:blue, [:red, :green]) #=> RuntimeError: Invalid environment setting: 'blue'.
|
122
122
|
|
123
|
-
my_mode =
|
123
|
+
my_mode = StateManager.new(:dynamic, [:red, :green])
|
124
124
|
|
125
125
|
my_mode.state = :development #=> RuntimeError: Invalid environment setting: 'development'.
|
126
126
|
|
@@ -132,7 +132,7 @@ or
|
|
132
132
|
|
133
133
|
Check for inclusion in the array of valid states:
|
134
134
|
|
135
|
-
my_mode =
|
135
|
+
my_mode = StateManager.new(:red, [:red, :green])
|
136
136
|
|
137
137
|
if my_mode.valid_states.include?(:purple) && my_mode.purple
|
138
138
|
# Purple code.
|
@@ -140,7 +140,7 @@ or
|
|
140
140
|
|
141
141
|
Use equivalency:
|
142
142
|
|
143
|
-
my_mode =
|
143
|
+
my_mode = StateManager.new(:red, [:red, :green])
|
144
144
|
|
145
145
|
if my_mode.state == :purple
|
146
146
|
# Purple code.
|
@@ -148,7 +148,7 @@ or
|
|
148
148
|
|
149
149
|
* If you are compelled to change the states for the global AppMode class,
|
150
150
|
that may be done by using the setup method, which accepts the same
|
151
|
-
arguments as
|
151
|
+
arguments as StateManager.new. Passing in a state of :dynamic will allow it
|
152
152
|
to set the state as it is intended.
|
153
153
|
|
154
154
|
* Although this document explains how to do so,
|
data/app_mode.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'app_mode'
|
3
|
-
s.version = '0.0
|
3
|
+
s.version = '1.0.0'
|
4
4
|
|
5
|
-
s.summary = '
|
5
|
+
s.summary = 'State management for objects and applications.'
|
6
6
|
s.description = 'AppMode provides state management for ' +
|
7
7
|
'modules, classes, libraries, and applications.'
|
8
8
|
|
data/lib/app_mode/app_mode.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
# This file contains a class to manage information about the mode
|
2
|
-
# executing code is running in.
|
1
|
+
# This file contains a global class to manage information about the mode
|
2
|
+
# that the executing code is running in.
|
3
3
|
|
4
4
|
#--
|
5
5
|
################################################################################
|
@@ -34,19 +34,24 @@
|
|
34
34
|
#++
|
35
35
|
|
36
36
|
# This class manages the mode that the executing code is running in.
|
37
|
-
class AppMode
|
38
|
-
attr_reader :state, :valid_states
|
39
|
-
|
37
|
+
class AppMode < StateManager
|
40
38
|
# Tracks a global mode setting.
|
41
39
|
@@mode = nil
|
42
40
|
|
41
|
+
# Constructor.
|
42
|
+
def initialize(*args)
|
43
|
+
# AppMode.new should not be called.
|
44
|
+
raise NotImplementedError, "#{self.class.name}.new is not implemented. " +
|
45
|
+
"You probably want #{self.class.superclass.name}.new."
|
46
|
+
end
|
47
|
+
|
43
48
|
class << self
|
44
49
|
# Override the send method.
|
45
50
|
#
|
46
51
|
# This was implemented to cover the case where test is used as a state.
|
47
52
|
# In that case, the default behavior was to call the private
|
48
53
|
# test method from Kernel. This prevents that behavior in cases where a
|
49
|
-
# public method is available via method_missing in
|
54
|
+
# public method is available via method_missing in the parent class.
|
50
55
|
def send(method, *args)
|
51
56
|
return method_missing(method, *args) if respond_to_missing?(method, false)
|
52
57
|
super
|
@@ -54,7 +59,7 @@ class AppMode
|
|
54
59
|
|
55
60
|
# Initializes the global mode setting.
|
56
61
|
def setup(*args)
|
57
|
-
@@mode = self.new(*args)
|
62
|
+
@@mode = self.superclass.new(*args)
|
58
63
|
end
|
59
64
|
|
60
65
|
########################################################################
|
@@ -78,107 +83,4 @@ class AppMode
|
|
78
83
|
super
|
79
84
|
end
|
80
85
|
end
|
81
|
-
|
82
|
-
# Constructor.
|
83
|
-
# ==== Input
|
84
|
-
# [env : Symbol] The environment that the mode should be set to.
|
85
|
-
# [valid_states : Array : (:development, :test, :production)] Valid states.
|
86
|
-
# ==== Notes
|
87
|
-
# <tt>env</tt> must be a member of <tt>states</tt>.
|
88
|
-
# ==== Examples
|
89
|
-
# AppMode.new #=> <AppMode @state=:production, @valid_states=[:development, :test, :production]>
|
90
|
-
# AppMode.new(:test) #=> <AppMode @state=:test, @valid_states=[:development, :test, :production]>
|
91
|
-
# AppMode.new(:dev, [:abc, :dev]) #=> <AppMode @state=:dev, @valid_states=[:abc, :dev]>
|
92
|
-
def initialize(
|
93
|
-
state = :dynamic,
|
94
|
-
valid_states = [:development, :test, :rake, :production])
|
95
|
-
@state = state
|
96
|
-
@valid_states = valid_states
|
97
|
-
set_state @state
|
98
|
-
end
|
99
|
-
|
100
|
-
# Sets the environment instance variable.
|
101
|
-
# ==== Input
|
102
|
-
# [value : Symbol] The value that will be used for the environment.
|
103
|
-
def state=(value)
|
104
|
-
set_state value
|
105
|
-
end
|
106
|
-
|
107
|
-
# Override the send method.
|
108
|
-
#
|
109
|
-
# This was implemented to cover the case where test is used as a state.
|
110
|
-
# In that case, the default behavior was to call the private
|
111
|
-
# test method from Kernel. This prevents that behavior in cases where a
|
112
|
-
# public method is available via method_missing in this class.
|
113
|
-
def send(method, *args)
|
114
|
-
return method_missing(method, *args) if respond_to_missing?(method, false)
|
115
|
-
super
|
116
|
-
end
|
117
|
-
|
118
|
-
############################################################################
|
119
|
-
private
|
120
|
-
############################################################################
|
121
|
-
|
122
|
-
# Returns the appropriate state to use when setting the state dynamically.
|
123
|
-
# ==== Output
|
124
|
-
# [Symbol] The state that should be used when setting the state dynamically.
|
125
|
-
def dynamic_state
|
126
|
-
call = origin
|
127
|
-
return @valid_states[0] unless call.sub(/^\.\//, '').match(/\//)
|
128
|
-
|
129
|
-
case call
|
130
|
-
when /rake_test_loader\.rb/,
|
131
|
-
%r[/tests?/test_\w+?.rb], %r[/tests?/\w+?_test.rb]
|
132
|
-
return @valid_states[1]
|
133
|
-
when %r[/bin/rake]
|
134
|
-
return @valid_states[2]
|
135
|
-
end
|
136
|
-
|
137
|
-
return @valid_states.last
|
138
|
-
end
|
139
|
-
|
140
|
-
# Returns the first call in the stack.
|
141
|
-
# ==== Output
|
142
|
-
# [String] The file that made the first call in the stack.
|
143
|
-
# ==== Notes
|
144
|
-
# This method is overridden during tests.
|
145
|
-
def origin
|
146
|
-
caller.last
|
147
|
-
end
|
148
|
-
|
149
|
-
# Allows the getting of the mode.
|
150
|
-
# ==== Input
|
151
|
-
# [method : Symbol] The method that was called.
|
152
|
-
# [*args : Array] Any arguments that were passed in.
|
153
|
-
# [&block : Block] A block, if specified.
|
154
|
-
def method_missing(method, *args, &block)
|
155
|
-
return method == @state if respond_to_missing?(method, false)
|
156
|
-
super
|
157
|
-
end
|
158
|
-
|
159
|
-
# Ensure that the object knows what it can respond to via method_missing.
|
160
|
-
# ==== Input
|
161
|
-
# [method : Symbol] The method to check for a response to.
|
162
|
-
# [include_private : Boolean] Whether to include private methods.
|
163
|
-
# ==== Output
|
164
|
-
# [Boolean] Indicates whether the object will respond to the specified method.
|
165
|
-
def respond_to_missing?(method, include_private)
|
166
|
-
return true if @valid_states.include?(method)
|
167
|
-
super
|
168
|
-
end
|
169
|
-
|
170
|
-
# Sets the state.
|
171
|
-
# ==== Input
|
172
|
-
# [value : Symbol] The value to use for the state.
|
173
|
-
def set_state(value)
|
174
|
-
unless @valid_states.include?(value) || value == :dynamic
|
175
|
-
raise "Invalid environment setting: '#{value}'."
|
176
|
-
end
|
177
|
-
|
178
|
-
if value == :dynamic
|
179
|
-
@state = dynamic_state || @valid_states.last
|
180
|
-
else
|
181
|
-
@state = value
|
182
|
-
end
|
183
|
-
end
|
184
86
|
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# This file contains a class to manage information about the state of
|
2
|
+
# a module, class, object, or application.
|
3
|
+
|
4
|
+
#--
|
5
|
+
################################################################################
|
6
|
+
# Copyright (C) 2011 Travis Herrick #
|
7
|
+
################################################################################
|
8
|
+
# #
|
9
|
+
# \v^V,^!v\^/ #
|
10
|
+
# ~% %~ #
|
11
|
+
# { _ _ } #
|
12
|
+
# ( * - ) #
|
13
|
+
# | / | #
|
14
|
+
# \ _, / #
|
15
|
+
# \__.__/ #
|
16
|
+
# #
|
17
|
+
################################################################################
|
18
|
+
# This program is free software: you can redistribute it #
|
19
|
+
# and/or modify it under the terms of the GNU General Public License #
|
20
|
+
# as published by the Free Software Foundation, #
|
21
|
+
# either version 3 of the License, or (at your option) any later version. #
|
22
|
+
# #
|
23
|
+
# Commercial licensing may be available for a fee under a different license. #
|
24
|
+
################################################################################
|
25
|
+
# This program is distributed in the hope that it will be useful, #
|
26
|
+
# but WITHOUT ANY WARRANTY; #
|
27
|
+
# without even the implied warranty of MERCHANTABILITY #
|
28
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
29
|
+
# See the GNU General Public License for more details. #
|
30
|
+
# #
|
31
|
+
# You should have received a copy of the GNU General Public License #
|
32
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
33
|
+
################################################################################
|
34
|
+
#++
|
35
|
+
|
36
|
+
# This class assists in managing the state of
|
37
|
+
# a module, class, object, or application.
|
38
|
+
class StateManager
|
39
|
+
attr_reader :state, :valid_states
|
40
|
+
|
41
|
+
# Constructor.
|
42
|
+
# ==== Input
|
43
|
+
# [state : Symbol] The state that should be used initially.
|
44
|
+
#
|
45
|
+
# :dynamic is a special state that will determine the best
|
46
|
+
# state based on the environment that the code is running in.
|
47
|
+
# [valid_states : Array : (:development, :test, :production)] Valid states.
|
48
|
+
# ==== Notes
|
49
|
+
# <tt>env</tt> must be a member of <tt>states</tt>.
|
50
|
+
# ==== Examples
|
51
|
+
# StateManager.new #=> <StateManager @state=:rake, @valid_states=[:development, :test, :rake, :production]>
|
52
|
+
# StateManager.new(:test) #=> <StateManager @state=:test, @valid_states=[:development, :test, :rake, :production]>
|
53
|
+
# StateManager.new(:dev, [:abc, :dev]) #=> <StateManager @state=:dev, @valid_states=[:abc, :dev]>
|
54
|
+
def initialize(
|
55
|
+
state = :dynamic,
|
56
|
+
valid_states = [:development, :test, :rake, :production])
|
57
|
+
@state = state
|
58
|
+
@valid_states = valid_states
|
59
|
+
set_state @state
|
60
|
+
end
|
61
|
+
|
62
|
+
# Override the send method.
|
63
|
+
#
|
64
|
+
# This was implemented to cover the case where test is used as a state.
|
65
|
+
# In that case, the default behavior was to call the private
|
66
|
+
# test method from Kernel. This prevents that behavior in cases where a
|
67
|
+
# public method is available via method_missing in this class.
|
68
|
+
def send(method, *args)
|
69
|
+
return method_missing(method, *args) if respond_to_missing?(method, false)
|
70
|
+
super
|
71
|
+
end
|
72
|
+
|
73
|
+
# Sets the state instance variable.
|
74
|
+
# ==== Input
|
75
|
+
# [value : Symbol] The value that will be used for the state.
|
76
|
+
def state=(value)
|
77
|
+
set_state value
|
78
|
+
end
|
79
|
+
|
80
|
+
############################################################################
|
81
|
+
private
|
82
|
+
############################################################################
|
83
|
+
|
84
|
+
# Returns the appropriate state to use when setting the state dynamically.
|
85
|
+
# ==== Output
|
86
|
+
# [Symbol] The state that should be used when setting the state dynamically.
|
87
|
+
def dynamic_state
|
88
|
+
call = origin
|
89
|
+
return @valid_states[0] unless call.sub(/^\.\//, '').match(/\//)
|
90
|
+
|
91
|
+
case call
|
92
|
+
when /rake_test_loader\.rb/,
|
93
|
+
%r[/tests?/test_\w+?.rb], %r[/tests?/\w+?_test.rb]
|
94
|
+
return @valid_states[1]
|
95
|
+
when %r[/bin/rake]
|
96
|
+
return @valid_states[2]
|
97
|
+
end
|
98
|
+
|
99
|
+
return @valid_states.last
|
100
|
+
end
|
101
|
+
|
102
|
+
# Allows the getting of the state since valid states
|
103
|
+
# may be specified at run time.
|
104
|
+
# ==== Input
|
105
|
+
# [method : Symbol] The method that was called.
|
106
|
+
# [*args : Array] Any arguments that were passed in.
|
107
|
+
# [&block : Block] A block, if specified.
|
108
|
+
def method_missing(method, *args, &block)
|
109
|
+
return method == @state if respond_to_missing?(method, false)
|
110
|
+
super
|
111
|
+
end
|
112
|
+
|
113
|
+
# Returns the first call in the stack.
|
114
|
+
# ==== Output
|
115
|
+
# [String] The file that made the first call in the stack.
|
116
|
+
# ==== Notes
|
117
|
+
# This method is overridden during tests.
|
118
|
+
def origin
|
119
|
+
caller.last
|
120
|
+
end
|
121
|
+
|
122
|
+
# Ensure that the object knows what it can respond to via method_missing.
|
123
|
+
# ==== Input
|
124
|
+
# [method : Symbol] The method to check for a response to.
|
125
|
+
# [include_private : Boolean] Whether to include private methods.
|
126
|
+
# ==== Output
|
127
|
+
# [Boolean] Indicates whether the object will respond to the specified method.
|
128
|
+
def respond_to_missing?(method, include_private)
|
129
|
+
return true if @valid_states.include?(method)
|
130
|
+
super
|
131
|
+
end
|
132
|
+
|
133
|
+
# Sets the state.
|
134
|
+
# ==== Input
|
135
|
+
# [value : Symbol] The value to use for the state.
|
136
|
+
def set_state(value)
|
137
|
+
unless @valid_states.include?(value) || value == :dynamic
|
138
|
+
raise "Invalid environment setting: '#{value}'."
|
139
|
+
end
|
140
|
+
|
141
|
+
if value == :dynamic
|
142
|
+
@state = dynamic_state || @valid_states.last
|
143
|
+
else
|
144
|
+
@state = value
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
data/lib/app_mode.rb
CHANGED
data/test/app_mode_test.rb
CHANGED
@@ -33,217 +33,14 @@
|
|
33
33
|
require_relative 'require'
|
34
34
|
|
35
35
|
class AppModeTest < Test::Unit::TestCase
|
36
|
-
include
|
36
|
+
include StateManagerSupport
|
37
37
|
|
38
38
|
def setup
|
39
39
|
@class = AppMode
|
40
40
|
@class.setup
|
41
|
-
@obj = nil
|
42
41
|
super
|
43
42
|
end
|
44
43
|
|
45
|
-
def test_class_type
|
46
|
-
assert_equal AppMode, @class
|
47
|
-
end
|
48
|
-
|
49
|
-
############################################################################
|
50
|
-
# Instance tests.
|
51
|
-
############################################################################
|
52
|
-
|
53
|
-
def test_default_settings
|
54
|
-
create
|
55
|
-
|
56
|
-
assert_equal :production, @obj.state
|
57
|
-
assert_equal states(:default), @obj.valid_states
|
58
|
-
|
59
|
-
assert_true @obj.production
|
60
|
-
assert_false @obj.development
|
61
|
-
assert_false @obj.test
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_setting_state
|
65
|
-
create :test
|
66
|
-
|
67
|
-
assert_equal :test, @obj.state
|
68
|
-
assert_true @obj.test
|
69
|
-
|
70
|
-
assert_nothing_raised { @obj.state = :development }
|
71
|
-
|
72
|
-
assert_equal :development, @obj.state
|
73
|
-
assert_true @obj.development
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_new_object
|
77
|
-
create :test
|
78
|
-
assert_equal :test, @obj.state
|
79
|
-
assert_false @obj.development
|
80
|
-
assert_true @obj.test
|
81
|
-
assert_false @obj.rake
|
82
|
-
assert_false @obj.production
|
83
|
-
|
84
|
-
create :development
|
85
|
-
assert_equal :development, @obj.state
|
86
|
-
assert_true @obj.development
|
87
|
-
assert_false @obj.test
|
88
|
-
assert_false @obj.rake
|
89
|
-
assert_false @obj.production
|
90
|
-
|
91
|
-
create :production
|
92
|
-
assert_equal :production, @obj.state
|
93
|
-
assert_false @obj.development
|
94
|
-
assert_false @obj.test
|
95
|
-
assert_false @obj.rake
|
96
|
-
assert_true @obj.production
|
97
|
-
|
98
|
-
create :rake
|
99
|
-
assert_equal :rake, @obj.state
|
100
|
-
assert_false @obj.development
|
101
|
-
assert_false @obj.test
|
102
|
-
assert_true @obj.rake
|
103
|
-
assert_false @obj.production
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_dynamic_state
|
107
|
-
assert_nothing_raised { create :dynamic }
|
108
|
-
assert_false @obj.development
|
109
|
-
assert_false @obj.test
|
110
|
-
assert_false @obj.rake
|
111
|
-
assert_true @obj.production
|
112
|
-
|
113
|
-
assert_nothing_raised { create :dynamic, states(:dev) }
|
114
|
-
assert_true @obj.dev_dev
|
115
|
-
assert_false @obj.dev_test
|
116
|
-
assert_false @obj.dev_rake
|
117
|
-
assert_false @obj.dev_prod
|
118
|
-
|
119
|
-
assert_nothing_raised { create :dynamic, states(:test) }
|
120
|
-
assert_false @obj.test_dev
|
121
|
-
assert_true @obj.test_test
|
122
|
-
assert_false @obj.test_rake
|
123
|
-
assert_false @obj.test_prod
|
124
|
-
|
125
|
-
assert_nothing_raised { create :dynamic, states(:rake) }
|
126
|
-
assert_false @obj.rake_dev
|
127
|
-
assert_false @obj.rake_test
|
128
|
-
assert_true @obj.rake_rake
|
129
|
-
assert_false @obj.rake_prod
|
130
|
-
|
131
|
-
assert_nothing_raised { create :dynamic, states(:prod) }
|
132
|
-
assert_false @obj.prod_dev
|
133
|
-
assert_false @obj.prod_test
|
134
|
-
assert_false @obj.prod_rake
|
135
|
-
assert_true @obj.prod_prod
|
136
|
-
|
137
|
-
assert_raise(NoMethodError) { @obj.dynamic }
|
138
|
-
assert_raise(NoMethodError) { @obj.test }
|
139
|
-
|
140
|
-
assert_nothing_raised { create :dynamic, states(:test_test_class) }
|
141
|
-
assert_false @obj.test_test_class_dev
|
142
|
-
assert_true @obj.test_ttc
|
143
|
-
assert_false @obj.rake
|
144
|
-
assert_false @obj.prod
|
145
|
-
|
146
|
-
assert_nothing_raised { create :dynamic, states(:tests_test_class) }
|
147
|
-
assert_false @obj.tests_test_class_dev
|
148
|
-
assert_true @obj.test_stc
|
149
|
-
assert_false @obj.rake
|
150
|
-
assert_false @obj.prod
|
151
|
-
|
152
|
-
assert_nothing_raised { create :dynamic, states(:test_class_test) }
|
153
|
-
assert_false @obj.test_class_test_dev
|
154
|
-
assert_true @obj.test_tct
|
155
|
-
assert_false @obj.rake
|
156
|
-
assert_false @obj.prod
|
157
|
-
|
158
|
-
assert_nothing_raised { create :dynamic, states(:tests_class_test) }
|
159
|
-
assert_false @obj.tests_class_test_dev
|
160
|
-
assert_true @obj.test_sct
|
161
|
-
assert_false @obj.rake
|
162
|
-
assert_false @obj.prod
|
163
|
-
end
|
164
|
-
|
165
|
-
def test_dynamic_state_with_less_than_ideal_number_of_states
|
166
|
-
[
|
167
|
-
:dev,
|
168
|
-
:test,
|
169
|
-
:rake,
|
170
|
-
:prod,
|
171
|
-
].each do |state|
|
172
|
-
state_array = states(state)[0..3]
|
173
|
-
end_value = state_array.length - 1
|
174
|
-
|
175
|
-
(0..end_value).each do |i|
|
176
|
-
assert_nothing_raised { create :dynamic, states(state)[0..i] }
|
177
|
-
case i
|
178
|
-
when 0
|
179
|
-
assert_true @obj.send("#{state}_dev")
|
180
|
-
when 1
|
181
|
-
case state
|
182
|
-
when :dev
|
183
|
-
assert_true @obj.dev_dev
|
184
|
-
else
|
185
|
-
assert_true @obj.send("#{state}_test")
|
186
|
-
end
|
187
|
-
when 2
|
188
|
-
case state
|
189
|
-
when :dev
|
190
|
-
assert_true @obj.dev_dev
|
191
|
-
when :test
|
192
|
-
assert_true @obj.test_test
|
193
|
-
else
|
194
|
-
assert_true @obj.send("#{state}_rake")
|
195
|
-
end
|
196
|
-
when 3
|
197
|
-
case state
|
198
|
-
when :dev
|
199
|
-
assert_true @obj.dev_dev
|
200
|
-
when :test
|
201
|
-
assert_true @obj.test_test
|
202
|
-
when :rake
|
203
|
-
assert_true @obj.rake_rake
|
204
|
-
when :prod
|
205
|
-
assert_true @obj.prod_prod
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
def test_invalid_state
|
213
|
-
assert_raise(RuntimeError) { create :invalid }
|
214
|
-
assert_raise(NoMethodError) { create :test; @obj.invalid }
|
215
|
-
end
|
216
|
-
|
217
|
-
def test_respond_to
|
218
|
-
create :test
|
219
|
-
|
220
|
-
method_list.each_value do |method|
|
221
|
-
assert_respond_to @obj, method
|
222
|
-
end
|
223
|
-
|
224
|
-
states(:default).each do |method|
|
225
|
-
assert_respond_to @obj, method
|
226
|
-
end
|
227
|
-
|
228
|
-
assert_not_respond_to @obj, :invalid
|
229
|
-
end
|
230
|
-
|
231
|
-
# This test exists because of Kernel::test.
|
232
|
-
def test_send_method
|
233
|
-
create :test
|
234
|
-
assert_equal :test, @obj.send(:state)
|
235
|
-
assert_false @obj.send(:development)
|
236
|
-
assert_true @obj.send(:test)
|
237
|
-
assert_false @obj.send(:rake)
|
238
|
-
assert_false @obj.send(:production)
|
239
|
-
|
240
|
-
create :development, [:development, :production]
|
241
|
-
assert_equal :development, @obj.send(:state)
|
242
|
-
assert_true @obj.send(:development)
|
243
|
-
assert_false @obj.send(:production)
|
244
|
-
assert_raise(ArgumentError) { @obj.send(:test) }
|
245
|
-
end
|
246
|
-
|
247
44
|
############################################################################
|
248
45
|
# Class tests.
|
249
46
|
############################################################################
|
@@ -305,14 +102,14 @@ class AppModeTest < Test::Unit::TestCase
|
|
305
102
|
assert_raise(ArgumentError) { @class.send(:test) }
|
306
103
|
end
|
307
104
|
|
105
|
+
def test_initialize_object_should_fail
|
106
|
+
assert_raise(NotImplementedError) { @class.new }
|
107
|
+
end
|
108
|
+
|
308
109
|
############################################################################
|
309
110
|
private
|
310
111
|
############################################################################
|
311
112
|
|
312
|
-
def create(*args)
|
313
|
-
@obj = @class.new(*args)
|
314
|
-
end
|
315
|
-
|
316
113
|
############################################################################
|
317
114
|
# Assertions - stolen from test_internals.
|
318
115
|
############################################################################
|
@@ -32,7 +32,7 @@
|
|
32
32
|
################################################################################
|
33
33
|
#++
|
34
34
|
|
35
|
-
class
|
35
|
+
class StateManager
|
36
36
|
############################################################################
|
37
37
|
private
|
38
38
|
############################################################################
|
@@ -36,7 +36,7 @@
|
|
36
36
|
# This module is provided to be used as an include in any necessary class.
|
37
37
|
#
|
38
38
|
# This serves as a bridge between test code and application code.
|
39
|
-
module
|
39
|
+
module StateManagerSupport
|
40
40
|
# These are things that will be used throughout testing in multiple locations.
|
41
41
|
ITEMS = {
|
42
42
|
:states => {
|
data/test/require.rb
CHANGED
@@ -2,7 +2,7 @@ Bundler.require :test
|
|
2
2
|
|
3
3
|
require 'test/unit'
|
4
4
|
|
5
|
-
require_relative '../lib/app_mode
|
5
|
+
require_relative '../lib/app_mode'
|
6
6
|
|
7
|
-
require_relative 'lib/
|
8
|
-
require_relative 'lib/
|
7
|
+
require_relative 'lib/state_manager_support'
|
8
|
+
require_relative 'lib/state_manager'
|
@@ -0,0 +1,265 @@
|
|
1
|
+
#--
|
2
|
+
################################################################################
|
3
|
+
# Copyright (C) 2011 Travis Herrick #
|
4
|
+
################################################################################
|
5
|
+
# #
|
6
|
+
# \v^V,^!v\^/ #
|
7
|
+
# ~% %~ #
|
8
|
+
# { _ _ } #
|
9
|
+
# ( * - ) #
|
10
|
+
# | / | #
|
11
|
+
# \ _, / #
|
12
|
+
# \__.__/ #
|
13
|
+
# #
|
14
|
+
################################################################################
|
15
|
+
# This program is free software: you can redistribute it #
|
16
|
+
# and/or modify it under the terms of the GNU General Public License #
|
17
|
+
# as published by the Free Software Foundation, #
|
18
|
+
# either version 3 of the License, or (at your option) any later version. #
|
19
|
+
# #
|
20
|
+
# Commercial licensing may be available for a fee under a different license. #
|
21
|
+
################################################################################
|
22
|
+
# This program is distributed in the hope that it will be useful, #
|
23
|
+
# but WITHOUT ANY WARRANTY; #
|
24
|
+
# without even the implied warranty of MERCHANTABILITY #
|
25
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
26
|
+
# See the GNU General Public License for more details. #
|
27
|
+
# #
|
28
|
+
# You should have received a copy of the GNU General Public License #
|
29
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
30
|
+
################################################################################
|
31
|
+
#++
|
32
|
+
|
33
|
+
require_relative 'require'
|
34
|
+
|
35
|
+
class StateManagerTest < Test::Unit::TestCase
|
36
|
+
include StateManagerSupport
|
37
|
+
|
38
|
+
def setup
|
39
|
+
@class = StateManager
|
40
|
+
@obj = nil
|
41
|
+
super
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_default_settings
|
45
|
+
create
|
46
|
+
|
47
|
+
assert_equal :production, @obj.state
|
48
|
+
assert_equal states(:default), @obj.valid_states
|
49
|
+
|
50
|
+
assert_true @obj.production
|
51
|
+
assert_false @obj.development
|
52
|
+
assert_false @obj.test
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_setting_state
|
56
|
+
create :test
|
57
|
+
|
58
|
+
assert_equal :test, @obj.state
|
59
|
+
assert_true @obj.test
|
60
|
+
|
61
|
+
assert_nothing_raised { @obj.state = :development }
|
62
|
+
|
63
|
+
assert_equal :development, @obj.state
|
64
|
+
assert_true @obj.development
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_new_object
|
68
|
+
create :test
|
69
|
+
assert_equal :test, @obj.state
|
70
|
+
assert_false @obj.development
|
71
|
+
assert_true @obj.test
|
72
|
+
assert_false @obj.rake
|
73
|
+
assert_false @obj.production
|
74
|
+
|
75
|
+
create :development
|
76
|
+
assert_equal :development, @obj.state
|
77
|
+
assert_true @obj.development
|
78
|
+
assert_false @obj.test
|
79
|
+
assert_false @obj.rake
|
80
|
+
assert_false @obj.production
|
81
|
+
|
82
|
+
create :production
|
83
|
+
assert_equal :production, @obj.state
|
84
|
+
assert_false @obj.development
|
85
|
+
assert_false @obj.test
|
86
|
+
assert_false @obj.rake
|
87
|
+
assert_true @obj.production
|
88
|
+
|
89
|
+
create :rake
|
90
|
+
assert_equal :rake, @obj.state
|
91
|
+
assert_false @obj.development
|
92
|
+
assert_false @obj.test
|
93
|
+
assert_true @obj.rake
|
94
|
+
assert_false @obj.production
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_dynamic_state
|
98
|
+
assert_nothing_raised { create :dynamic }
|
99
|
+
assert_false @obj.development
|
100
|
+
assert_false @obj.test
|
101
|
+
assert_false @obj.rake
|
102
|
+
assert_true @obj.production
|
103
|
+
|
104
|
+
assert_nothing_raised { create :dynamic, states(:dev) }
|
105
|
+
assert_true @obj.dev_dev
|
106
|
+
assert_false @obj.dev_test
|
107
|
+
assert_false @obj.dev_rake
|
108
|
+
assert_false @obj.dev_prod
|
109
|
+
|
110
|
+
assert_nothing_raised { create :dynamic, states(:test) }
|
111
|
+
assert_false @obj.test_dev
|
112
|
+
assert_true @obj.test_test
|
113
|
+
assert_false @obj.test_rake
|
114
|
+
assert_false @obj.test_prod
|
115
|
+
|
116
|
+
assert_nothing_raised { create :dynamic, states(:rake) }
|
117
|
+
assert_false @obj.rake_dev
|
118
|
+
assert_false @obj.rake_test
|
119
|
+
assert_true @obj.rake_rake
|
120
|
+
assert_false @obj.rake_prod
|
121
|
+
|
122
|
+
assert_nothing_raised { create :dynamic, states(:prod) }
|
123
|
+
assert_false @obj.prod_dev
|
124
|
+
assert_false @obj.prod_test
|
125
|
+
assert_false @obj.prod_rake
|
126
|
+
assert_true @obj.prod_prod
|
127
|
+
|
128
|
+
assert_raise(NoMethodError) { @obj.dynamic }
|
129
|
+
assert_raise(NoMethodError) { @obj.test }
|
130
|
+
|
131
|
+
assert_nothing_raised { create :dynamic, states(:test_test_class) }
|
132
|
+
assert_false @obj.test_test_class_dev
|
133
|
+
assert_true @obj.test_ttc
|
134
|
+
assert_false @obj.rake
|
135
|
+
assert_false @obj.prod
|
136
|
+
|
137
|
+
assert_nothing_raised { create :dynamic, states(:tests_test_class) }
|
138
|
+
assert_false @obj.tests_test_class_dev
|
139
|
+
assert_true @obj.test_stc
|
140
|
+
assert_false @obj.rake
|
141
|
+
assert_false @obj.prod
|
142
|
+
|
143
|
+
assert_nothing_raised { create :dynamic, states(:test_class_test) }
|
144
|
+
assert_false @obj.test_class_test_dev
|
145
|
+
assert_true @obj.test_tct
|
146
|
+
assert_false @obj.rake
|
147
|
+
assert_false @obj.prod
|
148
|
+
|
149
|
+
assert_nothing_raised { create :dynamic, states(:tests_class_test) }
|
150
|
+
assert_false @obj.tests_class_test_dev
|
151
|
+
assert_true @obj.test_sct
|
152
|
+
assert_false @obj.rake
|
153
|
+
assert_false @obj.prod
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_dynamic_state_with_less_than_ideal_number_of_states
|
157
|
+
[
|
158
|
+
:dev,
|
159
|
+
:test,
|
160
|
+
:rake,
|
161
|
+
:prod,
|
162
|
+
].each do |state|
|
163
|
+
state_array = states(state)[0..3]
|
164
|
+
end_value = state_array.length - 1
|
165
|
+
|
166
|
+
(0..end_value).each do |i|
|
167
|
+
assert_nothing_raised { create :dynamic, states(state)[0..i] }
|
168
|
+
case i
|
169
|
+
when 0
|
170
|
+
assert_true @obj.send("#{state}_dev")
|
171
|
+
when 1
|
172
|
+
case state
|
173
|
+
when :dev
|
174
|
+
assert_true @obj.dev_dev
|
175
|
+
else
|
176
|
+
assert_true @obj.send("#{state}_test")
|
177
|
+
end
|
178
|
+
when 2
|
179
|
+
case state
|
180
|
+
when :dev
|
181
|
+
assert_true @obj.dev_dev
|
182
|
+
when :test
|
183
|
+
assert_true @obj.test_test
|
184
|
+
else
|
185
|
+
assert_true @obj.send("#{state}_rake")
|
186
|
+
end
|
187
|
+
when 3
|
188
|
+
case state
|
189
|
+
when :dev
|
190
|
+
assert_true @obj.dev_dev
|
191
|
+
when :test
|
192
|
+
assert_true @obj.test_test
|
193
|
+
when :rake
|
194
|
+
assert_true @obj.rake_rake
|
195
|
+
when :prod
|
196
|
+
assert_true @obj.prod_prod
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_invalid_state
|
204
|
+
assert_raise(RuntimeError) { create :invalid }
|
205
|
+
assert_raise(NoMethodError) { create :test; @obj.invalid }
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_respond_to
|
209
|
+
create :test
|
210
|
+
|
211
|
+
method_list.each_value do |method|
|
212
|
+
assert_respond_to @obj, method
|
213
|
+
end
|
214
|
+
|
215
|
+
states(:default).each do |method|
|
216
|
+
assert_respond_to @obj, method
|
217
|
+
end
|
218
|
+
|
219
|
+
assert_not_respond_to @obj, :invalid
|
220
|
+
end
|
221
|
+
|
222
|
+
# This test exists because of Kernel::test.
|
223
|
+
def test_send_method
|
224
|
+
create :test
|
225
|
+
assert_equal :test, @obj.send(:state)
|
226
|
+
assert_false @obj.send(:development)
|
227
|
+
assert_true @obj.send(:test)
|
228
|
+
assert_false @obj.send(:rake)
|
229
|
+
assert_false @obj.send(:production)
|
230
|
+
|
231
|
+
create :development, [:development, :production]
|
232
|
+
assert_equal :development, @obj.send(:state)
|
233
|
+
assert_true @obj.send(:development)
|
234
|
+
assert_false @obj.send(:production)
|
235
|
+
assert_raise(ArgumentError) { @obj.send(:test) }
|
236
|
+
end
|
237
|
+
|
238
|
+
############################################################################
|
239
|
+
private
|
240
|
+
############################################################################
|
241
|
+
|
242
|
+
def create(*args)
|
243
|
+
@obj = @class.new(*args)
|
244
|
+
end
|
245
|
+
|
246
|
+
############################################################################
|
247
|
+
# Assertions - stolen from test_internals.
|
248
|
+
############################################################################
|
249
|
+
|
250
|
+
# Asserts that a value is equal to false.
|
251
|
+
# ==== Input
|
252
|
+
# [value : Any] The value to check for equality against false.
|
253
|
+
# [message : String : nil] The message to display if the value is not false.
|
254
|
+
def assert_false(value, message = nil)
|
255
|
+
assert_equal false, value, message
|
256
|
+
end
|
257
|
+
|
258
|
+
# Asserts that a value is equal to true.
|
259
|
+
# ==== Input
|
260
|
+
# [value : Any] The value to check for equality against true.
|
261
|
+
# [message : String : nil] The message to display if the value is not true.
|
262
|
+
def assert_true(value, message = nil)
|
263
|
+
assert_equal true, value, message
|
264
|
+
end
|
265
|
+
end
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: app_mode
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 1
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.4
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Travis Herrick
|
@@ -42,6 +42,7 @@ extra_rdoc_files:
|
|
42
42
|
- README
|
43
43
|
files:
|
44
44
|
- lib/app_mode/app_mode.rb
|
45
|
+
- lib/app_mode/state_manager.rb
|
45
46
|
- lib/app_mode.rb
|
46
47
|
- Gemfile.lock
|
47
48
|
- Gemfile
|
@@ -49,9 +50,10 @@ files:
|
|
49
50
|
- app_mode.gemspec
|
50
51
|
- README
|
51
52
|
- test/require.rb
|
53
|
+
- test/lib/state_manager.rb
|
54
|
+
- test/lib/state_manager_support.rb
|
52
55
|
- test/app_mode_test.rb
|
53
|
-
- test/
|
54
|
-
- test/lib/app_mode_support.rb
|
56
|
+
- test/state_manager_test.rb
|
55
57
|
has_rdoc: true
|
56
58
|
homepage: http://www.bitbucket.org/ToadJamb/gems_app_mode
|
57
59
|
licenses:
|
@@ -66,7 +68,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
68
|
requirements:
|
67
69
|
- - ">="
|
68
70
|
- !ruby/object:Gem::Version
|
69
|
-
hash:
|
71
|
+
hash: 3211737509278320283
|
70
72
|
segments:
|
71
73
|
- 0
|
72
74
|
version: "0"
|
@@ -84,9 +86,10 @@ rubyforge_project:
|
|
84
86
|
rubygems_version: 1.3.7
|
85
87
|
signing_key:
|
86
88
|
specification_version: 3
|
87
|
-
summary:
|
89
|
+
summary: State management for objects and applications.
|
88
90
|
test_files:
|
89
91
|
- test/require.rb
|
92
|
+
- test/lib/state_manager.rb
|
93
|
+
- test/lib/state_manager_support.rb
|
90
94
|
- test/app_mode_test.rb
|
91
|
-
- test/
|
92
|
-
- test/lib/app_mode_support.rb
|
95
|
+
- test/state_manager_test.rb
|