PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/exercise-solutions/churn-classes/exercise-7b-tests.rb

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 114 lines | 79 code | 30 blank | 5 comment | 1 complexity | fa22d7f6c326f4fb38659ca11fd01834 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-7b'
  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.use_subsystem_with_change_count('sub1', 30)
  28. formatter.use_subsystem_with_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. @formatter.use_date('2005-08-05')
  48. assert_equal("Changes since 2005-08-05:",
  49. @formatter.header)
  50. end
  51. def test_normal_subsystem_line_format
  52. assert_equal(' audit ********* (45)',
  53. @formatter.subsystem_line("audit", 45))
  54. end
  55. def test_asterisks_for_divides_by_five
  56. assert_equal('****', @formatter.asterisks_for(20))
  57. end
  58. def test_asterisks_for_rounds_up_and_down
  59. assert_equal('****', @formatter.asterisks_for(18))
  60. assert_equal('***', @formatter.asterisks_for(17))
  61. end
  62. def test_churn_line_to_int_extracts_parenthesized_change_count
  63. assert_equal(19, @formatter.churn_line_to_int(" churn2 **** (19)"))
  64. assert_equal(9, @formatter.churn_line_to_int(" churn ** (9)"))
  65. end
  66. def test_lines_are_ordered_by_descending_change_count
  67. @formatter.use_subsystem_with_change_count("a count matters for sorting, not a name", 1)
  68. @formatter.use_subsystem_with_change_count("inventory", 0)
  69. @formatter.use_subsystem_with_change_count("churn", 12)
  70. expected = [ " churn ** (12)",
  71. "all that really matters is the number in parens - (1)",
  72. " inventory (0)" ]
  73. actual = @formatter.lines_ordered_by_descending_change_count
  74. assert_match(/churn/, actual[0])
  75. assert_match(/a count matters/, actual[1])
  76. assert_match(/inventory/, actual[2])
  77. end
  78. end
  79. class ChurnTests < Test::Unit::TestCase
  80. def test_month_before_is_28_days
  81. assert_equal(Time.local(2005, 1, 1),
  82. month_before(Time.local(2005, 1, 29)))
  83. end
  84. end