spec/unit/yaks/runner_spec.rb in yaks-0.7.7 vs spec/unit/yaks/runner_spec.rb in yaks-0.8.0.alpha

- old
+ new

@@ -1,33 +1,40 @@ -require 'spec_helper' - RSpec.describe Yaks::Runner do subject(:runner) { described_class.new(object: object, config: config, options: options) } let(:object) { Object.new } - let(:config) { Yaks::Config.new } + let(:config) { Yaks.new } let(:options) { {} } - describe '#call' do + shared_examples 'high-level runner test' do + let(:options) { {env: {foo: "from_env"}} } let(:runner) { Class.new(described_class) do def steps [ [:step1, proc { |x| x + 35 }], - [:step2, proc { |x| "#{x} #{x}" }] ] + [:step2, proc { |x, env| "#{env[:foo]}[#{x} #{x}]" }] ] end end.new(object: object, config: config, options: options) } let(:object) { 7 } it 'should go through all the steps' do - expect(runner.call).to eql "42 42" + expect(runner.call).to eql "from_env[42 42]" end end + describe '#call' do + include_examples 'high-level runner test' + end + + describe '#process' do + include_examples 'high-level runner test' + end + describe '#context' do it 'should contain the policy, env, and an empty mapper_stack' do expect(runner.context).to eql(policy: config.policy, env: {}, mapper_stack: []) end @@ -40,11 +47,11 @@ end end describe '#format_class' do let(:config) do - Yaks::Config.new do + Yaks.new do default_format :collection_json end end let(:rack_env) { @@ -87,11 +94,11 @@ expect(runner.format_name).to eql :hal end end context 'with a default format specified' do - let(:config) { Yaks::Config.new { default_format :collection_json } } + let(:config) { Yaks.new { default_format :collection_json } } context 'with a format in the options' do let(:options) { { format: :json_api } } it 'should give preference to that one' do expect(runner.format_name).to eql :json_api @@ -106,11 +113,11 @@ end end describe '#formatter' do let(:config) { - Yaks::Config.new do + Yaks.new do default_format :json_api format_options :json_api, {format_option: [:foo]} end } @@ -140,73 +147,10 @@ expect(runner.env).to eql({}) end end end - describe '#insert_hooks' do - let(:options) { { mapper: Yaks::Mapper } } - let(:config) { Yaks::Config.new(&hooks) } - - describe 'before' do - let(:hooks) { proc { before(:map) { :before_map_impl } } } - - it 'should insert a hook before the step' do - expect(runner.steps.map(&:first)).to eql [ - :before_map, :map, :format, :primitivize, :serialize - ] - expect(runner.steps.assoc(:before_map).last.call).to be :before_map_impl - end - end - - describe 'after' do - let(:hooks) { proc { after(:format) { :after_format_impl } } } - - it 'should insert a hook after the step' do - expect(runner.steps.map(&:first)).to eql [ - :map, :format, :after_format, :primitivize, :serialize - ] - expect(runner.steps.assoc(:after_format).last.call).to be :after_format_impl - end - end - - describe 'around' do - let(:hooks) { proc { around(:format) { :around_format_impl } } } - - it 'should insert a hook around the step' do - expect(runner.steps.map(&:first)).to eql [ - :map, :format, :primitivize, :serialize - ] - expect(runner.steps.assoc(:format).last.call(nil, nil)).to be :around_format_impl - end - end - - describe 'around' do - let(:hooks) { proc { skip(:serialize) } } - - it 'should insert a hook before the step' do - expect(runner.steps.map(&:first)).to eql [ - :map, :format, :primitivize - ] - end - end - - describe 'multiple hooks' do - let(:hooks) { - proc { - after(:format) { :after_format_impl } - skip(:serialize) - } - } - - it 'should insert the hooks' do - expect(runner.steps.map(&:first)).to eql [ - :map, :format, :after_format, :primitivize - ] - end - end - end - describe '#mapper' do context 'with an explicit mapper in the options' do let(:mapper_class) { Class.new(Yaks::Mapper) } let(:options) { { mapper: mapper_class } } @@ -229,12 +173,12 @@ end describe '#serializer' do context 'with a serializer configured' do let(:config) { - Yaks::Config.new do - json_serializer do |input| + Yaks.new do + serializer(:json) do |input| "serialized #{input}" end end } @@ -257,28 +201,16 @@ [ :format, runner.formatter ], [ :primitivize, runner.primitivizer], [ :serialize, runner.serializer ] ] end - - context 'with hooks' do - let(:config) { - Yaks::Config.new do - after(:format, :my_hook) { :foo } - end - } - - it 'should insert hooks' do - expect(runner.steps.map(&:first)).to eql [:map, :format, :my_hook, :primitivize, :serialize] - end - end end describe '#primitivizer' do describe 'with a non json based format' do let(:config) do - Yaks::Config.new do + Yaks.new do default_format :html end end it 'should return the identity function' do @@ -292,24 +224,52 @@ end end end describe '#hooks' do - before do - Yaks::Config::DSL.new(config) do - after(:map, :this_happens_after_map) - end - end + let(:config) { + super().after(:map, :this_happens_after_map) + } it 'should contain the hooks from the config' do expect(runner.hooks).to eql [[:after, :map, :this_happens_after_map, nil]] end context 'with extra blocks in the options' do let(:options) { { hooks: [[:foo]] } } it 'should combine the hooks' do expect(runner.hooks).to eql [[:after, :map, :this_happens_after_map, nil], [:foo]] + end + end + end + + describe '#map' do + let(:mapper_class) do + Struct.new(:options) do + include Yaks::FP::Callable + def call(obj, env) "mapped[#{obj}]" end + end + end + + let(:options) { { mapper: mapper_class } } + let(:object) { "foo" } + + it 'should only run the mapper' do + expect(runner.map).to eql "mapped[foo]" + end + + context 'with a hook on the :map step' do + let(:config) do + Yaks.new do + around(:map) do |res, env, &block| + "around[#{block.call(res, env)}]" + end + end + end + + it 'should invoke the hook as well' do + expect(runner.map).to eql "around[mapped[foo]]" end end end end