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

describe "The return keyword" do
  it "assigns to block variables" do
    def r; return nil; end;      a = r(); a.should == nil
    def r; return 1; end;        a = r(); a.should == 1
    def r; return []; end;       a = r(); a.should == []
    def r; return [1]; end;      a = r(); a.should == [1]
    def r; return [nil]; end;    a = r(); a.should == [nil]
    def r; return [[]]; end;     a = r(); a.should == [[]]
    def r; return [*[]]; end;    a = r(); a.should == []
    def r; return [*[1]]; end;   a = r(); a.should == [1]
    def r; return [*[1,2]]; end; a = r(); a.should == [1,2]
    def r; return 1, 2, 3; end;  a =  r(); a.should == [1, 2, 3]
  end

  it "assigns splatted objects to block variables" do
    def r; return *nil; end;     a = r(); a.should == nil
    def r; return *1; end;       a = r(); a.should == 1
    def r; return *[]; end;      a = r(); a.should == nil
    def r; return *[1]; end;     a = r(); a.should == 1
    def r; return *[nil]; end;   a = r(); a.should == nil
    def r; return *[[]]; end;    a = r(); a.should == []
    def r; return *[*[1]]; end;  a = r(); a.should == 1
    def r; return *[*[1,2]]; end; a = r(); a.should == [1,2]
  end

  it "assigns to a block variable with a splat on the RHS" do
    def r; return; end;          a = *r(); a.should == nil
    def r; return nil; end;      a = *r(); a.should == nil
    def r; return 1; end;        a = *r(); a.should == 1
    def r; return []; end;       a = *r(); a.should == nil
    def r; return [1]; end;      a = *r(); a.should == 1
    def r; return [nil]; end;    a = *r(); a.should == nil
    def r; return [[]]; end;     a = *r(); a.should == []
    def r; return [1,2]; end;    a = *r(); a.should == [1,2]
    def r; return [*[]]; end;    a = *r(); a.should == nil
    def r; return [*[1]]; end;   a = *r(); a.should == 1
    def r; return [*[1,2]]; end; a = *r(); a.should == [1,2]
    def r; return 1, 2, 3; end;  a = *r(); a.should == [1, 2, 3]
  end

  it "assigns to a block variable with a splat on the LHS" do
    def r; return *nil; end;      *a = r(); a.should == [nil]
    def r; return *1; end;        *a = r(); a.should == [1]
    def r; return *[]; end;       *a = r(); a.should == [nil]
    def r; return *[1]; end;      *a = r(); a.should == [1]
    def r; return *[nil]; end;    *a = r(); a.should == [nil]
    def r; return *[[]]; end;     *a = r(); a.should == [[]]
    def r; return *[*[]]; end;    *a = r(); a.should == [nil]
    def r; return *[*[1]]; end;   *a = r(); a.should == [1]
    def r; return *[*[1,2]]; end; *a = r(); a.should == [[1,2]]
    def r; return 1, 2, 3; end;   *a = r(); a.should == [[1, 2, 3]]
  end

  it "assigns to a block variable with splats on the LHS and RHS" do
    def r; return *nil; end;      *a = *r(); a.should == [nil]
    def r; return *1; end;        *a = *r(); a.should == [1]
    def r; return *[]; end;       *a = *r(); a.should == [nil]
    def r; return *[1]; end;      *a = *r(); a.should == [1]
    def r; return *[nil]; end;    *a = *r(); a.should == [nil]
    def r; return *[[]]; end;     *a = *r(); a.should == []
    def r; return *[*[]]; end;    *a = *r(); a.should == [nil]
    def r; return *[*[1]]; end;   *a = *r(); a.should == [1]
    def r; return *[*[1,2]]; end; *a = *r(); a.should == [1,2]
    def r; return 1, 2, 3; end;   *a = *r(); a.should == [1, 2, 3]
  end

  it "assigns to multiple block variables" do
    def r; return; end;          a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return nil; end;      a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return 1; end;        a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return []; end;       a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return [1]; end;      a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return [nil]; end;    a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return [[]]; end;     a,b,*c = r(); [a,b,c].should == [[],nil,[]]
    def r; return [*[]]; end;    a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return [*[1]]; end;   a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return [*[1,2]]; end; a,b,*c = r(); [a,b,c].should == [1,2,[]]
    def r; return 1, 2, 3; end;  a,*c = r(); [a, c].should == [1, [2,3]]
  end

  it "assigns to multiple block variables with a splatted value" do
    def r; return *nil; end;      a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *1; end;        a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return *[]; end;       a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *[1]; end;      a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return *[nil]; end;    a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *[[]]; end;     a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *[*[]]; end;    a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *[*[1]]; end;   a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return *[*[1,2]]; end; a,b,*c = r(); [a,b,c].should == [1,2,[]]
  end

  it "raises a ThreadError if used to exit a thread" do
    lambda { Thread.new { return }.join }.should raise_error(ThreadError)
  end
end

describe "Return from within a begin" do
  it "executes ensure before returning from function" do<commit_msg>Move pathological return specs into their own 'example'<commit_after>require File.dirname(__FILE__) + '/../spec_helper'

describe "The return keyword" do
  it "assigns to block variables" do
    def r; return nil; end;      a = r(); a.should == nil
    def r; return 1; end;        a = r(); a.should == 1
    def r; return []; end;       a = r(); a.should == []
    def r; return [1]; end;      a = r(); a.should == [1]
    def r; return [nil]; end;    a = r(); a.should == [nil]
    def r; return [[]]; end;     a = r(); a.should == [[]]
    def r; return [*[]]; end;    a = r(); a.should == []
    def r; return [*[1]]; end;   a = r(); a.should == [1]
    def r; return [*[1,2]]; end; a = r(); a.should == [1,2]
    def r; return 1, 2, 3; end;  a =  r(); a.should == [1, 2, 3]
  end

  it "assigns splatted objects to block variables" do
    def r; return *nil; end;     a = r(); a.should == nil
    def r; return *1; end;       a = r(); a.should == 1
    def r; return *[]; end;      a = r(); a.should == nil
    def r; return *[1]; end;     a = r(); a.should == 1
    def r; return *[nil]; end;   a = r(); a.should == nil
    def r; return *[[]]; end;    a = r(); a.should == []
  end

  it "assigns to a block variable with a splat on the RHS" do
    def r; return; end;          a = *r(); a.should == nil
    def r; return nil; end;      a = *r(); a.should == nil
    def r; return 1; end;        a = *r(); a.should == 1
    def r; return []; end;       a = *r(); a.should == nil
    def r; return [1]; end;      a = *r(); a.should == 1
    def r; return [nil]; end;    a = *r(); a.should == nil
    def r; return [[]]; end;     a = *r(); a.should == []
    def r; return [1,2]; end;    a = *r(); a.should == [1,2]
    def r; return [*[]]; end;    a = *r(); a.should == nil
    def r; return [*[1]]; end;   a = *r(); a.should == 1
    def r; return [*[1,2]]; end; a = *r(); a.should == [1,2]
    def r; return 1, 2, 3; end;  a = *r(); a.should == [1, 2, 3]
  end

  it "assigns to a block variable with a splat on the LHS" do
    def r; return *nil; end;      *a = r(); a.should == [nil]
    def r; return *1; end;        *a = r(); a.should == [1]
    def r; return *[]; end;       *a = r(); a.should == [nil]
    def r; return *[1]; end;      *a = r(); a.should == [1]
    def r; return *[nil]; end;    *a = r(); a.should == [nil]
    def r; return *[[]]; end;     *a = r(); a.should == [[]]
    def r; return 1, 2, 3; end;   *a = r(); a.should == [[1, 2, 3]]
  end

  it "assigns to a block variable with splats on the LHS and RHS" do
    def r; return *nil; end;      *a = *r(); a.should == [nil]
    def r; return *1; end;        *a = *r(); a.should == [1]
    def r; return *[]; end;       *a = *r(); a.should == [nil]
    def r; return *[1]; end;      *a = *r(); a.should == [1]
    def r; return *[nil]; end;    *a = *r(); a.should == [nil]
    def r; return *[[]]; end;     *a = *r(); a.should == []
    def r; return 1, 2, 3; end;   *a = *r(); a.should == [1, 2, 3]
  end

  it "assigns to multiple block variables" do
    def r; return; end;          a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return nil; end;      a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return 1; end;        a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return []; end;       a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return [1]; end;      a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return [nil]; end;    a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return [[]]; end;     a,b,*c = r(); [a,b,c].should == [[],nil,[]]
    def r; return [*[]]; end;    a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return [*[1]]; end;   a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return [*[1,2]]; end; a,b,*c = r(); [a,b,c].should == [1,2,[]]
    def r; return 1, 2, 3; end;  a,*c = r(); [a, c].should == [1, [2,3]]
  end

  it "assigns to multiple block variables with a splatted value" do
    def r; return *nil; end;      a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *1; end;        a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return *[]; end;       a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *[1]; end;      a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return *[nil]; end;    a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
  end

  it "unwraps single-element and empty splatted arrays" do
    def r; return *[*[]]; end;  a = r(); a.should == nil
    def r; return *[*[1]]; end;  a = r(); a.should == 1
    def r; return *[*[1,2]]; end; a = r(); a.should == [1,2]
    def r; return *[*[]]; end;    *a = r(); a.should == [nil]
    def r; return *[*[1]]; end;   *a = r(); a.should == [1]
    def r; return *[*[1,2]]; end; *a = r(); a.should == [[1,2]]
    def r; return *[*[]]; end;    *a = *r(); a.should == [nil]
    def r; return *[*[1]]; end;   *a = *r(); a.should == [1]
    def r; return *[*[1,2]]; end; *a = *r(); a.should == [1,2]
    def r; return *[[]]; end;     a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *[*[]]; end;    a,b,*c = r(); [a,b,c].should == [nil,nil,[]]
    def r; return *[*[1]]; end;   a,b,*c = r(); [a,b,c].should == [1,nil,[]]
    def r; return *[*[1,2]]; end; a,b,*c = r(); [a,b,c].should == [1,2,[]]
  end

  it "raises a ThreadError if used to exit a thread" do
    lambda { Thread.new { return }.join }.should raise_error(ThreadError)
  end
end

describe "Return from within a begin" do
  it "executes ensure before returning from function" do
{'fraction_non_alphanumeric': 0.25269461077844313, 'fraction_numerical': 0.019161676646706587, 'mean_word_length': 2.3925, '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': '2944297', 'n_tokens_mistral': 4551, 'n_tokens_neox': 4372, 'n_words': 1507}