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, #settings

Instance Method Summary collapse

Methods included from DriverMethods

#add_serialization, #build_serializer, #construct_dataflow, #driver, #finalize_and_stop_dataflow, #finalize_dataflow, #lookup, #lookup_and_build, #setup_dataflow

Constructor Details

#initialize(label, settings) ⇒ UnitTestDriver

Returns a new instance of UnitTestDriver.



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

def initialize label, settings
  super()
  @settings = settings
  @dataflow = construct_dataflow(label, settings)
  setup_dataflow
end

Instance Attribute Details

#given_recordsObject (readonly)

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



41
42
43
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 41

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:



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

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:



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

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

#finalizeObject



17
18
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 17

def finalize
end

#given(*records) ⇒ Object

Give a collection of records to the processor.

Parameters:

  • records (Array)


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

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)


82
83
84
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 82

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)


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

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)


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

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)


74
75
76
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 74

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:



136
137
138
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 136

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:



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

def output
  run
  self
end

#process(output) ⇒ Object



23
24
25
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 23

def process output
  self << output
end

#processorObject



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

def processor
  dataflow.first
end

#runObject



27
28
29
30
31
32
33
34
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 27

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

#setupObject



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

def setup
end

#stopObject



20
21
# File 'lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb', line 20

def stop
end

#tsv_outputArray<String>

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

Returns:

See Also:



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

def tsv_output
  delimited_output("\t")
end