Class: Quarry::Design::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/quarry/design/spec.rb

Overview

Design specification. Sepcification can contains sub-specifications.

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, description, context = nil, &specification) ⇒ Specification

New Specification



44
45
46
47
48
49
50
51
# File 'lib/quarry/design/spec.rb', line 44

def initialize(relation, description, context=nil, &specification)
  @relation       = relation
  @description    = description
  @specification  = specification
  @context        = context
  @before         = context.before if context
  @after          = context.after  if context
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



40
41
42
# File 'lib/quarry/design/spec.rb', line 40

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



39
40
41
# File 'lib/quarry/design/spec.rb', line 39

def before
  @before
end

#contextObject (readonly)

Returns the value of attribute context.



38
39
40
# File 'lib/quarry/design/spec.rb', line 38

def context
  @context
end

#descriptionObject (readonly)

Returns the value of attribute description.



36
37
38
# File 'lib/quarry/design/spec.rb', line 36

def description
  @description
end

#relationObject (readonly)

Returns the value of attribute relation.



35
36
37
# File 'lib/quarry/design/spec.rb', line 35

def relation
  @relation
end

#specificationObject (readonly)

Returns the value of attribute specification.



37
38
39
# File 'lib/quarry/design/spec.rb', line 37

def specification
  @specification
end

Instance Method Details

#check(level = 0) ⇒ Object

Check Specification



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
# File 'lib/quarry/design/spec.rb', line 55

def check(level=0)
  padding = " " * 2 * level

  clause  = "#{relation} #{description}".strip

  new_context = Context.new
  new_context.instance_eval(&before) if before
  begin
    new_context.instance_eval(&specification)
    puts ("=" * (level+1)) + " " + clause.gsub(/\b\w/){$&.upcase}
  rescue Assertion => e
    #puts padding + " (FAIL) [#{e.backtrace[2]}] " + clause
    puts "* " + clause.gsub('_',' ') + " [#{e.backtrace[2]}]"
    puts e if $VERBOSE
  rescue Exception => e
    raise e if $DEBUG
    print padding
    #puts padding + " (ERROR) [#{e.backtrace[2]}] " + clause
    puts padding + " (ERROR) #{e}"
    puts e.backtrace if $VERBOSE
  ensure
    new_context.instance_eval(&after) if after
  end

  new_context.specifications.each do |s|
    s.check(level+1)
  end
end

#outline(level = 0) ⇒ Object

Outline Specification



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/quarry/design/spec.rb', line 86

def outline(level=0)
  padding = (" " * 2 * level) + "- "
  context = Context.new
  begin
    context.instance_eval(&specification)
  rescue Assertion => e
  #rescue Exception => e
  end

  str = ''
  str << "#{relation} " if relation
  str << "#{description}"
  #str = str.strip #.gsub(/\b\w/){$&.upcase}

  puts padding + str.strip

  context.specifications.each do |s|
    s.outline(level+1)
  end
end