PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/exercise-solutions/churn-classes/exercise-4.rb

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 113 lines | 76 code | 32 blank | 5 comment | 0 complexity | 4727773c5ece0fa2fbb32d1aee2e0e83 MD5 | raw file
  1. #---
  2. # Excerpted from "Everyday Scripting in Ruby"
  3. # We make no guarantees that this code is fit for any purpose.
  4. # Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
  5. #---
  6. require 'test/unit'
  7. require 'exercise-7'
  8. class SubversionRepositoryTests < Test::Unit::TestCase
  9. def setup
  10. @repository = SubversionRepository.new('root')
  11. end
  12. def test_date
  13. assert_equal('2005-03-04',
  14. @repository.date(Time.local(2005, 3, 4)))
  15. end
  16. def test_subversion_log_can_have_no_changes
  17. assert_equal(0, @repository.extract_change_count_from("------------------------------------------------------------------------\n"))
  18. end
  19. def test_subversion_log_with_changes
  20. assert_equal(2, @repository.extract_change_count_from("------------------------------------------------------------------------\nr2531 | bem | 2005-07-01 01:11:44 -0500 (Fri, 01 Jul 2005) | 1 line\n\nrevisions up through ch 3 exercises\n------------------------------------------------------------------------\nr2524 | bem | 2005-06-30 18:45:59 -0500 (Thu, 30 Jun 2005) | 1 line\n\nresults of read-through; including renaming mistyping to snapshots\n------------------------------------------------------------------------\n"))
  21. end
  22. end
  23. class FormatterNormalUseTests < Test::Unit::TestCase
  24. def setup
  25. formatter = Formatter.new
  26. formatter.use_date('1960-02-19')
  27. formatter.add_subsystem_change_count('sub1', 30)
  28. formatter.add_subsystem_change_count('sub2', 39)
  29. @output_lines = formatter.output.split("\n") #(1)
  30. end
  31. def test_header_comes_before_subsystem_lines #(2)
  32. assert_match(/Changes since 1960-02-19/, @output_lines[0])
  33. end
  34. def test_both_lines_are_present_in_descending_change_count_order #(3)
  35. assert_match(/sub2.*39/, @output_lines[1])
  36. assert_match(/sub1.*30/, @output_lines[2])
  37. end
  38. def test_nothing_else_is_present #(4)
  39. assert_equal(3, @output_lines.size)
  40. end
  41. end
  42. class FormatterPrivateMethodTests < Test::Unit::TestCase
  43. def setup
  44. @formatter = Formatter.new
  45. end
  46. def test_header_format
  47. assert_equal("Changes since 2005-08-05:",
  48. @formatter.header('2005-08-05'))
  49. end
  50. def test_normal_subsystem_line_format
  51. assert_equal(' audit ********* (45)',
  52. @formatter.subsystem_line("audit", 45))
  53. end
  54. def test_asterisks_for_divides_by_five
  55. assert_equal('****', @formatter.asterisks_for(20))
  56. end
  57. def test_asterisks_for_rounds_up_and_down
  58. assert_equal('****', @formatter.asterisks_for(18))
  59. assert_equal('***', @formatter.asterisks_for(17))
  60. end
  61. def test_churn_line_to_int_extracts_parenthesized_change_count
  62. assert_equal(19, @formatter.churn_line_to_int(" churn2 **** (19)"))
  63. assert_equal(9, @formatter.churn_line_to_int(" churn ** (9)"))
  64. end
  65. def test_order_by_descending_change_count
  66. original = [ "all that really matters is the number in parens - (1)",
  67. " inventory (0)",
  68. " churn ** (12)" ]
  69. expected = [ " churn ** (12)",
  70. "all that really matters is the number in parens - (1)",
  71. " inventory (0)" ]
  72. actual = @formatter.order_by_descending_change_count(original)
  73. assert_equal(expected, actual)
  74. end
  75. end
  76. class ChurnTests < Test::Unit::TestCase
  77. def test_month_before_is_28_days
  78. assert_equal(Time.local(2005, 1, 1),
  79. month_before(Time.local(2005, 1, 29)))
  80. end
  81. end