assit 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ require 'ruby-debug'
2
+ Debugger.start
3
+
4
+ module Assit
5
+
6
+ # Error class for the asserts
7
+ class AssertionFailure < StandardError
8
+ end
9
+
10
+ # Raises an error if the assertion fails
11
+ class DebugAction
12
+
13
+ # The action
14
+ def assert_it(message)
15
+ debugger
16
+ end
17
+ end
18
+
19
+ end
@@ -19,7 +19,7 @@ module Assit
19
19
  # Assert if something is of the right type
20
20
  def assit_kind_of(klass, object, message = "Object of wrong type")
21
21
  if(!object.kind_of?(klass))
22
- message += " expected #{klass} but was #{object.class}"
22
+ message += " (Expected #{klass} but was #{object.class})"
23
23
  assit(false, message)
24
24
  end
25
25
  end
@@ -54,5 +54,12 @@ module Assit
54
54
  end
55
55
  end
56
56
 
57
+ # Executes the given block and asserts if the result is true. This allows
58
+ # you to assert on complex, custom expressions and be able to disable
59
+ # those expressions together with the assertions. See the README for more.
60
+ def assit_block(message = "Assertion failed.", &block)
61
+ assit(block.call, message)
62
+ end
63
+
57
64
  end
58
65
  end
@@ -3,7 +3,7 @@
3
3
  class Object
4
4
 
5
5
  # The base assertion is the NOP
6
- def assit(*params) # :nodoc:
6
+ def assit(*params, &block) # :nodoc:
7
7
  end
8
8
 
9
9
  # Now create an alias for each assertion
@@ -1,10 +1,10 @@
1
1
  # Loads the Assit framework and configures it
2
- require 'config.rb'
2
+ require 'assit_config.rb'
3
3
 
4
4
  # Do the configuration
5
- Assit::Config::do_config!
5
+ AssitConfig::do_config!
6
6
 
7
- if(Assit::Config.disabled?)
7
+ if(AssitConfig.disabled?)
8
8
  require 'disable_assertions'
9
9
  else
10
10
  require 'setup_assertions'
@@ -5,7 +5,7 @@ class Object
5
5
  # The main assertion method
6
6
  def assit(bool, message = "Assertion failed.")
7
7
  unless(bool)
8
- Assit::Config::action.assert_it(message)
8
+ AssitConfig::action.assert_it(message)
9
9
  end
10
10
  end
11
11
 
@@ -0,0 +1,119 @@
1
+ # Helper module for the configuration
2
+ # Ths is used to load the Assit module. The following configuration
3
+ # options exist:
4
+ #
5
+ # * assit_disabled - If set it will completely disable the assertions
6
+ # * assit_action - Selects the action that will be executed if the assert fails
7
+ #
8
+ # The Assit framework can be configured through environment variables or
9
+ # a config file (assit_config.rb) in the current load path.
10
+ #
11
+ # If environment variables are used, the should have ALL UPPERCASE
12
+ # names, as ASSIT_DISABLED and ASSIT_ACTION.
13
+ #
14
+ # Environment variables take precedence over the config file.
15
+ #
16
+ # If no configuration is given, the module is configured
17
+ # automatically:
18
+ #
19
+ # A Rails installation run in 'production' mode will have the
20
+ # assertions disabled.
21
+ #
22
+ # A Rails installation in the other modes will use :raise as
23
+ # the default action.
24
+ #
25
+ # Other scripts will use :raise if the $DEBUG flag is set, otherwise
26
+ # :log is used as the default action
27
+ module AssitConfig
28
+
29
+ # defines the methods used for the config file
30
+
31
+ # Sets the "disable" flag
32
+ def self.assit_disabled(disable)
33
+ @assit_disabled = disable
34
+ @assit_disabled_conf = true
35
+ end
36
+
37
+ # Gets the "disable" flag
38
+ def self.disabled?
39
+ @assit_disabled
40
+ end
41
+
42
+ # Get the action for failed assertions
43
+ def self.action
44
+ @@assit_action_object ||= begin
45
+ action_name = "#{@assit_action.to_s.downcase}_action"
46
+
47
+ # require the action's file
48
+ require File.join('assit', 'actions', action_name)
49
+
50
+ # Get the class of the widget and check, just to be sure
51
+ klass = Assit.const_get(camelize(action_name))
52
+ raise(RuntimeError, "Action class does not exist") unless(klass.is_a?(Class))
53
+
54
+ # Create the new widget
55
+ klass.new()
56
+ end
57
+ end
58
+
59
+ # Sets the action for failed assertions. This will
60
+ # create the AssitAction object that can be used by the
61
+ # framework
62
+ def self.assit_action(action)
63
+ @assit_action = action.to_sym
64
+ end
65
+
66
+ # Sets up the Assit configuration parameters
67
+ def self.do_config!
68
+ # Try to load the configuration
69
+ begin
70
+ require 'assit_config'
71
+ rescue LoadError
72
+ # Fail silently, we don't care if there is no config file
73
+ end
74
+
75
+ # Check for the environment variables. They can overwrite the
76
+ # config file variables.
77
+ @assit_disabled ||= ENV['ASSIT_DISABLED'] && (ENV['ASSIT_DISABLED'].downcase == 'true' || ENV['ASSIT_DISABLED'].downcase == 'yes')
78
+ @assit_disabled_conf ||= (ENV['ASSIT_DISABLED'] != nil)
79
+ @assit_action ||= ENV['ASSIT_ACTION'] ? ENV['ASSIT_ACTION'].downcase.to_sym : nil
80
+
81
+ # If still not configured, use the auto-config
82
+ @assit_disabled ||= auto_disable unless(@assit_disabled_conf)
83
+ @assit_action ||= auto_action
84
+
85
+ # Call the action one time to create and load it
86
+ action
87
+
88
+ # return true
89
+ true
90
+ end
91
+
92
+ protected
93
+
94
+ # Used to auto-configure the action
95
+ def self.auto_action
96
+ # Raise if the debug mode is set or rails is in test or development
97
+ if($DEBUG || (ENV['RAILS_ENV'] && ENV['RAILS_ENV'] != 'production'))
98
+ :raise
99
+ else
100
+ :console
101
+ end
102
+ end
103
+
104
+ # Used to auto-configure the "disable" flag
105
+ def self.auto_disable
106
+ # In Rails production we auto-disable
107
+ (ENV['RAILS_ENV'] && ENV['RAILS_ENV'] == 'production')
108
+ end
109
+
110
+ # Stolen from Rails ;-)
111
+ def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
112
+ if first_letter_in_uppercase
113
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
114
+ else
115
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
116
+ end
117
+ end
118
+
119
+ end
@@ -65,4 +65,14 @@ class AssitTest < Test::Unit::TestCase
65
65
  assert_raise(Assit::AssertionFailure) { assit_quack(object_duck, :growl) }
66
66
  end
67
67
 
68
+ # Test the block assertion
69
+ def test_assert_block
70
+ assit_block { true }
71
+ end
72
+
73
+ # Test the block assertions failure
74
+ def test_assert_block_fail
75
+ assert_raise(Assit::AssertionFailure) { assit_block { 'foo' == 'bar' } }
76
+ end
77
+
68
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Hahn
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-20 00:00:00 +01:00
12
+ date: 2008-02-25 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,14 +25,15 @@ files:
25
25
  - lib/assit
26
26
  - lib/assit/actions
27
27
  - lib/assit/actions/console_action.rb
28
+ - lib/assit/actions/debug_action.rb
28
29
  - lib/assit/actions/raise_action.rb
29
30
  - lib/assit/assertions.rb
30
31
  - lib/assit/assit_config.rb_example
31
- - lib/assit/config.rb
32
32
  - lib/assit/disable_assertions.rb
33
33
  - lib/assit/loader.rb
34
34
  - lib/assit/setup_assertions.rb
35
35
  - lib/assit.rb
36
+ - lib/assit_config.rb
36
37
  has_rdoc: true
37
38
  homepage: http://assit.rubyforge.org
38
39
  post_install_message:
@@ -1,122 +0,0 @@
1
- module Assit
2
-
3
- # Helper module for the configuration
4
- # Ths is used to load the Assit module. The following configuration
5
- # options exist:
6
- #
7
- # * assit_disabled - If set it will completely disable the assertions
8
- # * assit_action - Selects the action that will be executed if the assert fails
9
- #
10
- # The Assit framework can be configured through environment variables or
11
- # a config file (assit_config.rb) in the current load path.
12
- #
13
- # If environment variables are used, the should have ALL UPPERCASE
14
- # names, as ASSIT_DISABLED and ASSIT_ACTION.
15
- #
16
- # Environment variables take precedence over the config file.
17
- #
18
- # If no configuration is given, the module is configured
19
- # automatically:
20
- #
21
- # A Rails installation run in 'production' mode will have the
22
- # assertions disabled.
23
- #
24
- # A Rails installation in the other modes will use :raise as
25
- # the default action.
26
- #
27
- # Other scripts will use :raise if the $DEBUG flag is set, otherwise
28
- # :log is used as the default action
29
- module Config
30
-
31
- # defines the methods used for the config file
32
-
33
- # Sets the "disable" flag
34
- def self.assit_disabled(disable)
35
- @assit_disabled = disable
36
- @assit_disabled_conf = true
37
- end
38
-
39
- # Gets the "disable" flag
40
- def self.disabled?
41
- @assit_disabled
42
- end
43
-
44
- # Get the action for failed assertions
45
- def self.action
46
- @@assit_action_object ||= begin
47
- action_name = "#{@assit_action.to_s.downcase}_action"
48
-
49
- # require the action's file
50
- require File.join('actions', action_name)
51
-
52
- # Get the class of the widget and check, just to be sure
53
- klass = Assit.const_get(camelize(action_name))
54
- raise(RuntimeError, "Action class does not exist") unless(klass.is_a?(Class))
55
-
56
- # Create the new widget
57
- klass.new()
58
- end
59
- end
60
-
61
- # Sets the action for failed assertions. This will
62
- # create the AssitAction object that can be used by the
63
- # framework
64
- def self.assit_action(action)
65
- @assit_action = action.to_sym
66
- end
67
-
68
- # Sets up the Assit configuration parameters
69
- def self.do_config!
70
- # Try to load the configuration
71
- begin
72
- require 'assit_config'
73
- rescue LoadError
74
- # Fail silently, we don't care if there is no config file
75
- end
76
-
77
- # Check for the environment variables. They can overwrite the
78
- # config file variables.
79
- @assit_disabled ||= ENV['ASSIT_DISABLED'] && (ENV['ASSIT_DISABLED'].downcase == 'true' || ENV['ASSIT_DISABLED'].downcase == 'yes')
80
- @assit_disabled_conf ||= (ENV['ASSIT_DISABLED'] != nil)
81
- @assit_action ||= ENV['ASSIT_ACTION'] ? ENV['ASSIT_ACTION'].downcase.to_sym : nil
82
-
83
- # If still not configured, use the auto-config
84
- @assit_disabled ||= auto_disable unless(@assit_disabled_conf)
85
- @assit_action ||= auto_action
86
-
87
- # Call the action one time to create and load it
88
- action
89
-
90
- # return true
91
- true
92
- end
93
-
94
- protected
95
-
96
- # Used to auto-configure the action
97
- def self.auto_action
98
- # Raise if the debug mode is set or rails is in test or development
99
- if($DEBUG || (ENV['RAILS_ENV'] && ENV['RAILS_ENV'] != 'production'))
100
- :raise
101
- else
102
- :console
103
- end
104
- end
105
-
106
- # Used to auto-configure the "disable" flag
107
- def self.auto_disable
108
- # In Rails production we auto-disable
109
- (ENV['RAILS_ENV'] && ENV['RAILS_ENV'] == 'production')
110
- end
111
-
112
- # Stolen from Rails ;-)
113
- def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
114
- if first_letter_in_uppercase
115
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
116
- else
117
- lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
118
- end
119
- end
120
-
121
- end
122
- end