PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 87 lines | 57 code | 25 blank | 5 comment | 0 complexity | 6586a4e967b1cc28eec5f28c12521047 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-3'
  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 FormatterTests < Test::Unit::TestCase #(1)
  24. def setup #(2)
  25. @formatter = Formatter.new
  26. end
  27. def test_header_format
  28. assert_equal("Changes since 2005-08-05:",
  29. @formatter.header('2005-08-05')) #(3)
  30. end
  31. def test_normal_subsystem_line_format
  32. assert_equal(' audit ********* (45)',
  33. @formatter.subsystem_line("audit", 45))
  34. end
  35. def test_asterisks_for_divides_by_five
  36. assert_equal('****', @formatter.asterisks_for(20))
  37. end
  38. def test_asterisks_for_rounds_up_and_down
  39. assert_equal('****', @formatter.asterisks_for(18))
  40. assert_equal('***', @formatter.asterisks_for(17))
  41. end
  42. def test_churn_line_to_int_extracts_parenthesized_change_count
  43. assert_equal(19, @formatter.churn_line_to_int(" churn2 **** (19)"))
  44. assert_equal(9, @formatter.churn_line_to_int(" churn ** (9)"))
  45. end
  46. def test_order_by_descending_change_count
  47. original = [ "all that really matters is the number in parens - (1)",
  48. " inventory (0)",
  49. " churn ** (12)" ]
  50. expected = [ " churn ** (12)",
  51. "all that really matters is the number in parens - (1)",
  52. " inventory (0)" ]
  53. actual = @formatter.order_by_descending_change_count(original)
  54. assert_equal(expected, actual)
  55. end
  56. end
  57. class ChurnTests < Test::Unit::TestCase
  58. def test_month_before_is_28_days
  59. assert_equal(Time.local(2005, 1, 1),
  60. month_before(Time.local(2005, 1, 29)))
  61. end
  62. end