remarkable 3.0.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,18 +5,18 @@ require File.join(dir, 'dsl', 'matches')
5
5
  require File.join(dir, 'dsl', 'callbacks')
6
6
 
7
7
  module Remarkable
8
- module DSL
9
- ATTR_READERS = [ :matcher_arguments, :matcher_optionals, :matcher_single_assertions,
10
- :matcher_collection_assertions, :before_assert_callbacks, :after_initialize_callbacks
11
- ] unless self.const_defined?(:ATTR_READERS)
8
+ module DSL
9
+ ATTR_READERS = [ :matcher_arguments, :matcher_optionals, :matcher_single_assertions,
10
+ :matcher_collection_assertions, :before_assert_callbacks, :after_initialize_callbacks
11
+ ] unless self.const_defined?(:ATTR_READERS)
12
12
 
13
- def self.extended(base) #:nodoc:
14
- base.extend Assertions
15
- base.send :include, Callbacks
13
+ def self.extended(base) #:nodoc:
14
+ base.extend Assertions
15
+ base.send :include, Callbacks
16
16
  base.send :include, Matches
17
- base.send :include, Optionals
18
-
19
- # Initialize matcher_arguments hash with names as an empty array
17
+ base.send :include, Optionals
18
+
19
+ # Initialize matcher_arguments hash with names as an empty array
20
20
  base.instance_variable_set('@matcher_arguments', { :names => [] })
21
21
  end
22
22
 
@@ -28,11 +28,11 @@ module Remarkable
28
28
  attr_reader *ATTR_READERS
29
29
  end
30
30
  end
31
-
32
- ATTR_READERS.each do |attr|
33
- current_value = self.instance_variable_get("@#{attr}")
34
- base.instance_variable_set("@#{attr}", current_value ? current_value.dup : [])
35
- end
31
+
32
+ ATTR_READERS.each do |attr|
33
+ current_value = self.instance_variable_get("@#{attr}")
34
+ base.instance_variable_set("@#{attr}", current_value ? current_value.dup : [])
35
+ end
36
36
  end
37
37
  end
38
38
  end
@@ -64,12 +64,12 @@ module Remarkable
64
64
  @matcher_arguments[:names] = names
65
65
 
66
66
  if collection = options.delete(:collection)
67
- @matcher_arguments[:collection] = collection
68
-
67
+ @matcher_arguments[:collection] = collection
68
+
69
69
  if options[:as]
70
- @matcher_arguments[:as] = options.delete(:as)
71
- else
72
- raise ArgumentError, 'You gave me :collection as option but have not give me :as as well'
70
+ @matcher_arguments[:as] = options.delete(:as)
71
+ else
72
+ raise ArgumentError, 'You gave me :collection as option but have not give me :as as well'
73
73
  end
74
74
 
75
75
  args << "*#{collection}"
@@ -147,7 +147,7 @@ END
147
147
  # Or you can set the message in the instance variable @expectation in the
148
148
  # assertion method if you don't want to rely on I18n API.
149
149
  #
150
- # As you might have noticed from the examples above, this method is also
150
+ # As you might have noticed from the examples above, this method is also
151
151
  # aliased as <tt>collection_assertion</tt>.
152
152
  #
153
153
  def collection_assertions(*methods, &block)
@@ -156,8 +156,8 @@ END
156
156
  end
157
157
  alias :collection_assertion :collection_assertions
158
158
 
159
- # In contrast to <tt>collection_assertions</tt>, the methods given here
160
- # are called just once. In other words, it does not iterate through the
159
+ # In contrast to <tt>collection_assertions</tt>, the methods given here
160
+ # are called just once. In other words, it does not iterate through the
161
161
  # collection given in arguments.
162
162
  #
163
163
  # It also accepts blocks and is aliased as assertion.
@@ -1,52 +1,52 @@
1
1
  module Remarkable
2
2
  module DSL
3
3
  module Callbacks
4
-
5
- def self.included(base) #:nodoc:
6
- base.extend ClassMethods
7
- end
8
-
9
- module ClassMethods
4
+
5
+ def self.included(base) #:nodoc:
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
10
  protected
11
11
  # Class method that accepts a block or a symbol which is called after initialization.
12
12
  #
13
- def after_initialize(symbol=nil, &block)
14
- if block_given?
15
- @after_initialize_callbacks << block
16
- elsif symbol
17
- @after_initialize_callbacks << symbol
18
- end
13
+ def after_initialize(symbol=nil, &block)
14
+ if block_given?
15
+ @after_initialize_callbacks << block
16
+ elsif symbol
17
+ @after_initialize_callbacks << symbol
18
+ end
19
19
  end
20
20
 
21
21
  # Class method that accepts a block or a symbol which is called before assertion.
22
22
  #
23
- def before_assert(symbol=nil, &block)
24
- if block_given?
25
- @before_assert_callbacks << block
26
- elsif symbol
27
- @before_assert_callbacks << symbol
28
- end
29
- end
30
- end
31
-
32
- def run_after_initialize_callbacks #:nodoc:
33
- self.class.after_initialize_callbacks.each do |method|
34
- if method.is_a?(Proc)
35
- instance_eval &method
36
- elsif method.is_a?(Symbol)
37
- send(method)
38
- end
39
- end
40
- end
41
-
42
- def run_before_assert_callbacks #:nodoc:
43
- self.class.before_assert_callbacks.each do |method|
44
- if method.is_a?(Proc)
45
- instance_eval &method
46
- elsif method.is_a?(Symbol)
47
- send(method)
48
- end
49
- end
23
+ def before_assert(symbol=nil, &block)
24
+ if block_given?
25
+ @before_assert_callbacks << block
26
+ elsif symbol
27
+ @before_assert_callbacks << symbol
28
+ end
29
+ end
30
+ end
31
+
32
+ def run_after_initialize_callbacks #:nodoc:
33
+ self.class.after_initialize_callbacks.each do |method|
34
+ if method.is_a?(Proc)
35
+ instance_eval &method
36
+ elsif method.is_a?(Symbol)
37
+ send(method)
38
+ end
39
+ end
40
+ end
41
+
42
+ def run_before_assert_callbacks #:nodoc:
43
+ self.class.before_assert_callbacks.each do |method|
44
+ if method.is_a?(Proc)
45
+ instance_eval &method
46
+ elsif method.is_a?(Symbol)
47
+ send(method)
48
+ end
49
+ end
50
50
  end
51
51
 
52
52
  end
@@ -49,51 +49,51 @@ module Remarkable
49
49
  {}
50
50
  end
51
51
 
52
- # Overwrites default_i18n_options to provide arguments and optionals
53
- # to interpolation options.
54
- #
55
- # If you still need to provide more other interpolation options, you can
56
- # do that in two ways:
57
- #
58
- # 1. Overwrite interpolation_options:
59
- #
60
- # def interpolation_options
61
- # { :real_value => real_value }
62
- # end
63
- #
64
- # 2. Return a hash from your assertion method:
65
- #
66
- # def my_assertion
67
- # return true if real_value == expected_value
68
- # return false, :real_value => real_value
69
- # end
70
- #
52
+ # Overwrites default_i18n_options to provide arguments and optionals
53
+ # to interpolation options.
54
+ #
55
+ # If you still need to provide more other interpolation options, you can
56
+ # do that in two ways:
57
+ #
58
+ # 1. Overwrite interpolation_options:
59
+ #
60
+ # def interpolation_options
61
+ # { :real_value => real_value }
62
+ # end
63
+ #
64
+ # 2. Return a hash from your assertion method:
65
+ #
66
+ # def my_assertion
67
+ # return true if real_value == expected_value
68
+ # return false, :real_value => real_value
69
+ # end
70
+ #
71
71
  # In both cases, :real_value will be available as interpolation option.
72
72
  #
73
73
  def default_i18n_options #:nodoc:
74
- i18n_options = {}
75
-
76
- @options.each do |key, value|
77
- i18n_options[key] = value.inspect
78
- end if @options
79
-
74
+ i18n_options = {}
75
+
76
+ @options.each do |key, value|
77
+ i18n_options[key] = value.inspect
78
+ end if @options
79
+
80
80
  # Also add arguments as interpolation options.
81
81
  self.class.matcher_arguments[:names].each do |name|
82
82
  i18n_options[name] = instance_variable_get("@#{name}").inspect
83
- end
84
-
85
- # Add collection interpolation options.
86
- i18n_options.update(collection_interpolation)
87
-
88
- # Add default options (highest priority). They should not be overwritten.
83
+ end
84
+
85
+ # Add collection interpolation options.
86
+ i18n_options.update(collection_interpolation)
87
+
88
+ # Add default options (highest priority). They should not be overwritten.
89
89
  i18n_options.update(super)
90
- end
91
-
92
- # Method responsible to add collection as interpolation.
93
- #
94
- def collection_interpolation #:nodoc:
95
- options = {}
96
-
90
+ end
91
+
92
+ # Method responsible to add collection as interpolation.
93
+ #
94
+ def collection_interpolation #:nodoc:
95
+ options = {}
96
+
97
97
  if collection_name = self.class.matcher_arguments[:collection]
98
98
  collection_name = collection_name.to_sym
99
99
  collection = instance_variable_get("@#{collection_name}")
@@ -102,26 +102,26 @@ module Remarkable
102
102
  object_name = self.class.matcher_arguments[:as].to_sym
103
103
  object = instance_variable_get("@#{object_name}")
104
104
  options[object_name] = object if object
105
- end
106
-
107
- options
105
+ end
106
+
107
+ options
108
108
  end
109
109
 
110
- # Send the assertion methods given and create a expectation message
111
- # if any of those methods returns false.
112
- #
113
- # Since most assertion methods ends with an question mark and it's not
114
- # readable in yml files, we remove question and exclation marks at the
115
- # end of the method name before translating it. So if you have a method
116
- # called is_valid? on I18n yml file we will check for a key :is_valid.
110
+ # Send the assertion methods given and create a expectation message
111
+ # if any of those methods returns false.
112
+ #
113
+ # Since most assertion methods ends with an question mark and it's not
114
+ # readable in yml files, we remove question and exclation marks at the
115
+ # end of the method name before translating it. So if you have a method
116
+ # called is_valid? on I18n yml file we will check for a key :is_valid.
117
117
  #
118
118
  def send_methods_and_generate_message(methods) #:nodoc:
119
119
  methods.each do |method|
120
- bool, hash = send(method)
120
+ bool, hash = send(method)
121
121
 
122
- unless bool
123
- @expectation ||= Remarkable.t "expectations.#{method.to_s.gsub(/(\?|\!)$/, '')}",
124
- default_i18n_options.merge(hash || {})
122
+ unless bool
123
+ @expectation ||= Remarkable.t "expectations.#{method.to_s.gsub(/(\?|\!)$/, '')}",
124
+ default_i18n_options.merge(hash || {})
125
125
  return false
126
126
  end
127
127
  end
@@ -1,15 +1,15 @@
1
1
  module Remarkable
2
2
  module DSL
3
- module Optionals
4
-
5
- OPTIONAL_KEYS = [ :positive, :negative, :not_given ]
6
-
7
- def self.included(base) #:nodoc:
8
- base.extend ClassMethods
3
+ module Optionals
4
+
5
+ OPTIONAL_KEYS = [ :positive, :negative, :not_given ]
6
+
7
+ def self.included(base) #:nodoc:
8
+ base.extend ClassMethods
9
9
  end
10
-
11
- module ClassMethods
12
-
10
+
11
+ module ClassMethods
12
+
13
13
  protected
14
14
 
15
15
  # Creates optional handlers for matchers dynamically. The following
@@ -28,7 +28,7 @@ module Remarkable
28
28
  # Options:
29
29
  #
30
30
  # * <tt>:default</tt> - The default value for this optional
31
- # * <tt>:alias</tt> - An alias for this optional
31
+ # * <tt>:alias</tt> - An alias for this optional
32
32
  # * <tt>:splat</tt> - Should be true if you expects multiple arguments
33
33
  #
34
34
  # Examples:
@@ -59,11 +59,11 @@ module Remarkable
59
59
  # validate_uniqueness_of :project_id, :scope => :company_id, :case_sensitive => false
60
60
  # #=> "validate uniqueness of project_id scoped to company_id and case insensitive"
61
61
  #
62
- # The interpolation options available are "value" and "inspect". Where
63
- # the first is the optional value transformed into a string and the
64
- # second is the inspected value.
65
- #
66
- # Three keys are available to be used in I18n files and control how
62
+ # The interpolation options available are "value" and "inspect". Where
63
+ # the first is the optional value transformed into a string and the
64
+ # second is the inspected value.
65
+ #
66
+ # Three keys are available to be used in I18n files and control how
67
67
  # optionals are appended to your description:
68
68
  #
69
69
  # * <tt>positive</tt> - When the optional is given and it evaluates to true (everything but false and nil).
@@ -72,9 +72,9 @@ module Remarkable
72
72
  #
73
73
  def optionals(*names)
74
74
  options = names.extract_options!
75
- @matcher_optionals += names
76
-
77
- splat = options[:splat] ? '*' : ''
75
+ @matcher_optionals += names
76
+
77
+ splat = options[:splat] ? '*' : ''
78
78
  default = options[:default] ? "=#{options[:default].inspect}" : ""
79
79
 
80
80
  names.each do |name|
@@ -90,50 +90,50 @@ module Remarkable
90
90
 
91
91
  # Call unique to avoid duplicate optionals.
92
92
  @matcher_optionals.uniq!
93
- end
94
- alias :optional :optionals
95
-
96
- # Instead of appending, prepend optionals to the beginning of optionals
97
- # array. This is important because this decide how the description
98
- # message is generated.
99
- #
100
- def prepend_optionals(*names)
101
- current_optionals = @matcher_optionals.dup
102
- @matcher_optionals = []
103
- optional(*names)
104
- @matcher_optionals += current_optionals
105
- @matcher_optionals.uniq!
106
- end
107
- alias :prepend_optional :prepend_optionals
108
-
109
- end
110
-
93
+ end
94
+ alias :optional :optionals
95
+
96
+ # Instead of appending, prepend optionals to the beginning of optionals
97
+ # array. This is important because this decide how the description
98
+ # message is generated.
99
+ #
100
+ def prepend_optionals(*names)
101
+ current_optionals = @matcher_optionals.dup
102
+ @matcher_optionals = []
103
+ optional(*names)
104
+ @matcher_optionals += current_optionals
105
+ @matcher_optionals.uniq!
106
+ end
107
+ alias :prepend_optional :prepend_optionals
108
+
109
+ end
110
+
111
111
  # Overwrites description to support optionals. Check <tt>optional</tt> for
112
112
  # more information.
113
113
  #
114
114
  def description(options={}) #:nodoc:
115
- message = super(options)
115
+ message = super(options)
116
116
  message.strip!
117
117
 
118
118
  optionals = self.class.matcher_optionals.map do |optional|
119
119
  scope = matcher_i18n_scope + ".optionals.#{optional}"
120
120
 
121
- if @options.key?(optional)
121
+ if @options.key?(optional)
122
122
  value = @options[optional]
123
- defaults = [ (value ? :positive : :negative) ]
124
-
125
- # If optional is a symbol and it's not any to any of the reserved symbols, search for it also
126
- defaults.unshift(value) if value.is_a?(Symbol) && !OPTIONAL_KEYS.include?(value)
123
+ defaults = [ (value ? :positive : :negative) ]
124
+
125
+ # If optional is a symbol and it's not any to any of the reserved symbols, search for it also
126
+ defaults.unshift(value) if value.is_a?(Symbol) && !OPTIONAL_KEYS.include?(value)
127
127
 
128
- Remarkable.t defaults.shift, :default => defaults, :raise => true, :scope => scope,
129
- :inspect => value.inspect, :value => value.to_s
128
+ Remarkable.t defaults.shift, :default => defaults, :raise => true, :scope => scope,
129
+ :inspect => value.inspect, :value => value.to_s
130
130
  else
131
131
  Remarkable.t :not_given, :raise => true, :scope => scope
132
132
  end rescue nil
133
133
  end.compact
134
134
 
135
- message << ' ' << array_to_sentence(optionals)
136
- message.strip!
135
+ message << ' ' << array_to_sentence(optionals)
136
+ message.strip!
137
137
  message
138
138
  end
139
139
 
@@ -1,48 +1,48 @@
1
1
  module Remarkable
2
- module Macros
2
+ module Macros
3
3
 
4
4
  protected
5
-
5
+
6
6
  def method_missing(method_id, *args, &block) #:nodoc:
7
- if method_id.to_s =~ /^(should_not|should)_(.+)/
8
- should_or_should_not_method_missing($1, $2, caller, *args, &block)
7
+ if method_id.to_s =~ /^(should_not|should)_(.+)/
8
+ should_or_should_not_method_missing($1, $2, caller, *args, &block)
9
9
  elsif method_id.to_s =~ /^x(should_not|should)_(.+)/
10
10
  disabled_method_missing($1, $2, *args, &block)
11
- else
11
+ else
12
12
  super(method_id, *args, &block)
13
13
  end
14
- end
15
-
14
+ end
15
+
16
16
  def should_or_should_not_method_missing(should_or_should_not, method, calltrace, *args, &block) #:nodoc:
17
- it {
18
- begin
19
- send(should_or_should_not, send(method, *args, &block))
20
- rescue Exception => e
21
- e.set_backtrace(calltrace.to_a)
22
- raise e
23
- end
17
+ it {
18
+ begin
19
+ send(should_or_should_not, send(method, *args, &block))
20
+ rescue Exception => e
21
+ e.set_backtrace(calltrace.to_a)
22
+ raise e
23
+ end
24
24
  }
25
- end
25
+ end
26
26
 
27
- def disabled_method_missing(should_or_should_not, method, *args, &block) #:nodoc:
28
- description = get_description_from_matcher(should_or_should_not, method, *args, &block)
27
+ def disabled_method_missing(should_or_should_not, method, *args, &block) #:nodoc:
28
+ description = get_description_from_matcher(should_or_should_not, method, *args, &block)
29
29
  xexample(description)
30
- end
31
-
32
- # Try to get the description from the matcher. If an error is raised, we
33
- # deduct the description from the matcher name, but it will be shown in
34
- # english.
35
- #
36
- def get_description_from_matcher(should_or_should_not, method, *args, &block) #:nodoc:
37
- verb = should_or_should_not.to_s.gsub('_', ' ')
38
-
39
- desc = Remarkable::Matchers.send(method, *args, &block).spec(self).description
40
- verb = Remarkable.t("remarkable.core.#{should_or_should_not}", :default => verb)
41
- rescue
42
- desc = method.to_s.gsub('_', ' ')
43
- ensure
44
- verb << ' ' << desc
45
- end
30
+ end
31
+
32
+ # Try to get the description from the matcher. If an error is raised, we
33
+ # deduct the description from the matcher name, but it will be shown in
34
+ # english.
35
+ #
36
+ def get_description_from_matcher(should_or_should_not, method, *args, &block) #:nodoc:
37
+ verb = should_or_should_not.to_s.gsub('_', ' ')
38
+
39
+ desc = Remarkable::Matchers.send(method, *args, &block).spec(self).description
40
+ verb = Remarkable.t("remarkable.core.#{should_or_should_not}", :default => verb)
41
+ rescue
42
+ desc = method.to_s.gsub('_', ' ')
43
+ ensure
44
+ verb << ' ' << desc
45
+ end
46
46
 
47
47
  end
48
48
  end