PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/test/test_all.rb

http://github.com/topfunky/sparklines
Ruby | 353 lines | 305 code | 41 blank | 7 comment | 3 complexity | c1a628233f940f5a3f4daca322271563 MD5 | raw file
  1. #!/usr/bin/ruby
  2. require 'test/unit'
  3. require 'lib/sparklines'
  4. require 'fileutils'
  5. require 'tidy_table'
  6. require 'dust'
  7. class SparklinesTest < Test::Unit::TestCase
  8. def setup
  9. @output_dir = "test/actual"
  10. FileUtils.mkdir_p(@output_dir)
  11. @data = %w( 1 5 15 20 30 50 57 58 55 48
  12. 44 43 42 42 46 48 49 53 55 59
  13. 60 65 75 90 105 106 107 110 115 120
  14. 115 120 130 140 150 160 170 100 100 10).map {|i| i.to_f}
  15. @nil_data = @data.dup
  16. [5, 10, 11, 20, 25, 26, 27].each {|i| @nil_data[i] = nil}
  17. @constant_data = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
  18. end
  19. test "basic bullet" do
  20. Sparklines.plot_to_file("#{@output_dir}/bullet_basic.png", 85, {
  21. :type => "bullet",
  22. :target => 80,
  23. :good => 100,
  24. :height => 15
  25. })
  26. end
  27. test "full-featured bullet" do
  28. Sparklines.plot_to_file("#{@output_dir}/bullet_full_featured.png", 85, {
  29. :type => "bullet",
  30. :target => 90,
  31. :bad => 60,
  32. :satisfactory => 80,
  33. :good => 100,
  34. :height => 15
  35. })
  36. end
  37. test "colorful bullet" do
  38. Sparklines.plot_to_file("#{@output_dir}/bullet_colorful.png", 85, {
  39. :type => "bullet",
  40. :target => 90,
  41. :bad => 60,
  42. :satisfactory => 80,
  43. :good => 100,
  44. :height => 15,
  45. :bad_color => '#c3e3bf',
  46. :satisfactory_color => '#96cf90',
  47. :good_color => "#6ab162"
  48. })
  49. end
  50. test "tall bullet" do
  51. Sparklines.plot_to_file("#{@output_dir}/bullet_tall.png", 85, {
  52. :type => "bullet",
  53. :target => 90,
  54. :bad => 60,
  55. :satisfactory => 80,
  56. :good => 100,
  57. :height => 30
  58. })
  59. end
  60. test "wide bullet" do
  61. Sparklines.plot_to_file("#{@output_dir}/bullet_wide.png", 85, {
  62. :type => "bullet",
  63. :target => 90,
  64. :bad => 60,
  65. :satisfactory => 80,
  66. :good => 100,
  67. :height => 15,
  68. :width => 200
  69. })
  70. end
  71. test "smooth with target" do
  72. quick_graph("smooth_with_target", {
  73. :type => "smooth",
  74. :target => 50,
  75. :target_color => '#999999',
  76. :line_color => "#6699cc",
  77. :underneath_color => "#ebf3f6"
  78. })
  79. end
  80. test "whisker with step" do
  81. quick_graph("whisker_with_step", {
  82. :type => "whisker",
  83. :step => 5
  84. })
  85. end
  86. def test_each_graph
  87. %w{pie area discrete smooth bar}.each do |type|
  88. quick_graph("#{type}", :type => type)
  89. end
  90. end
  91. def test_each_graph_with_label
  92. %w{pie area discrete smooth bar}.each do |type|
  93. quick_graph("labeled_#{type}", :type => type, :label => 'Glucose')
  94. end
  95. end
  96. test "bar graph with sprintf for label" do
  97. quick_graph("labeled_bar_sprintf", :type => "bar", :label => "Formatted", :label_format => "%i")
  98. end
  99. def test_each_graph_with_nil
  100. %w{pie area discrete smooth bar}.each do |type|
  101. nil_graph("nil_#{type}", :type => type, :label => 'Glucose')
  102. end
  103. end
  104. def test_smooth_graph_with_target_and_constant_data
  105. constant_graph("constant_smooth", :type => 'smooth', :label => 'a-label', :target => 1)
  106. end
  107. def test_whisker_decimals
  108. @data = (1..200).map {|n| n.to_f/100 }
  109. quick_graph("labeled_whisker_decimals", {
  110. :height => 30,
  111. :type => 'smooth',
  112. :label => 'png'
  113. })
  114. end
  115. def test_whisker_random
  116. # Need data ranging from -2 to +2
  117. @data = (1..40).map { |i| rand(3) * (rand(2) == 1 ? -1 : 1) }
  118. quick_graph("whisker", :type => 'whisker')
  119. end
  120. def test_whisker_non_exceptional
  121. @data = [1,1,1,1,1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
  122. quick_graph("whisker_non_exceptional", :type => 'whisker')
  123. end
  124. ##
  125. # Send random values in the range (-9..9)
  126. def test_whisker_junk
  127. @data = (1..40).map { |i| rand(10) * (rand(2) == 1 ? -1 : 1) }
  128. quick_graph("whisker_junk", :type => 'whisker')
  129. end
  130. def test_pie
  131. # Test extremes which previously did not work right
  132. [0, 1, 45, 95, 99, 100].each do |value|
  133. Sparklines.plot_to_file("#{@output_dir}/pie#{value}.png",
  134. value, {
  135. :type => 'pie',
  136. :diameter => 128
  137. })
  138. end
  139. Sparklines.plot_to_file("#{@output_dir}/pie_flat.png",
  140. [60],
  141. {
  142. :type => 'pie'
  143. })
  144. end
  145. def test_special_conditions
  146. tests = { 'smooth_colored' =>
  147. {
  148. :type => 'smooth',
  149. :line_color => 'purple'
  150. },
  151. 'labeled_smooth_without_last' =>
  152. {
  153. :type => 'smooth',
  154. :label => 'Glucose',
  155. :has_last => false
  156. },
  157. 'pie_large' => {
  158. :type => 'pie',
  159. :diameter => 200
  160. },
  161. 'area_high' => {
  162. :type => 'area',
  163. :upper => 80,
  164. :step => 4,
  165. :height => 20
  166. },
  167. 'discrete_wide' => {
  168. :type => 'discrete',
  169. :step => 8
  170. },
  171. 'discrete_max' => {
  172. :type => 'discrete',
  173. :max => @data.max * 4
  174. },
  175. 'discrete_min' => {
  176. :type => 'discrete',
  177. :min => @data.max * -4
  178. },
  179. 'bar_wide' => {
  180. :type => 'bar',
  181. :step => 8
  182. },
  183. 'bar_tall' => {
  184. :type => 'bar',
  185. :below_color => 'blue',
  186. :above_color => 'red',
  187. :upper => 90,
  188. :height => 50,
  189. :step => 8
  190. }
  191. }
  192. tests.each do |name, options|
  193. quick_graph(name, options)
  194. end
  195. end
  196. def test_bar_extreme_values
  197. Sparklines.plot_to_file("#{@output_dir}/bar_extreme_values.png",
  198. [0,1,100,2,99,3,98,4,97,5,96,6,95,7,94,8,93,9,92,10,91], {
  199. :type => 'bar',
  200. :below_color => 'blue',
  201. :above_color => 'red',
  202. :upper => 90,
  203. :step => 4
  204. })
  205. end
  206. def test_string_args
  207. quick_graph("bar_string.png", {
  208. 'type' => 'bar',
  209. 'below_color' => 'blue',
  210. 'above_color' => 'red',
  211. 'upper' => 50,
  212. 'height' => 50,
  213. 'step' => 8
  214. })
  215. end
  216. def test_area_min_max
  217. quick_graph("area_min_max", {
  218. :has_min => true,
  219. :has_max => true,
  220. :has_first => true,
  221. :has_last => true
  222. })
  223. end
  224. def test_smooth_underneath_color
  225. quick_graph("smooth_underneath_color", {
  226. :type => 'smooth',
  227. :line_color => "#6699cc",
  228. :underneath_color => "#ebf3f6"
  229. })
  230. end
  231. def test_close_values
  232. Sparklines.plot_to_file("#{@output_dir}/smooth_similar_nonzero_values.png", [100, 90, 95, 99, 80, 90], {
  233. :type => 'smooth',
  234. :line_color => "#6699cc",
  235. :underneath_color => "#ebf3f6"
  236. })
  237. end
  238. def test_no_type
  239. Sparklines.plot_to_file("#{@output_dir}/error.png", 0, :type => 'nonexistent')
  240. end
  241. def test_standard_deviation
  242. quick_graph('standard_deviation', {
  243. :type => 'smooth',
  244. :height => 100,
  245. :line_color => '#666',
  246. :has_std_dev => true,
  247. :std_dev_color => '#cccccc'
  248. })
  249. end
  250. def test_standard_deviation_tall
  251. quick_graph('standard_deviation_tall', {
  252. :type => 'smooth',
  253. :height => 300,
  254. :line_color => '#666',
  255. :has_std_dev => true,
  256. :std_dev_color => '#cccccc'
  257. })
  258. end
  259. def test_standard_deviation_short
  260. quick_graph('standard_deviation_short', {
  261. :type => 'smooth',
  262. :height => 20,
  263. :line_color => '#666',
  264. :has_std_dev => true,
  265. :std_dev_color => '#cccccc'
  266. })
  267. end
  268. def test_zeros
  269. @data = [0,0,0,0]
  270. quick_graph('zeros', {
  271. :type => 'smooth',
  272. })
  273. end
  274. private
  275. def quick_graph(name, options)
  276. Sparklines.plot_to_file("#{@output_dir}/#{name}.png", @data, options)
  277. end
  278. def nil_graph(name, options)
  279. Sparklines.plot_to_file("#{@output_dir}/#{name}.png", @nil_data, options)
  280. end
  281. def constant_graph(name, options)
  282. Sparklines.plot_to_file("#{@output_dir}/#{name}.png", @constant_data, options)
  283. end
  284. end
  285. # HACK Make reference HTML file for viewing output
  286. END {
  287. def image_tag(image_path)
  288. %(<img src="../#{image_path}" />)
  289. end
  290. reference_files = Dir['test/expected/*']
  291. output = TidyTable.new(reference_files).to_html(%w(Expected Actual)) do |record|
  292. [image_tag(record), image_tag(record.gsub('expected', 'actual'))]
  293. end
  294. FileUtils.mkdir_p("test/actual")
  295. File.open("test/output.html", "w") do |f|
  296. f.write <<-EOL
  297. <style>
  298. .first_column {
  299. text-align: right;
  300. }
  301. .last_column {
  302. text-align: left;
  303. }
  304. img {
  305. border: 1px solid #e0e0e0;
  306. }
  307. </style>
  308. EOL
  309. f.write output
  310. end
  311. # TODO: Squawk if there are files in actual that aren't in expected
  312. }