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
- #emit(*expected) ⇒ Object
- #emit_csv(*expected) ⇒ Object
- #emit_delimited(delimiter, *expected) ⇒ Object
- #emit_json(*expected) ⇒ Object
- #emit_tsv(*expected) ⇒ Object
-
#runner(klass, program_name, *args, &block) ⇒ Object
Create and boot up a runner of the given
klass. -
#unit_test_runner(*args) {|runner.driver.processor| ... } ⇒ Object
(also: #processor)
Create a runner for unit tests in a variety of convenient ways.
Instance Method Details
#emit(*expected) ⇒ Object
112 113 114 |
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 112 def emit *expected UnitTestMatcher.new(*expected) end |
#emit_csv(*expected) ⇒ Object
128 129 130 |
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 128 def emit_csv *expected CsvMatcher.new(*expected) end |
#emit_delimited(delimiter, *expected) ⇒ Object
120 121 122 |
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 120 def emit_delimited delimiter, *expected DelimiterMatcher.new(delimiter, *expected) end |
#emit_json(*expected) ⇒ Object
116 117 118 |
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 116 def emit_json *expected JsonMatcher.new(*expected) end |
#emit_tsv(*expected) ⇒ Object
124 125 126 |
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 124 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.
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. 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) {|runner.driver.processor| ... } ⇒ 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 |
# File 'lib/wukong/spec_helpers/unit_tests.rb', line 102 def unit_test_runner *args settings = args. name = (args.first || self.class.description) runner = UnitTestRunner.new(name, settings) yield runner.driver.processor if block_given? runner.boot! runner.driver end |