File: test-barplot.rb

package info (click to toggle)
ruby-unicode-plot 0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,492 kB
  • sloc: ruby: 2,871; makefile: 4
file content (198 lines) | stat: -rw-r--r-- 6,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class BarplotTest < Test::Unit::TestCase
  include Helper::Fixture
  include Helper::WithTerm

  sub_test_case("UnicodePlot.barplot") do
    test("errors") do
      assert_raise(ArgumentError) do
        UnicodePlot.barplot([:a], [-1, 2])
      end
      assert_raise(ArgumentError) do
        UnicodePlot.barplot([:a, :b], [-1, 2])
      end
    end

    sub_test_case("with invalid arguments") do
      test("unknown border type") do
        assert_raise(ArgumentError.new("unknown border type: invalid_border_name")) do
          UnicodePlot.barplot(data: {bar: 23, foo: 37}, border: :invalid_border_name)
        end
      end
    end

    test("colored") do
      data = { bar: 23, foo: 37 }
      plot = UnicodePlot.barplot(data: data)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/default.txt").read,
                   output)

      plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/default.txt").read,
                   output)
    end

    test("not colored") do
      data = { bar: 23, foo: 37 }
      plot = UnicodePlot.barplot(data: data)
      output = StringIO.open do |sio|
        sio.print(plot)
        sio.close
        sio.string
      end
      assert_equal(fixture_path("barplot/nocolor.txt").read,
                   output)
    end

    test("mixed") do
      data = { bar: 23.0, 2.1 => 10, foo: 37.0 }
      plot = UnicodePlot.barplot(data: data)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/default_mixed.txt").read,
                   output)
    end

    sub_test_case("xscale: :log10") do
      test("default") do
        plot = UnicodePlot.barplot(
          [:a, :b, :c, :d, :e],
          [0, 1, 10, 100, 1000],
          title: "Logscale Plot",
          xscale: :log10
        )
        _, output = with_term { plot.render($stdout, newline: false) }
        assert_equal(fixture_path("barplot/log10.txt").read,
                     output)
      end

      test("with custom label") do
        plot = UnicodePlot.barplot(
          [:a, :b, :c, :d, :e],
          [0, 1, 10, 100, 1000],
          title: "Logscale Plot",
          xlabel: "custom label",
          xscale: :log10
        )
        _, output = with_term { plot.render($stdout, newline: false) }
        assert_equal(fixture_path("barplot/log10_label.txt").read,
                     output)
      end
    end

    sub_test_case("with parameters") do
      test("parameters1") do
        plot = UnicodePlot.barplot(
          ["Paris", "New York", "Moskau", "Madrid"],
          [2.244, 8.406, 11.92, 3.165],
          title: "Relative sizes of cities",
          xlabel: "population [in mil]",
          color: :blue,
          margin: 7,
          padding: 3
        )
        _, output = with_term { plot.render($stdout, newline: false) }
        assert_equal(fixture_path("barplot/parameters1.txt").read,
                     output)
      end

      test("parameters1_nolabels") do
        plot = UnicodePlot.barplot(
          ["Paris", "New York", "Moskau", "Madrid"],
          [2.244, 8.406, 11.92, 3.165],
          title: "Relative sizes of cities",
          xlabel: "population [in mil]",
          color: :blue,
          margin: 7,
          padding: 3,
          labels: false
        )
        _, output = with_term { plot.render($stdout, newline: false) }
        assert_equal(fixture_path("barplot/parameters1_nolabels.txt").read,
                     output)
      end

      test("parameters2") do
        plot = UnicodePlot.barplot(
          ["Paris", "New York", "Moskau", "Madrid"],
          [2.244, 8.406, 11.92, 3.165],
          title: "Relative sizes of cities",
          xlabel: "population [in mil]",
          color: :yellow,
          border: :solid,
          symbol: "=",
          width: 60
        )
        _, output = with_term { plot.render($stdout, newline: false) }
        assert_equal(fixture_path("barplot/parameters2.txt").read,
                     output)
      end
    end

    test("ranges") do
      plot = UnicodePlot.barplot(2..6, 11..15)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/ranges.txt").read,
                   output)
    end

    test("all zeros") do
      plot = UnicodePlot.barplot([5, 4, 3, 2, 1], [0, 0, 0, 0, 0])
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/edgecase_zeros.txt").read,
                   output)
    end

    test("one large") do
      plot = UnicodePlot.barplot([:a, :b, :c, :d], [1, 1, 1, 1000000])
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/edgecase_onelarge.txt").read,
                   output)
    end
  end

  sub_test_case("UnicodePlot.barplot!") do
    test("errors") do
      plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
      assert_raise(ArgumentError) do
        UnicodePlot.barplot!(plot, ["zoom"], [90, 80])
      end
      assert_raise(ArgumentError) do
        UnicodePlot.barplot!(plot, ["zoom", "boom"], [90])
      end
      UnicodePlot.barplot!(plot, "zoom", 90.1)
    end

    test("return value") do
      plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
      assert_same(plot,
                  UnicodePlot.barplot!(plot, ["zoom"], [90]))
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/default2.txt").read,
                   output)

      plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
      assert_same(plot,
                  UnicodePlot.barplot!(plot, "zoom", 90))
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/default2.txt").read,
                   output)

      plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
      assert_same(plot,
                  UnicodePlot.barplot!(plot, data: { zoom: 90 }))
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/default2.txt").read,
                   output)
    end

    test("ranges") do
      plot = UnicodePlot.barplot(2..6, 11..15)
      assert_same(plot,
                  UnicodePlot.barplot!(plot, 9..10, 20..21))
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("barplot/ranges2.txt").read,
                   output)
    end
  end
end