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

/exercise-solutions/churn-classes/exercise-9/formatter-tests.rb

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 81 lines | 59 code | 17 blank | 5 comment | 1 complexity | d9153e7ff4d008c23211dc29f6fe57e2 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 'formatter'
  8. class FormatterNormalUseTests < Test::Unit::TestCase
  9. def setup
  10. formatter = Formatter.new
  11. formatter.report_range(Time.local(2005, 1, 1), Time.local(2005, 2, 1))
  12. formatter.use_subsystem_with_change_count('sub1', 30)
  13. formatter.use_subsystem_with_change_count('sub2', 39)
  14. @output_lines = formatter.output.split("\n")
  15. end
  16. def test_header_comes_before_subsystem_lines
  17. assert_match(/Changes between/, @output_lines[0])
  18. end
  19. def test_both_lines_are_present_in_descending_change_count_order
  20. assert_match(/sub2.*39/, @output_lines[1])
  21. assert_match(/sub1.*30/, @output_lines[2])
  22. end
  23. def test_nothing_else_is_present
  24. assert_equal(3, @output_lines.size)
  25. end
  26. end
  27. class FormatterPrivateMethodTests < Test::Unit::TestCase
  28. def setup
  29. @formatter = Formatter.new
  30. end
  31. def test_header_format
  32. @formatter.report_range(Time.local(2001, 3, 3),
  33. Time.local(2002, 2, 2))
  34. assert_equal("Changes between March 3, 2001, and February 2, 2002:",
  35. @formatter.header)
  36. end
  37. def test_normal_subsystem_line_format
  38. assert_equal(' audit ********* (45)',
  39. @formatter.subsystem_line("audit", 45))
  40. end
  41. def test_asterisks_for_divides_by_five
  42. assert_equal('****', @formatter.asterisks_for(20))
  43. end
  44. def test_asterisks_for_rounds_up_and_down
  45. assert_equal('****', @formatter.asterisks_for(18))
  46. assert_equal('***', @formatter.asterisks_for(17))
  47. end
  48. def test_churn_line_to_int_extracts_parenthesized_change_count
  49. assert_equal(19, @formatter.churn_line_to_int(" churn2 **** (19)"))
  50. assert_equal(9, @formatter.churn_line_to_int(" churn ** (9)"))
  51. end
  52. def test_lines_are_ordered_by_descending_change_count
  53. @formatter.use_subsystem_with_change_count("a count matters for sorting, not a name", 1)
  54. @formatter.use_subsystem_with_change_count("inventory", 0)
  55. @formatter.use_subsystem_with_change_count("churn", 12)
  56. expected = [ " churn ** (12)",
  57. "all that really matters is the number in parens - (1)",
  58. " inventory (0)" ]
  59. actual = @formatter.lines_ordered_by_descending_change_count
  60. assert_match(/churn/, actual[0])
  61. assert_match(/a count matters/, actual[1])
  62. assert_match(/inventory/, actual[2])
  63. end
  64. end