Module: Speculation::Test
- Extended by:
- NamespacedSymbols
- Defined in:
- lib/speculation/test.rb
Class Attribute Summary collapse
-
.instrument_enabled ⇒ Object
if false, instrumented methods call straight through.
Class Method Summary collapse
-
.abbrev_result(x) ⇒ Hash
Given a check result, returns an abbreviated version suitable for summary use.
-
.check(method_or_methods = nil, opts = {}) ⇒ Array<Identifier>
Run generative tests for spec conformance on method_or_methods.
-
.check_method(method, spec, opts = {}) ⇒ Hash
Runs generative tests for method using spec and opts.
-
.checkable_methods(opts = {}) ⇒ Array<Method>
The array of methods that can be checked.
-
.enumerate_methods(*modules) ⇒ Array<Method>
An array of public and protected singleton methods belonging to modules.
-
.instrument(method_or_methods = instrumentable_methods, opts = {}) ⇒ Array<Method>
A collection of methods instrumented.
-
.instrumentable_methods(opts = {}) ⇒ Array<Identifier>
Given an opts hash as per instrument, returns the set of methods that can be instrumented.
-
.summarize_results(check_results) {|Hash| ... } ⇒ Hash
Given a collection of check_results, e.g.
-
.unstrument(method_or_methods = nil) ⇒ Array<Method>
Undoes instrument on the method_or_methods, specified as in instrument.
-
.with_instrument_disabled ⇒ Object
Disables instrument’s checking of calls within a block.
Methods included from NamespacedSymbols
Class Attribute Details
.instrument_enabled ⇒ Object
if false, instrumented methods call straight through
21 22 23 |
# File 'lib/speculation/test.rb', line 21 def instrument_enabled @instrument_enabled end |
Class Method Details
.abbrev_result(x) ⇒ Hash
Given a check result, returns an abbreviated version suitable for summary use.
171 172 173 174 175 176 177 178 179 |
# File 'lib/speculation/test.rb', line 171 def self.abbrev_result(x) if x[:failure] x.reject { |k, _| k == ns(:ret) }. merge(:spec => x[:spec].inspect, :failure => unwrap_failure(x[:failure])) else x.reject { |k, _| [:spec, ns(:ret)].include?(k) } end end |
.check(method_or_methods = nil, opts = {}) ⇒ Array<Identifier>
Run generative tests for spec conformance on method_or_methods. If method_or_methods is not specified, check all checkable methods.
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/speculation/test.rb', line 154 def self.check(method_or_methods = nil, opts = {}) method_or_methods ||= checkable_methods checkable = Set(checkable_methods(opts)) checkable.map!(&S.method(:Identifier)) methods = Set(method_or_methods) methods.map!(&S.method(:Identifier)) pmap(methods.intersection(checkable)) { |ident| check1(ident, S.get_spec(ident), opts) } end |
.check_method(method, spec, opts = {}) ⇒ Hash
Runs generative tests for method using spec and opts.
114 115 116 117 |
# File 'lib/speculation/test.rb', line 114 def self.check_method(method, spec, opts = {}) validate_check_opts(opts) check1(S.Identifier(method), spec, opts) end |
.checkable_methods(opts = {}) ⇒ Array<Method>
Returns the array of methods that can be checked.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/speculation/test.rb', line 121 def self.checkable_methods(opts = {}) validate_check_opts(opts) S. registry. keys. select { |k| fn_spec_name?(k) && !k.instance_method? }. concat(Hash(opts[:spec]).keys). map(&method(:Method)) end |
.enumerate_methods(*modules) ⇒ Array<Method>
Returns an array of public and protected singleton methods belonging to modules.
208 209 210 |
# File 'lib/speculation/test.rb', line 208 def self.enumerate_methods(*modules) modules.flat_map { |mod| mod.methods(false).map(&mod.method(:method)) } # method end |
.instrument(method_or_methods = instrumentable_methods, opts = {}) ⇒ Array<Method>
Returns a collection of methods instrumented.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/speculation/test.rb', line 80 def self.instrument(method_or_methods = instrumentable_methods, opts = {}) if opts[:gen] gens = opts[:gen].reduce({}) { |h, (k, v)| h.merge(S.Identifier(k) => v) } opts = opts.merge(:gen => gens) end Array(method_or_methods). map { |method| S.Identifier(method) }. uniq. map { |ident| instrument1(ident, opts) }. compact. map(&method(:Method)) end |
.instrumentable_methods(opts = {}) ⇒ Array<Identifier>
Given an opts hash as per instrument, returns the set of methods that can be instrumented.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/speculation/test.rb', line 36 def self.instrumentable_methods(opts = {}) if opts[:gen] unless opts[:gen].keys.all? { |k| k.is_a?(Method) || k.is_a?(Symbol) } raise ArgumentError, "instrument :gen expects Method or Symbol keys" end end S.registry.keys.select(&method(:fn_spec_name?)).to_set.tap { |set| set.merge(opts[:spec].keys) if opts[:spec] set.merge(opts[:stub]) if opts[:stub] set.merge(opts[:replace].keys) if opts[:replace] }.map(&method(:Method)) end |
.summarize_results(check_results) {|Hash| ... } ⇒ Hash
Given a collection of check_results, e.g. from ‘check`, pretty prints the summary_result (default abbrev_result) of each.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/speculation/test.rb', line 190 def self.summarize_results(check_results, &summary_result) summary_result ||= method(:abbrev_result) check_results.reduce(:total => 0) { |summary, result| pp summary_result.call(result) result_key = result_type(result) summary.merge( :total => summary[:total].next, result_key => summary.fetch(result_key, 0).next ) } end |
.unstrument(method_or_methods = nil) ⇒ Array<Method>
Undoes instrument on the method_or_methods, specified as in instrument. With no args, unstruments all instrumented methods.
98 99 100 101 102 103 104 105 106 |
# File 'lib/speculation/test.rb', line 98 def self.unstrument(method_or_methods = nil) method_or_methods ||= @instrumented_methods.value.keys Array(method_or_methods). map { |method| S.Identifier(method) }. map { |ident| unstrument1(ident) }. compact. map(&method(:Method)) end |
.with_instrument_disabled ⇒ Object
Disables instrument’s checking of calls within a block
25 26 27 28 29 30 |
# File 'lib/speculation/test.rb', line 25 def self.with_instrument_disabled instrument_enabled.value = false yield ensure instrument_enabled.value = true end |