File: each_spec.rb

package info (click to toggle)
ruby-dry-logic 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 728 kB
  • sloc: ruby: 4,929; makefile: 6
file content (49 lines) | stat: -rw-r--r-- 1,409 bytes parent folder | download
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
# frozen_string_literal: true

RSpec.describe Dry::Logic::Operations::Each do
  subject(:operation) { described_class.new(is_string) }

  include_context "predicates"

  let(:is_string) { Dry::Logic::Rule::Predicate.build(str?) }

  describe "#call" do
    it "applies its rules to all elements in the input" do
      expect(operation.(["Address"])).to be_success

      expect(operation.([nil, "Address"])).to be_failure
      expect(operation.([:Address, "Address"])).to be_failure
    end
  end

  describe "#to_ast" do
    it "returns ast" do
      expect(operation.to_ast).to eql([:each, [:predicate, [:str?, [[:input, undefined]]]]])
    end

    it "returns result ast" do
      expect(operation.([nil, 12, nil]).to_ast).to eql(
        [:set, [
          [:key, [0, [:predicate, [:str?, [[:input, nil]]]]]],
          [:key, [1, [:predicate, [:str?, [[:input, 12]]]]]],
          [:key, [2, [:predicate, [:str?, [[:input, nil]]]]]]
        ]]
      )
    end

    it "returns failure result ast" do
      expect(operation.with(id: :tags).([nil, "red", 12]).to_ast).to eql(
        [:failure, [:tags, [:set, [
          [:key, [0, [:predicate, [:str?, [[:input, nil]]]]]],
          [:key, [2, [:predicate, [:str?, [[:input, 12]]]]]]
        ]]]]
      )
    end
  end

  describe "#to_s" do
    it "returns string representation" do
      expect(operation.to_s).to eql("each(str?)")
    end
  end
end