Module: Wukong::SpecHelpers::UnitTests

Included in:
Wukong::SpecHelpers
Defined in:
lib/wukong/spec_helpers/unit_tests.rb

Overview

This module defines helpers that are useful when running unit tests for processors.

Instance Method Summary collapse

Instance Method Details

#emit(*expected) ⇒ Object



113
114
115
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 113

def emit *expected
  UnitTestMatcher.new(*expected)
end

#emit_csv(*expected) ⇒ Object



129
130
131
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 129

def emit_csv *expected
  CsvMatcher.new(*expected)
end

#emit_delimited(delimiter, *expected) ⇒ Object



121
122
123
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 121

def emit_delimited delimiter, *expected
  DelimiterMatcher.new(delimiter, *expected)
end

#emit_json(*expected) ⇒ Object



117
118
119
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 117

def emit_json *expected
  JsonMatcher.new(*expected)
end

#emit_tsv(*expected) ⇒ Object



125
126
127
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 125

def emit_tsv *expected
  TsvMatcher.new(*expected)
end

#runner(klass, program_name, *args, &block) ⇒ Object

Create and boot up a runner of the given klass.

Options to the runner class are given in the args Array. The last element of this Array can be a Hash of options to directly pass to the runner (especially useful in unit tests). The rest of the elements are strings that will be parsed as though they were command-line arguments.

A passed block will be eval'd in the context of the newlyl created runner instance. This can be used to interact with the runner's insides after initialization.

Examples:

Create a runner that simulates wu-local with a set of arguments


runner Wukong::Local::LocalRunner, 'wu-local', '--foo=bar', '--baz=boof', wof: 'bing'

Create a custom runner and set a property on it


runner(CustomRunner, 'wu-custom', '--foo=bar') do
  # eval'd in scope of new runner instance
  do_some_special_thing!
end

Parameters:

  • klass (Class)
  • program_name (String)
  • args (Array<String>, Hash)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 38

def runner klass, program_name, *args, &block
  settings = args.extract_options!
  
  ARGV.replace(args.map(&:to_s))

  klass.new.tap do |the_runner|
    the_runner.program_name = program_name
    the_runner.instance_eval(&block) if block_given?
    the_runner.boot!(settings)
  end
end

#unit_test_runner(*args, &block) ⇒ Object Also known as: processor

Create a runner for unit tests in a variety of convenient ways.

Most simply, called without args, will return a UnitTestRunner a the klass named in the containing describe or context:

context MyApp::Tokenizer do it "uses whitespace as the default separator between tokens" do processor.separator.should == /\s+/ end end

if your processor has been registered (you created it with the Wukong.processor helper method or otherwise registered it yourself) then you can use its name:

context :tokenizer do it "uses whitespace as the default separator between tokens" do processor.separator.should == /\s+/ end end

The processor method can also be used inside RSpec's subject and let methods:

context "with no arguments" do subject { processor } it "uses whitespace as the default separator between tokens" do separator.should == /\s+/ end end end

and you can easily pass arguments, just like you would on the command line or in a dataflow definition:

context "with arguments" do subject { processor(separator: ' ') } it "uses whitespace as the default separator between tokens" do separator.should == ' ' end end end

You can even name the processor directly if you want to:

context "tokenizers" do let(:default_tokenizer) { processor(:tokenizer) } let(:complex_tokenizer) { processor(:complex_tokenizer, stemming: true) } let(:french_tokenizer) { processor(:complex_tokenizer, stemming: true, language: 'fr') } ... end



102
103
104
105
106
107
108
109
110
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 102

def unit_test_runner *args, &block
  settings = args.extract_options!
  name     = (args.first || self.class.description)
  UnitTestRunner.new(name, settings).tap do |the_runner|
    the_runner.program_name = 'wu-local'
    yield the_runner.driver.processor if block_given?
    the_runner.boot!(settings)
  end.driver
end