Permalink
Browse files

More tests + fixes

.gitignore
  • Loading branch information...
1 parent fcdfe8f commit ac534cec613bbc704cff8a49e86a395f7cffcac0 Erik Hollensbe committed Jul 11, 2011
Showing with 60 additions and 5 deletions.
  1. +5 −0 .gitignore
  2. +12 −5 lib/chart.rb
  3. +43 −0 spec/lib/chart_spec.rb
View
@@ -8,3 +8,8 @@ tmp/**/*
config/database.yml
*.css
config/mail_password
+public/javascripts/controls.js
+public/javascripts/dragdrop.js
+public/javascripts/effects.js
+public/javascripts/prototype.js
+public/javascripts/rails.js
View
@@ -29,22 +29,29 @@ def preamble
end
def prepare_data
- javascript <<-EOF
+ javascript = <<-EOF
var data = new google.visualization.DataTable();
EOF
- column_names.each do |column_name|
- javascript += "data.addColumn('string', '#{column_name}');\n"
+ column_names.each_with_index do |column_name, i|
+ case set[0][i]
+ when Numeric
+ javascript += "data.addColumn('number', "
+ else
+ javascript += "data.addColumn('string', "
+ end
+
+ javascript += "'#{column_name}');\n"
end
javascript += "data.addRows(#{set.size});\n"
set.each_with_index do |row, i|
row.each_with_index do |item, j|
- javascript += "data.setValue(#{i}, #{j},"
+ javascript += "data.setValue(#{i}, #{j}, "
case item
when Numeric
- javascript += item + ");\n"
+ javascript += item.to_s + ");\n"
else
javascript += "'#{item}');\n"
end
View
@@ -29,5 +29,48 @@
EOF
end
end
+
+ describe "prepare_data" do
+ before(:each) do
+ @obj1 = GoogleChart::Column.new(%w[one two], [[1,2], [3,4]])
+ @obj2 = GoogleChart::Column.new(%w[one two], [%w[1 2], %w[3 4]])
+ @obj3 = GoogleChart::Column.new(%w[one two], [[1,2], [3,4], [5,6]])
+ end
+
+ it "should prepare columns according to the column names" do
+ prepped = @obj1.prepare_data
+ prepped.should =~ /'number', 'one'/
+ prepped.should =~ /'number', 'two'/
+
+ prepped = @obj2.prepare_data
+
+ prepped.should =~ /'string', 'one'/
+ prepped.should =~ /'string', 'two'/
+ end
+
+ it "should add the appropriate number of rows" do
+ prepped = @obj1.prepare_data
+ prepped.should =~ /data\.addRows\(2\);/
+
+ prepped = @obj3.prepare_data
+ prepped.should =~ /data\.addRows\(3\);/
+ end
+
+ it "should set a value for each item in the set" do
+ prepped = @obj1.prepare_data
+ @obj1.set.each_with_index do |row, i|
+ row.each_with_index do |item, j|
+ prepped.should =~ /data\.setValue\(#{i}, #{j}, #{item}/
+ end
+ end
+
+ prepped = @obj2.prepare_data
+ @obj2.set.each_with_index do |row, i|
+ row.each_with_index do |item, j|
+ prepped.should =~ /data\.setValue\(#{i}, #{j}, '#{item}'/
+ end
+ end
+ end
+ end
end
end

0 comments on commit ac534ce

Please sign in to comment.