Module: Jade::Tasks::Matcher
Instance Method Summary collapse
- #arg_match?(actual, expected) ⇒ Boolean
- #match?(actual, name, positional, named) ⇒ Boolean
- #resolve(name) ⇒ Object
Instance Method Details
#arg_match?(actual, expected) ⇒ Boolean
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/jade/tasks/rspec.rb', line 162 def arg_match?(actual, expected) return expected === actual || expected == actual unless expected.is_a?(::Array) case expected in [::Symbol | ::String | ::Module => name, *positional, ::Hash => named] match?(actual, name, positional, named) in [::Symbol | ::String | ::Module => name, *positional] match?(actual, name, positional, {}) else expected == actual end end |
#match?(actual, name, positional, named) ⇒ Boolean
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/jade/tasks/rspec.rb', line 145 def match?(actual, name, positional, named) klass = resolve(name) return false unless actual.is_a?(klass) if named.any? return false if positional.any? actual_kw = actual.deconstruct_keys(named.keys) named.all? { |k, expected| arg_match?(actual_kw[k], expected) } else actual_args = actual.respond_to?(:deconstruct) ? actual.deconstruct : [] return false unless actual_args.length == positional.length actual_args.zip(positional).all? { |a, e| arg_match?(a, e) } end end |
#resolve(name) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/jade/tasks/rspec.rb', line 125 def resolve(name) case name when ::Class, ::Module name when ::String Object.const_get(name) when ::Symbol full_names = lookup_short_name(name.to_s).map(&:name).uniq case full_names.length when 1 then Object.const_get(full_names.first) when 0 fail "No constant named #{name.inspect}; " \ "pass the class itself or a 'Module::Name' string" else fail "Ambiguous #{name.inspect}: #{full_names.join(', ')}; " \ "use the qualified 'Module::Name' string" end end end |