averell23-assit 0.1.1 → 0.1.2

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.
@@ -0,0 +1,14 @@
1
+ =begin
2
+ This adds simple assert() functionality to the object class. It can be
3
+ packagd into it's own gem and extended later.
4
+
5
+ We call the functions "assit_*" to differentiate them from the unit
6
+ test asserts.
7
+
8
+
9
+ =end
10
+
11
+ $: << File.expand_path(File.join(File.dirname(__FILE__), 'assit'))
12
+
13
+ require 'assertions.rb'
14
+ require 'loader.rb'
@@ -0,0 +1,14 @@
1
+ module Assit
2
+
3
+ # Prints to stderr if the assertion fails
4
+ class ConsoleAction
5
+
6
+ # The action
7
+ def assert_it(message)
8
+ $stderr.puts("Assertion failed: " + message.to_s)
9
+ $stderr.puts("at: ")
10
+ caller.each { |trace| $stderr.puts trace }
11
+ end
12
+ end
13
+
14
+ end
@@ -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
@@ -0,0 +1,16 @@
1
+ module Assit
2
+
3
+ # Error class for the asserts
4
+ class AssertionFailure < StandardError
5
+ end
6
+
7
+ # Raises an error if the assertion fails
8
+ class RaiseAction
9
+
10
+ # The action
11
+ def assert_it(message)
12
+ raise AssertionFailure.new("ASSIT FAILED: #{message}")
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,70 @@
1
+ module Assit
2
+
3
+ # Contains the assertion methods for the framework
4
+ module Assertions
5
+
6
+ # Assert if an object is not nil
7
+ def assit_not_nil(object, message = "Object is nil")
8
+ assit(object != nil, message)
9
+ end
10
+
11
+ # Assert if two objects are equal
12
+ def assit_equal(expected, actual, message = "Object expected to be equal")
13
+ if(expected != actual)
14
+ message << " expected #{expected} but was #{actual}"
15
+ assit(false, message)
16
+ end
17
+ end
18
+
19
+ # Assert if something is of the right type
20
+ def assit_kind_of(klass, object, message = "Object of wrong type")
21
+ if(!object.kind_of?(klass))
22
+ message << " (Expected #{klass} but was #{object.class})"
23
+ assit(false, message)
24
+ end
25
+ end
26
+
27
+ # Fails the assertion
28
+ def assit_fail(message = "Assertion with assit_fail")
29
+ assit(false, message)
30
+ end
31
+
32
+ # Duck typing assertion: This checks if the given object responds to the
33
+ # given method calls. This won't detect any calls that will be handled
34
+ # through method_missing, of course.
35
+ #
36
+ # Methods can be a single method name, or an Enumerable with multiple names
37
+ def assit_quack(object, methods, message = "Quack assert failed.")
38
+ unless(methods.kind_of?(Enumerable))
39
+ methods = [methods]
40
+ end
41
+
42
+ methods.each do |method|
43
+ unless(object.respond_to?(method.to_sym))
44
+ assit(false, "#{message} - Method: #{method.to_s}")
45
+ end
46
+ end
47
+ end
48
+
49
+ # Asserts that the given element is a string that is not nil and not an
50
+ # empty string, or a string only containing whitspaces
51
+ def assit_real_string(object, message = "Not a non-empty string.")
52
+ unless(object && object.kind_of?(String) && object.strip != "")
53
+ assit(false, message)
54
+ end
55
+ end
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
+ #
61
+ # The block will be passed a single array, to which error messages can be
62
+ # append. The assertion will always fail if an error is appended to the
63
+ # array.
64
+ def assit_block(&block)
65
+ errors = []
66
+ assit((block.call(errors) && errors.size == 0), errors.join(', '))
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,8 @@
1
+ # Configuration file for the Assit framework
2
+ module Assit
3
+ # Set to true to completely disable the framework
4
+ assit_disabled false
5
+
6
+ # Sets the action to use when an assert fails
7
+ assit_action :raise
8
+ end
@@ -0,0 +1,17 @@
1
+ # This disables all assertions by providing a "nop" operation for each
2
+ # assertion that is defined
3
+ class Object
4
+
5
+ # The base assertion is the NOP
6
+ def assit(*params, &block) # :nodoc:
7
+ end
8
+
9
+ # Now create an alias for each assertion
10
+ Assit::Assertions.instance_methods.each do |assertion_method|
11
+ # Check if this is an assert method, if yes create alias
12
+ if(assertion_method =~ /assit_.*/)
13
+ alias_method assertion_method.to_s, :assit
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,11 @@
1
+ # Loads the Assit framework and configures it
2
+ require 'assit_config.rb'
3
+
4
+ # Do the configuration
5
+ AssitConfig::do_config!
6
+
7
+ if(AssitConfig.disabled?)
8
+ require 'disable_assertions'
9
+ else
10
+ require 'setup_assertions'
11
+ end
@@ -0,0 +1,15 @@
1
+ # This sets up the assertions to be used
2
+
3
+ class Object
4
+
5
+ # The main assertion method
6
+ def assit(bool, message = "Assertion failed.")
7
+ unless(bool)
8
+ AssitConfig::action.assert_it(message)
9
+ end
10
+ end
11
+
12
+ # Include the assertions
13
+ include Assit::Assertions
14
+
15
+ end
@@ -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
@@ -0,0 +1,94 @@
1
+ # Set up
2
+ ENV['ASSIT_ACTION'] = 'raise'
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require File.dirname(__FILE__) + "/../lib/assit"
7
+
8
+ if(File.exists?(File.dirname(__FILE__) + '/tesly_reporter.rb'))
9
+ printf("Continuing with tesly\n")
10
+ require File.dirname(__FILE__) + '/tesly_reporter'
11
+ end
12
+
13
+ # Helper class for the duck typing assertion
14
+ class QuackClass
15
+ def quack
16
+ end
17
+
18
+ def moo
19
+ end
20
+ end
21
+
22
+ # Test the simple assert facility
23
+ class AssitTest < Test::Unit::TestCase
24
+
25
+ # Tests the basic assert facility
26
+ def test_assert
27
+ assit(true)
28
+ assert_raise(Assit::AssertionFailure) { assit(false, "boing") }
29
+ end
30
+
31
+ # Test nil assert
32
+ def test_assert_not_nil
33
+ not_nil = "xxx"
34
+ assit_not_nil(not_nil, "message")
35
+ assert_raise(Assit::AssertionFailure) { assit_not_nil(nil) }
36
+ end
37
+
38
+ # Test type assert
39
+ def test_assert_kind_of
40
+ my_string = String.new
41
+ my_hash = Hash.new
42
+
43
+ assit_kind_of(Object, my_string)
44
+ assit_kind_of(String, my_string, "message")
45
+ assert_raise(Assit::AssertionFailure) { assit_kind_of(String, my_hash, "message") }
46
+ end
47
+
48
+ # Test equality assert
49
+ def test_assert_equal
50
+ assit_equal(1, 1)
51
+ assert_raise(Assit::AssertionFailure) { assit_equal(0, 1) }
52
+ end
53
+
54
+ # Test fail
55
+ def test_assert_fail
56
+ assert_raise(Assit::AssertionFailure) { assit_fail }
57
+ end
58
+
59
+ # Test the duck typing assertions
60
+ def test_assert_quack
61
+ object_duck = QuackClass.new
62
+ assit_quack(object_duck, :quack)
63
+ assit_quack(object_duck, [:quack, :moo])
64
+ assit_quack(object_duck, :moo, "test")
65
+ assert_raise(Assit::AssertionFailure) { assit_quack(object_duck, :growl) }
66
+ end
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
+
78
+ # Test the block assertions failure
79
+ def test_assert_block_fail_implicit
80
+ assert_raise(Assit::AssertionFailure) { assit_block { |err| err << 'failed' } }
81
+ end
82
+ # Test block assertion error messages
83
+ def test_asser_block_message
84
+ assert_raise(Assit::AssertionFailure) do
85
+ begin
86
+ assit_block { |errors| errors << 'message'; false }
87
+ rescue Assit::AssertionFailure => e
88
+ assert('message', e.message)
89
+ raise
90
+ end
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,26 @@
1
+ # Helper test for the 'disabled' mode. This must be run separate
2
+ # from the main tests
3
+
4
+ # Set up
5
+ ENV['ASSIT_ACTION'] = 'raise'
6
+ ENV['ASSIT_DISABLED'] = 'yes'
7
+
8
+ require 'rubygems'
9
+ require 'test/unit'
10
+ require File.dirname(__FILE__) + "/../lib/assit"
11
+
12
+ class DisabledTest < Test::Unit::TestCase
13
+
14
+ # Test the assit method itself
15
+ def test_assert
16
+ assert_nil(assit(true))
17
+ assert_nil(assit(false))
18
+ end
19
+
20
+ # Test if the aliases were built correctly
21
+ def test_assert
22
+ assit_kind_of(1, 1)
23
+ assert_nil(assit_kind_of(true))
24
+ assert_nil(assit_kind_of(false))
25
+ end
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: averell23-assit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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: 2009-07-14 00:00:00 -07:00
12
+ date: 2009-07-15 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,20 @@ extra_rdoc_files:
24
24
  - CHANGES
25
25
  - LICENSE
26
26
  files:
27
+ - lib/assit
28
+ - lib/assit/actions
29
+ - lib/assit/actions/console_action.rb
30
+ - lib/assit/actions/debug_action.rb
31
+ - lib/assit/actions/raise_action.rb
32
+ - lib/assit/assertions.rb
33
+ - lib/assit/assit_config.rb_example
34
+ - lib/assit/disable_assertions.rb
35
+ - lib/assit/loader.rb
36
+ - lib/assit/setup_assertions.rb
37
+ - lib/assit.rb
38
+ - lib/assit_config.rb
39
+ - test/assit_test.rb
40
+ - test/disabled_helper.rb
27
41
  - README.rdoc
28
42
  - CHANGES
29
43
  - LICENSE