PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test_progress_bar.rb

http://github.com/koraktor/rubikon
Ruby | 123 lines | 95 code | 24 blank | 4 comment | 0 complexity | fd0eca5ed51ff672244b11c80a4455fe MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # This code is free software; you can redistribute it and/or modify it under
  2. # the terms of the new BSD License.
  3. #
  4. # Copyright (c) 2009-2010, Sebastian Staudt
  5. require 'helper'
  6. class TestProgressBar < Test::Unit::TestCase
  7. context 'A progress bar' do
  8. should 'have default settings' do
  9. @bar = ProgressBar.new
  10. assert_equal 0.2, @bar.instance_variable_get(:@factor)
  11. assert_equal 100, @bar.instance_variable_get(:@maximum)
  12. assert_equal $stdout, @bar.instance_variable_get(:@ostream)
  13. assert_equal 0, @bar.instance_variable_get(:@progress)
  14. assert_equal '#', @bar.instance_variable_get(:@progress_char)
  15. assert_equal 0, @bar.instance_variable_get(:@value)
  16. end
  17. should 'have an easily changeable maximum' do
  18. @bar = ProgressBar.new(10)
  19. assert_equal 2, @bar.instance_variable_get(:@factor)
  20. assert_equal 10, @bar.instance_variable_get(:@maximum)
  21. assert_equal $stdout, @bar.instance_variable_get(:@ostream)
  22. assert_equal 0, @bar.instance_variable_get(:@progress)
  23. assert_equal '#', @bar.instance_variable_get(:@progress_char)
  24. assert_equal 0, @bar.instance_variable_get(:@value)
  25. end
  26. should 'be customizable' do
  27. options = {
  28. :char => '+',
  29. :maximum => 10,
  30. :ostream => StringIO.new,
  31. :size => 100,
  32. :start => 5
  33. }
  34. @bar = ProgressBar.new(options)
  35. assert_equal options[:size].to_f / options[:maximum], @bar.instance_variable_get(:@factor)
  36. assert_equal options[:maximum], @bar.instance_variable_get(:@maximum)
  37. assert_equal options[:ostream], @bar.instance_variable_get(:@ostream)
  38. assert_equal options[:start].to_f / options[:maximum] * options[:size], @bar.instance_variable_get(:@progress)
  39. assert_equal options[:char], @bar.instance_variable_get(:@progress_char)
  40. assert_equal options[:start], @bar.instance_variable_get(:@value)
  41. end
  42. should 'draw correctly for different sizes' do
  43. ostream = StringIO.new
  44. options = { :ostream => ostream, :start => 50 }
  45. @bar = ProgressBar.new(options)
  46. assert_equal "#" * 10, ostream.string
  47. @bar + 50
  48. assert_equal ("#" * 20) << "\n", ostream.string
  49. ostream.string = ''
  50. options[:size] = 10
  51. @bar = ProgressBar.new(options)
  52. assert_equal "#" * 5, ostream.string
  53. @bar + 10
  54. assert_equal "#" * 6, ostream.string
  55. ostream.string = ''
  56. options[:size] = 100
  57. @bar = ProgressBar.new(options)
  58. assert_equal "#" * 50, ostream.string
  59. @bar + 30
  60. assert_equal "#" * 80, ostream.string
  61. end
  62. should 'not overflow' do
  63. ostream = StringIO.new
  64. options = { :ostream => ostream, :size => 100 }
  65. @bar = ProgressBar.new options
  66. @bar + 101
  67. assert_equal ("#" * 100) << "\n", ostream.string
  68. assert_equal 100, @bar.instance_variable_get(:@progress)
  69. assert_equal 101, @bar.instance_variable_get(:@value)
  70. ostream.string = ''
  71. options[:start] = 50
  72. @bar = ProgressBar.new options
  73. @bar + 51
  74. assert_equal ("#" * 100) << "\n", ostream.string
  75. assert_equal 100, @bar.instance_variable_get(:@progress)
  76. assert_equal 101, @bar.instance_variable_get(:@value)
  77. ostream.string = ''
  78. options[:start] = 101
  79. @bar = ProgressBar.new options
  80. assert_equal ("#" * 100) << "\n", ostream.string
  81. assert_equal 100, @bar.instance_variable_get(:@progress)
  82. assert_equal 101, @bar.instance_variable_get(:@value)
  83. end
  84. should 'have brackets when enabled' do
  85. ostream = StringIO.new
  86. options = {
  87. :brackets => true,
  88. :bracket_filler => '-',
  89. :ostream => ostream
  90. }
  91. @bar = ProgressBar.new options
  92. assert_equal '[' + '-' * 20 + ']' + "\b" * 21, ostream.string
  93. @bar + 50
  94. assert_equal '[' + '-' * 20 + ']' + "\b" * 21 + '#' * 10, ostream.string
  95. @bar + 50
  96. assert_equal '[' + '-' * 20 + ']' + "\b" * 21 + '#' * 20 + "\n", ostream.string
  97. end
  98. end
  99. end