Added the Gruff library to the lib/ directory of the the rails folder, and the
[selectricity] / lib / gruff-0.2.8 / test / gruff_test_case.rb
1 $:.unshift(File.dirname(__FILE__) + "/../lib/")
2
3 require 'test/unit'
4 require 'gruff'
5 # require 'test_timer'
6
7 class GruffTestCase < Test::Unit::TestCase
8
9   def setup
10     @datasets = [
11       [:Jimmy, [25, 36, 86, 39, 25, 31, 79, 88]],
12       [:Charles, [80, 54, 67, 54, 68, 70, 90, 95]],
13       [:Julie, [22, 29, 35, 38, 36, 40, 46, 57]],
14       [:Jane, [95, 95, 95, 90, 85, 80, 88, 100]],
15       [:Philip, [90, 34, 23, 12, 78, 89, 98, 88]],
16       ["Arthur", [5, 10, 13, 11, 6, 16, 22, 32]],
17       ]
18
19     @labels = {
20         0 => '5/6', 
21         1 => '5/15', 
22         2 => '5/24', 
23         3 => '5/30', 
24         4 => '6/4', 
25         5 => '6/12', 
26         6 => '6/21', 
27         7 => '6/28', 
28       }      
29   end
30
31   def setup_single_dataset
32     @datasets = [
33       [:Jimmy, [25, 36, 86]]
34       ]
35
36     @labels = {
37         0 => 'You', 
38         1 => 'Average', 
39         2 => 'Lifetime' 
40       }      
41   end
42
43   def setup_wide_dataset
44     @datasets = [
45       ["Auto", 25],
46       ["Food", 5],
47       ["Entertainment", 15]
48       ]
49
50     @labels = { 0 => 'This Month' }
51   end
52
53   def test_dummy
54     assert true
55   end
56
57 protected
58
59   # Generate graphs at several sizes.
60   #
61   # Also writes the graph to disk.
62   #
63   #   graph_sized 'bar_basic' do |g|
64   #     g.data('students', [1, 2, 3, 4])
65   #   end
66   #
67   def graph_sized(filename, sizes=['', 400])
68     class_name = self.class.name.gsub(/^TestGruff/, '')
69     Array(sizes).each do |size|
70       g = instance_eval("Gruff::#{class_name}.new #{size}")
71       g.title = "#{class_name} Graph"
72       yield g      
73       write_test_file g, "#{filename}_#{size}.png"
74     end
75   end
76
77   def write_test_file(graph, filename)
78     graph.write(File.dirname(__FILE__) + "/output/#{filename}")
79   end
80
81   ##
82   # Example:
83   #
84   #   setup_basic_graph Gruff::Pie, 400
85   #
86   def setup_basic_graph(*args)
87     klass, size = Gruff::Bar, 400
88     # Allow args to be klass, size or just klass or just size.
89     #
90     # TODO Refactor
91     case args.length
92     when 1
93       case args[0]
94       when Fixnum
95         size = args[0]
96         klass = eval("Gruff::#{self.class.name.gsub(/^TestGruff/, '')}") 
97       when String
98         size = args[0]
99         klass = eval("Gruff::#{self.class.name.gsub(/^TestGruff/, '')}") 
100       else
101         klass = args[0]
102       end
103     when 2
104       klass, size = args[0], args[1]
105     end
106     
107     g = klass.new(size)
108     g.title = "My Bar Graph"
109     g.labels = @labels
110
111
112     @datasets.each do |data|
113       g.data(data[0], data[1])
114     end
115     g
116   end
117
118 end

Benjamin Mako Hill || Want to submit a patch?