Skip to content

Commit

Permalink
Introduce support for hanami generate part (#18)
Browse files Browse the repository at this point in the history
* Introduce support for `hanami generate part`

* Cleanup

* Ensure to generate specs for base part classes

* Point back to `hanami/cli` `main` branch

* Introduce `let(:value)`

* `#ruby_implicity_keyword_argument?` -> `#ruby_omit_hash_values?`
  • Loading branch information
jodosha authored Oct 26, 2023
1 parent 6351998 commit b8cfe6e
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/hanami/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def self.gem_loader
Hanami::CLI.after "install", Commands::Install
Hanami::CLI.after "generate slice", Commands::Generate::Slice
Hanami::CLI.after "generate action", Commands::Generate::Action
Hanami::CLI.after "generate part", Commands::Generate::Part
end
end
end
15 changes: 15 additions & 0 deletions lib/hanami/rspec/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ def call(options, **)
generator.call(app.namespace, slice, controller, action)
end
end

# @since 2.1.0
# @api private
class Part < Hanami::CLI::Commands::App::Command
# @since 2.1.0
# @api private
def call(options, **)
# FIXME: dry-cli kwargs aren't correctly forwarded in Ruby 3
slice = inflector.underscore(Shellwords.shellescape(options[:slice])) if options[:slice]
name = inflector.underscore(Shellwords.shellescape(options[:name]))

generator = Generators::Part.new(fs: fs, inflector: inflector)
generator.call(app.namespace, slice, name)
end
end
end
end
end
Expand Down
102 changes: 102 additions & 0 deletions lib/hanami/rspec/generators/part.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: true

require "erb"

module Hanami
module RSpec
module Generators
# @since 2.1.0
# @api private
class Part
# @since 2.1.0
# @api private
def initialize(fs:, inflector:)
@fs = fs
@inflector = inflector
end

# @since 2.1.0
# @api private
def call(app, slice, name, context: Hanami::CLI::Generators::App::PartContext.new(inflector, app, slice, name))
if slice
generate_for_slice(slice, context)
else
generate_for_app(context)
end
end

private

# @since 2.1.0
# @api private
def generate_for_slice(slice, context)
generate_base_part_for_app(context)
generate_base_part_for_slice(context, slice)

fs.write(
"spec/slices/#{slice}/views/parts/#{context.underscored_name}_spec.rb",
t("part_slice_spec.erb", context)
)
end

# @since 2.1.0
# @api private
def generate_for_app(context)
generate_base_part_for_app(context)

fs.write(
"spec/views/parts/#{context.underscored_name}_spec.rb",
t("part_spec.erb", context)
)
end

# @since 2.1.0
# @api private
def generate_base_part_for_app(context)
path = fs.join("spec", "views", "part_spec.rb")
return if fs.exist?(path)

fs.write(
path,
t("part_base_spec.erb", context)
)
end

# @since 2.1.0
# @api private
def generate_base_part_for_slice(context, slice)
path = "spec/slices/#{slice}/views/part_spec.rb"
return if fs.exist?(path)

fs.write(
path,
t("part_slice_base_spec.erb", context)
)
end

# @since 2.1.0
# @api private
attr_reader :fs

# @since 2.1.0
# @api private
attr_reader :inflector

# @since 2.1.0
# @api private
def template(path, context)
require "erb"

ERB.new(
File.read(__dir__ + "/part/#{path}"),
trim_mode: "-"
).result(context.ctx)
end

# @since 2.1.0
# @api private
alias_method :t, :template
end
end
end
end
14 changes: 14 additions & 0 deletions lib/hanami/rspec/generators/part/part_base_spec.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

RSpec.describe <%= camelized_app_name %>::Views::Part do
<%- if ruby_omit_hash_values? -%>
subject { described_class.new(value:) }
<%- else -%>
subject { described_class.new(value: value) }
<%- end -%>
let(:value) { double("value") }

it "works" do
expect(subject).to be_kind_of(described_class)
end
end
14 changes: 14 additions & 0 deletions lib/hanami/rspec/generators/part/part_slice_base_spec.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

RSpec.describe <%= camelized_slice_name %>::Views::Part do
<%- if ruby_omit_hash_values? -%>
subject { described_class.new(value:) }
<%- else -%>
subject { described_class.new(value: value) }
<%- end -%>
let(:value) { double("value") }

it "works" do
expect(subject).to be_kind_of(described_class)
end
end
14 changes: 14 additions & 0 deletions lib/hanami/rspec/generators/part/part_slice_spec.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

RSpec.describe <%= camelized_slice_name %>::Views::Parts::<%= camelized_name %> do
<%- if ruby_omit_hash_values? -%>
subject { described_class.new(value:) }
<%- else -%>
subject { described_class.new(value: value) }
<%- end -%>
let(:value) { double("<%= underscored_name %>") }

it "works" do
expect(subject).to be_kind_of(described_class)
end
end
14 changes: 14 additions & 0 deletions lib/hanami/rspec/generators/part/part_spec.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

RSpec.describe <%= camelized_app_name %>::Views::Parts::<%= camelized_name %> do
<%- if ruby_omit_hash_values? -%>
subject { described_class.new(value:) }
<%- else -%>
subject { described_class.new(value: value) }
<%- end -%>
let(:value) { double("<%= underscored_name %>") }

it "works" do
expect(subject).to be_kind_of(described_class)
end
end
9 changes: 7 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

require "hanami/rspec"
require "fileutils"

TMP = File.join(Dir.pwd, "tmp")

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
Expand All @@ -11,6 +14,10 @@
mocks.verify_partial_doubles = true
end

config.after do
FileUtils.rm_rf(TMP) if File.directory?(TMP)
end

config.shared_context_metadata_behavior = :apply_to_host_groups

config.filter_run_when_matching :focus
Expand All @@ -28,5 +35,3 @@

Kernel.srand config.seed
end

TMP = File.join(Dir.pwd, "tmp")
Loading

0 comments on commit b8cfe6e

Please sign in to comment.