|
| 1 | +require File.expand_path(File.dirname(__FILE__) + '/spec_helper') |
| 2 | + |
| 3 | +# RSpec::Parameterized |
| 4 | +# Sample |
| 5 | +# plus |
| 6 | +# [1, 2, 3] |
| 7 | +# should do additions |
| 8 | +# [5, 8, 13] |
| 9 | +# should do additions |
| 10 | +# [0, 0, 0] |
| 11 | +# should do additions |
| 12 | + |
| 13 | +describe RSpec::Parameterized do |
| 14 | + describe "where and with_them" do |
| 15 | + where(:a, :b, :answer) do |
| 16 | + [ |
| 17 | + [1 , 2 , 3], |
| 18 | + [5 , 8 , 13], |
| 19 | + [0 , 0 , 0] |
| 20 | + ] |
| 21 | + end |
| 22 | + |
| 23 | + with_them do |
| 24 | + it "should do additions" do |
| 25 | + expect(a + b).to eq answer |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + with_them pending: "PENDING" do |
| 30 | + it "should do additions" do |
| 31 | + expect(a + b).to == answer |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + describe "lambda parameter" do |
| 37 | + where(:a, :b, :answer) do |
| 38 | + [ |
| 39 | + [1 , 2 , -> {should == 3}], |
| 40 | + [5 , 8 , -> {should == 13}], |
| 41 | + [0 , 0 , -> {should == 0}] |
| 42 | + ] |
| 43 | + end |
| 44 | + |
| 45 | + with_them do |
| 46 | + subject {a + b} |
| 47 | + it "should do additions" do |
| 48 | + self.instance_exec(&answer) |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + describe "table separated with pipe" do |
| 54 | + where_table(:a, :b, :answer) do |
| 55 | + 1 | 2 | 3 |
| 56 | + "hello " | "world" | "hello world" |
| 57 | + [1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6] |
| 58 | + end |
| 59 | + |
| 60 | + with_them do |
| 61 | + it "a plus b is answer" do |
| 62 | + expect(a + b).to eq answer |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + if RUBY_VERSION >= "2.1" |
| 68 | + describe "table separated with pipe (using TableSyntax)" do |
| 69 | + using RSpec::Parameterized::TableSyntax |
| 70 | + |
| 71 | + where(:a, :b, :answer) do |
| 72 | + 1 | 2 | 3 |
| 73 | + "hello " | "world" | "hello world" |
| 74 | + [1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6] |
| 75 | + end |
| 76 | + |
| 77 | + with_them do |
| 78 | + it "a plus b is answer" do |
| 79 | + expect(a + b).to eq answer |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + describe "table separated with pipe and lambda parameter (using TableSyntax)" do |
| 85 | + using RSpec::Parameterized::TableSyntax |
| 86 | + |
| 87 | + where(:a, :b, :matcher) do |
| 88 | + 1 | 2 | -> { eq(3) } |
| 89 | + "hello " | "world" | -> { eq("hello world") } |
| 90 | + [1, 2, 3] | [4, 5, 6] | -> { be_a(Array) } |
| 91 | + end |
| 92 | + |
| 93 | + with_them do |
| 94 | + it "a plus b is answer" do |
| 95 | + expect(a + b).to instance_exec(&matcher) |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context "when the where block is after with_them" do |
| 102 | + with_them do |
| 103 | + it "should do additions" do |
| 104 | + expect(a + b).to eq answer |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + with_them do |
| 109 | + subject { a } |
| 110 | + it { should be_a Numeric } |
| 111 | + end |
| 112 | + |
| 113 | + where(:a, :b, :answer) do |
| 114 | + [ |
| 115 | + [1 , 2 , 3], |
| 116 | + [5 , 8 , 13], |
| 117 | + [0 , 0 , 0] |
| 118 | + ] |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context "when the where block is between with_thems" do |
| 123 | + with_them do |
| 124 | + it "should do additions" do |
| 125 | + expect(a + b).to eq answer |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + where(:a, :b, :answer) do |
| 130 | + [ |
| 131 | + [1 , 2 , 3], |
| 132 | + [5 , 8 , 13], |
| 133 | + [0 , 0 , 0] |
| 134 | + ] |
| 135 | + end |
| 136 | + |
| 137 | + with_them do |
| 138 | + subject { a } |
| 139 | + it { should be_a Numeric } |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + context "when the where has only one parameter to be set" do |
| 144 | + where(:x) do |
| 145 | + [1, 2, 3] |
| 146 | + end |
| 147 | + |
| 148 | + with_them do |
| 149 | + it 'can take an array of elements' do |
| 150 | + expect(x).to eq x |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + context "when the table has only a row" do |
| 156 | + where_table(:a, :b, :answer) do |
| 157 | + 1 | 2 | 3 |
| 158 | + end |
| 159 | + |
| 160 | + with_them do |
| 161 | + it "a plus b is answer" do |
| 162 | + expect(a + b).to eq answer |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | + |
| 167 | + if RUBY_VERSION >= "2.1" |
| 168 | + context "when the table has only a row (using TableSyntax)" do |
| 169 | + using RSpec::Parameterized::TableSyntax |
| 170 | + |
| 171 | + where(:a, :b, :answer) do |
| 172 | + 1 | 2 | 3 |
| 173 | + end |
| 174 | + |
| 175 | + with_them do |
| 176 | + it "a plus b is answer" do |
| 177 | + expect(a + b).to eq answer |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | + context "when 1st column is nil or true or false" do |
| 182 | + using RSpec::Parameterized::TableSyntax |
| 183 | + where(:a, :result) do |
| 184 | + nil | nil |
| 185 | + false | false |
| 186 | + true | true |
| 187 | + end |
| 188 | + |
| 189 | + with_them do |
| 190 | + it "a is result" do |
| 191 | + expect(a).to be result |
| 192 | + end |
| 193 | + end |
| 194 | + end |
| 195 | + end |
| 196 | + |
| 197 | + context "when the where has let variables, defined by parent example group" do |
| 198 | + describe "parent (define let)" do |
| 199 | + let(:five) { 5 } |
| 200 | + let(:eight) { 8 } |
| 201 | + |
| 202 | + describe "child 1" do |
| 203 | + where(:a, :b, :answer) do |
| 204 | + [ |
| 205 | + [1 , 2 , 3], |
| 206 | + [five , eight , 13], |
| 207 | + ] |
| 208 | + end |
| 209 | + |
| 210 | + with_them do |
| 211 | + it "a plus b is answer" do |
| 212 | + expect(a + b).to eq answer |
| 213 | + end |
| 214 | + end |
| 215 | + end |
| 216 | + |
| 217 | + describe "child 2 (where_table)" do |
| 218 | + where_table(:a, :b, :answer) do |
| 219 | + 1 | 2 | 3 |
| 220 | + five | eight | 13 |
| 221 | + end |
| 222 | + |
| 223 | + with_them do |
| 224 | + it "a plus b is answer" do |
| 225 | + expect(a + b).to eq answer |
| 226 | + end |
| 227 | + end |
| 228 | + end |
| 229 | + |
| 230 | + if RUBY_VERSION >= "2.1" |
| 231 | + describe "child 3 (Using TableSyntax)" do |
| 232 | + using RSpec::Parameterized::TableSyntax |
| 233 | + |
| 234 | + where(:a, :b, :answer) do |
| 235 | + 1 | 2 | 3 |
| 236 | + five | eight | 13 |
| 237 | + end |
| 238 | + |
| 239 | + with_them do |
| 240 | + it "a plus b is answer" do |
| 241 | + expect(a + b).to eq answer |
| 242 | + end |
| 243 | + end |
| 244 | + end |
| 245 | + end |
| 246 | + |
| 247 | + let(:eq_matcher) { eq(13) } |
| 248 | + describe "child 3 (use matcher)" do |
| 249 | + where(:a, :b, :matcher) do |
| 250 | + [ |
| 251 | + [1 , 2 , eq(3) ], |
| 252 | + [five , eight , eq_matcher], |
| 253 | + ] |
| 254 | + end |
| 255 | + |
| 256 | + with_them do |
| 257 | + it "a plus b is answer" do |
| 258 | + expect(a + b).to matcher |
| 259 | + end |
| 260 | + end |
| 261 | + end |
| 262 | + end |
| 263 | + end |
| 264 | +end |
0 commit comments