@@ -123,4 +123,60 @@ def test_JSON
123
123
assert_equal @json , JSON ( @hash )
124
124
assert_equal @hash , JSON ( @json )
125
125
end
126
+
127
+ def test_load_file
128
+ test_load_shared ( :load_file )
129
+ end
130
+
131
+ def test_load_file!
132
+ test_load_shared ( :load_file! )
133
+ end
134
+
135
+ def test_load_file_with_option
136
+ test_load_file_with_option_shared ( :load_file )
137
+ end
138
+
139
+ def test_load_file_with_option!
140
+ test_load_file_with_option_shared ( :load_file! )
141
+ end
142
+
143
+ private
144
+
145
+ def test_load_shared ( method_name )
146
+ temp_file_containing ( @json ) do |filespec |
147
+ assert_equal JSON . public_send ( method_name , filespec ) , @hash
148
+ end
149
+ end
150
+
151
+ def test_load_file_with_option_shared ( method_name )
152
+ temp_file_containing ( @json ) do |filespec |
153
+ parsed_object = JSON . public_send ( method_name , filespec , symbolize_names : true )
154
+ key_classes = parsed_object . keys . map ( &:class )
155
+ assert_true key_classes . include? ( Symbol ) && ( ! key_classes . include? ( String ) )
156
+ end
157
+ end
158
+
159
+ # Copied and slightly modified from https://github.com/keithrbennett/trick_bag
160
+ # (https://github.com/keithrbennett/trick_bag/blob/master/lib/trick_bag/io/temp_files.rb).
161
+ #
162
+ # For the easy creation and deletion of a temp file populated with text,
163
+ # wrapped around the code block you provide.
164
+ #
165
+ # @param text the text to write to the temporary file
166
+ # @param file_prefix optional prefix for the temporary file's name
167
+ # @yield filespec of the temporary file
168
+ def temp_file_containing ( text , file_prefix = '' )
169
+ raise "This method must be called with a code block." unless block_given?
170
+
171
+ filespec = nil
172
+ begin
173
+ Tempfile . open ( file_prefix ) do |file |
174
+ file << text
175
+ filespec = file . path
176
+ end
177
+ yield ( filespec )
178
+ ensure
179
+ File . delete ( filespec ) if filespec && File . exist? ( filespec )
180
+ end
181
+ end
126
182
end
0 commit comments