summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/guards/feature_spec.rb
blob: d14e5f8e677881f68502bb802f84ede566f2a32e (plain)
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
require 'spec_helper'
require 'mspec/guards'

describe FeatureGuard, ".enabled?" do
  it "returns true if the feature is enabled" do
    MSpec.should_receive(:feature_enabled?).with(:encoding).and_return(true)
    FeatureGuard.enabled?(:encoding).should be_true
  end

  it "returns false if the feature is not enabled" do
    MSpec.should_receive(:feature_enabled?).with(:encoding).and_return(false)
    FeatureGuard.enabled?(:encoding).should be_false
  end

  it "returns true if all the features are enabled" do
    MSpec.should_receive(:feature_enabled?).with(:one).and_return(true)
    MSpec.should_receive(:feature_enabled?).with(:two).and_return(true)
    FeatureGuard.enabled?(:one, :two).should be_true
  end

  it "returns false if any of the features are not enabled" do
    MSpec.should_receive(:feature_enabled?).with(:one).and_return(true)
    MSpec.should_receive(:feature_enabled?).with(:two).and_return(false)
    FeatureGuard.enabled?(:one, :two).should be_false
  end
end

describe Object, "#with_feature" do
  before :each do
    ScratchPad.clear

    @guard = FeatureGuard.new :encoding
    FeatureGuard.stub(:new).and_return(@guard)
  end

  it "sets the name of the guard to :with_feature" do
    with_feature(:encoding) { }
    @guard.name.should == :with_feature
  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
      with_feature { raise Exception }
    end.should raise_error(Exception)
  end
end

describe Object, "#with_feature" do
  before :each do
    ScratchPad.clear
  end

  it "yields if the feature is enabled" do
    MSpec.should_receive(:feature_enabled?).with(:encoding).and_return(true)
    with_feature(:encoding) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "yields if all the features are enabled" do
    MSpec.should_receive(:feature_enabled?).with(:one).and_return(true)
    MSpec.should_receive(:feature_enabled?).with(:two).and_return(true)
    with_feature(:one, :two) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "does not yield if the feature is not enabled" do
    MSpec.should_receive(:feature_enabled?).with(:encoding).and_return(false)
    with_feature(:encoding) { ScratchPad.record :yield }
    ScratchPad.recorded.should be_nil
  end

  it "does not yield if any of the features are not enabled" do
    MSpec.should_receive(:feature_enabled?).with(:one).and_return(true)
    MSpec.should_receive(:feature_enabled?).with(:two).and_return(false)
    with_feature(:one, :two) { ScratchPad.record :yield }
    ScratchPad.recorded.should be_nil
  end
end