diff --git a/lib/kangaru/controller.rb b/lib/kangaru/controller.rb index 1d20065..a626fec 100644 --- a/lib/kangaru/controller.rb +++ b/lib/kangaru/controller.rb @@ -1,13 +1,16 @@ module Kangaru class Controller - attr_reader :command + attr_reader :command, :renderer def initialize(command) @command = command + @renderer = Renderer.new(command) end def execute public_send(command.action) + + renderer.render(binding) end end end diff --git a/sig/kangaru/controller.rbs b/sig/kangaru/controller.rbs index 8f8ff6d..554a69e 100644 --- a/sig/kangaru/controller.rbs +++ b/sig/kangaru/controller.rbs @@ -1,6 +1,7 @@ module Kangaru class Controller attr_reader command: Command + attr_reader renderer: Renderer def initialize: (Command) -> void diff --git a/spec/features/command_spec.rb b/spec/features/command_spec.rb index 668cb41..4639690 100644 --- a/spec/features/command_spec.rb +++ b/spec/features/command_spec.rb @@ -1,9 +1,11 @@ RSpec.describe "Running a command", :with_gem do + subject(:run_command!) { SomeGem.run!(argv) } + + let(:argv) { [] } + include_context :kangaru_initialised describe "calling SomeGem.run! directly" do - subject(:run_command!) { SomeGem.run!(argv) } - let(:target) do Module.new { def self.do_something = nil } end @@ -266,4 +268,49 @@ def some_action end end end + + describe "rendering view files" do + before do + gem.gem_file("default_controller").write(<<~RUBY) + module SomeGem + class DefaultController < Kangaru::Controller + def default + @name = "Some Name" + @age = 30 + end + end + end + RUBY + end + + context "when view file does not exist" do + before { gem.load! } + + it "does not output to stdout" do + expect { run_command! }.not_to output.to_stdout + end + end + + context "when view file exists" do + before do + gem.gem_dir("views").mkdir + gem.gem_dir("views/default").mkdir + gem.view_file(controller: "default", action: "default").write(view_file) + + gem.load! + end + + let(:view_file) do + <<~ERB + Hello <%= @name %>, you are <%= @age %> years old. + ERB + end + + it "outputs the interpolated view file" do + expect { run_command! }.to output(<<~TEXT).to_stdout + Hello Some Name, you are 30 years old. + TEXT + end + end + end end diff --git a/spec/kangaru/controller_spec.rb b/spec/kangaru/controller_spec.rb index f2a007b..dfed56b 100644 --- a/spec/kangaru/controller_spec.rb +++ b/spec/kangaru/controller_spec.rb @@ -7,10 +7,25 @@ let(:action) { :some_action } + let(:renderer) { instance_spy(Kangaru::Renderer) } + + before do + allow(Kangaru::Renderer).to receive(:new).and_return(renderer) + end + describe "#initialize" do it "sets the command" do expect(controller.command).to eq(command) end + + it "instantiates a renderer" do + controller + expect(Kangaru::Renderer).to have_received(:new).with(command).once + end + + it "sets the renderer" do + expect(controller.renderer).to eq(renderer) + end end describe "#execute" do @@ -30,6 +45,11 @@ execute expect(controller).to have_received(action).once end + + it "renders the command output" do + execute + expect(renderer).to have_received(:render).with(a_kind_of(Binding)).once + end end end