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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
# frozen_string_literal: true
require "dry/core/constants"
RSpec.describe Dry::Core::Constants do
before do
class ClassWithConstants
include Dry::Core::Constants
def empty_array
EMPTY_ARRAY
end
def empty_hash
EMPTY_HASH
end
def empty_set
EMPTY_SET
end
def empty_string
EMPTY_STRING
end
def empty_opts
EMPTY_OPTS
end
def undefined
Undefined
end
end
end
after do
Object.send(:remove_const, :ClassWithConstants)
end
subject { ClassWithConstants.new }
it "makes constants available in your class" do
expect(subject.empty_array).to be Dry::Core::Constants::EMPTY_ARRAY
expect(subject.empty_array).to eql([])
expect(subject.empty_hash).to be Dry::Core::Constants::EMPTY_HASH
expect(subject.empty_hash).to eql({})
expect(subject.empty_set).to be Dry::Core::Constants::EMPTY_SET
expect(subject.empty_set).to eql(Set.new)
expect(subject.empty_string).to be Dry::Core::Constants::EMPTY_STRING
expect(subject.empty_string).to eql("")
expect(subject.empty_opts).to be Dry::Core::Constants::EMPTY_OPTS
expect(subject.empty_opts).to eql({})
expect(subject.undefined).to be Dry::Core::Constants::Undefined
end
describe "nested" do
before do
class ClassWithConstants
class Nested
def empty_array
EMPTY_ARRAY
end
end
end
end
subject { ClassWithConstants::Nested.new }
example "constants available in lexical scope" do
expect(subject.empty_array).to be Dry::Core::Constants::EMPTY_ARRAY
end
end
describe "Undefined" do
subject(:undefined) { Dry::Core::Constants::Undefined }
describe ".inspect" do
it 'returns "Undefined"' do
expect(subject.inspect).to eql("Undefined")
end
end
describe ".to_s" do
it 'returns "Undefined"' do
expect(subject.to_s).to eql("Undefined")
end
end
describe ".default" do
it "returns the first arg if it's not Undefined" do
expect(subject.default(:first, :second)).to eql(:first)
end
it "returns the second arg if the first one is Undefined" do
expect(subject.default(subject, :second)).to eql(:second)
end
it "yields a block" do
expect(subject.default(subject) { :second }).to eql(:second)
end
end
describe ".map" do
it "maps non-undefined value" do
expect(subject.map("foo", &:to_sym)).to be(:foo)
expect(subject.map(subject, &:to_sym)).to be(subject)
end
end
describe ".dup" do
subject { undefined.dup }
it { is_expected.to be(undefined) }
end
describe ".clone" do
subject { undefined.clone }
it { is_expected.to be(undefined) }
end
describe ".coalesce" do
it "returns first non-undefined value in a list" do
expect(undefined.coalesce(1, 2)).to be(1)
expect(undefined.coalesce(undefined, 1, 2)).to be(1)
expect(undefined.coalesce(undefined, undefined, 1, 2)).to be(1)
expect(undefined.coalesce(undefined, undefined)).to be(undefined)
expect(undefined.coalesce(nil)).to be(nil)
expect(undefined.coalesce(undefined, nil)).to be(nil)
expect(undefined.coalesce(undefined, nil, false)).to be(nil)
expect(undefined.coalesce(undefined, false, nil)).to be(false)
end
end
end
end
|