diff options
author | Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com> | 2009-06-18 17:44:35 -0700 |
---|---|---|
committer | Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com> | 2009-06-23 14:49:03 -0700 |
commit | a3309e4d70101ec1b32064a99042622c43619d09 (patch) | |
tree | 56c286cc67bd0ecc8f64eee676df6fbea5217b9b /railties/test/new_initializer_test.rb | |
parent | 99b8248f6e83b87fc7f09e43a11c68aebcb2712e (diff) | |
download | rails-a3309e4d70101ec1b32064a99042622c43619d09.tar.gz rails-a3309e4d70101ec1b32064a99042622c43619d09.tar.bz2 rails-a3309e4d70101ec1b32064a99042622c43619d09.zip |
Started making progress on implementing a new initializer.
Class.any_instance.expects(:require).raises(LoadError)
... w0t
Diffstat (limited to 'railties/test/new_initializer_test.rb')
-rw-r--r-- | railties/test/new_initializer_test.rb | 174 |
1 files changed, 113 insertions, 61 deletions
diff --git a/railties/test/new_initializer_test.rb b/railties/test/new_initializer_test.rb index ffa10060ce..8d9ef7bee3 100644 --- a/railties/test/new_initializer_test.rb +++ b/railties/test/new_initializer_test.rb @@ -1,113 +1,165 @@ require 'abstract_unit' require 'active_support/ruby/shim' - -module Rails - class Initializer - class Error < StandardError ; end - class Runner - def initialize - @names = {} - @initializers = [] - end - - def add(name, options = {}, &block) - # If :before or :after is specified, set the index to the right spot - if other = options[:before] || options[:after] - raise Error, "The #{other.inspect} initializer does not exist" unless @names[other] - index = @initializers.index(@names[other]) - index += 1 if options[:after] - end - - Class.new(Initializer, &block).new.tap do |initializer| - @initializers.insert(index || -1, initializer) - @names[name] = initializer - end - end - - def run - @initializers.each { |init| init.run } - end - end - - def self.run(&blk) - define_method(:run, &blk) - end - end -end - +require 'initializer' class InitializerRunnerTest < ActiveSupport::TestCase - + def setup @runner = Rails::Initializer::Runner.new end - + test "A new runner can be created" do assert @runner end - - test "You can create initializers" do - init = @runner.add :foo do - - end - - assert_kind_of Rails::Initializer, init - end - + test "The initializers actually get run when the runner is run" do state = nil - + @runner.add :foo do run { state = true } end - + @runner.run assert state end - + test "By default, initializers get run in the order that they are added" do state = [] - + @runner.add :first do run { state << :first } end - + @runner.add :second do run { state << :second } end - + @runner.run assert_equal [:first, :second], state end - + test "Raises an exception if :before or :after are specified, but don't exist" do assert_raise(Rails::Initializer::Error) do @runner.add(:fail, :before => :whale) { 1 } end - + assert_raise(Rails::Initializer::Error) do @runner.add(:fail, :after => :whale) { 1 } end end - + test "When adding an initializer, specifying :after allows you to move an initializer after another" do state = [] - + @runner.add :first do run { state << :first } end - + @runner.add :second do run { state << :second } end - + @runner.add :third, :after => :first do run { state << :third } end - + @runner.run assert_equal [:first, :third, :second], state end - -end + test "An initializer can be deleted" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + @runner.delete(:second) + + @runner.run + assert_equal [:first], state + end + + test "A runner can be initialized with an existing runner, which it copies" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + Rails::Initializer::Runner.new(@runner).run + assert_equal [:first, :second], state + end + + test "A child runner can be still be modified without modifying the parent" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + new_runner = Rails::Initializer::Runner.new(@runner) + new_runner.add :trois do + run { state << :trois } + end + new_runner.delete(:second) + + new_runner.run + assert_equal [:first, :trois], state + state.clear + @runner.run + assert_equal [:first, :second], state + end + + test "A child runner that is modified does not modify any other children of the same parent" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + child_one = Rails::Initializer::Runner.new(@runner) + child_two = Rails::Initializer::Runner.new(@runner) + + child_one.delete(:second) + child_two.run + + assert_equal [:first, :second], state + end + + test "It does not run the initializer block immediately" do + state = [] + @runner.add :first do + state << :first + end + + assert_equal [], state + end + + test "It runs the block when the runner is run" do + state = [] + @runner.add :first do + state << :first + end + + @runner.run + assert_equal [:first], state + end + +end
\ No newline at end of file |