assit 0.0.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.
- data/lib/assit.rb +14 -0
- data/lib/assit/actions/console_action.rb +13 -0
- data/lib/assit/actions/raise_action.rb +16 -0
- data/lib/assit/assertions.rb +58 -0
- data/lib/assit/assit_config.rb_example +8 -0
- data/lib/assit/config.rb +122 -0
- data/lib/assit/disable_assertions.rb +17 -0
- data/lib/assit/loader.rb +11 -0
- data/lib/assit/setup_assertions.rb +15 -0
- data/test/assit_test.rb +68 -0
- metadata +63 -0
data/lib/assit.rb
ADDED
@@ -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,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,58 @@
|
|
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
|
+
end
|
58
|
+
end
|
data/lib/assit/config.rb
ADDED
@@ -0,0 +1,122 @@
|
|
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
|
@@ -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) # :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
|
data/lib/assit/loader.rb
ADDED
@@ -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
|
+
Assit::Config::action.assert_it(message)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Include the assertions
|
13
|
+
include Assit::Assertions
|
14
|
+
|
15
|
+
end
|
data/test/assit_test.rb
ADDED
@@ -0,0 +1,68 @@
|
|
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
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: assit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Hahn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-01-20 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: This is an assertion framework for Ruby.
|
17
|
+
email: dhahn@gmx.de
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/assit
|
26
|
+
- lib/assit/actions
|
27
|
+
- lib/assit/actions/console_action.rb
|
28
|
+
- lib/assit/actions/raise_action.rb
|
29
|
+
- lib/assit/assertions.rb
|
30
|
+
- lib/assit/assit_config.rb_example
|
31
|
+
- lib/assit/config.rb
|
32
|
+
- lib/assit/disable_assertions.rb
|
33
|
+
- lib/assit/loader.rb
|
34
|
+
- lib/assit/setup_assertions.rb
|
35
|
+
- lib/assit.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://assit.rubyforge.org
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project: assit
|
58
|
+
rubygems_version: 1.0.1
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: An assertion framework for Ruby
|
62
|
+
test_files:
|
63
|
+
- test/assit_test.rb
|