|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'stringio' |
| 4 | +require_relative '../test_helper' |
| 5 | + |
| 6 | +class YouPlotProgressiveTest < Test::Unit::TestCase |
| 7 | + def build_options(overrides = {}) |
| 8 | + out = StringIO.new |
| 9 | + defaults = YouPlot::Options::DEFAULTS.merge(output: out, pass: false) |
| 10 | + options_hash = defaults.merge(overrides) |
| 11 | + YouPlot::Options.new(*YouPlot::Options.members.map { |k| options_hash[k] }) |
| 12 | + end |
| 13 | + |
| 14 | + def build_data_with_progressive(command, lines) |
| 15 | + data = nil |
| 16 | + lines.each do |line| |
| 17 | + row = command.send(:parse_progressive_row, line) |
| 18 | + next if row.nil? |
| 19 | + |
| 20 | + data = command.send(:progressive_update_data, row) |
| 21 | + end |
| 22 | + data |
| 23 | + end |
| 24 | + |
| 25 | + def suppress_stderr |
| 26 | + stderr = $stderr |
| 27 | + $stderr = StringIO.new |
| 28 | + yield |
| 29 | + ensure |
| 30 | + $stderr = stderr |
| 31 | + end |
| 32 | + |
| 33 | + test 'progressive incremental parsing without headers matches DSV.parse' do |
| 34 | + command = YouPlot::Command.new([]) |
| 35 | + command.options = build_options(delimiter: "\t", headers: false, transpose: false) |
| 36 | + |
| 37 | + lines = %W[1\t2\n 3\n \n] |
| 38 | + actual = build_data_with_progressive(command, lines) |
| 39 | + expected = YouPlot::DSV.parse(lines.join, "\t", false, false) |
| 40 | + |
| 41 | + assert_equal expected.headers, actual.headers |
| 42 | + assert_equal expected.series, actual.series |
| 43 | + assert_nil command.instance_variable_get(:@raw_data) |
| 44 | + end |
| 45 | + |
| 46 | + test 'progressive incremental parsing with headers waits for first data row' do |
| 47 | + command = YouPlot::Command.new([]) |
| 48 | + command.options = build_options(delimiter: "\t", headers: true, transpose: false) |
| 49 | + |
| 50 | + header_row = command.send(:parse_progressive_row, "h1\th2\n") |
| 51 | + first = command.send(:progressive_update_data, header_row) |
| 52 | + assert_nil first |
| 53 | + |
| 54 | + data_row = command.send(:parse_progressive_row, "1\t2\n") |
| 55 | + actual = command.send(:progressive_update_data, data_row) |
| 56 | + expected = YouPlot::DSV.parse("h1\th2\n1\t2\n", "\t", true, false) |
| 57 | + |
| 58 | + assert_equal expected.headers, actual.headers |
| 59 | + assert_equal expected.series, actual.series |
| 60 | + end |
| 61 | + |
| 62 | + test 'progressive incremental parsing with headers and transpose matches DSV.parse' do |
| 63 | + command = YouPlot::Command.new([]) |
| 64 | + command.options = build_options(delimiter: "\t", headers: true, transpose: true) |
| 65 | + |
| 66 | + lines = %W[h1\t1\t2\n h2\t3\t4\n] |
| 67 | + actual = build_data_with_progressive(command, lines) |
| 68 | + expected = YouPlot::DSV.parse(lines.join, "\t", true, true) |
| 69 | + |
| 70 | + assert_equal expected.headers, actual.headers |
| 71 | + assert_equal expected.series, actual.series |
| 72 | + end |
| 73 | + |
| 74 | + test 'progressive with headers returns nil when headers are fewer than series' do |
| 75 | + command = YouPlot::Command.new([]) |
| 76 | + command.options = build_options(delimiter: "\t", headers: true, transpose: false) |
| 77 | + |
| 78 | + lines = %W[h1\n 1\t2\n] |
| 79 | + actual = nil |
| 80 | + expected = nil |
| 81 | + suppress_stderr do |
| 82 | + actual = build_data_with_progressive(command, lines) |
| 83 | + expected = YouPlot::DSV.parse(lines.join, "\t", true, false) |
| 84 | + end |
| 85 | + |
| 86 | + assert_nil expected |
| 87 | + assert_nil actual |
| 88 | + end |
| 89 | + |
| 90 | + test 'progressive with headers returns nil when headers are greater than series' do |
| 91 | + command = YouPlot::Command.new([]) |
| 92 | + command.options = build_options(delimiter: "\t", headers: true, transpose: false) |
| 93 | + |
| 94 | + lines = %W[h1\th2\n 1\n] |
| 95 | + actual = nil |
| 96 | + expected = nil |
| 97 | + suppress_stderr do |
| 98 | + actual = build_data_with_progressive(command, lines) |
| 99 | + expected = YouPlot::DSV.parse(lines.join, "\t", true, false) |
| 100 | + end |
| 101 | + |
| 102 | + assert_nil expected |
| 103 | + assert_nil actual |
| 104 | + end |
| 105 | +end |
0 commit comments