|
2 | 2 |
|
3 | 3 | RSpec.describe JsonLogic::Evaluator do
|
4 | 4 | describe '#apply' do
|
5 |
| - subject(:evaluator) { described_class.new.apply(rules, data) } |
| 5 | + subject(:evaluator) { logic.apply(rules, data) } |
| 6 | + |
| 7 | + let(:logic) { described_class.new } |
6 | 8 |
|
7 | 9 | context 'with var' do
|
8 | 10 | let(:rules) { { 'var' => var } }
|
|
152 | 154 |
|
153 | 155 | it { is_expected.to eq(result) }
|
154 | 156 | end
|
| 157 | + |
| 158 | + context 'when not(!)' do |
| 159 | + context 'when not between' do |
| 160 | + let(:rules) { { '!' => { '<=' => [70, { 'var' => 'age' }, 75] } } } |
| 161 | + |
| 162 | + context 'when main part is false' do |
| 163 | + let(:data) { { 'age' => 69 } } |
| 164 | + |
| 165 | + it 'returns and tracks true' do |
| 166 | + expect(evaluator).to be(true) |
| 167 | + expect(logic.tracker.result).to be(true) |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + context 'when main part is true' do |
| 172 | + let(:data) { { 'age' => 72 } } |
| 173 | + |
| 174 | + it 'returns and tracks false' do |
| 175 | + expect(evaluator).to be(false) |
| 176 | + expect(logic.tracker.result).to be(false) |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + |
| 181 | + context 'when not one of (select not any in)' do |
| 182 | + context 'with string' do |
| 183 | + let(:rules) { { '!' => { 'in' => [{ 'var' => 'drink' }, 'sell cola'] } } } |
| 184 | + |
| 185 | + context 'when main part is false' do |
| 186 | + let(:data) { { 'drink' => 'beer' } } |
| 187 | + |
| 188 | + it 'returns and tracks true' do |
| 189 | + expect(evaluator).to be(true) |
| 190 | + expect(logic.tracker.result).to be(true) |
| 191 | + end |
| 192 | + end |
| 193 | + |
| 194 | + context 'when main part is true' do |
| 195 | + let(:data) { { 'drink' => 'cola' } } |
| 196 | + |
| 197 | + it 'returns and tracks false' do |
| 198 | + expect(evaluator).to be(false) |
| 199 | + expect(logic.tracker.result).to be(false) |
| 200 | + end |
| 201 | + end |
| 202 | + end |
| 203 | + |
| 204 | + context 'with array' do |
| 205 | + let(:rules) { { '!' => { 'in' => [{ 'var' => 'drink' }, %w[cola juice]] } } } |
| 206 | + |
| 207 | + context 'when main part is false' do |
| 208 | + let(:data) { { 'drink' => 'beer' } } |
| 209 | + |
| 210 | + it 'returns and tracks true' do |
| 211 | + expect(evaluator).to be(true) |
| 212 | + expect(logic.tracker.result).to be(true) |
| 213 | + end |
| 214 | + end |
| 215 | + |
| 216 | + context 'when main part is true' do |
| 217 | + let(:data) { { 'drink' => 'cola' } } |
| 218 | + |
| 219 | + it 'returns and tracks false' do |
| 220 | + expect(evaluator).to be(false) |
| 221 | + expect(logic.tracker.result).to be(false) |
| 222 | + end |
| 223 | + end |
| 224 | + end |
| 225 | + end |
| 226 | + |
| 227 | + context 'when does not contain any of (not like)' do |
| 228 | + context 'with string' do |
| 229 | + let(:rules) { { '!' => { 'in' => ['ol', { 'var' => 'drink' }] } } } |
| 230 | + |
| 231 | + context 'when main part is false' do |
| 232 | + let(:data) { { 'drink' => 'juice' } } |
| 233 | + |
| 234 | + it 'returns and tracks true' do |
| 235 | + expect(evaluator).to be(true) |
| 236 | + expect(logic.tracker.result).to be(true) |
| 237 | + end |
| 238 | + end |
| 239 | + |
| 240 | + context 'when main part is true' do |
| 241 | + let(:data) { { 'drink' => 'cola' } } |
| 242 | + |
| 243 | + it 'returns and tracks false' do |
| 244 | + expect(evaluator).to be(false) |
| 245 | + expect(logic.tracker.result).to be(false) |
| 246 | + end |
| 247 | + end |
| 248 | + end |
| 249 | + |
| 250 | + context 'with array' do |
| 251 | + let(:rules) { { '!' => { 'in' => ['beer', { 'var' => 'drinks' }] } } } |
| 252 | + |
| 253 | + context 'when main part is false' do |
| 254 | + let(:data) { { 'drinks' => %w[cola juice] } } |
| 255 | + |
| 256 | + it 'returns and tracks true' do |
| 257 | + expect(evaluator).to be(true) |
| 258 | + expect(logic.tracker.result).to be(true) |
| 259 | + end |
| 260 | + end |
| 261 | + |
| 262 | + context 'when main part is true' do |
| 263 | + let(:data) { { 'drinks' => %w[beer cola] } } |
| 264 | + |
| 265 | + it 'returns and tracks false' do |
| 266 | + expect(evaluator).to be(false) |
| 267 | + expect(logic.tracker.result).to be(false) |
| 268 | + end |
| 269 | + end |
| 270 | + end |
| 271 | + end |
| 272 | + end |
155 | 273 | end
|
156 | 274 |
|
157 | 275 | describe '#get_var_name' do
|
|
308 | 426 | end
|
309 | 427 |
|
310 | 428 | describe '#fetch_var_values' do
|
311 |
| - subject { described_class.new.send(:fetch_var_values, rules, var_name) } |
| 429 | + subject { described_class.new.fetch_var_values(rules, var_name) } |
312 | 430 |
|
313 | 431 | context 'when variable present' do
|
314 | 432 | let(:rules) do
|
|
0 commit comments