Sha256: 6d2f35459fb87b3be53ca789e2bb11600f5265014c46dd85976ce35a22ff958c

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

describe Nanoc::Helpers::Filtering, helper: true do
  describe '#filter' do
    before do
      ctx.create_item('some content', { title: 'Hello!' }, '/about.md')
      ctx.create_rep(ctx.items['/about.md'], '/about.html')

      ctx.item = ctx.items['/about.md']
      ctx.item_rep = ctx.item.reps[:default]
    end

    let(:content) do
      "A<% filter :erb do %><%%= 'X' %><% end %>B"
    end

    subject { ::ERB.new(content).result(helper.get_binding) }

    context 'basic case' do
      it { is_expected.to eql('AXB') }

      it 'notifies' do
        ns = Set.new
        Nanoc::Int::NotificationCenter.on(:filtering_started) { ns << :filtering_started }
        Nanoc::Int::NotificationCenter.on(:filtering_ended)   { ns << :filtering_ended   }

        subject

        expect(ns).to include(:filtering_started)
        expect(ns).to include(:filtering_ended)
      end
    end

    context 'with assigns' do
      let(:content) do
        'A<% filter :erb do %><%%= @item[:title] %><% end %>B'
      end

      it { is_expected.to eql('AHello!B') }
    end

    context 'unknonwn filter name' do
      let(:content) do
        'A<% filter :donkey do %>X<% end %>B'
      end

      it 'errors' do
        expect { subject }.to raise_error(Nanoc::Int::Errors::UnknownFilter)
      end
    end

    context 'with locals' do
      let(:content) do
        "A<% filter :erb, locals: { sheep: 'baah' } do %><%%= @sheep %><% end %>B"
      end

      it { is_expected.to eql('AbaahB') }
    end

    context 'with Haml' do
      let(:content) do
        "%p Foo.\n" \
        "- filter(:erb) do\n" \
        "  <%= 'abc' + 'xyz' %>\n" \
        "%p Bar.\n"
      end

      before do
        require 'haml'
      end

      subject { ::Haml::Engine.new(content).render(helper.get_binding) }

      it { is_expected.to match(%r{^<p>Foo.</p>\s*abcxyz\s*<p>Bar.</p>$}) }
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
nanoc-4.8.12 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.11 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.10 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.9 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.8 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.7 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.6 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.5 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.4 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.3 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.2 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.1 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.8.0 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.7.14 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.7.13 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.7.12 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.7.11 spec/nanoc/helpers/filtering_spec.rb
nanoc-4.7.10 spec/nanoc/helpers/filtering_spec.rb