summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/guards/feature_spec.rb
blob: fcb89975912dd4f2ee36e9061b3a1db44c1f8f21 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'spec_helper'
require 'mspec/guards'

RSpec.describe FeatureGuard, ".enabled?" do
  it "returns true if the feature is enabled" do
    expect(MSpec).to receive(:feature_enabled?).with(:encoding).and_return(true)
    expect(FeatureGuard.enabled?(:encoding)).to be_truthy
  end

  it "returns false if the feature is not enabled" do
    expect(MSpec).to receive(:feature_enabled?).with(:encoding).and_return(false)
    expect(FeatureGuard.enabled?(:encoding)).to be_falsey
  end

  it "returns true if all the features are enabled" do
    expect(MSpec).to receive(:feature_enabled?).with(:one).and_return(true)
    expect(MSpec).to receive(:feature_enabled?).with(:two).and_return(true)
    expect(FeatureGuard.enabled?(:one, :two)).to be_truthy
  end

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

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

    @guard = FeatureGuard.new :encoding
    allow(FeatureGuard).to receive(:new).and_return(@guard)
  end

  it "sets the name of the guard to :with_feature" do
    with_feature(:encoding) { }
    expect(@guard.name).to eq(:with_feature)
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    expect(@guard).to receive(:match?).and_return(true)
    expect(@guard).to receive(:unregister)
    expect do
      with_feature { raise Exception }
    end.to raise_error(Exception)
  end
end

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

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

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

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

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

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

    @guard = FeatureGuard.new :encoding
    allow(FeatureGuard).to receive(:new).and_return(@guard)
  end

  it "sets the name of the guard to :without_feature" do
    without_feature(:encoding) { }
    expect(@guard.name).to eq(:without_feature)
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    expect(@guard).to receive(:match?).and_return(false)
    expect(@guard).to receive(:unregister)
    expect do
      without_feature { raise Exception }
    end.to raise_error(Exception)
  end
end

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

  it "does not yield if the feature is enabled" do
    expect(MSpec).to receive(:feature_enabled?).with(:encoding).and_return(true)
    without_feature(:encoding) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).to be_nil
  end

  it "yields if the feature is disabled" do
    expect(MSpec).to receive(:feature_enabled?).with(:encoding).and_return(false)
    without_feature(:encoding) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).to eq(:yield)
  end
end