Module: Kintama

Includes:
Mocha::API
Defined in:
lib/kintama.rb,
lib/kintama/test.rb,
lib/kintama/runner.rb,
lib/kintama/context.rb,
lib/kintama/reporter.rb,
lib/kintama/runnable.rb,
lib/kintama/assertions.rb

Defined Under Namespace

Modules: Assertions, Context, Test Classes: Reporter, Runnable, Runner, TestFailure

Class Method Summary collapse

Class Method Details

.add_exit_hookObject

Adds the hook to automatically run all known tests using #run when ruby exits; this is most useful when running a test file from the command line or from within an editor



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

def add_exit_hook
  return if @__added_exit_hook
  at_exit { exit(options.runner.with(Kintama.default_context).run(options.reporter) ? 0 : 1) }
  @__added_exit_hook = true
end

.context(name, parent = default_context, &block) ⇒ Object Also known as: testcase, describe

Create a new context. Aliases are ‘testcase’ and ‘describe’



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

def context(name, parent=default_context, &block)
  default_context.context(name, parent, &block)
end

.default_contextObject



19
20
21
22
# File 'lib/kintama.rb', line 19

def default_context
  reset unless @default_context
  @default_context
end

.extend(mod) ⇒ Object

Make new testing behaviour available for the definition of tests. Methods included in this way are available during the definition of tests.



61
62
63
# File 'lib/kintama.rb', line 61

def extend(mod)
  default_context.extend(mod)
end

.given(name, parent = default_context, &block) ⇒ Object

Create a new context starting with “given ”



32
33
34
# File 'lib/kintama.rb', line 32

def given(name, parent=default_context, &block)
  default_context.given(name, parent, &block)
end

.include(mod) ⇒ Object

Makes behaviour available within tests:

module SomeModule
  def blah
  end
end
Kintama.include SomeModule

Any methods will then be available within setup, teardown or tests.



55
56
57
# File 'lib/kintama.rb', line 55

def include(mod)
  default_context.send(:include, mod)
end

.optionsObject



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
93
94
95
# File 'lib/kintama.rb', line 65

def options
  unless @options
    @options = OpenStruct.new(
      :reporter => Kintama::Reporter.default,
      :runner => Kintama::Runner.default
    )
    opts = OptionParser.new do |opts|
      opts.banner = "Usage: ruby <test_file> [options]"

      opts.separator ""
      opts.separator "Specific options:"

      opts.on("-r", "--reporter NAME",
              "Use the given reporter (inline or verbose)") do |reporter|
              puts "reporter!"
        options.reporter = Kintama::Reporter.called(reporter)
        p options.reporter
      end
      opts.on("-l", "--line LINE",
              "Run the test or context on the given line") do |line|
        options.runner = Kintama::Runner::Line.new(line)
      end
      opts.on_tail("-h", "--help", "Show this message") do
        puts opts
        exit
      end
    end
    opts.parse!(ARGV)
  end
  @options
end

.resetObject



14
15
16
17
# File 'lib/kintama.rb', line 14

def reset
  @default_context = Class.new(Runnable)
  @default_context.send(:include, Kintama::Context)
end

.setup(&block) ⇒ Object

Add a setup which will run at the start of every test.



37
38
39
# File 'lib/kintama.rb', line 37

def setup(&block)
  default_context.setup(&block)
end

.should_run_on_exit?Boolean

Tries to determine whether or not this is a sensible situation to automatically run all tests when ruby exits. At the moment, this is true when either:

  • the test was run via rake

  • the test file was run as the top-level ruby script

This method will always return false if the environment variable KINTAMA_EXPLICITLY_DONT_RUN is not nil.

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/kintama.rb', line 113

def should_run_on_exit?
  return false if ENV["KINTAMA_EXPLICITLY_DONT_RUN"]
  return test_file_was_run? || run_via_rake?
end

.teardown(&block) ⇒ Object

Add a teardown which will be run at the end of every test.



42
43
44
# File 'lib/kintama.rb', line 42

def teardown(&block)
  default_context.teardown(&block)
end