stats_package_syntax_file_generator 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/tests/tc_maker.rb ADDED
@@ -0,0 +1,172 @@
1
+ # This file is part of the Minnesota Population Center's stats_package_syntax_file_generator project.
2
+ # For copyright and licensing information, see the NOTICE and LICENSE files
3
+ # in this project's top-level directory, and also on-line at:
4
+ # https://github.com/mnpopcenter/stats_package_syntax_file_generator
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'setup.rb'))
7
+
8
+ module StatsPackageSyntaxFileGeneratorTest
9
+ class Maker < Test::Unit::TestCase
10
+
11
+ include StatsPackageSyntaxFileGeneratorTestSetup
12
+
13
+ def test_create_maker
14
+ msg = 'Try to create an object.'
15
+ mk = new_maker()
16
+ assert_instance_of StatsPackageSyntaxFileGenerator::Maker, mk, msg
17
+ end
18
+
19
+ def test_syntax_end
20
+ msg = 'Compare against hardcoded result.'
21
+ mk = new_maker()
22
+ mk.cmd_end = '.'
23
+ assert_equal ['.', ''], mk.syntax_end, msg
24
+ end
25
+
26
+ def test_q
27
+ msg = 'Compare against hardcoded result.'
28
+ mk = new_maker()
29
+ orig = %q{"A "quoted" 'string' with ""extra-quotes"""}
30
+ quoted = %q{"""A ""quoted"" 'string' with """"extra-quotes"""""""}
31
+ assert_equal quoted, mk.q(orig), msg
32
+ end
33
+
34
+ def test_val_q
35
+ msg = 'Numeric variable: the method should convert to string.'
36
+ mk = new_maker()
37
+ v = new_variable()
38
+ assert_equal '1234', mk.val_q(v, 1234), msg
39
+
40
+ msg = 'String variable: the method should convert to string and quote.'
41
+ v.is_string_var = true
42
+ assert_equal '"123456"', mk.val_q(v, 123456), msg
43
+ assert_equal '"-123456"', mk.val_q(v, -123456), msg
44
+ end
45
+
46
+ def test_val_as_s
47
+ msg = 'Numeric variable: the method should convert to string.'
48
+ mk = new_maker()
49
+ v = new_variable()
50
+ assert_equal '1234', mk.val_as_s(v, 1234), msg
51
+
52
+ msg = 'String variable, non-integer value: the method should convert to string.'
53
+ v.is_string_var = true
54
+ assert_equal 'ab', mk.val_as_s(v, 'ab'), msg
55
+ assert_equal '123.2', mk.val_as_s(v, 123.2), msg
56
+ assert_equal '', mk.val_as_s(v, ''), msg
57
+
58
+ msg = 'String variable, integer value: the method should convert to string and zero-pad.'
59
+ assert_equal '0012', mk.val_as_s(v, 12), msg
60
+ assert_equal '-034', mk.val_as_s(v, -34), msg
61
+ assert_equal '123456', mk.val_as_s(v, 123456), msg
62
+ assert_equal '-123456', mk.val_as_s(v, -123456), msg
63
+ end
64
+
65
+ def test_label_trunc
66
+ msg = 'Compare against hardcoded result.'
67
+ mk = new_maker()
68
+ assert_equal '12', mk.label_trunc(123, 2), msg
69
+ assert_equal '123', mk.label_trunc(123, 12), msg
70
+ assert_equal '', mk.label_trunc('', 12), msg
71
+ assert_equal 'foo_', mk.label_trunc('foo_bar', 4), msg
72
+ end
73
+
74
+ def test_label_segments
75
+ mk = new_maker()
76
+
77
+ msg = 'Non-string should be converted to string.'
78
+ assert_equal ['12345'], mk.label_segments(12345, 10), msg
79
+ assert_equal ['123', '45'], mk.label_segments(12345, 3), msg
80
+
81
+ msg = 'Empty string and nil should work.'
82
+ assert_equal [''], mk.label_segments('', 10), msg
83
+ assert_equal [''], mk.label_segments(nil, 10), msg
84
+
85
+ msg = 'Compare against hardcoded result.'
86
+ label = "A really long label"
87
+ label_copy = String.new(label)
88
+ expected = [
89
+ 'A reall',
90
+ 'y long ',
91
+ 'label',
92
+ ]
93
+ assert_equal expected, mk.label_segments(label, 7), msg
94
+
95
+ msg = 'Make sure the original string was not destroyed.'
96
+ assert_equal label_copy, label, msg
97
+ end
98
+
99
+ def test_weave_label_segments
100
+ msg = 'Compare against hardcoded result.'
101
+ mk = new_maker()
102
+ fmt = "%-7s %s %-7s"
103
+ a = [
104
+ "a very",
105
+ "long v",
106
+ "alue",
107
+ ]
108
+ b = [
109
+ "a very",
110
+ "long l",
111
+ "abel",
112
+ ]
113
+ expected = [
114
+ "a very + ",
115
+ "long v + ",
116
+ "alue = a very ",
117
+ " + long l ",
118
+ " + abel ",
119
+ ]
120
+ assert_equal expected, mk.weave_label_segments(fmt, a, b, '=', '+')
121
+ end
122
+
123
+ def test_labelable_values
124
+ msg = 'Non-integer values should not be treated as labelable values.'
125
+ mk = new_maker()
126
+ var = new_variable()
127
+ add_new_values_to_var(var)
128
+ n = var.values.size
129
+ var.add_value(:value => 'X')
130
+ var.add_value(:value => '123x')
131
+ var.add_value(:value => '')
132
+ var.add_value(:value => -123)
133
+ assert_equal n + 4, var.values.size, msg
134
+ assert_equal n + 1, mk.labelable_values(var).size, msg
135
+ end
136
+
137
+ def test_max_value_length
138
+ msg = 'Compare against hardcoded result.'
139
+ mk = new_maker()
140
+ var = new_variable()
141
+
142
+ msg = 'A variable with no values should have a max_value_length of zero.'
143
+ assert_equal 0, mk.max_value_length(var, var.values), msg
144
+
145
+ msg = 'The method should agree with direct computation.'
146
+ add_new_values_to_var(var)
147
+ mx = params_values().map { |pv| pv[:value].to_s.length }.max
148
+ assert_equal mx, mk.max_value_length(var, var.values), msg
149
+
150
+ msg = 'Compare against hardcoded result.'
151
+ var.add_value( params_value(123456789012345) )
152
+ assert_equal 15, mk.max_value_length(var, var.values), msg
153
+ end
154
+
155
+ def test_comments_start
156
+ msg = 'Only web_app uses comments.'
157
+ mk = new_maker()
158
+ mk.sfc.caller = 'vb'
159
+ assert_equal '', mk.comments_start.join(''), msg
160
+ mk.sfc.caller = 'web_app'
161
+ assert_not_equal '', mk.comments_start.join(''), msg
162
+ end
163
+
164
+ def test_comments_end
165
+ msg = 'Compare against hardcoded result.'
166
+ mk = new_maker()
167
+ assert_equal [], mk.comments_end, msg
168
+ end
169
+
170
+
171
+ end
172
+ end
@@ -0,0 +1,251 @@
1
+ # This file is part of the Minnesota Population Center's stats_package_syntax_file_generator project.
2
+ # For copyright and licensing information, see the NOTICE and LICENSE files
3
+ # in this project's top-level directory, and also on-line at:
4
+ # https://github.com/mnpopcenter/stats_package_syntax_file_generator
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'setup.rb'))
7
+
8
+ module StatsPackageSyntaxFileGeneratorTest
9
+ class MakerSAS < Test::Unit::TestCase
10
+
11
+ include StatsPackageSyntaxFileGeneratorTestSetup
12
+
13
+ def test_create_maker_sas
14
+ msg = 'Try to create an object.'
15
+ mk = new_maker('sas')
16
+ assert_instance_of StatsPackageSyntaxFileGenerator::MakerSAS, mk, msg
17
+ end
18
+
19
+ def test_syn_fmt_big_nums
20
+ msg = "Compare against hardcoded result."
21
+ mk = new_maker('sas')
22
+ expected = [
23
+ " BIGDEC 11.5",
24
+ " BIGINT 19."
25
+ ]
26
+ var_list = names_to_vars( mk.sfc, %w(BIGDEC BIGINT) )
27
+ actual = mk.syn_fmt_big_nums_for_var_list(var_list)
28
+ assert_equal expected, actual, msg
29
+ end
30
+
31
+ def test_syn_val_labs_for_var_start
32
+ msg = 'Compare against hardcoded result.'
33
+ mk = new_maker('sas')
34
+ expected = [
35
+ 'value $ RECTYPE_f',
36
+ 'value SEX_f',
37
+ ]
38
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE SEX) )
39
+ var_list.each_index { |i|
40
+ actual = mk.syn_val_labs_for_var_start(var_list[i])
41
+ assert_equal expected[i], actual, msg
42
+ }
43
+ end
44
+
45
+ def test_syn_val_labs_for_var
46
+ msg = 'Compare against hardcoded result.'
47
+ mk = new_maker('sas')
48
+ expected = [
49
+ [
50
+ ' "H" = "Household"',
51
+ ' "P" = "Person"',
52
+ ],
53
+ [
54
+ ' 1 = "Directly covered person"',
55
+ ' 2 = "Covered through family member"',
56
+ ' 3 = "Not covered"',
57
+ ],
58
+ ]
59
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE SOCSEC) )
60
+ var_list.each_index { |i|
61
+ actual = mk.syn_val_labs_for_var(var_list[i])
62
+ assert_equal expected[i], actual, msg
63
+ }
64
+ end
65
+
66
+ def test_syn_val_lab_for_val
67
+ msg = 'Compare against hardcoded result.'
68
+ fmt = " %-8s %s %s"
69
+ mk = new_maker('sas')
70
+ var = new_variable()
71
+ var.values[0] = new_value()
72
+ tests = [
73
+ [ false, 99, 'fubb', [' 99 = "fubb"'] ],
74
+ [ false, 9, 'fubb', [' 9 = "fubb"'] ],
75
+ [ true, 'a', 'fubb', [' "a" = "fubb"'] ],
76
+ [
77
+ true,
78
+ 'x' * 120,
79
+ 'fubb' * 30,
80
+ [
81
+ ' "' + ('x' * 100) + '" ',
82
+ ' "' + ('x' * 20) + '" = "' + ('fubb' * 25) + '"',
83
+ ' "' + ('fubb' * 5) + '"',
84
+ ]
85
+ ],
86
+ ]
87
+ tests.each_index { |i|
88
+ var.is_string_var = tests[i][0]
89
+ var.values[0].value = tests[i][1]
90
+ var.values[0].label = tests[i][2]
91
+ actual = mk.syn_val_lab_for_val(var, var.values[0], fmt)
92
+ assert_equal tests[i][3], actual, msg
93
+ }
94
+ end
95
+
96
+ def test_syn_var_locations
97
+ msg = 'Compare against hardcoded result.'
98
+ mk = new_maker('sas')
99
+ expected = [
100
+ ' RECTYPE $ 1-1',
101
+ ' DWNUM 2-7',
102
+ ' SEX 68-68',
103
+ ' AGE 69-71',
104
+ ' RESPREV2 80-83 .3',
105
+ ]
106
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE DWNUM SEX AGE RESPREV2) )
107
+ actual = mk.syn_var_locations(var_list)
108
+ assert_equal expected, actual, msg
109
+ end
110
+
111
+ def test_syn_dfh_retain
112
+ mk = new_maker('sas')
113
+
114
+ msg = 'Compare against hardcoded result.'
115
+ expected = [
116
+ 'retain',
117
+ ' CANTON',
118
+ ' URBAN',
119
+ ' DWTYPE',
120
+ ' OWNERSHP',
121
+ ' RENT',
122
+ ';',
123
+ '',
124
+ ]
125
+ mk.sfc.rectangularize = true
126
+ assert_equal expected, mk.syn_dfh_retain, msg
127
+
128
+ msg = 'Should be empty list if rectangularize option is not set.'
129
+ expected = []
130
+ mk.sfc.rectangularize = false
131
+ assert_equal expected, mk.syn_dfh_retain, msg
132
+
133
+ msg = 'Should be empty list if there are no variables.'
134
+ expected = []
135
+ mk.sfc.rectangularize = true
136
+ mk.sfc.clear_variables
137
+ assert_equal expected, mk.syn_dfh_retain, msg
138
+ end
139
+
140
+ def test_syn_dfh_rec_type_block
141
+ msg = 'Compare against hardcoded result.'
142
+ mk = new_maker('sas')
143
+ expected = [
144
+ 'input',
145
+ ' RECTYPE $ 1-1 @',
146
+ ';',
147
+ ]
148
+ actual = mk.syn_dfh_rec_type_block
149
+ assert_equal expected, actual, msg
150
+ end
151
+
152
+ def test_syn_dfh_if_start
153
+ msg = 'Compare against hardcoded result.'
154
+ mk = new_maker('sas')
155
+ expected = [
156
+ 'if RECTYPE = "H" then do;',
157
+ 'else if RECTYPE = "P" then do;',
158
+ ]
159
+ actual = [
160
+ mk.syn_dfh_if_start('if', 'H'),
161
+ mk.syn_dfh_if_start('else if', 'P'),
162
+ ]
163
+ expected.each_index { |i| assert_equal expected[i], actual[i], msg }
164
+ end
165
+
166
+ def test_syn_dfh_if_end
167
+ msg = 'Compare against hardcoded result: no rectangularize, '
168
+ mk = new_maker('sas')
169
+ expected = {
170
+ 'H' => [ 'output;', 'end;', '' ],
171
+ 'P' => [ 'output;', 'end;', '' ],
172
+ }
173
+ mk.sfc.rectangularize = false
174
+ expected.each { |k,v|
175
+ assert_equal expected[k], mk.syn_dfh_if_end(k), msg + k
176
+ }
177
+
178
+ msg = 'Compare against hardcoded result: rectangularize, '
179
+ mk.sfc.rectangularize = true
180
+ expected['H'].shift
181
+ expected.each { |k,v|
182
+ assert_equal expected[k], mk.syn_dfh_if_end(k), msg + k
183
+ }
184
+ end
185
+
186
+ def test_syn_var_lab_for_var
187
+ msg = 'Compare against hardcoded result.'
188
+ mk = new_maker('sas')
189
+ expected = [
190
+ [ ' RECTYPE = "Record type"' ],
191
+ [ ' HDFIRSTD = "Head not first [""dwelling-wide""] {note}"' ],
192
+ [
193
+ ' SEX = "' + ('fubb' * 25) + '"',
194
+ ' "' + ('fubb' * 5) + '"',
195
+ ],
196
+ ]
197
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE HDFIRSTD SEX) )
198
+ var_list[2].label = 'fubb' * 30
199
+ expected.each_index { |i|
200
+ actual = mk.syn_var_lab_for_var(var_list[i])
201
+ assert_equal expected[i], actual, msg
202
+ }
203
+ end
204
+
205
+ def test_syn_fmt_link_for_var_list
206
+ msg = 'Compare against hardcoded result.'
207
+ mk = new_maker('sas')
208
+ expected = [
209
+ ' RECTYPE RECTYPE_f.',
210
+ ' HDFIRSTD HDFIRSTD_f.',
211
+ ' SEX SEX_f.',
212
+ ]
213
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE HDFIRSTD SEX) )
214
+ actual = mk.syn_fmt_link_for_var_list(var_list)
215
+ assert_equal expected, actual, msg
216
+
217
+ msg = 'Should return [] if there are no variables.'
218
+ mk.sfc.clear_variables
219
+ assert_equal [], mk.syn_fmt_link, msg
220
+ end
221
+
222
+ def test_implied_decimal_fmt
223
+ msg = 'Compare against hardcoded result.'
224
+ mk = new_maker('sas')
225
+ expected = [
226
+ '',
227
+ '',
228
+ ' .1',
229
+ ' .3',
230
+ ]
231
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE SEX CANTON RESPREV2) )
232
+ actual = var_list.map { |v| mk.implied_decimal_fmt(v) }
233
+ assert_equal expected, actual, msg
234
+ end
235
+
236
+ def test_non_last_non_common_vars
237
+ mk = new_maker('sas')
238
+
239
+ msg = 'Compare against hardcoded result.'
240
+ expected = ['CANTON', 'URBAN', 'DWTYPE', 'OWNERSHP', 'RENT']
241
+ var_list = mk.non_last_non_common_vars
242
+ assert_equal expected, vars_to_names(var_list), msg
243
+
244
+ msg = 'Should return empty list if there are no variables.'
245
+ mk.sfc.clear_variables
246
+ var_list = mk.non_last_non_common_vars
247
+ assert_equal [], vars_to_names(var_list), msg
248
+ end
249
+
250
+ end
251
+ end
@@ -0,0 +1,121 @@
1
+ # This file is part of the Minnesota Population Center's stats_package_syntax_file_generator project.
2
+ # For copyright and licensing information, see the NOTICE and LICENSE files
3
+ # in this project's top-level directory, and also on-line at:
4
+ # https://github.com/mnpopcenter/stats_package_syntax_file_generator
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'setup.rb'))
7
+
8
+ module StatsPackageSyntaxFileGeneratorTest
9
+ class MakerSPSS < Test::Unit::TestCase
10
+
11
+ include StatsPackageSyntaxFileGeneratorTestSetup
12
+
13
+ def test_create_maker_spss
14
+ msg = 'Try to create an object.'
15
+ mk = new_maker('spss')
16
+ assert_instance_of StatsPackageSyntaxFileGenerator::MakerSPSS, mk, msg
17
+ end
18
+
19
+ def test_syn_dfh_file_type
20
+ msg = 'Compare against hardcoded result.'
21
+ mk = new_maker('spss')
22
+ expected = [
23
+ 'file type mixed',
24
+ ' /file = "xx9999a.dat"',
25
+ ' /record = 1-1 (a)',
26
+ '.',
27
+ '',
28
+ ]
29
+ assert_equal expected, mk.syn_dfh_file_type, msg
30
+ end
31
+
32
+ def test_syn_dfh_data_block_start
33
+ msg = 'Compare against hardcoded result.'
34
+ mk = new_maker('spss')
35
+ expected = [
36
+ 'record type "H".',
37
+ 'data list /',
38
+ ]
39
+ actual = mk.syn_dfh_data_block_start('H')
40
+ assert_equal expected, actual, msg
41
+ end
42
+
43
+ def test_syn_var_locations
44
+ msg = 'Compare against hardcoded result.'
45
+ mk = new_maker('spss')
46
+ expected = [
47
+ ' RECTYPE 1-1 (a)',
48
+ ' CANTON 67-69 (1)',
49
+ ' AGE 69-71',
50
+ ' LIT 101-101',
51
+ ]
52
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE CANTON AGE LIT) )
53
+ actual = mk.syn_var_locations(var_list)
54
+ assert_equal expected, actual, msg
55
+ end
56
+
57
+ def test_syn_var_labs
58
+ msg = 'Compare against hardcoded result.'
59
+ mk = new_maker('spss')
60
+ expected = [
61
+ 'variable labels',
62
+ ' HHNUM """Household number"""',
63
+ ' HDFIRSTD "Head not first [""dwelling-wide""] {note}"',
64
+ ' CANTON "Canton ""geo area"""',
65
+ ' SEX ".........x.........x.........x.........x.........x.........x.........x.........x.........x.........x"',
66
+ ' + ".........x.........x"',
67
+ '.',
68
+ '',
69
+ ]
70
+ var_list = names_to_vars( mk.sfc, %w(HHNUM HDFIRSTD CANTON SEX) )
71
+ var_list[-1].label = '.........x' * 15
72
+ actual = mk.syn_var_labs(var_list)
73
+ assert_equal expected, actual, msg
74
+ end
75
+
76
+ def test_syn_val_labs_for_var
77
+ msg = 'Compare against hardcoded result.'
78
+ mk = new_maker('spss')
79
+ expected = [
80
+ [
81
+ ' /RECTYPE',
82
+ ' "H" "Household"',
83
+ ' "P" "Person"',
84
+ ],
85
+ [
86
+ ' /URBAN',
87
+ ' 1 "Urban"',
88
+ ' 2 "Rural"',
89
+ ],
90
+ [
91
+ ' /SEX',
92
+ ' 1 "Male"',
93
+ ' 2 ".........x.........x.........x.........x.........x.........x.........x.........x.........x.........x"',
94
+ ' + ".........x.........x"',
95
+ ],
96
+ ]
97
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE URBAN SEX) )
98
+ var_list[-1].values[-1].label = '.........x' * 15
99
+ var_list.each_index { |i|
100
+ actual = mk.syn_val_labs_for_var(var_list[i])
101
+ assert_equal expected[i], actual, msg
102
+ }
103
+ end
104
+
105
+ def test_var_fmt
106
+ msg = 'Compare against hardcoded result.'
107
+ mk = new_maker('spss')
108
+ expected = [
109
+ ' (a)',
110
+ ' (1)',
111
+ '',
112
+ ' (3)',
113
+ ]
114
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE CANTON URBAN RESPREV2) )
115
+ actual = var_list.map { |v| mk.var_fmt(v) }
116
+ assert_equal expected, actual, msg
117
+ end
118
+
119
+
120
+ end
121
+ end