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

/user-choices/test/builder-tests.rb

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 171 lines | 136 code | 28 blank | 7 comment | 2 complexity | a93e4662c3cdf98cc9862bce1338210c 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. load "set-standalone-test-paths.rb" unless $started_from_rakefile
  7. require 'test/unit'
  8. require 's4t-utils'
  9. require 'user-choices'
  10. include S4tUtils
  11. class TestDefaultsAndTypes < Test::Unit::TestCase
  12. include UserChoices
  13. def test_builder_can_add_defaults
  14. b = ChoicesBuilder.new
  15. b.add_choice(:trip_steps, :default => '5')
  16. choices = b.build
  17. assert_equal('5', choices[:trip_steps])
  18. end
  19. def test_builder_can_declare_types_and_do_error_checking
  20. b = ChoicesBuilder.new
  21. b.add_choice(:trip_steps, :default => 'a', :type => :integer)
  22. assert_raises_with_matching_message(StandardError,
  23. /':trip_steps' requires an integer value, and 'a'/) {
  24. b.build
  25. }
  26. end
  27. def test_builder_can_declare_types_and_do_conversions
  28. b = ChoicesBuilder.new
  29. b.add_choice(:csv, :default => 'true', :type => :boolean)
  30. choices = b.build
  31. assert_equal(true, choices[:csv])
  32. end
  33. def test_some_types_cause_no_conversion
  34. # Checking is done
  35. b = ChoicesBuilder.new
  36. b.add_choice(:trip_steps, :default => 'a', :type => ['b', 'c'])
  37. assert_raises_with_matching_message(StandardError,
  38. /'a' is not a valid value/) {
  39. b.build
  40. }
  41. # ... but, if checking passes, no changes are made
  42. b = ChoicesBuilder.new
  43. b.add_choice(:trip_steps, :default => 'b', :type => ['b', 'c'])
  44. assert_equal('b', b.build[:trip_steps])
  45. end
  46. def test_arrays_can_be_built_from_comma_separated_list
  47. b = ChoicesBuilder.new
  48. b.add_choice(:targets, :default => 'a,b,cd',
  49. :type => [:string])
  50. assert_equal(['a', 'b', 'cd'],
  51. b.build[:targets])
  52. end
  53. def test_arrays_can_be_accepted_as_is
  54. b = ChoicesBuilder.new
  55. b.add_choice(:targets, :default => ['a', 'b', 'c'],
  56. :type => [:string])
  57. assert_equal(['a', 'b', 'c'], b.build[:targets])
  58. end
  59. def arrays_are_constructed_from_single_elements
  60. b = ChoicesBuilder.new
  61. b.add_choice(:targets, :default => 'a',
  62. :type => [:string])
  63. assert_equal(['a'], b.build[:targets])
  64. end
  65. end
  66. class TestChainingOfSources < Test::Unit::TestCase
  67. include UserChoices
  68. def test_sources_are_chained_correctly
  69. with_environment_vars("prefix_in_ecd" => "e") {
  70. with_local_config_file(".builder_rc",
  71. "<config>
  72. <in_ecd>c</in_ecd>
  73. <in_cd>c</in_cd>
  74. </config>") {
  75. b = ChoicesBuilder.new
  76. b.add_source(EnvironmentChoices, :with_prefix, "prefix_")
  77. b.add_source(XmlConfigFileChoices, :from_file, ".builder_rc")
  78. b.add_choice(:in_ecd, :default => "d")
  79. b.add_choice(:in_cd, :default => "d")
  80. b.add_choice(:in_d, :default => "d")
  81. choices = b.build
  82. assert_equal('e', choices[:in_ecd])
  83. assert_equal('c', choices[:in_cd])
  84. assert_equal('d', choices[:in_d])
  85. }
  86. }
  87. end
  88. end
  89. class TestCommandLineConstruction < Test::Unit::TestCase
  90. include UserChoices
  91. def test_command_line_choices_requires_blocks_for_initialization
  92. with_command_args("--switch -c 5 arg") {
  93. b = ChoicesBuilder.new
  94. b.add_source(CommandLineChoices, :fill)
  95. b.add_choice(:unused) { | command_line |
  96. command_line.uses_switch("-u", "--unused")
  97. }
  98. b.add_choice(:switch, :type=>:boolean) { | command_line |
  99. command_line.uses_switch("--switch")
  100. }
  101. b.add_choice(:clear, :type => :integer) { | command_line |
  102. command_line.uses_option("-c", "--clear N",
  103. "Clear the frobbistat N times.")
  104. }
  105. b.add_choice(:args) { | command_line |
  106. command_line.uses_arglist
  107. }
  108. choices = b.build
  109. assert_equal(true, choices[:switch])
  110. assert_false(choices.has_key?(:unused))
  111. assert_equal(5, choices[:clear])
  112. assert_equal(['arg'], choices[:args])
  113. }
  114. end
  115. def test_command_line_source_initializes_help_text
  116. with_command_args('--help') {
  117. output = capturing_stderr do
  118. assert_wants_to_exit do
  119. b = ChoicesBuilder.new
  120. b.add_source(CommandLineChoices, :usage,
  121. "Usage: prog [options]",
  122. "This is supplemental.")
  123. b.add_choice(:test) { | command_line |
  124. command_line.uses_switch("--test",
  125. "Here's text for a switch")
  126. command_line.uses_option("-r", "--renew VALUE",
  127. "Here's text for an option")
  128. }
  129. b.build
  130. end
  131. end
  132. assert(l1 = output.index("Usage: prog [options]"))
  133. assert(l2 = output.index("This is supplemental"))
  134. assert(l3 = output.index(/--\[no-\]test.*Here's text for a switch/))
  135. assert(l4 = output.index(/-r.*--renew.*VALUE.*Here's text for an option/))
  136. assert(l5 = output.index("--help"))
  137. assert(l1 < l2)
  138. assert(l2 < l3)
  139. assert(l3 < l4)
  140. assert(l4 < l5)
  141. }
  142. end
  143. end