|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -# rubocop:disable Style/WordArray |
4 | 3 | RSpec.describe SmarterCSV::Writer do |
5 | 4 | subject(:create_csv_file) do |
6 | 5 | writer = SmarterCSV::Writer.new(file_path, options) |
|
27 | 26 | ] |
28 | 27 | end |
29 | 28 |
|
| 29 | + context 'when empty data is handed in' do |
| 30 | + let(:options) { {} } |
| 31 | + |
| 32 | + subject(:write_csv) do |
| 33 | + SmarterCSV.generate(file_path, options) do |csv_writer| |
| 34 | + csv_writer << data |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'when nil is passed in' do |
| 39 | + let(:data) { nil } |
| 40 | + it 'does not generate output' do |
| 41 | + write_csv |
| 42 | + output = File.read(file_path) |
| 43 | + expect(output).to eq '' |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context 'when {} is passed in' do |
| 48 | + let(:data) { {} } |
| 49 | + it 'does not generate output' do |
| 50 | + write_csv |
| 51 | + output = File.read(file_path) |
| 52 | + expect(output).to eq '' |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context 'when [] is passed in' do |
| 57 | + let(:data) { [] } |
| 58 | + it 'does not generate output' do |
| 59 | + write_csv |
| 60 | + output = File.read(file_path) |
| 61 | + expect(output).to eq '' |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'when [{},nil] is passed in' do |
| 66 | + let(:data) { [{}, nil] } |
| 67 | + it 'does not generate output' do |
| 68 | + write_csv |
| 69 | + output = File.read(file_path) |
| 70 | + expect(output).to eq '' |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context 'when [nil, {}, [], [{},nil]] is passed in' do |
| 75 | + let(:data) { [nil, {}, [], [{}, nil]] } |
| 76 | + it 'does not generate output' do |
| 77 | + write_csv |
| 78 | + output = File.read(file_path) |
| 79 | + expect(output).to eq '' |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
30 | 84 | context 'simplest case: one hash given' do |
31 | 85 | let(:options) { {} } |
32 | 86 | let(:data) do |
|
236 | 290 | end |
237 | 291 | end |
238 | 292 |
|
239 | | - context 'when we explicitly disable header discovery' do |
| 293 | + # NOTE: |
| 294 | + # * setting `discover_headers: false` is implicit when setting :headers or :map_headers |
| 295 | + # * that's why it does not make sense to set it manually to `false`. |
| 296 | + # * if you want to turn off header discovery, just provide one of those two options |
| 297 | + # |
| 298 | + context 'when we explicitly disable header discovery, but do not provide headers' do |
240 | 299 | let(:options) do |
241 | | - { discover_headers: false } # THIS SHOULD NOT BE USED LIKE THIS!! |
| 300 | + { discover_headers: false } # THIS DOES NOT MAKE SENSE without providing :headers or :map_headers |
242 | 301 | end |
243 | 302 |
|
244 | 303 | it 'limits the CSV file to only the given headers' do |
245 | 304 | create_csv_file |
246 | 305 |
|
247 | 306 | output = File.read(file_path) |
248 | | - expect(output).to eq "\n\n\n\n\n" # THIS SHOULD NOT BE USED LIKE THIS!! |
| 307 | + expect(output).to eq '' # because it turns off header discovery and no headers provided |
249 | 308 | end |
250 | 309 | end |
251 | 310 | end |
|
268 | 327 | end |
269 | 328 | end |
270 | 329 |
|
271 | | - |
272 | 330 | describe 'when special_char row_sep' do |
273 | 331 | let(:options) { {} } |
274 | 332 | let(:data_batches) do |
|
339 | 397 | v.to_s |
340 | 398 | end |
341 | 399 | end, |
342 | | - _all: ->(k, v) { v.is_a?(String) ? "\"#{v}\"" : v } # only double-quote string fields |
| 400 | + _all: ->(_k, v) { v.is_a?(String) ? "\"#{v}\"" : v } # only double-quote string fields |
343 | 401 | } |
344 | 402 | } |
345 | 403 | end |
346 | 404 | it 'applies all mappings in the correct order' do |
347 | 405 | writer = SmarterCSV::Writer.new(file_path, options) |
348 | 406 | writer << { name: 'Alice', age: 42, active: true, balance: 234.235 } |
349 | | - writer << { name: 'Joe', age: 53, active: false, balance: 32100 } |
| 407 | + writer << { name: 'Joe', age: 53, active: false, balance: 32_100 } |
350 | 408 | writer.finalize |
351 | 409 |
|
352 | 410 | output = File.read(file_path) |
|
389 | 447 | end |
390 | 448 | end |
391 | 449 | end |
392 | | -# rubocop:enable Style/WordArray |
|
0 commit comments