Skip to content

Commit

Permalink
A-done
Browse files Browse the repository at this point in the history
  • Loading branch information
khamusa committed Jan 10, 2025
1 parent 121ce97 commit 6b8b4be
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
3 changes: 3 additions & 0 deletions lib/tabasco.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
module Tabasco
class Error < StandardError; end
class PreconditionNotMetError < Error; end
class InconsistentPortalKlassError < Error; end
class PortalNotConfiguredError < Error; end
class PortalAlreadyConfiguredError < Error; end

def self.configure
yield configuration.dsl
Expand Down
10 changes: 5 additions & 5 deletions lib/tabasco/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

module Tabasco
class Configuration
class Error < ::Tabasco::Error; end
class PortalNotConfigured < Error; end

def initialize
@portals = {}
end
Expand All @@ -23,7 +20,7 @@ def portal(name)
portals. Refer to the README document for more information.
ERR

raise PortalNotConfigured, message
raise PortalNotConfiguredError, message
end

class DSL
Expand Down Expand Up @@ -54,7 +51,10 @@ def portal(portal_name, klass = nil, test_id: nil)

portals = configuration.instance_variable_get(:@portals)

raise Error, "The portal #{portal_name.inspect} is already defined" if portals.key?(portal_name.to_sym)
if portals.key?(portal_name.to_sym)
raise PortalAlreadyConfiguredError,
"The portal #{portal_name.inspect} is already defined"
end

portals[portal_name] = {klass:, test_id:}

Expand Down
9 changes: 8 additions & 1 deletion lib/tabasco/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ def section(name, klass = nil, test_id: nil, &)
define_inline_section(name, klass, test_id: test_id, &)
end

def portal(name, local_concrete_klass = nil, &)
def portal(name, concrete_klass = nil, local_concrete_klass = nil, &)
Tabasco.configuration.portal(name) in { klass:, test_id: }

if klass && concrete_klass && !concrete_klass.ancestors.include?(klass)
raise InconsistentPortalKlassError,
"The class #{concrete_klass} provided to the portal \"#{name}\" " \
"must inherit from the configured portal class #{klass}."
end

klass = concrete_klass || klass
define_inline_section(name, klass, test_id:, portal: true, &)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/tabasco/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
it "raises an error when trying to read a portal that has not been defined" do
expect do
configuration.portal(:my_portal)
end.to raise_error(Tabasco::Configuration::PortalNotConfigured)
end.to raise_error(Tabasco::PortalNotConfiguredError)
end

it "raises an error when trying to define a portal with the same name twice" do
dsl.portal(:my_portal)

expect do
dsl.portal(:my_portal)
end.to raise_error(Tabasco::Configuration::Error, "The portal :my_portal is already defined")
end.to raise_error(Tabasco::PortalAlreadyConfiguredError, "The portal :my_portal is already defined")
end
end
end
34 changes: 31 additions & 3 deletions spec/tabasco/section_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

RSpec.describe Tabasco::Section do
def def_klass(container_test_id: :section_container, &block)
Class.new(described_class) do
def def_klass(parent: described_class, container_test_id: :section_container, &block)
Class.new(parent) do
container_test_id container_test_id

ensure_loaded { true }
Expand Down Expand Up @@ -199,7 +199,7 @@ def has_custom_query?
end

it "raises an error if the portal has not been configured" do
expect { section_klass.load.ipsum.lorem }.to raise_error(Tabasco::Configuration::PortalNotConfigured)
expect { section_klass.load.ipsum.lorem }.to raise_error(Tabasco::PortalNotConfiguredError)
end

context "when the portal is configured" do
Expand Down Expand Up @@ -289,6 +289,34 @@ def hello

expect(section.portal).not_to be(section.another_portal)
end

it "can be assigned a different concrete class as long as it's a subclass of the configured one" do
another_concrete_klass = def_klass(parent: concrete_klass, container_test_id: nil) do
def hello
"Hello from the multiverse!"
end
end

section_klass = def_klass do
portal :portal, another_concrete_klass
end

expect(section_klass.load.portal.hello).to eq("Hello from the multiverse!")
end

it "raises an error if the provided concrete class is not a subclass of the configured one" do
another_concrete_klass = def_klass(container_test_id: nil) do
def hello
"Hello from the multiverse!"
end
end

expect do
def_klass do
portal :portal, another_concrete_klass
end
end.to raise_error(Tabasco::InconsistentPortalKlassError)
end
end
end
end

0 comments on commit 6b8b4be

Please sign in to comment.