Class: Tryouts::Tryout

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts/tryout.rb,
lib/tryouts/drill/context.rb

Overview

Tryout

A Tryout is a set of drills (each drill is a test).

Defined Under Namespace

Classes: DrillContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, dtype, command = nil, *args) ⇒ Tryout

Returns a new instance of Tryout.



34
35
36
37
38
39
40
# File 'lib/tryouts/tryout.rb', line 34

def initialize(name, dtype, command=nil, *args)
  raise "Must supply command for dtype :cli" if dtype == :cli && command.nil?
  raise "#{dtype} is not a valid drill type" if !Drill.valid_dtype?(dtype)
  @name, @dtype, @command = name, dtype, command
  @drills, @dream_catcher = [], []
  @passed, @failed, @skipped = 0, 0, 0
end

Instance Attribute Details

#clean(&block) ⇒ Object (readonly)

A block to executed one time after the drills



17
18
19
# File 'lib/tryouts/tryout.rb', line 17

def clean
  @clean
end

#commandObject (readonly)

For drill type :cli, this is the name of the command to test. It should be a valid method available to a Rye::Box object. For drill type :api, this attribute is ignored.



29
30
31
# File 'lib/tryouts/tryout.rb', line 29

def command
  @command
end

#dream_catcherObject (readonly)

A Hash of Dream objects for this Tryout. The keys are drill names.



31
32
33
# File 'lib/tryouts/tryout.rb', line 31

def dream_catcher
  @dream_catcher
end

#drillsObject (readonly)

An Array of Drill objects



19
20
21
# File 'lib/tryouts/tryout.rb', line 19

def drills
  @drills
end

#dtypeObject (readonly)

A default value for Drill.dtype



13
14
15
# File 'lib/tryouts/tryout.rb', line 13

def dtype
  @dtype
end

#failedObject (readonly)

The number of dreams that did not come true (failed drills)



23
24
25
# File 'lib/tryouts/tryout.rb', line 23

def failed
  @failed
end

#nameObject (readonly)

The name of this tryout



11
12
13
# File 'lib/tryouts/tryout.rb', line 11

def name
  @name
end

#passedObject (readonly)

The number of dreams that came true (successful drills)



21
22
23
# File 'lib/tryouts/tryout.rb', line 21

def passed
  @passed
end

#setup(&block) ⇒ Object (readonly)

A block to executed one time before starting the drills



15
16
17
# File 'lib/tryouts/tryout.rb', line 15

def setup
  @setup
end

#skippedObject (readonly)

The number of skipped drills



25
26
27
# File 'lib/tryouts/tryout.rb', line 25

def skipped
  @skipped
end

Instance Method Details

#add_drill(d) ⇒ Object

Add a Drill object to the list for this Tryout. If there is one or more dreams in @dream_catcher, it will be added to drill d.



103
104
105
106
107
108
109
110
# File 'lib/tryouts/tryout.rb', line 103

def add_drill(d)
  unless @dream_catcher.empty?
    d.add_dreams *@dream_catcher.clone   # We need to clone here b/c
    @dream_catcher.clear                 # Ruby passes by reference.
  end
  drills << d
  d
end

#dream(*args, &definition) ⇒ Object

NOTE: This method is DSL-only. It’s not intended to be used in OO syntax.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tryouts/tryout.rb', line 145

def dream(*args, &definition) 
  if definition.nil?
    args = args.size == 1 ? [args.first] : args.reverse
    dobj = Tryouts::Drill::Dream.new(*args)
  else
    if args.size > 1
      raise "Dreams with a block can take only one argument (#{@name})"
    end
    dobj = Tryouts::Drill::Dream.from_block definition
    dobj.format = args.first if args.size == 1
  end
  @dream_catcher.push dobj
  dobj
end

#drill(dname, *args, &definition) ⇒ Object

Create and add a Drill object to the list for this Tryout name is the name of the drill. args is sent directly to the Drill class. The values are specific on the Sergeant.



129
130
131
132
133
134
135
# File 'lib/tryouts/tryout.rb', line 129

def drill(dname, *args, &definition)
  raise "Empty drill name (#{@name})" if dname.nil? || dname.empty?
  # The command name to run should be the first argument
  args.unshift @command if @dtype == :cli
  drill = Tryouts::Drill.new(dname, @dtype, *args, &definition)
  self.add_drill drill
end

#from_block(b = nil, &inline) ⇒ Object

Populate this Tryout from a block. The block should contain calls to the external DSL methods: dream, drill, xdrill



46
47
48
49
50
51
52
53
# File 'lib/tryouts/tryout.rb', line 46

def from_block(b=nil, &inline)
  runtime = b.nil? ? inline : b
  begin
    instance_eval &runtime
  rescue => ex
    raise ex
  end
end

#reportObject

Prints error output. If there are no errors, it prints nothing.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tryouts/tryout.rb', line 76

def report
  return if Tryouts.verbose < 0
  failed = @drills.select { |d| !d.skip? && !d.success? }
  failed.each_with_index do |drill,index|
    title = ' %-69s %2d/%-2d  ' % ["\"#{drill.name}\"", index+1, failed.size]
    puts $/, ' ' << title.color(:red).att(:reverse)
    puts drill.report
  end
  # Print errors for successful runs too
  success = @drills.select { |d| !d.skip? && d.success? }
  success.each do |drill,index|
    next unless drill.has_error?
    title = ' Non-fatal error in: %-69s ' % ["\"#{drill.name}\""]
    puts $/, ' ' << title.color(:red)
    puts drill.report
  end
end

#runObject

Execute all Drill objects



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tryouts/tryout.rb', line 56

def run
  DrillContext.module_eval &setup if setup.is_a?(Proc)
  puts "\n  %s ".bright % @name unless Tryouts.verbose < 0
  @drills.each do |drill|
    print '   %-69s ' % "\"#{drill.name}\"" unless Tryouts.verbose < 0
    drill.run DrillContext.new
    if drill.skip?
      @skipped += 1
    elsif drill.success?
      @passed += 1
    else
      @failed += 1
    end
    puts drill.flag                           # PASS, FAIL, SKIP
    puts drill.info if Tryouts.verbose > 0 && !drill.skip?  
  end
  DrillContext.module_eval &clean if clean.is_a?(Proc)
end

#success?Boolean

Did every Tryout finish successfully?

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/tryouts/tryout.rb', line 95

def success?
  return @success unless @success.nil?
  # Returns true only when every Tryout result returns true
  @success = !(@drills.collect { |r| r.success? }.member?(false))
end

#xdream(*args, &b) ⇒ Object

A quick way to comment out a dream



160
# File 'lib/tryouts/tryout.rb', line 160

def xdream(*args, &b); end

#xdrill(dname, *args, &b) ⇒ Object

A quick way to comment out a drill



137
138
139
140
# File 'lib/tryouts/tryout.rb', line 137

def xdrill(dname, *args, &b)
  @dream_catcher.clear     # Otherwise the next drill will get them...
  self.add_drill Tryouts::Drill.new(dname, :skip)
end