# encoding: utf-8 require "spec_helper" module Hexx describe Helpers::Parameters do before { class Test; include Helpers::Parameters; end } let(:described_class) { Test } after { Hexx.send :remove_const, :Test } describe "#params" do subject { described_class.new } it "is defined" do expect(subject).to respond_to :params end it "has no setter" do expect(subject).not_to respond_to :params= end end describe ".new" do it "accepts parameters" do expect { described_class.new name: "name" }.not_to raise_error end it "doesn't require parameters" do expect { described_class.new }.not_to raise_error end end describe ".allow_params" do it "is defined" do expect(described_class).to respond_to(:allow_params) end it "accepts one argument" do expect { described_class.send :allow_params, :name } .not_to raise_error end it "accepts a list of arguments" do expect { described_class.send :allow_params, :name, :type } .not_to raise_error end it "accepts an array of arguments" do expect { described_class.send :allow_params, %w(name type) } .not_to raise_error end it "requires strings or symbols" do expect { described_class.send :allow_params, 1 } .to raise_error SyntaxError end it "requires non-empty values" do expect { described_class.send :allow_params, "" } .to raise_error ArgumentError end it "requires values" do expect { described_class.send :allow_params } .to raise_error ArgumentError end context "with valid argument" do before { described_class.send :allow_params, :name } subject { described_class.new name: "name" } it "allows initialization of the parameter" do expect(subject.params).to eq("name" => "name") end it "defines private getter for the parameter" do expect(described_class.private_instance_methods) .to be_include :name expect { subject.params["name"] = "new" } .to change { subject.send :name }.to "new" end it "defines private setter for the parameter" do expect(described_class.private_instance_methods) .to be_include :name= expect { subject.send :name=, "new" } .to change { subject.params["name"] }.to "new" end end end end end