PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/churn/snapshots/churn-tests-classes.rb

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 107 lines | 52 code | 23 blank | 32 comment | 0 complexity | 6f16f1c687a595a1ba441688de9dfd5d 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 'churn'
  8. # If I grouped the methods under test, it seems sensible to also group
  9. # the methods testing them, so I put them all inside their own test
  10. # class.
  11. class SubversionRepositoryTests < Test::Unit::TestCase
  12. # Each one of the repository test methods needs a repository. Each
  13. # one would create it exactly the same way. I can remove that
  14. # duplication by putting it in this setup method. Like methods
  15. # beginning with "test", it's special. Test::Unit knows to run it
  16. # before each test method.
  17. #
  18. # Since SubversionRepositoryTests is a class like any other, I can
  19. # use an instance variable to communicate the repository to the test
  20. # methods.
  21. def setup
  22. @repository = SubversionRepository.new('root')
  23. end
  24. def test_date
  25. assert_equal('2005-03-04',
  26. @repository.date(Time.local(2005, 3, 4)))
  27. end
  28. def test_subversion_log_can_have_no_changes
  29. assert_equal(0, @repository.extract_change_count_from("------------------------------------------------------------------------\n"))
  30. end
  31. def test_subversion_log_with_changes
  32. 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"))
  33. end
  34. end
  35. class ChurnTests < Test::Unit::TestCase
  36. def test_month_before_is_28_days
  37. assert_equal(Time.local(2005, 1, 1),
  38. month_before(Time.local(2005, 1, 29)))
  39. end
  40. def test_header_format
  41. assert_equal("Changes since 2005-08-05:",
  42. header('2005-08-05'))
  43. end
  44. # The above test used to look like this:
  45. #
  46. # def test_header_format
  47. # assert_equal("Changes since 2005-08-05:",
  48. # header(svn_date(month_before(Time.local(2005, 9, 2)))))
  49. # end
  50. #
  51. # I could have changed it to create a SubversionRepository object
  52. # and use it to convert Ruby's Time to a date string, but it's not
  53. # really header's responsibility to convert a *Subversion* time; its
  54. # responsibility is really just to add formatting to whatever time
  55. # string is passed in, no matter what the format.
  56. #
  57. # I faced the same decision earlier and made the opposite choice. I
  58. # guess I was wrong one of the times.
  59. def test_normal_subsystem_line_format
  60. assert_equal(' audit ********* (45)',
  61. subsystem_line("audit", 45))
  62. end
  63. def test_asterisks_for_divides_by_five
  64. assert_equal('****', asterisks_for(20))
  65. end
  66. def test_asterisks_for_rounds_up_and_down
  67. assert_equal('****', asterisks_for(18))
  68. assert_equal('***', asterisks_for(17))
  69. end
  70. def test_churn_line_to_int_extracts_parenthesized_change_count
  71. assert_equal(19, churn_line_to_int(" ui2 **** (19)"))
  72. assert_equal(9, churn_line_to_int(" ui ** (9)"))
  73. end
  74. def test_order_by_descending_change_count
  75. original = [ "all that really matters is the number in parens - (1)",
  76. " inventory (0)",
  77. " ui ** (12)" ]
  78. expected = [ " ui ** (12)",
  79. "all that really matters is the number in parens - (1)",
  80. " inventory (0)" ]
  81. actual = order_by_descending_change_count(original)
  82. assert_equal(expected, actual)
  83. end
  84. end