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
|
# frozen_string_literal: true
require "dry/core/inflector"
RSpec.describe Dry::Core::Inflector do
shared_examples "an inflector" do
it "singularises" do
expect(api.singularize("tasks")).to eql("task")
end
it "pluralizes" do
expect(api.pluralize("task")).to eql("tasks")
end
it "camelizes" do
expect(api.camelize("task_user")).to eql("TaskUser")
end
it "underscores" do
expect(api.underscore("TaskUser")).to eql("task_user")
end
it "demodulizes" do
expect(api.demodulize("Task::User")).to eql("User")
end
it "classifies" do
expect(api.classify("task_user/name")).to eql("TaskUser::Name")
end
end
shared_examples "an inflector with constantize" do
it "constantizes" do
expect(api.constantize("String")).to be String
end
end
subject(:api) { Dry::Core::Inflector }
context "with detected inflector" do
before do
if api.instance_variables.include?(:@inflector)
api.__send__(:remove_instance_variable, :@inflector)
end
end
it "prefers ActiveSupport::Inflector" do
expect(api.inflector).to be ::ActiveSupport::Inflector
end
end
context "with automatic detection" do
before do
if api.instance_variables.include?(:@inflector)
api.__send__(:remove_instance_variable, :@inflector)
end
end
it "automatically selects an inflector backend" do
expect(api.inflector).not_to be nil
end
end
context "with ActiveSupport::Inflector" do
before do
api.select_backend(:activesupport)
end
it "is ActiveSupport::Inflector" do
expect(api.inflector).to be(::ActiveSupport::Inflector)
end
it_behaves_like "an inflector"
it_behaves_like "an inflector with constantize"
end
context "with Inflecto" do
before do
api.select_backend(:inflecto)
end
it "is Inflecto" do
expect(api.inflector).to be(::Inflecto)
end
it_behaves_like "an inflector"
it_behaves_like "an inflector with constantize"
end
context "with Dry::Inflector" do
before do
api.select_backend(:dry_inflector)
end
it "is Dry::Inflector" do
expect(api.inflector).to be_a(Dry::Inflector)
end
it_behaves_like "an inflector"
end
context "an unrecognized inflector library is selected" do
it "raises a NameError" do
expect { api.select_backend(:foo) }.to raise_error(NameError)
end
end
end
|