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.



36
37
38
39
40
41
42
43
# File 'lib/tryouts/tryout.rb', line 36

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, @locals = [], [], {}
  @passed, @failed, @skipped = 0, 0, 0
  @drill_context = DrillContext.new
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

#drill_contextObject (readonly)

The instance of Drill::Context in which the drills will run in.



34
35
36
# File 'lib/tryouts/tryout.rb', line 34

def drill_context
  @drill_context
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.



108
109
110
111
112
113
114
115
# File 'lib/tryouts/tryout.rb', line 108

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.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tryouts/tryout.rb', line 159

def dream(*args, &definition) 
  if definition.nil?
    args = args.size == 1 ? [args.first] : args.reverse
    dobj = Tryouts::Drill::Dream.new(*args)
  else
    msg = "Dreams with blocks take only 1 argument (Tryout: '#{@name}')"
    raise TooManyArgs, msg if args.size > 1
    dobj = Tryouts::Drill::Dream.new
    dobj.format = args.first if args.size == 1
    dobj.output_block = definition
  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.



143
144
145
146
147
148
149
# File 'lib/tryouts/tryout.rb', line 143

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



49
50
51
52
53
54
55
56
# File 'lib/tryouts/tryout.rb', line 49

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tryouts/tryout.rb', line 79

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
  if Tryouts.verbose > 0
    # 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
end

#runObject

Execute all Drill objects



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

def run
  @drill_context.instance_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 @drill_context
    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
  @drill_context.instance_eval &clean if clean.is_a?(Proc)
end

#set(key, value) ⇒ Object

Define a method named key for only the current instances of Tryout and DrillContext so it’s not available anywhere else. The methods return value.



122
123
124
125
126
# File 'lib/tryouts/tryout.rb', line 122

def set(key, value)
  self.meta_def( key )           { value }
  @drill_context.meta_def( key ) { value }
  value
end

#success?Boolean

Did every Tryout finish successfully?

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/tryouts/tryout.rb', line 100

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



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

def xdream(*args, &b); end

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

A quick way to comment out a drill



151
152
153
154
# File 'lib/tryouts/tryout.rb', line 151

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