<commit_before>require File.dirname(__FILE__) + '/../../spec_helper'

module Spec
  module DSL
    class FakeReporter < Spec::Runner::Reporter
      attr_reader :added_behaviour
      def add_behaviour(description)
        @added_behaviour = description
      end
    end

    describe Behaviour, "class methods" do
      before :each do
        @original_before_all_parts = Behaviour.before_all_parts.dup
        @original_after_all_parts = Behaviour.after_all_parts.dup
        @original_before_each_parts = Behaviour.before_each_parts.dup
        @original_after_each_parts = Behaviour.after_each_parts.dup

        @reporter = FakeReporter.new(mock("formatter", :null_object => true), mock("backtrace_tweaker", :null_object => true))
        @behaviour = Behaviour.new("example") {}
      end

      after :each do
        Behaviour.instance_variable_set(:@before_all_parts, @original_before_all_parts)
        Behaviour.instance_variable_set(:@after_all_parts, @original_after_all_parts)
        Behaviour.instance_variable_set(:@before_each_parts, @original_before_each_parts)
        Behaviour.instance_variable_set(:@after_each_parts, @original_after_each_parts)
      end

      it "should not run before(:all) or after(:all) on dry run" do
        before_all_ran = false
        after_all_ran = false
        Behaviour.before(:all) { before_all_ran = true }
        Behaviour.after(:all) { after_all_ran = true }
        @behaviour.it("should") {}
        @behaviour.run(@reporter, true)
        before_all_ran.should be_false
        after_all_ran.should be_false
      end

      it "should not run any example if before(:all) fails" do
        spec_ran = false
        Behaviour.before(:all) { raise "help" }
        @behaviour.specify("test") {spec_ran = true}
        @behaviour.run(@reporter)
        spec_ran.should be_false
      end

      it "should run after(:all) if before(:all) fails" do
        after_all_ran = false
        Behaviour.before(:all) { raise }
        Behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should run after(:all) if before(:each) fails" do
        after_all_ran = false
        Behaviour.before(:each) { raise }
        Behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should run after(:all) if any example fails" do
        after_all_ran = false
        @behaviour.it("should") { raise "before all error" }
        Behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should supply before(:all) as description if failure in before(:all)" do
        @reporter.should_receive(:example_finished) do |name, error, location|
          name.should eql("before(:all)")
          error.message.should eql("in before(:all)")
          location.should eql("before(:all)")
        end

        Behaviour.before(:all) { raise "in before(:all)" }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
      end

      it "should provide after(:all) as description if failure in after(:all)" do
        @reporter.should_receive(:example_finished) do |name, error, location|
          name.should eql("after(:all)")
          error.message.should eql("in after(:all)")
          location.should eql("after(:all)")
        end

        Behaviour.after(:all) { raise "in after(:all)" }
        @behaviour.run(@reporter)
      end
    end

    describe Behaviour do
      before :each do
        @original_before_all_parts = Behaviour.before_all_parts.dup
        @original_after_all_parts = Behaviour.after_all_parts.dup
        @original_before_each_parts = Behaviour.before_each_parts.dup
        @original_after_each_parts = Behaviour.after_each_parts.dup

        @reporter = FakeReporter.new(mock("formatter", :null_object => true), mock("backtrace_tweaker", :null_object => true))
        @behaviour = Behaviour.new("example") {}
      end

      after :each do
        Behaviour.instance_variable_set(:@before_all_parts, @original_before_all_parts)
        Behaviour.instance_variable_set(:@after_all_parts, @original_after_all_parts)
        Behaviour.instance_variable_set(:@before_each_parts, @original_before_each_parts)
        Behaviour.instance_variable_set(:@after_each_parts, @original_after_each_parts)
      end

      it "should send reporter add_behaviour" do
        @behaviour.run(@reporter)
        @reporter.added_behaviour.should == "example"
      end

      it "should run example on run" do
        example_ran = false
        @behaviour.it("should") {example_ran = true}
        @behaviour.run(@reporter)
        example_ran.should be_true
      end

      it "should not run example on dry run" do
        example_ran = false
        @behaviour.it("should") {example_ran = true}
        @behaviour.run(@reporter, true)
        example_ran.should be_false
      end

      it "should not run before(:all) or after(:all) on dry run" do
        before_all_ran = false
        after_all_ran = false
        @behaviour.before(:all) { before_all_ran = true }
        @behaviour.after(:all) { after_all_ran = true }
        @behaviour.it("should") {}
        @behaviour.run(@reporter, true)
        before_all_ran.should be_false
        after_all_ran.should be_false
      end

      it "should not run any example if before(:all) fails" do
        spec_ran = false
        @behaviour.before(:all) { raise "help" }
        @behaviour.specify("test") {spec_ran = true}
        @behaviour.run(@reporter)
        spec_ran.should be_false
      end

      it "should run after(:all) if before(:all) fails" do
        after_all_ran = false
        @behaviour.before(:all) { raise }
        @behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should run after(:all) if before(:each) fails" do
        after_all_ran = false
        @behaviour.before(:each) { raise }
        @behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should run after(:all) if any example fails" do
        after_all_ran = false
        @behaviour.it("should") { raise "before all error" }
        @behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should supply before(:all) as description if failure in before(:all)" do
        @reporter.should_receive(:example_finished) do |name, error, location|
          name.should eql("before(:all)")
          error.message.should eql("in before(:all)")
          location.should eql("before(:all)")
        end

        @behaviour.before(:all) { raise "in before(:all)" }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
      end

      it "should provide after(:all) as description if failure in after(:all)" do
        @reporter.should_receive(:example_finished) do |name, error, location|
          name.should eql("after(:all)")
          error.message.should eql("in after(:all)")
          location.should eql("after(:all)")
        end

        @behaviour.after(:all) { raise "in after(:all)" }
        @behaviour.run(@reporter)
      end

      it "should run superclass context_setup and context_setup block only once per context" do
        super_class_context_setup_run_count = 0
        super_class = Class.new do
          define_method :context_setup do
            super_class_context_setup_run_count += 1
          end
        end
        @behaviour.inherit(super_class)
    
        context_setup_run_count = 0
        @behaviour.before(:all) {context_setup_run_count += 1}
        @behaviour.specify("test") {true}
        @behaviour.specify("test2") {true}
        @behaviour.run(@reporter)
        super_class_context_setup_run_count.should == 1
        context_setup_run_count.should == 1
      end

      it "should run superclass setup method and setup block" do
        super_class_setup_ran = false
        super_class = Class.new do
          define_method :setup do
            super_class_setup_ran = true
          end
        end
        @behaviour.inherit super_class
    
        setup_ran = false
        @behaviour.setup {setup_ran = true}
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
        super_class_setup_ran.should be_true
        setup_ran.should be_true
      end
    
      it "should run superclass context_teardown method and after(:all) block only once" do
        super_class_context_teardown_run_count = 0
        super_class = Class.new do
          define_method :context_teardown do
            super_class_context_teardown_run_count += 1
          end
        end
        @behaviour.inherit super_class
    
        context_teardown_run_count = 0
        @behaviour.after(:all) {context_teardown_run_count += 1}
        @behaviour.specify("test") {true}
        @behaviour.specify("test2") {true}
        @behaviour.run(@reporter)
        super_class_context_teardown_run_count.should == 1
        context_teardown_run_count.should == 1
        @reporter.rspec_verify
      end

      it "after(:all) should have access to all instance variables defined in before(:all)" do
        context_instance_value_in = "Hello there"
        context_instance_value_out = ""
        @behaviour.before(:all) { @instance_var = context_instance_value_in }
        @behaviour.after(:all) { context_instance_value_out = @instance_var }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
        context_instance_value_in.should == context_instance_value_out
      end
    
      it "should copy instance variables from before(:all)'s execution context into spec's execution context" do
        context_instance_value_in = "Hello there"
        context_instance_value_out = ""
        @behaviour.before(:all) { @instance_var = context_instance_value_in }
        @behaviour.specify("test") {context_instance_value_out = @instance_var}
        @behaviour.run(@reporter)
        context_instance_value_in.should == context_instance_value_out
      end
    
      it "before callbacks are ordered from global to local" do
        fiddle = []
        super_class = Class.new do
          define_method :context_setup do
            fiddle << "superclass context_setup"
          end
          define_method :setup do
            fiddle << "superclass setup"
          end
        end
        @behaviour.inherit super_class

        Behaviour.before(:all) { fiddle << "Behaviour.before(:all)" }
        @behaviour.before(:all) { fiddle << "before(:all)" }
        @behaviour.setup { fiddle << "setup" }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
        fiddle.should == ['Behaviour.before(:all)', 'superclass context_setup', 'before(:all)', 'superclass setup', 'setup']
      end

      it "after callbacks are ordered from local to global" do
        @reporter.should_receive(:add_behaviour).with :any_args
        @reporter.should_receive(:example_finished).with :any_args

        fiddle = []
        super_class = Class.new do
          define_method :context_teardown do
            fiddle << "superclass context_teardown"
          end
          define_method :teardown do
            fiddle << "superclass teardown"
          end
        end
        @behaviour.inherit super_class

        @behaviour.after(:all) { fiddle << "after(:all)" }
        Behaviour.after(:all) { fiddle << "Behaviour.after(:all)" }
        @behaviour.teardown { fiddle << "teardown" }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
        fiddle.should == ['teardown', 'superclass teardown', 'after(:all)', 'superclass context_teardown', 'Behaviour.after(:all)']
      end<commit_msg>Updated a couple specs to use before(:each) and after(:each) syntax.<commit_after>require File.dirname(__FILE__) + '/../../spec_helper'

module Spec
  module DSL
    class FakeReporter < Spec::Runner::Reporter
      attr_reader :added_behaviour
      def add_behaviour(description)
        @added_behaviour = description
      end
    end

    describe Behaviour, "class methods" do
      before :each do
        @original_before_all_parts = Behaviour.before_all_parts.dup
        @original_after_all_parts = Behaviour.after_all_parts.dup
        @original_before_each_parts = Behaviour.before_each_parts.dup
        @original_after_each_parts = Behaviour.after_each_parts.dup

        @reporter = FakeReporter.new(mock("formatter", :null_object => true), mock("backtrace_tweaker", :null_object => true))
        @behaviour = Behaviour.new("example") {}
      end

      after :each do
        Behaviour.instance_variable_set(:@before_all_parts, @original_before_all_parts)
        Behaviour.instance_variable_set(:@after_all_parts, @original_after_all_parts)
        Behaviour.instance_variable_set(:@before_each_parts, @original_before_each_parts)
        Behaviour.instance_variable_set(:@after_each_parts, @original_after_each_parts)
      end

      it "should not run before(:all) or after(:all) on dry run" do
        before_all_ran = false
        after_all_ran = false
        Behaviour.before(:all) { before_all_ran = true }
        Behaviour.after(:all) { after_all_ran = true }
        @behaviour.it("should") {}
        @behaviour.run(@reporter, true)
        before_all_ran.should be_false
        after_all_ran.should be_false
      end

      it "should not run any example if before(:all) fails" do
        spec_ran = false
        Behaviour.before(:all) { raise "help" }
        @behaviour.specify("test") {spec_ran = true}
        @behaviour.run(@reporter)
        spec_ran.should be_false
      end

      it "should run after(:all) if before(:all) fails" do
        after_all_ran = false
        Behaviour.before(:all) { raise }
        Behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should run after(:all) if before(:each) fails" do
        after_all_ran = false
        Behaviour.before(:each) { raise }
        Behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should run after(:all) if any example fails" do
        after_all_ran = false
        @behaviour.it("should") { raise "before all error" }
        Behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should supply before(:all) as description if failure in before(:all)" do
        @reporter.should_receive(:example_finished) do |name, error, location|
          name.should eql("before(:all)")
          error.message.should eql("in before(:all)")
          location.should eql("before(:all)")
        end

        Behaviour.before(:all) { raise "in before(:all)" }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
      end

      it "should provide after(:all) as description if failure in after(:all)" do
        @reporter.should_receive(:example_finished) do |name, error, location|
          name.should eql("after(:all)")
          error.message.should eql("in after(:all)")
          location.should eql("after(:all)")
        end

        Behaviour.after(:all) { raise "in after(:all)" }
        @behaviour.run(@reporter)
      end
    end

    describe Behaviour do
      before :each do
        @original_before_all_parts = Behaviour.before_all_parts.dup
        @original_after_all_parts = Behaviour.after_all_parts.dup
        @original_before_each_parts = Behaviour.before_each_parts.dup
        @original_after_each_parts = Behaviour.after_each_parts.dup

        @reporter = FakeReporter.new(mock("formatter", :null_object => true), mock("backtrace_tweaker", :null_object => true))
        @behaviour = Behaviour.new("example") {}
      end

      after :each do
        Behaviour.instance_variable_set(:@before_all_parts, @original_before_all_parts)
        Behaviour.instance_variable_set(:@after_all_parts, @original_after_all_parts)
        Behaviour.instance_variable_set(:@before_each_parts, @original_before_each_parts)
        Behaviour.instance_variable_set(:@after_each_parts, @original_after_each_parts)
      end

      it "should send reporter add_behaviour" do
        @behaviour.run(@reporter)
        @reporter.added_behaviour.should == "example"
      end

      it "should run example on run" do
        example_ran = false
        @behaviour.it("should") {example_ran = true}
        @behaviour.run(@reporter)
        example_ran.should be_true
      end

      it "should not run example on dry run" do
        example_ran = false
        @behaviour.it("should") {example_ran = true}
        @behaviour.run(@reporter, true)
        example_ran.should be_false
      end

      it "should not run before(:all) or after(:all) on dry run" do
        before_all_ran = false
        after_all_ran = false
        @behaviour.before(:all) { before_all_ran = true }
        @behaviour.after(:all) { after_all_ran = true }
        @behaviour.it("should") {}
        @behaviour.run(@reporter, true)
        before_all_ran.should be_false
        after_all_ran.should be_false
      end

      it "should not run any example if before(:all) fails" do
        spec_ran = false
        @behaviour.before(:all) { raise "help" }
        @behaviour.specify("test") {spec_ran = true}
        @behaviour.run(@reporter)
        spec_ran.should be_false
      end

      it "should run after(:all) if before(:all) fails" do
        after_all_ran = false
        @behaviour.before(:all) { raise }
        @behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should run after(:all) if before(:each) fails" do
        after_all_ran = false
        @behaviour.before(:each) { raise }
        @behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should run after(:all) if any example fails" do
        after_all_ran = false
        @behaviour.it("should") { raise "before all error" }
        @behaviour.after(:all) { after_all_ran = true }
        @behaviour.run(@reporter)
        after_all_ran.should be_true
      end

      it "should supply before(:all) as description if failure in before(:all)" do
        @reporter.should_receive(:example_finished) do |name, error, location|
          name.should eql("before(:all)")
          error.message.should eql("in before(:all)")
          location.should eql("before(:all)")
        end

        @behaviour.before(:all) { raise "in before(:all)" }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
      end

      it "should provide after(:all) as description if failure in after(:all)" do
        @reporter.should_receive(:example_finished) do |name, error, location|
          name.should eql("after(:all)")
          error.message.should eql("in after(:all)")
          location.should eql("after(:all)")
        end

        @behaviour.after(:all) { raise "in after(:all)" }
        @behaviour.run(@reporter)
      end

      it "should run superclass context_setup and context_setup block only once per context" do
        super_class_context_setup_run_count = 0
        super_class = Class.new do
          define_method :context_setup do
            super_class_context_setup_run_count += 1
          end
        end
        @behaviour.inherit(super_class)
    
        context_setup_run_count = 0
        @behaviour.before(:all) {context_setup_run_count += 1}
        @behaviour.specify("test") {true}
        @behaviour.specify("test2") {true}
        @behaviour.run(@reporter)
        super_class_context_setup_run_count.should == 1
        context_setup_run_count.should == 1
      end

      it "should run superclass setup method and setup block" do
        super_class_setup_ran = false
        super_class = Class.new do
          define_method :setup do
            super_class_setup_ran = true
          end
        end
        @behaviour.inherit super_class
    
        setup_ran = false
        @behaviour.setup {setup_ran = true}
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
        super_class_setup_ran.should be_true
        setup_ran.should be_true
      end
    
      it "should run superclass context_teardown method and after(:all) block only once" do
        super_class_context_teardown_run_count = 0
        super_class = Class.new do
          define_method :context_teardown do
            super_class_context_teardown_run_count += 1
          end
        end
        @behaviour.inherit super_class
    
        context_teardown_run_count = 0
        @behaviour.after(:all) {context_teardown_run_count += 1}
        @behaviour.specify("test") {true}
        @behaviour.specify("test2") {true}
        @behaviour.run(@reporter)
        super_class_context_teardown_run_count.should == 1
        context_teardown_run_count.should == 1
        @reporter.rspec_verify
      end

      it "after(:all) should have access to all instance variables defined in before(:all)" do
        context_instance_value_in = "Hello there"
        context_instance_value_out = ""
        @behaviour.before(:all) { @instance_var = context_instance_value_in }
        @behaviour.after(:all) { context_instance_value_out = @instance_var }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
        context_instance_value_in.should == context_instance_value_out
      end
    
      it "should copy instance variables from before(:all)'s execution context into spec's execution context" do
        context_instance_value_in = "Hello there"
        context_instance_value_out = ""
        @behaviour.before(:all) { @instance_var = context_instance_value_in }
        @behaviour.specify("test") {context_instance_value_out = @instance_var}
        @behaviour.run(@reporter)
        context_instance_value_in.should == context_instance_value_out
      end
    
      it "before callbacks are ordered from global to local" do
        fiddle = []
        super_class = Class.new do
          define_method :context_setup do
            fiddle << "superclass context_setup"
          end
          define_method :setup do
            fiddle << "superclass setup"
          end
        end
        @behaviour.inherit super_class

        Behaviour.before(:all) { fiddle << "Behaviour.before(:all)" }
        @behaviour.before(:all) { fiddle << "before(:all)" }
        @behaviour.before(:each) { fiddle << "before(:each)" }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
        fiddle.should == ['Behaviour.before(:all)', 'superclass context_setup', 'before(:all)', 'superclass setup', 'before(:each)']
      end

      it "after callbacks are ordered from local to global" do
        @reporter.should_receive(:add_behaviour).with :any_args
        @reporter.should_receive(:example_finished).with :any_args

        fiddle = []
        super_class = Class.new do
          define_method :context_teardown do
            fiddle << "superclass context_teardown"
          end
          define_method :teardown do
            fiddle << "superclass teardown"
          end
        end
        @behaviour.inherit super_class

        @behaviour.after(:all) { fiddle << "after(:all)" }
        Behaviour.after(:all) { fiddle << "Behaviour.after(:all)" }
        @behaviour.teardown { fiddle << "after(:each)" }
        @behaviour.specify("test") {true}
        @behaviour.run(@reporter)
        fiddle.should == ['after(:each)', 'superclass teardown', 'after(:all)', 'superclass context_teardown', 'Behaviour.after(:all)']
      end
{'fraction_non_alphanumeric': 0.10254276083066295, 'fraction_numerical': 0.001165258645802988, 'mean_word_length': 2.6592051164915484, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 11, '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': '915125', 'n_tokens_mistral': 7453, 'n_tokens_neox': 7087, 'n_words': 1550}