/test/unit/gem_test.rb

https://github.com/ToadJamb/gems_rake_tasks · Ruby · 145 lines · 91 code · 23 blank · 31 comment · 0 complexity · 56e41aa67ef99c35c6cf3c1d246824fc MD5 · raw file

  1. #--
  2. ################################################################################
  3. # Copyright (C) 2011 Travis Herrick #
  4. ################################################################################
  5. # #
  6. # \v^V,^!v\^/ #
  7. # ~% %~ #
  8. # { _ _ } #
  9. # ( * - ) #
  10. # | / | #
  11. # \ _, / #
  12. # \__.__/ #
  13. # #
  14. ################################################################################
  15. # This program is free software: you can redistribute it #
  16. # and/or modify it under the terms of the GNU Lesser General Public License #
  17. # as published by the Free Software Foundation, #
  18. # either version 3 of the License, or (at your option) any later version. #
  19. ################################################################################
  20. # This program is distributed in the hope that it will be useful, #
  21. # but WITHOUT ANY WARRANTY; #
  22. # without even the implied warranty of MERCHANTABILITY #
  23. # or FITNESS FOR A PARTICULAR PURPOSE. #
  24. # See the GNU Lesser General Public License for more details. #
  25. # #
  26. # You should have received a copy of the GNU Lesser General Public License #
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>. #
  28. ################################################################################
  29. #++
  30. require_relative '../require'
  31. class GemTest < Test::Unit::TestCase
  32. def setup
  33. @class = RakeTasks::Gem
  34. @spec_class = Gem::Specification
  35. end
  36. test '.gem_spec_file returns the gemspec file name' do
  37. expect :gemspec_file
  38. assert_equal gem_spec_file_name, @class.gem_spec_file
  39. end
  40. test '.gem_spec_file returns the first gemspec' do
  41. expect :gemspec_file, ['b.gemspec', 'a.gemspec']
  42. assert_equal 'b.gemspec', @class.gem_spec_file
  43. end
  44. test '.gem_spec_file returns nil if the gemspec file does not exist' do
  45. expect :gemspec_file, []
  46. assert_nil @class.gem_spec_file
  47. end
  48. test '.gemspec_file? returns true if the gem spec exists' do
  49. expect :gemspec_file
  50. assert @class.gemspec_file?
  51. end
  52. test '.gemspec_file? returns false if the gem spec does not exist' do
  53. expect :gemspec_file, []
  54. assert !@class.gemspec_file?
  55. end
  56. test '.gem_spec expects to load a gem spec' do
  57. expect :gemspec_file
  58. @spec_class.expects(:load => true).with(gem_spec_file_name)
  59. assert @class.gem_spec
  60. end
  61. test '.gem_spec returns nil if there is not a gemspec file' do
  62. expect :gemspec_file, []
  63. @spec_class.expects(:load => true).never
  64. assert_nil @class.gem_spec
  65. end
  66. test '.gem_title returns the proper name of the gem' do
  67. mock_gem_spec :name => 'test_gem'
  68. assert_equal 'TestGem', @class.gem_title(@spec)
  69. end
  70. test '.gem_title returns nil if a valid gem spec is not passed in' do
  71. assert_nil @class.gem_title(nil)
  72. assert_nil @class.gem_title(Object.new)
  73. end
  74. test '.version returns the version of the gem' do
  75. mock_gem_spec :name => 'test_gem', :version => '0.0.1'
  76. assert_equal 'test_gem version 0.0.1', @class.version(@spec)
  77. end
  78. test '.version returns nil if a valid gem spec is not passed in' do
  79. assert_nil @class.version(nil)
  80. assert_nil @class.version(Object.new)
  81. spec = mock
  82. spec.stubs :name => 'test_gem'
  83. assert_nil @class.version(spec)
  84. spec = mock
  85. spec.stubs :version => '0.0.1'
  86. assert_nil @class.version(spec)
  87. end
  88. ############################################################################
  89. private
  90. ############################################################################
  91. def expect(method, result = nil)
  92. case method
  93. when :gemspec_file
  94. result ||= [gem_spec_file_name]
  95. Dir.expects(:glob => result).with('*.gemspec').at_least_once
  96. end
  97. end
  98. def gem_spec_file_name
  99. 'test_gem.gemspec'
  100. end
  101. def mock_gem_spec(options = {})
  102. @spec = mock.responds_like(@spec_class.new)
  103. @spec.stubs options
  104. end
  105. def gem_spec(version)
  106. %Q{
  107. Gem::Specification.new do |s|
  108. s.name = 'test_gem'
  109. s.version = '#{version}'
  110. s.summary = 'Basic test gem.'
  111. s.description = %Q{
  112. This gem is a test gem.
  113. It is used in tests.
  114. }.strip
  115. s.author = 'Travis Herrick'
  116. s.email = 'tthetoad@gmail.com'
  117. s.homepage = 'http://www.bitbucket.org/ToadJamb/gems_test_gem'
  118. s.license = 'LGPLv3'
  119. end
  120. }.strip
  121. end
  122. end