Class: Tryouts::Drill
- Inherits:
-
Object
- Object
- Tryouts::Drill
- Defined in:
- lib/tryouts/drill.rb,
lib/tryouts/drill/response.rb,
lib/tryouts/drill/sergeant/api.rb,
lib/tryouts/drill/sergeant/cli.rb,
lib/tryouts/drill/sergeant/benchmark.rb,
lib/tryouts/drill/sergeant/rbenchmark.rb
Overview
Drill
This class represents a drill. A drill is single test.
Defined Under Namespace
Modules: Sergeant Classes: Dream, NoSergeant, Reality, Response, UnknownFormat
Constant Summary collapse
- @@valid_dtypes =
[:api, :cli, :benchmark]
Instance Attribute Summary collapse
-
#dreams ⇒ Object
readonly
An Array of Dream objects (the expected output of the test).
-
#drill ⇒ Object
readonly
A Proc object which contains the drill logic.
-
#dtype ⇒ Object
readonly
A symbol specifying the drill type.
-
#name ⇒ Object
readonly
The name of the drill.
-
#reality ⇒ Object
readonly
A Reality object (the actual output of the test).
-
#sergeant ⇒ Object
readonly
A Sergeant object which executes the drill.
Class Method Summary collapse
Instance Method Summary collapse
- #add_dream(d) ⇒ Object
- #add_dreams(*d) ⇒ Object
- #flag ⇒ Object
- #has_error? ⇒ Boolean
- #info ⇒ Object
-
#initialize(name, dtype, *args, &drill) ⇒ Drill
constructor
-
name
The display name of this drill *dtype
A Symbol representing the drill type.
-
- #report ⇒ Object
- #run(context = nil) ⇒ Object
- #skip? ⇒ Boolean
- #success? ⇒ Boolean
Constructor Details
#initialize(name, dtype, *args, &drill) ⇒ Drill
-
name
The display name of this drill -
dtype
A Symbol representing the drill type. One of: :api, :benchmark -
args
These are dependent on the drill type. See the Sergeant classes -
&drill The body of the drill. The return value of this block is compared to the exepected output of the dreams.
The DSL syntax:
-
dream OUTPUT
-
dream FORMAT, OUTPUT
-
dream FORMAT, OUTPUT, REPS (benchmark only)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/tryouts/drill.rb', line 48 def initialize(name, dtype, *args, &drill) @name, @dtype, @drill, @skip = name, dtype, drill, false @dreams = [] # We create a default empty reality but if the drill runs correctly # this reality gets replaced with the return value from the drill. @reality = Tryouts::Drill::Reality.new case @dtype when :cli # For CLI drills, a block takes precedence over inline args. # A block will contain multiple shell commands (see Rye::Box#batch) args = [] if dtype == :cli && drill.is_a?(Proc) @sergeant = Tryouts::Drill::Sergeant::CLI.new *args when :api default_output = drill.nil? ? args.shift : nil @sergeant = Tryouts::Drill::Sergeant::API.new default_output unless args.empty? if args.size == 1 dream_output, format = args.first, nil else dream_output, format = args[1], args[0] end @dreams << Tryouts::Drill::Dream.new(dream_output, format) end when :benchmark if args.size == 1 reps = args.first else dream_output, format, reps = args[1], args[0], args[2] end @sergeant = Tryouts::Drill::Sergeant::Benchmark.new reps @dreams << Tryouts::Drill::Dream.new(Tryouts::Stats, :class) unless dream_output.nil? @dreams << Tryouts::Drill::Dream.new(dream_output, format) end when :skip @skip = true else raise NoSergeant, "Weird drill sergeant: #{@dtype}" end @clr = :red end |
Instance Attribute Details
#dreams ⇒ Object (readonly)
An Array of Dream objects (the expected output of the test)
31 32 33 |
# File 'lib/tryouts/drill.rb', line 31 def dreams @dreams end |
#drill ⇒ Object (readonly)
A Proc object which contains the drill logic.
26 27 28 |
# File 'lib/tryouts/drill.rb', line 26 def drill @drill end |
#dtype ⇒ Object (readonly)
A symbol specifying the drill type. One of: :cli, :api
22 23 24 |
# File 'lib/tryouts/drill.rb', line 22 def dtype @dtype end |
#name ⇒ Object (readonly)
The name of the drill. This should match the name used in the dreams file.
24 25 26 |
# File 'lib/tryouts/drill.rb', line 24 def name @name end |
#reality ⇒ Object (readonly)
A Reality object (the actual output of the test)
33 34 35 |
# File 'lib/tryouts/drill.rb', line 33 def reality @reality end |
#sergeant ⇒ Object (readonly)
A Sergeant object which executes the drill
29 30 31 |
# File 'lib/tryouts/drill.rb', line 29 def sergeant @sergeant end |
Class Method Details
.valid_dtype?(t) ⇒ Boolean
95 |
# File 'lib/tryouts/drill.rb', line 95 def self.valid_dtype?(t); @@valid_dtypes.member?(t); end |
.valid_dtypes ⇒ Object
94 |
# File 'lib/tryouts/drill.rb', line 94 def self.valid_dtypes; @@valid_dtypes; end |
Instance Method Details
#add_dream(d) ⇒ Object
209 |
# File 'lib/tryouts/drill.rb', line 209 def add_dream(d); @dreams << d; end |
#add_dreams(*d) ⇒ Object
210 |
# File 'lib/tryouts/drill.rb', line 210 def add_dreams(*d); @dreams += d; end |
#flag ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/tryouts/drill.rb', line 116 def flag if skip? "SKIP" elsif success? "PASS".color(@clr).bright else note = @dreams.empty? ? '[nodream]' : '' "FAIL #{note}".color(@clr).bright end end |
#has_error? ⇒ Boolean
192 193 194 |
# File 'lib/tryouts/drill.rb', line 192 def has_error? !@reality.error.nil? end |
#info ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/tryouts/drill.rb', line 127 def info out = StringIO.new if Tryouts.verbose > 0 if @dtype == :benchmark unless @reality.output.nil? mean, sdev, sum = @reality.output.mean, @reality.output.sdev, @reality.output.sum out.puts '%6s%.4f (sdev:%.4f sum:%.4f)'.color(@clr) % ['', mean, sdev, sum] end elsif @dtype == :cli out.puts '%6s%s'.color(@clr) % ['', @reality.command] output = @reality.output output = output.join($/ + ' '*6) if output.kind_of?(Array) out.puts '%6s%s'.color(@clr) % ['', output] else out.puts '%6s%s'.color(@clr) % ['', @reality.output.inspect] end unless @reality.stash.empty? @reality.stash.each_pair do |n,v| out.puts '%18s: %s'.color(@clr) % [n,v.inspect] end end end if Tryouts.verbose > 1 @dreams.each do |dream| if dream != @reality out.puts '%6s%s'.color(:red) % ['', dream.test_to_string(@reality)] else out.puts '%6s%s'.color(:green) % ["", dream.test_to_string(@reality)] end end out.puts end out.rewind out.read end |
#report ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/tryouts/drill.rb', line 165 def report return if skip? out = StringIO.new out.puts '%12s'.color(:red) % '[nodream]' if @dreams.empty? @dreams.each do |dream| next if dream == reality #? :normal : :red out.puts '%12s: %s'.color(@clr) % ["failed", dream.test_to_string(@reality)] out.puts '%12s: %s' % ["drill", @reality.comparison_value(dream).inspect] out.puts '%12s: %s' % ["dream", dream.comparison_value.inspect] out.puts end unless @reality.error.nil? out.puts '%14s: %s' % [@reality.etype, @reality.error.to_s.split($/).join($/ + ' '*16)] end unless @reality.trace.nil? trace = Tryouts.verbose > 1 ? @reality.trace : [@reality.trace.first] out.puts '%14s %s' % ['', trace.join($/ + ' '*16)] out.puts end out.rewind out.read end |
#run(context = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tryouts/drill.rb', line 99 def run(context=nil) begin @reality = @sergeant.run @drill, context # Store the stash from the drill block @reality.stash = context.stash if context.respond_to? :stash # If the drill block returned true we assume success if there's no dream if @dreams.empty? && @reality.output == true @dreams << Tryouts::Drill::Dream.new @dreams.first.output = true end rescue => ex @reality.ecode, @reality.etype = -2, ex.class @reality.error, @reality.trace = ex., ex.backtrace end self.success? end |
#skip? ⇒ Boolean
97 |
# File 'lib/tryouts/drill.rb', line 97 def skip?; @skip; end |
#success? ⇒ Boolean
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/tryouts/drill.rb', line 196 def success? return false if @dreams.empty? && @reality.output != true begin @dreams.each { |d| return false unless d == @reality } rescue => ex puts ex., ex.backtrace if Tryouts.debug? return false end @clr = :green true end |