<commit_before>require "spec_helper"

describe Docile do

  context :dsl_eval do

    it "should return the DSL object" do
      Docile.dsl_eval([]) do
        push 1
        push 2
        pop
        push 3
      end.should == [1, 3]
    end

    Pizza = Struct.new(:cheese, :pepperoni, :bacon, :sauce)

    class PizzaBuilder
      def cheese(v=true);    @cheese    = v; end
      def pepperoni(v=true); @pepperoni = v; end
      def bacon(v=true);     @bacon     = v; end
      def sauce(v=nil);      @sauce     = v; end
      def build
        Pizza.new(!!@cheese, !!@pepperoni, !!@bacon, @sauce)
      end
    end

    it "should handle a Builder pattern" do
      @sauce = :extra
      Docile.dsl_eval(PizzaBuilder.new) do
        bacon
        cheese
        sauce @sauce
      end.build.should == Pizza.new(true, false, true, :extra)
    end

    class InnerDSL
      def initialize; @b = 'b'; end
      attr_accessor :b
    end

    class OuterDSL
      def initialize; @a = 'a'; end
      attr_accessor :a
      def inner(&block)
        Docile.dsl_eval(InnerDSL.new, &block)
      end
    end

    def outer(&block)
      Docile.dsl_eval(OuterDSL.new, &block)
    end

    context "methods" do
      it "should find method of outer dsl in outer dsl scope" do
        outer { a.should == 'a' }
      end

      it "should find method of inner dsl in inner dsl scope" do
        outer { inner { b.should == 'b' } }
      end

      it "should find method of outer dsl in inner dsl scope" do
        outer { inner { a.should == 'a' } }
      end

      it "should find method of block's context in outer dsl scope" do
        def c; 'c'; end
        outer { c.should == 'c' }
      end<commit_msg>Added tests for parameterized blocks.<commit_after>require "spec_helper"

describe Docile do

  context :dsl_eval do

    it "should return the DSL object" do
      Docile.dsl_eval([]) do
        push 1
        push 2
        pop
        push 3
      end.should == [1, 3]
    end

    Pizza = Struct.new(:cheese, :pepperoni, :bacon, :sauce)

    class PizzaBuilder
      def cheese(v=true);    @cheese    = v; end
      def pepperoni(v=true); @pepperoni = v; end
      def bacon(v=true);     @bacon     = v; end
      def sauce(v=nil);      @sauce     = v; end
      def build
        Pizza.new(!!@cheese, !!@pepperoni, !!@bacon, @sauce)
      end
    end

    it "should handle a Builder pattern" do
      @sauce = :extra
      Docile.dsl_eval(PizzaBuilder.new) do
        bacon
        cheese
        sauce @sauce
      end.build.should == Pizza.new(true, false, true, :extra)
    end

    class InnerDSL
      def initialize; @b = 'b'; end
      attr_accessor :b
    end

    class OuterDSL
      def initialize; @a = 'a'; end
      attr_accessor :a
      def inner(&block)
        Docile.dsl_eval(InnerDSL.new, &block)
      end

      def inner_with_params(param,&block)
        Docile.dsl_eval(InnerDSL.new, param, :foo, &block)
      end
    end

    def outer(&block)
      Docile.dsl_eval(OuterDSL.new, &block)
    end

    def parameterized(*args,&block)
      Docile.dsl_eval(OuterDSL.new, *args, &block)
    end

    context "parameters" do
      it "should pass parameters to the block" do
        parameterized(1,2,3) do |x,y,z|
          x.should == 1
          y.should == 2
          z.should == 3
        end
      end

      it "should find parameters before methods" do
        parameterized(1) { |a| a.should == 1 }
      end

      it "should find outer parameters in inner dsl scope" do
        parameterized(1,2,3) do |a,b,c|
          inner_with_params(c) do |d,e|
            a.should == 1
            b.should == 2
            c.should == 3
            d.should == c
            e.should == :foo
          end
        end
      end
    end

    context "methods" do
      it "should find method of outer dsl in outer dsl scope" do
        outer { a.should == 'a' }
      end

      it "should find method of inner dsl in inner dsl scope" do
        outer { inner { b.should == 'b' } }
      end

      it "should find method of outer dsl in inner dsl scope" do
        outer { inner { a.should == 'a' } }
      end

      it "should find method of block's context in outer dsl scope" do
        def c; 'c'; end
        outer { c.should == 'c' }
      end
{'fraction_non_alphanumeric': 0.10216934919524143, 'fraction_numerical': 0.005598320503848845, 'mean_word_length': 1.8173455978975033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2087478', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1421, 'n_words': 454}