File: test_gnuplot.rb

package info (click to toggle)
ruby-gnuplot 2.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 224 kB
  • sloc: ruby: 456; makefile: 4
file content (132 lines) | stat: -rw-r--r-- 3,025 bytes parent folder | download | duplicates (3)
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
# -*- ruby -*-

require 'gnuplot'
require 'test/unit'

class StdDataTest < Test::Unit::TestCase
    
  def test_array_1d
    data = (0..5).to_a
    ds = Gnuplot::DataSet.new( data )
    
    assert data == ds.data
    assert data.join("\n") + "\n", ds.to_gplot
  end


  # Test a multidimensional array.

  def test_array_nd
    d1 = (0..3).to_a
    d2 = d1.collect { |v| 3 * v }
    d3 = d2.collect { |v| 4 * v }
    
    data = [ d1, d2, d3 ]
    ds = Gnuplot::DataSet.new( data )
    
    assert data == ds.data
    assert "0 0 0\n1 3 12\n2 6 24\n3 9 36\n", ds.to_gplot
  end
end


class DataSetTest < Test::Unit::TestCase

  def test_yield_ctor
    ds = Gnuplot::DataSet.new do |ds|
      ds.with = "lines" 
      ds.using = "1:2"
      ds.data = [ [0, 1, 2], [1, 2, 5] ]
    end
    
    assert "lines", ds.with
    assert "1:2",   ds.using
    assert nil == ds.title
    assert [ [0, 1, 2], [1, 2, 5] ] == ds.data
    assert "'-' using 1:2 with lines",  ds.plot_args
    assert "0 1\n1 2\n2 5\n", ds.to_gplot 
  end
  
end


class PlotTest < Test::Unit::TestCase
  
  def test_no_data
    plot = Gnuplot::Plot.new do |p|
      p.set "output", "'foo'"
      p.set "terminal", "postscript enhanced"
      p.unset "border"
    end

    assert( plot.settings ==
		 [ [:set, "output", "'foo'"], 
		   [:set, "terminal", "postscript enhanced"],
                   [:unset, "border"] ] )
    

    assert( plot.to_gplot, \
		 "set output 'foo'\nset terminal postscript enhanced\n" )

  end

  def test_set
    plot = Gnuplot::Plot.new do |p|
      p.set "title", "foo"
    end
    assert "'foo'", plot["title"]

    plot.set "title", "'foo'"
    assert "'foo'", plot["title"]
  end

  def test_unset
    plot = Gnuplot::Plot.new do |p|
      p.unset "title"
    end
    assert_nil plot["title"]

    plot.unset "title"
    assert_nil plot["title"]
  end

end


require 'rbconfig'
CONFIG = RbConfig::MAKEFILE_CONFIG

# This attempts to test the functions that comprise the gnuplot package.  Most
# of the bug reports that I get for this package have to do with finding the
# gnuplot executable under different environments so that makes it difficult
# to test on a single environment.  To try to get around this I'm using the
# rbconfig library and its path to the sh environment variable.

class GnuplotModuleTest

  def test_which
    # Put the spaces around the command to make sure that it gets stripped
    # properly. 
    assert( CONFIG["SHELL"], Gnuplot::which(" sh " ) )
    assert( CONFIG["SHELL"], Gnuplot::which( CONFIG["SHELL"] ) )
  end


  def test_gnuplot
    cmd = Gnuplot.gnuplot
    assert( Gnuplot::which("gnuplot") + " -persist", cmd )

    cmd = Gnuplot.gnuplot(false)
    assert( Gnuplot::which("gnuplot"), cmd )

    # If I set the name of the gnuplot environment variable to a different
    # name (one that is in the path) then I should get the shell name as the
    # result of the gnuplot call.

    ENV["RB_GNUPLOT"] = "sh" 
    assert( CONFIG["SHELL"], Gnuplot.gnuplot(false) )
  end
      
end