-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclass_transformations_spec.rb
50 lines (39 loc) · 1.24 KB
/
class_transformations_spec.rb
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
# frozen_string_literal: true
require "dry/equalizer"
RSpec.describe Dry::Transformer::ClassTransformations do
describe ".constructor_inject" do
let(:klass) do
Struct.new(:name, :age) { include Dry::Equalizer.new(:name, :age) }
end
it "returns a new object initialized with the given arguments" do
constructor_inject = described_class.t(:constructor_inject, klass)
input = ["Jane", 25]
output = klass.new(*input)
result = constructor_inject[*input]
expect(result).to eql(output)
expect(result).to be_instance_of(klass)
end
end
describe ".set_ivars" do
let(:klass) do
Class.new do
include Dry::Equalizer.new(:name, :age)
attr_reader :name, :age, :test
def initialize(name:, age:)
@name = name
@age = age
@test = true
end
end
end
it "allocates a new object and sets instance variables from hash key/value pairs" do
set_ivars = described_class.t(:set_ivars, klass)
input = { name: "Jane", age: 25 }
output = klass.new(**input)
result = set_ivars[input]
expect(result).to eql(output)
expect(result.test).to be(nil)
expect(result).to be_instance_of(klass)
end
end
end