PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/padrino-gen/lib/padrino-gen/generators/components/tests/testunit.rb

http://github.com/padrino/padrino-framework
Ruby | 112 lines | 83 code | 20 blank | 9 comment | 0 complexity | bddff1e1c854eabdf7662aa7043b6e78 MD5 | raw file
Possible License(s): MIT
  1. TESTUNIT_SETUP = (<<-TEST).gsub(/^ {10}/, '') unless defined?(TESTUNIT_SETUP)
  2. RACK_ENV = 'test' unless defined?(RACK_ENV)
  3. require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
  4. Dir[File.expand_path(File.dirname(__FILE__) + "/../app/helpers/**/*.rb")].each(&method(:require))
  5. class Test::Unit::TestCase
  6. include Rack::Test::Methods
  7. # You can use this method to custom specify a Rack app
  8. # you want rack-test to invoke:
  9. #
  10. # app CLASS_NAME
  11. # app CLASS_NAME.tap { |a| }
  12. # app(CLASS_NAME) do
  13. # set :foo, :bar
  14. # end
  15. #
  16. def app(app = nil, &blk)
  17. @app ||= block_given? ? app.instance_eval(&blk) : app
  18. @app ||= Padrino.application
  19. end
  20. end
  21. TEST
  22. TESTUNIT_RAKE = (<<-TEST).gsub(/^ {10}/, '') unless defined?(TESTUNIT_RAKE)
  23. require 'rake/testtask'
  24. test_tasks = Dir['test/*/'].map { |d| File.basename(d) }
  25. test_tasks.each do |folder|
  26. Rake::TestTask.new("test:\#{folder}") do |test|
  27. test.pattern = "test/\#{folder}/**/*_test.rb"
  28. test.verbose = true
  29. end
  30. end
  31. desc "Run application test suite"
  32. task 'test' => test_tasks.map { |f| "test:\#{f}" }
  33. task :default => :test
  34. TEST
  35. TESTUNIT_CONTROLLER_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(TESTUNIT_CONTROLLER_TEST)
  36. require File.expand_path(File.dirname(__FILE__) + '/../../test_config.rb')
  37. class !NAME!ControllerTest < Test::Unit::TestCase
  38. def setup
  39. get "/"
  40. end
  41. def test_returns_hello_world_text
  42. assert_equal "Hello World", last_response.body
  43. end
  44. end
  45. TEST
  46. TESTUNIT_MODEL_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(TESTUNIT_MODEL_TEST)
  47. require File.expand_path(File.dirname(__FILE__) + '!PATH!/test_config.rb')
  48. class !NAME!Test < Test::Unit::TestCase
  49. def test_constructs_a_new_instance
  50. @!DNAME! = !NAME!.new
  51. refute_nil @!DNAME!
  52. end
  53. end
  54. TEST
  55. TESTUNIT_HELPER_TEST = (<<-TEST) unless defined?(TESTUNIT_HELPER_TEST)
  56. require File.expand_path(File.dirname(__FILE__) + '!PATH!/test_config.rb')
  57. class !NAME!Test < Test::Unit::TestCase
  58. def self.setup
  59. @helpers = Class.new
  60. @helpers.extend !CONSTANT_NAME!
  61. end
  62. def helpers
  63. @helpers
  64. end
  65. def test_foo_helper
  66. assert_equal nil, helpers.foo
  67. end
  68. end
  69. TEST
  70. def setup_test
  71. require_dependencies 'rack-test', :require => 'rack/test', :group => 'test'
  72. require_dependencies 'test-unit', :require => 'test/unit', :group => 'test'
  73. insert_test_suite_setup TESTUNIT_SETUP
  74. create_file destination_root("test/test.rake"), TESTUNIT_RAKE
  75. end
  76. def generate_controller_test(name, path)
  77. test_unit_contents = TESTUNIT_CONTROLLER_TEST.gsub(/!NAME!/, name.to_s.underscore.camelize)
  78. controller_test_path = File.join('test',options[:app],'controllers',"#{name.to_s.underscore}_controller_test.rb")
  79. create_file destination_root(controller_test_path), test_unit_contents, :skip => true
  80. end
  81. def generate_model_test(name)
  82. test_unit_contents = TESTUNIT_MODEL_TEST.gsub(/!NAME!/, name.to_s.underscore.camelize).gsub(/!DNAME!/, name.to_s.underscore)
  83. test_unit_contents.gsub!(/!PATH!/, recognize_path)
  84. model_test_path = File.join('test',options[:app],'models',"#{name.to_s.underscore}_test.rb")
  85. create_file destination_root(model_test_path), test_unit_contents, :skip => true
  86. end
  87. def generate_helper_test(name, project_name, app_name)
  88. test_unit_contents = TESTUNIT_HELPER_TEST.gsub(/!NAME!/, name).gsub(/!CONSTANT_NAME!/, "#{project_name}::#{app_name}::#{name}")
  89. test_unit_contents.gsub!(/!PATH!/, recognize_path)
  90. helper_spec_path = File.join('test', options[:app], 'helpers', "#{name.underscore}_test.rb")
  91. create_file destination_root(helper_spec_path), test_unit_contents, :skip => true
  92. end