<commit_before>require 'spec_helper'
require 'mspec/guards'

describe Object, "#platform_is" do
  before :each do
    @guard = PlatformGuard.new :dummy
    PlatformGuard.stub(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "does not yield when #os? returns false" do
    PlatformGuard.stub(:os?).and_return(false)
    platform_is(:ruby) { ScratchPad.record :yield }
    ScratchPad.recorded.should_not == :yield
  end

  it "yields when #os? returns true" do
    PlatformGuard.stub(:os?).and_return(true)
    platform_is(:solarce) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "sets the name of the guard to :platform_is" do
    platform_is(:solarce) { }
    @guard.name.should == :platform_is
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    @guard.should_receive(:match?).and_return(true)
    @guard.should_receive(:unregister)
    lambda do
      platform_is(:solarce) { raise Exception }
    end.should raise_error(Exception)
  end
end

describe Object, "#platform_is_not" do
  before :each do
    @guard = PlatformGuard.new :dummy
    PlatformGuard.stub(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "does not yield when #os? returns true" do
    PlatformGuard.stub(:os?).and_return(true)
    platform_is_not(:ruby) { ScratchPad.record :yield }
    ScratchPad.recorded.should_not == :yield
  end

  it "yields when #os? returns false" do
    PlatformGuard.stub(:os?).and_return(false)
    platform_is_not(:solarce) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "sets the name of the guard to :platform_is_not" do
    platform_is_not(:solarce) { }
    @guard.name.should == :platform_is_not
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    @guard.should_receive(:match?).and_return(false)
    @guard.should_receive(:unregister)
    lambda do
      platform_is_not(:solarce) { raise Exception }
    end.should raise_error(Exception)
  end
end

describe Object, "#platform_is :wordsize => SIZE_SPEC" do
  before :each do
    @guard = PlatformGuard.new :darwin, :wordsize => 32
    PlatformGuard.stub(:os?).and_return(true)
    PlatformGuard.stub(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "yields when #wordsize? returns true" do
    PlatformGuard.stub(:wordsize?).and_return(true)
    platform_is(:wordsize => 32) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "doesn not yield when #wordsize? returns false" do
    PlatformGuard.stub(:wordsize?).and_return(false)
    platform_is(:wordsize => 32) { ScratchPad.record :yield }
    ScratchPad.recorded.should_not == :yield
  end
end

describe Object, "#platform_is_not :wordsize => SIZE_SPEC" do
  before :each do
    @guard = PlatformGuard.new :darwin, :wordsize => 32
    PlatformGuard.stub(:os?).and_return(true)
    PlatformGuard.stub(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "yields when #wordsize? returns false" do
    PlatformGuard.stub(:wordsize?).and_return(false)
    platform_is_not(:wordsize => 32) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "doesn not yield when #wordsize? returns true" do
    PlatformGuard.stub(:wordsize?).and_return(true)
    platform_is_not(:wordsize => 32) { ScratchPad.record :yield }
    ScratchPad.recorded.should_not == :yield
  end
end

describe PlatformGuard, ".implementation?" do
  before :all do
    @verbose = $VERBOSE
    $VERBOSE = nil
  end

  after :all do
    $VERBOSE = @verbose
  end

  before :each do
    @ruby_engine = Object.const_get :RUBY_ENGINE
  end

  after :each do
    Object.const_set :RUBY_ENGINE, @ruby_engine
  end

  it "returns true if passed :ruby and RUBY_ENGINE == 'ruby'" do
    Object.const_set :RUBY_ENGINE, 'ruby'
    PlatformGuard.implementation?(:ruby).should == true
  end

  it "returns true if passed :rubinius and RUBY_ENGINE == 'rbx'" do
    Object.const_set :RUBY_ENGINE, 'rbx'
    PlatformGuard.implementation?(:rubinius).should == true
  end

  it "returns true if passed :jruby and RUBY_ENGINE == 'jruby'" do
    Object.const_set :RUBY_ENGINE, 'jruby'
    PlatformGuard.implementation?(:jruby).should == true
  end

  it "returns true if passed :ironruby and RUBY_ENGINE == 'ironruby'" do
    Object.const_set :RUBY_ENGINE, 'ironruby'
    PlatformGuard.implementation?(:ironruby).should == true
  end

  it "returns true if passed :maglev and RUBY_ENGINE == 'maglev'" do
    Object.const_set :RUBY_ENGINE, 'maglev'
    PlatformGuard.implementation?(:maglev).should == true
  end

  it "returns true if passed :topaz and RUBY_ENGINE == 'topaz'" do
    Object.const_set :RUBY_ENGINE, 'topaz'
    PlatformGuard.implementation?(:topaz).should == true
  end

  it "returns true if passed :ruby and RUBY_ENGINE matches /^ruby/" do
    Object.const_set :RUBY_ENGINE, 'ruby'
    PlatformGuard.implementation?(:ruby).should == true

    Object.const_set :RUBY_ENGINE, 'ruby1.8'
    PlatformGuard.implementation?(:ruby).should == true

    Object.const_set :RUBY_ENGINE, 'ruby1.9'
    PlatformGuard.implementation?(:ruby).should == true
  end

  it "raises an error when passed an unrecognized name" do
    Object.const_set :RUBY_ENGINE, 'ruby'
    lambda {
      PlatformGuard.implementation?(:python)
    }.should raise_error(/unknown implementation/)
  end
end

describe PlatformGuard, ".standard?" do
  it "returns true if implementation? returns true" do
    PlatformGuard.should_receive(:implementation?).with(:ruby).and_return(true)
    PlatformGuard.standard?.should be_true<commit_msg>Use stub_const in PlatformGuard specs<commit_after>require 'spec_helper'
require 'mspec/guards'

describe Object, "#platform_is" do
  before :each do
    @guard = PlatformGuard.new :dummy
    PlatformGuard.stub(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "does not yield when #os? returns false" do
    PlatformGuard.stub(:os?).and_return(false)
    platform_is(:ruby) { ScratchPad.record :yield }
    ScratchPad.recorded.should_not == :yield
  end

  it "yields when #os? returns true" do
    PlatformGuard.stub(:os?).and_return(true)
    platform_is(:solarce) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "sets the name of the guard to :platform_is" do
    platform_is(:solarce) { }
    @guard.name.should == :platform_is
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    @guard.should_receive(:match?).and_return(true)
    @guard.should_receive(:unregister)
    lambda do
      platform_is(:solarce) { raise Exception }
    end.should raise_error(Exception)
  end
end

describe Object, "#platform_is_not" do
  before :each do
    @guard = PlatformGuard.new :dummy
    PlatformGuard.stub(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "does not yield when #os? returns true" do
    PlatformGuard.stub(:os?).and_return(true)
    platform_is_not(:ruby) { ScratchPad.record :yield }
    ScratchPad.recorded.should_not == :yield
  end

  it "yields when #os? returns false" do
    PlatformGuard.stub(:os?).and_return(false)
    platform_is_not(:solarce) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "sets the name of the guard to :platform_is_not" do
    platform_is_not(:solarce) { }
    @guard.name.should == :platform_is_not
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    @guard.should_receive(:match?).and_return(false)
    @guard.should_receive(:unregister)
    lambda do
      platform_is_not(:solarce) { raise Exception }
    end.should raise_error(Exception)
  end
end

describe Object, "#platform_is :wordsize => SIZE_SPEC" do
  before :each do
    @guard = PlatformGuard.new :darwin, :wordsize => 32
    PlatformGuard.stub(:os?).and_return(true)
    PlatformGuard.stub(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "yields when #wordsize? returns true" do
    PlatformGuard.stub(:wordsize?).and_return(true)
    platform_is(:wordsize => 32) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "doesn not yield when #wordsize? returns false" do
    PlatformGuard.stub(:wordsize?).and_return(false)
    platform_is(:wordsize => 32) { ScratchPad.record :yield }
    ScratchPad.recorded.should_not == :yield
  end
end

describe Object, "#platform_is_not :wordsize => SIZE_SPEC" do
  before :each do
    @guard = PlatformGuard.new :darwin, :wordsize => 32
    PlatformGuard.stub(:os?).and_return(true)
    PlatformGuard.stub(:new).and_return(@guard)
    ScratchPad.clear
  end

  it "yields when #wordsize? returns false" do
    PlatformGuard.stub(:wordsize?).and_return(false)
    platform_is_not(:wordsize => 32) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "doesn not yield when #wordsize? returns true" do
    PlatformGuard.stub(:wordsize?).and_return(true)
    platform_is_not(:wordsize => 32) { ScratchPad.record :yield }
    ScratchPad.recorded.should_not == :yield
  end
end

describe PlatformGuard, ".implementation?" do
  it "returns true if passed :ruby and RUBY_ENGINE == 'ruby'" do
    stub_const 'RUBY_ENGINE', 'ruby'
    PlatformGuard.implementation?(:ruby).should == true
  end

  it "returns true if passed :rubinius and RUBY_ENGINE == 'rbx'" do
    stub_const 'RUBY_ENGINE', 'rbx'
    PlatformGuard.implementation?(:rubinius).should == true
  end

  it "returns true if passed :jruby and RUBY_ENGINE == 'jruby'" do
    stub_const 'RUBY_ENGINE', 'jruby'
    PlatformGuard.implementation?(:jruby).should == true
  end

  it "returns true if passed :ironruby and RUBY_ENGINE == 'ironruby'" do
    stub_const 'RUBY_ENGINE', 'ironruby'
    PlatformGuard.implementation?(:ironruby).should == true
  end

  it "returns true if passed :maglev and RUBY_ENGINE == 'maglev'" do
    stub_const 'RUBY_ENGINE', 'maglev'
    PlatformGuard.implementation?(:maglev).should == true
  end

  it "returns true if passed :topaz and RUBY_ENGINE == 'topaz'" do
    stub_const 'RUBY_ENGINE', 'topaz'
    PlatformGuard.implementation?(:topaz).should == true
  end

  it "returns true if passed :ruby and RUBY_ENGINE matches /^ruby/" do
    stub_const 'RUBY_ENGINE', 'ruby'
    PlatformGuard.implementation?(:ruby).should == true

    stub_const 'RUBY_ENGINE', 'ruby1.8'
    PlatformGuard.implementation?(:ruby).should == true

    stub_const 'RUBY_ENGINE', 'ruby1.9'
    PlatformGuard.implementation?(:ruby).should == true
  end

  it "raises an error when passed an unrecognized name" do
    stub_const 'RUBY_ENGINE', 'ruby'
    lambda {
      PlatformGuard.implementation?(:python)
    }.should raise_error(/unknown implementation/)
  end
end

describe PlatformGuard, ".standard?" do
  it "returns true if implementation? returns true" do
    PlatformGuard.should_receive(:implementation?).with(:ruby).and_return(true)
    PlatformGuard.standard?.should be_true
{'fraction_non_alphanumeric': 0.11198536139066789, 'fraction_numerical': 0.00292772186642269, 'mean_word_length': 4.067686601761706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '7459660', 'n_tokens_mistral': 3839, 'n_tokens_neox': 3557, 'n_words': 895}