Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 144 lines (137 sloc) 4.378 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
require 'spec_helper'
require 'dep_definer_support'

describe "declaration" do
  before {
    @meta = meta 'test'
  }
  it "should work" do
    L{ meta 'count_test' }.should change(Base.sources.anonymous.templates, :count).by(1)
  end
  it "should set the name" do
    @meta.name.should == 'test'
  end
  it "should downcase the name" do
    meta("Case_Test").name.should == 'case_test'
  end
  it "should set the source" do
    @meta.source.should == Base.sources.anonymous
  end
  it "should define a dep context" do
    @meta.context_class.should be_an_instance_of(Class)
    @meta.context_class.ancestors.should include(Babushka::DepContext)
  end
  it "should define template on the context" do
    @meta.context_class.source_template.should == @meta
  end
  it "should not define a dep helper" do
    Object.new.should_not respond_to('test')
  end
  it "should not be marked as suffixed" do
    @meta.opts[:suffix].should be_false
  end
  after { Base.sources.anonymous.templates.clear! }
end

describe "using" do
  describe "invalid templates" do
    it "should not define deps as options" do
      L{
        dep('something undefined', :template => 'undefined').should be_nil
      }.should raise_error(TemplateNotFound, "There is no template named 'undefined' to define 'something undefined' against.")
    end
    it "should define deps as options" do
      dep('something.undefined').should be_an_instance_of(Dep)
    end
  end

  describe "without template" do
    before {
      @meta = meta('templateless_test') {}
    }
    it "should define deps based on the template" do
      dep('templateless dep.templateless_test').template.should == @meta
    end
    after { Base.sources.anonymous.templates.clear! }
  end

  describe "with template" do
    before {
      @meta = meta 'template_test' do
        def a_helper_method
          'hello from the helper!'
        end
        template {
          def a_helper
            'hello from the helper in the template!'
          end
          met? {
            'this dep is met.'
          }
        }
      end
    }
    it "should define the helper on the context class" do
      @meta.context_class.respond_to?(:a_helper).should be_false
      @meta.context_class.new(nil).respond_to?(:a_helper).should be_false
      dep('dep1.template_test').context.define!.respond_to?(:a_helper).should be_true
    end
    it "should correctly define the helper method" do
      dep('dep2.template_test').context.a_helper_method.should == 'hello from the helper!'
    end
    it "should correctly define the helper" do
      dep('dep2.template_test').context.define!.a_helper.should == 'hello from the helper in the template!'
    end
    it "should correctly define the met? block" do
      dep('dep3.template_test').send(:process_task, :met?).should == 'this dep is met.'
    end
    it "should override the template correctly" do
      dep('dep4.template_test') {
        met? { 'overridden met? block.' }
      }.send(:process_task, :met?).should == 'overridden met? block.'
    end
    after { Base.sources.anonymous.templates.clear! }
  end

  describe "acceptors" do
    before {
      @meta = meta 'acceptor_test' do
        accepts_list_for :list_test
        accepts_block_for :block_test
        template {
          met? {
            list_test == ['valid']
          }
          meet {
            block_test.call
          }
        }
      end
    }
    it "should handle accepts_list_for" do
      dep('unmet accepts_list_for.acceptor_test') { list_test 'invalid' }.met?.should be_false
      dep('met accepts_list_for.acceptor_test') { list_test 'valid' }.met?.should be_true
    end
    it "should handle accepts_block_for" do
      block_called = false
      dep('accepts_block_for.acceptor_test') {
        list_test 'invalid'
        block_test {
          block_called = true
        }
      }.meet
      block_called.should be_true
    end
    after { Base.sources.anonymous.templates.clear! }
  end

  describe "calling accepted blocks" do
    let(:a_meta) {
      meta :acceptor_calling_test do
        accepts_block_for(:testing) {
          self
        }
      end
    }
    let(:a_dep) {
      dep 'acceptor calling test', :template => a_meta.name
    }

    it "should run the default block in the dep's context" do
      a_dep.context.invoke(:testing).should == a_dep.context
    end
  end
end
Something went wrong with that request. Please try again.