Class: Wukong::SpecHelpers::UnitTestDriver

Inherits:
Array
  • Object
show all
Includes:
DriverMethods
Defined in:
lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb

Instance Attribute Summary collapse

Attributes included from DriverMethods

#dataflow, #label, #settings

Instance Method Summary collapse

Methods included from DriverMethods

#construct_dataflow, #finalize, #finalize_and_stop_dataflow, #finalize_dataflow, #send_through_dataflow, #setup, #setup_dataflow, #stop

Constructor Details

#initialize(label, settings = {}) ⇒ UnitTestDriver

Returns a new instance of UnitTestDriver.



7
8
9
10
11
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 7

def initialize label, settings={}
  super()
  construct_dataflow(label, settings)
  setup_dataflow
end

Instance Attribute Details

#given_recordsObject (readonly)

An array of accumulated records to process come match-time.



31
32
33
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 31

def given_records
  @given_records
end

Instance Method Details

#csv_outputArray<String>

Return the output of the processor on the given records, parsing as CSV first.

Returns:

See Also:



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

def csv_output
  delimited_output(",")
end

#delimited_output(delimiter) ⇒ Array<String>

Return the output of the processor on the given records, parsing as a string with the given delimiter first.

Parameters:

Returns:

See Also:



97
98
99
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 97

def delimited_output(delimiter)
  output.map { |record| record.split(delimiter) }
end

#given(*records) ⇒ Object

Give a collection of records to the processor.

Parameters:

  • records (Array)


36
37
38
39
40
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 36

def given *records
  @given_records ||= []
  @given_records.concat(records)
  self                    # for chaining
end

#given_csv(*records) ⇒ Object

Give a collection of records to the processor but join each in CSV format first.

Parameters:

  • records (Array)


72
73
74
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 72

def given_csv *records
  self.given_delimited(",", *records)
end

#given_delimited(delimiter, *records) ⇒ Object

Give a collection of records to the processor but join each in a delimited format first.

Parameters:

  • records (Array)


54
55
56
57
58
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 54

def given_delimited delimiter, *records
  self.given(*records.map do |record|
               record.map(&:to_s).join(delimiter)
             end.join("\n"))
end

#given_json(*records) ⇒ Object

Give a collection of records to the processor but turn each to JSON first.

Parameters:

  • records (Array)


46
47
48
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 46

def given_json *records
  self.given(*records.map { |record| MultiJson.dump(record) })
end

#given_tsv(*records) ⇒ Object

Give a collection of records to the processor but join each in TSV format first.

Parameters:

  • records (Array)


64
65
66
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 64

def given_tsv *records
  self.given_delimited("\t", *records)
end

#json_outputHash, Array

Return the output of the processor on the given records, parsing as JSONS first.

Returns:

  • (Hash, Array)

See Also:



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

def json_output
  output.map { |record| MultiJson.load(record) }
end

#outputUnitTestDriver

Return the output of the processor on the given records.

Calling this method, like passing the processor to an emit matcher, will trigger processing of all the given records.

Returns a UnitTestDriver, which is a subclass of array, so the usual matchers like include and so on should work, as well as explicitly indexing to introspect on particular records.

Returns:



86
87
88
89
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 86

def output
  run
  self
end

#process(output) ⇒ Object



13
14
15
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 13

def process output
  self << output
end

#processorObject



26
27
28
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 26

def processor
  dataflow.root
end

#runObject



17
18
19
20
21
22
23
24
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 17

def run
  return false unless dataflow
  given_records.each do |input|
    send_through_dataflow(input)
  end
  finalize_and_stop_dataflow
  self
end

#tsv_outputArray<String>

Return the output of the processor on the given records, parsing as TSV first.

Returns:

See Also:



107
108
109
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 107

def tsv_output
  delimited_output("\t")
end