Skip to content

Commit

Permalink
Update validators to use consistent initializer sig
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed May 22, 2024
1 parent 943bfe6 commit 9d1d399
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 70 deletions.
23 changes: 11 additions & 12 deletions lib/turbo_boost/commands/command_validator.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
# frozen_string_literal: true

class TurboBoost::Commands::CommandValidator
def initialize(class_name, method_name)
@class_name = class_name
def initialize(command, method_name)
@command = command
@method_name = method_name.to_sym
@command_class = class_name&.safe_constantize
end

attr_reader :class_name, :method_name, :command_class
attr_reader :command, :method_name

def validate
valid_class? && valid_method?
end
alias_method :valid?, :validate

def validate!
message = "`#{class_name}` is not a subclass of `#{TurboBoost::Commands::Command.name}`!"
raise TurboBoost::Commands::InvalidClassError.new(message, command: command_class) unless valid_class?
message = "`#{command.class.name}` is not a subclass of `#{TurboBoost::Commands::Command.name}`!"
raise TurboBoost::Commands::InvalidClassError.new(message, command: command.class) unless valid_class?

message = "`#{command_class.name}` does not define the public method `#{method_name}`!"
raise TurboBoost::Commands::InvalidMethodError.new(message, command: command_class) unless valid_method?
message = "`#{command.class.name}` does not define the public method `#{method_name}`!"
raise TurboBoost::Commands::InvalidMethodError.new(message, command: command.class) unless valid_method?

true
end

private

def command_ancestors
range = 0..(command_class&.ancestors&.index(TurboBoost::Commands::Command).to_i - 1)
command_class&.ancestors&.[](range) || []
range = 0..(command.class&.ancestors&.index(TurboBoost::Commands::Command).to_i - 1)
command.class&.ancestors&.[](range) || []
end

def valid_class?
command_class&.ancestors&.include? TurboBoost::Commands::Command
command.class&.ancestors&.include? TurboBoost::Commands::Command
end

def valid_method?
return false unless valid_class?
return false unless command_ancestors.any? { |a| a.public_instance_methods(false).any? method_name }

method = command_class.public_instance_method(method_name)
method = command.class.public_instance_method(method_name)
method&.parameters&.none?
end
end
2 changes: 1 addition & 1 deletion lib/turbo_boost/commands/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def command_instance
def command_valid?
return false unless command_requested?

validator = TurboBoost::Commands::CommandValidator.new(command_class_name, command_method_name)
validator = TurboBoost::Commands::CommandValidator.new(command_instance, command_method_name)
raise_on_invalid_command? ? validator.validate! : validator.valid?

validator = TurboBoost::Commands::TokenValidator.new(command_instance, command_method_name)
Expand Down
121 changes: 64 additions & 57 deletions test/turbo_boost/commands/command_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,162 +59,169 @@ def perform
end
end

setup do
controller = TestsController.new
state = TurboBoost::Commands::State.new
@command = TurboBoost::Commands::CommandValidatorTest::TestCommand.new(controller, state)
@invalid_command = TurboBoost::Commands::CommandValidatorTest::InvalidTestCommand.new
end

test "valid? with missing class" do
refute TurboBoost::Commands::CommandValidator.new("MissingCommand", :perform).valid?
refute TurboBoost::Commands::CommandValidator.new(nil, :perform).valid?
end

test "validate with missing class" do
refute TurboBoost::Commands::CommandValidator.new("MissingCommand", :perform).validate
refute TurboBoost::Commands::CommandValidator.new(nil, :perform).validate
end

test "validate! with missing class" do
assert_raises TurboBoost::Commands::InvalidClassError do
TurboBoost::Commands::CommandValidator.new("MissingCommand", :perform).validate!
TurboBoost::Commands::CommandValidator.new(nil, :perform).validate!
end
end

test "valid? with invalid class" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::InvalidTestCommand", :perform).valid?
refute TurboBoost::Commands::CommandValidator.new(@invalid_command, :perform).valid?
end

test "validate with invalid class" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::InvalidTestCommand", :perform).validate
refute TurboBoost::Commands::CommandValidator.new(@invalid_command, :perform).validate
end

test "validate! with invalid class" do
assert_raises TurboBoost::Commands::InvalidClassError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::InvalidTestCommand", :perform).validate!
TurboBoost::Commands::CommandValidator.new(@invalid_command, :perform).validate!
end
end

test "valid? with public methods" do
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform).valid?
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform).valid?
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform).valid?
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform).valid?
assert TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform).valid?
assert TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform).valid?
assert TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform).valid?
assert TurboBoost::Commands::CommandValidator.new(@command, :perform).valid?
end

test "valid? with protected methods" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform_protected).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform_protected).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform_protected).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_protected).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform_protected).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform_protected).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform_protected).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :perform_protected).valid?
end

test "valid? with private methods" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform_private).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform_private).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform_private).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_private).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform_private).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform_private).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform_private).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :perform_private).valid?
end

test "valid? with inherited methods" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).valid?
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :inspect).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :inspect).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :inspect).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :inspect).valid?
end

test "validate with public methods" do
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform).validate
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform).validate
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform).validate
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform).validate
assert TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform).validate
assert TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform).validate
assert TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform).validate
assert TurboBoost::Commands::CommandValidator.new(@command, :perform).validate
end

test "validate! with public methods that accept arguments" do
assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_with_args).validate!
TurboBoost::Commands::CommandValidator.new(@command, :perform_with_args).validate!
end
end

test "validate with public methods that accept arguments" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_with_args).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :perform_with_args).validate
end

test "valid? with public methods that accept arguments" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_with_args).valid?
refute TurboBoost::Commands::CommandValidator.new(@command, :perform_with_args).valid?
end

test "validate with protected methods" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform_protected).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform_protected).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform_protected).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_protected).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform_protected).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform_protected).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform_protected).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :perform_protected).validate
end

test "validate with private methods" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform_private).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform_private).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform_private).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_private).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform_private).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform_private).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform_private).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :perform_private).validate
end

test "validate with inherited methods" do
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).validate
refute TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :inspect).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :inspect).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :inspect).validate
refute TurboBoost::Commands::CommandValidator.new(@command, :inspect).validate
end

test "validate! with public methods" do
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform).validate!
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform).validate!
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform).validate!
assert TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform).validate!
assert TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform).validate!
assert TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform).validate!
assert TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform).validate!
assert TurboBoost::Commands::CommandValidator.new(@command, :perform).validate!
end

test "validate! with protected methods" do
assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform_protected).validate!
TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform_protected).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform_protected).validate!
TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform_protected).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform_protected).validate!
TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform_protected).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_protected).validate!
TurboBoost::Commands::CommandValidator.new(@command, :perform_protected).validate!
end
end

test "validate! with private methods" do
assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_mixin_perform_private).validate!
TurboBoost::Commands::CommandValidator.new(@command, :superclass_mixin_perform_private).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :superclass_perform_private).validate!
TurboBoost::Commands::CommandValidator.new(@command, :superclass_perform_private).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :mixin_perform_private).validate!
TurboBoost::Commands::CommandValidator.new(@command, :mixin_perform_private).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :perform_private).validate!
TurboBoost::Commands::CommandValidator.new(@command, :perform_private).validate!
end
end

test "validate! with inherited methods" do
assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).validate!
TurboBoost::Commands::CommandValidator.new(@command, :inspect).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).validate!
TurboBoost::Commands::CommandValidator.new(@command, :inspect).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).validate!
TurboBoost::Commands::CommandValidator.new(@command, :inspect).validate!
end

assert_raises TurboBoost::Commands::InvalidMethodError do
TurboBoost::Commands::CommandValidator.new("TurboBoost::Commands::CommandValidatorTest::TestCommand", :inspect).validate!
TurboBoost::Commands::CommandValidator.new(@command, :inspect).validate!
end
end
end

0 comments on commit 9d1d399

Please sign in to comment.