Module: CommandUnit

Defined in:
lib/command-unit.rb,
lib/command-unit/test.rb,
lib/command-unit/hooks.rb,
lib/command-unit/scenario.rb,
lib/command-unit/totaliser.rb,
lib/command-unit/expectation.rb,
lib/command-unit/expectation_result.rb,
lib/command-unit/expectation_helpers.rb

Defined Under Namespace

Classes: Expectation, ExpectationResult, Hooks, Scenario, Test, Totaliser

Constant Summary collapse

@@scenario_count =
0
@@scenarios =
[]
@@current_scenario =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_scenarioObject (readonly)

Returns the value of attribute current_scenario.



15
16
17
# File 'lib/command-unit.rb', line 15

def current_scenario
  @current_scenario
end

Instance Method Details

#contains(this_thing) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/command-unit/expectation_helpers.rb', line 40

def contains(this_thing)
	return Proc.new do |collection|
		if not collection.respond_to? :include?
			[false, "Unable to test if '#{collection}' contains '#{this_thing}' as it has no include? method."]
		elsif collection.include? this_thing
			true
		else
			[false, "Expected '#{collection}' to contain '#{this_thing}'."]
		end
	end
end

#ensure_inside_scenarioObject



71
72
73
# File 'lib/command-unit.rb', line 71

def ensure_inside_scenario
  raise "#{caller[0]} must be called from inside a scenario block" if @@current_scenario == nil
end

#expect(thing, &proc) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/command-unit/expectation_helpers.rb', line 11

def expect(thing, &proc)
	if proc.nil?
		if thing == true
			return ExpectationResult.new('', true)
		elsif thing == false
			return ExpectationResult.new('Expected true but was false.', false)
		else
			raise "CommandUnit::expect requires either true, false or any other value with a Proc"
		end
	end

	result = proc.call(thing)
	if result.is_a? Array
		return ExpectationResult.new(result[1], result[0])
	else
		return ExpectationResult.new('', result)	
	end
end

#fail(desc = '') ⇒ Object



7
8
9
# File 'lib/command-unit/expectation_helpers.rb', line 7

def fail(desc = '')
	ExpectationResult.new(desc, false)
end

#i_expect(desc, &i_expect_block) ⇒ Object



100
101
102
103
# File 'lib/command-unit.rb', line 100

def i_expect(desc, &i_expect_block)
  ensure_inside_scenario
  @@current_scenario.current_test.add_expectation Expectation.new(desc, &i_expect_block)
end

#is_equal_to(this_thing) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/command-unit/expectation_helpers.rb', line 30

def is_equal_to(this_thing)
	return Proc.new do |other_thing|
		if other_thing == this_thing
			true
		else
			[false, "Expecting exactly '#{this_thing}', but got '#{other_thing}'"]
		end
	end
end

#pass(desc = '') ⇒ Object



3
4
5
# File 'lib/command-unit/expectation_helpers.rb', line 3

def pass(desc = '')
	ExpectationResult.new(desc, true)
end

#run(namespace_or_scenario_or_nowt = nil, out_stream = STDOUT) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/command-unit.rb', line 40

def run(namespace_or_scenario_or_nowt=nil, out_stream=STDOUT)
  hooks = Hooks.new
  if namespace_or_scenario_or_nowt.nil?
    # Run the lot...
    out_stream.puts "\nRunning #{@@scenarios.count} scenarios..."
    @@scenarios.each do |scenario|
      scenario.run(hooks, out_stream)
    end
  else
    if namespace_or_scenario_or_nowt.is_a? Symbol
      namespace = namespace_or_scenario_or_nowt
      scenarios_in_namespace = @@scenarios.select { |s| s.namespace == namespace }
      out_stream.puts "\nRunning #{scenarios_in_namespace.length} scenarios in namespace '#{namespace}'..."
      scenarios_in_namespace.each do |scenario|
        scenario.run(hooks, out_stream)
      end
    elsif namespace_or_scenario.is_a? Scenario
      scenario = namespace_or_scenario
      out_stream.puts "\nRunning single scenario..."
      scenario.run(hooks, out_stream)
    else
      raise "You must pass either a Scenario, a Symbol (namespace), or nil into run. You passed a #{namespace_or_scenario_or_nowt.class}"
    end
  end

  t = hooks.totaliser
  message = "\nRan #{t.scenarios_run} scenarios, #{t.scenarios_passed} passed, #{t.scenarios_failed} failed (tests passed: #{t.tests_passed}, failed: #{t.tests_failed}) (expectations passed: #{t.expectations_passed}, failed: #{t.expectations_failed})\n"

  out_stream.puts message
end

#run_silent(namespace_or_nil = nil) ⇒ Object



34
35
36
37
38
# File 'lib/command-unit.rb', line 34

def run_silent(namespace_or_nil=nil)
  output = StringIO.new
  run(namespace_or_nil, output)
  return output.string
end

#scenario(namespace_or_description, description_or_nil = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/command-unit.rb', line 17

def scenario(namespace_or_description, description_or_nil=nil, &block)
  raise "You must provide a description (String) for each scenario." if namespace_or_description.nil? or description_or_nil.is_a? Symbol
  raise "namespace must be a symbol, if you meant it as a description, use a string instead." if description_or_nil.is_a? String and not namespace_or_description.is_a? Symbol

  if description_or_nil.nil?
    description = namespace_or_description
    namespace = nil
  else
    description = description_or_nil
    namespace = namespace_or_description
  end

  @@scenarios.push Scenario.new(namespace, description, STDOUT, &block)

  return @@scenarios.last
end

#scenario_set_up(&scenario_set_up_block) ⇒ Object



75
76
77
78
# File 'lib/command-unit.rb', line 75

def scenario_set_up(&scenario_set_up_block)
  ensure_inside_scenario
  @@current_scenario.scenario_set_up_block = scenario_set_up_block
end

#scenario_tear_down(&scenario_tear_down_block) ⇒ Object



80
81
82
83
# File 'lib/command-unit.rb', line 80

def scenario_tear_down(&scenario_tear_down_block)
  ensure_inside_scenario
  @@current_scenario.scenario_tear_down_block = scenario_tear_down_block
end

#set_up(&set_up_block) ⇒ Object



90
91
92
93
# File 'lib/command-unit.rb', line 90

def set_up(&set_up_block)
  ensure_inside_scenario
  @@current_scenario.set_up_block = set_up_block
end

#tear_down(&tear_down_block) ⇒ Object



85
86
87
88
# File 'lib/command-unit.rb', line 85

def tear_down(&tear_down_block)
  ensure_inside_scenario
  @@current_scenario.tear_down_block = tear_down_block
end

#when_i(desc, &when_i_block) ⇒ Object



95
96
97
98
# File 'lib/command-unit.rb', line 95

def when_i(desc, &when_i_block)
  ensure_inside_scenario
  @@current_scenario.add_test Test.new(desc, &when_i_block)
end