Sha256: 4652af2bff6a24f2228970cd3bb18a1982ddb33f6a3e74d0496c34e016e05137

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

require 'test_helper'

class AutoloaderTest < Minitest::Test
  include FakesFilesystem
  include TestsAutoloading

  def test_throws_constant_when_found
    const = assert_catches :const do
      Truck::Autoloader.handle :A, from: Foo
    end
    assert_equal 'Foo::A', const.name
  end

  def test_raises_error_when_const_not_found
    exception = assert_raises NameError do
      Truck::Autoloader.handle :Abracadabra, from: Foo
    end
    assert_equal 'uninitialized constant Abracadabra (in Foo)', exception.message
  end

  def test_simple_implicit_namespace_case
    Truck::Autoloader.handle :B, from: Foo
    const = assert_catches :const do
      Truck::Autoloader.handle :BA, from: Foo
    end
    assert_equal 'Foo::B::BA', const.name
    assert_nil Truck::Autoloader.current_autoloader
  end

  def test_cleanup_after_implicit_namespace
    Truck::Autoloader.handle :B, from: Foo
    assert_raises NameError do
      Truck::Autoloader.handle :Abracadabra, from: Foo
    end
    assert_nil Truck::Autoloader.current_autoloader
  end

  def test_deeply_nested_module_with_implicit_namespaces
    %i(C CA CAA).each do |implicit_namespace|
      Truck::Autoloader.handle implicit_namespace, from: Foo
    end
    const = assert_catches :const do
      Truck::Autoloader.handle :CAAA, from: Foo
    end
    assert_equal 'Foo::C::CA::CAA::CAAA', const.name
  end

  def test_shallowly_nested_module_with_implicit_namespaces
    @foo.resolve_const 'A::AB::ABA'

    const = assert_catches :const do
      Truck::Autoloader.handle :ABB, from: Foo::A::AB::ABA
    end
    assert_equal 'Foo::A::AB::ABB', const.name
  end

  private

  def assert_catches(thrown)
    val = catch thrown do
      yield
      :not_found
    end
    refute_equal :not_found, val, "Expected block to throw #{thrown.inspect}"
    val
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
truck-0.8.6 test/unit/autoloader_test.rb
truck-0.8.4 test/unit/autoloader_test.rb
truck-0.8.3 test/unit/autoloader_test.rb
truck-0.8.2 test/unit/autoloader_test.rb
truck-0.8.1 test/unit/autoloader_test.rb
truck-0.8.0 test/unit/autoloader_test.rb