22
33require_relative '../helper'
44
5- require 'csv'
6-
75class TestFilter < Test ::Unit ::TestCase
86
97 def setup
10- @rows = [
11- %w[ aaa bbb ccc ] ,
12- %w[ ddd eee fff ] ,
13- ]
14- end
15-
16- # Return CSV string generated from rows array and options.
17- def make_csv_s ( rows : Rows , **options )
18- CSV . generate ( **options ) do |csv |
19- rows . each do |row |
20- csv << row
21- end
22- end
8+ @input = [
9+ %w[ aaa bbb ccc ] . join ( ',' ) ,
10+ %w[ ddd eee fff ] . join ( ',' ) ,
11+ '' # Force trailing newline.
12+ ] . join ( "\n " )
2313 end
2414
2515 # Return filepath of file containing CSV data.
26- def csv_filepath ( csv_in_s , dirpath , option_sym )
27- filename = "#{ option_sym } .csv"
16+ def csv_filepath ( input , dirpath , option )
17+ filename = "#{ option } .csv"
2818 filepath = File . join ( dirpath , filename )
29- File . write ( filepath , csv_in_s )
19+ File . write ( filepath , input )
3020 filepath
3121 end
3222
3323 # Return stdout and stderr from CLI execution.
3424 def run_csv_filter ( filepath , *cli_option_names )
3525 top_dir = File . join ( __dir__ , ".." , ".." , ".." )
36- command_line_s = [
26+ command_line = [
3727 Gem . ruby ,
3828 "-I" ,
3929 File . join ( top_dir , "lib" ) ,
@@ -43,7 +33,7 @@ def run_csv_filter(filepath, *cli_option_names)
4333 ] . join ( ' ' )
4434 Tempfile . create ( "stdout" , mode : File ::RDWR ) do |stdout |
4535 Tempfile . create ( "stderr" , mode : File ::RDWR ) do |stderr |
46- status = system ( command_line_s , { 1 => stdout , 2 => stderr } )
36+ status = system ( command_line , { 1 => stdout , 2 => stderr } )
4737 stdout . rewind
4838 stderr . rewind
4939 [ stdout . read , stderr . read ]
@@ -53,51 +43,50 @@ def run_csv_filter(filepath, *cli_option_names)
5343
5444 # Return results for CLI-only option (or invalid option).
5545 def results_for_cli_option ( option_name )
56- cli_out_s = ''
57- cli_err_s = ''
46+ output = ''
47+ error = ''
5848 Dir . mktmpdir do |dirpath |
5949 sym = option_name . to_sym
6050 filepath = csv_filepath ( '' , dirpath , sym )
61- cli_out_s , cli_err_s = run_csv_filter ( filepath , [ option_name ] )
51+ output , error = run_csv_filter ( filepath , [ option_name ] )
6252 end
63- [ cli_out_s , cli_err_s ]
53+ [ output , error ]
6454 end
6555
6656 # Get and return the actual output from the API.
67- def get_via_api ( csv_in_s , **api_options )
68- cli_out_s = ''
69- CSV . filter ( csv_in_s , cli_out_s , **api_options ) { |row | }
70- cli_out_s
57+ def api_output ( input , **api_options )
58+ output = ''
59+ CSV . filter ( input , output , **api_options ) { |row | }
60+ output
7161 end
7262
7363 # Test for invalid option.
7464
7565 def test_invalid_option
76- cli_out_s , cli_err_s = results_for_cli_option ( '-Z' )
77- assert_equal ( "" , cli_out_s )
78- assert_match ( /OptionParser::InvalidOption/ , cli_err_s )
66+ output , error = results_for_cli_option ( '-Z' )
67+ assert_equal ( "" , output )
68+ assert_match ( /OptionParser::InvalidOption/ , error )
7969 end
8070
8171 # Test for no options.
8272
8373 def test_no_options
84- csv_in_s = make_csv_s
85- cli_out_s = get_via_api ( csv_in_s )
86- assert_equal ( csv_in_s , cli_out_s )
74+ output = api_output ( @input )
75+ assert_equal ( @input , output )
8776 end
8877
8978 # Tests for general options.
9079
9180 def test_option_h
9281 output , error = results_for_cli_option ( '-h' )
93- assert_equal ( "Usage: csv-filter [options]\n " , cli_out_s . lines . first )
94- assert_equal ( '' , cli_err_s )
82+ assert_equal ( "Usage: csv-filter [options]\n " , output . lines . first )
83+ assert_equal ( '' , error )
9584 end
9685
9786 def test_option_v
98- cli_out_s , cli_err_s = results_for_cli_option ( '-v' )
99- assert_match ( /\d +\. \d +\. \d +/ , cli_out_s )
100- assert_equal ( '' , cli_err_s )
87+ output , error = results_for_cli_option ( '-v' )
88+ assert_match ( /\d +\. \d +\. \d +/ , output )
89+ assert_equal ( '' , error )
10190 end
10291
10392end
0 commit comments