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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
require "spec_helper"
describe Clamp::Parameter::Definition do
context "normal" do
let(:parameter) do
described_class.new("COLOR", "hue of choice")
end
it "has a name" do
expect(parameter.name).to eql "COLOR"
end
it "has a description" do
expect(parameter.description).to eql "hue of choice"
end
it "is single-valued" do
expect(parameter).to_not be_multivalued
end
describe "#attribute_name" do
it "is derived from the name" do
expect(parameter.attribute_name).to eql "color"
end
it "can be overridden" do
parameter = described_class.new("COLOR", "hue of choice", :attribute_name => "hue")
expect(parameter.attribute_name).to eql "hue"
end
end
describe "#consume" do
it "consumes one argument" do
arguments = %w(a b c)
expect(parameter.consume(arguments)).to eql ["a"]
expect(arguments).to eql %w(b c)
end
describe "with no arguments" do
it "raises an Argument error" do
arguments = []
expect do
parameter.consume(arguments)
end.to raise_error(ArgumentError)
end
end
end
end
context "optional (name in square brackets)" do
let(:parameter) do
described_class.new("[COLOR]", "hue of choice")
end
it "is single-valued" do
expect(parameter).to_not be_multivalued
end
describe "#attribute_name" do
it "omits the brackets" do
expect(parameter.attribute_name).to eql "color"
end
end
describe "#consume" do
it "consumes one argument" do
arguments = %w(a b c)
expect(parameter.consume(arguments)).to eql ["a"]
expect(arguments).to eql %w(b c)
end
describe "with no arguments" do
it "consumes nothing" do
arguments = []
expect(parameter.consume(arguments)).to eql []
end
end
end
end
context "list (name followed by ellipsis)" do
let(:parameter) do
described_class.new("FILE ...", "files to process")
end
it "is multi-valued" do
expect(parameter).to be_multivalued
end
describe "#attribute_name" do
it "gets a _list suffix" do
expect(parameter.attribute_name).to eql "file_list"
end
end
describe "#append_method" do
it "is derived from the attribute_name" do
expect(parameter.append_method).to eql "append_to_file_list"
end
end
describe "#consume" do
it "consumes all the remaining arguments" do
arguments = %w(a b c)
expect(parameter.consume(arguments)).to eql %w(a b c)
expect(arguments).to eql []
end
describe "with no arguments" do
it "raises an Argument error" do
arguments = []
expect do
parameter.consume(arguments)
end.to raise_error(ArgumentError)
end
end
end
context "with a weird parameter name, and an explicit attribute_name" do
let(:parameter) do
described_class.new("KEY=VALUE ...", "config-settings", :attribute_name => :config_settings)
end
describe "#attribute_name" do
it "is the specified one" do
expect(parameter.attribute_name).to eql "config_settings"
end
end
end
end
context "optional list" do
let(:parameter) do
described_class.new("[FILES] ...", "files to process")
end
it "is multi-valued" do
expect(parameter).to be_multivalued
end
describe "#attribute_name" do
it "gets a _list suffix" do
expect(parameter.attribute_name).to eql "files_list"
end
end
describe "#default_value" do
it "is an empty list" do
expect(parameter.default_value).to eql []
end
end
describe "#help" do
it "does not include default" do
expect(parameter.help_rhs).to_not include("default:")
end
end
context "with specified default value" do
let(:parameter) do
described_class.new("[FILES] ...", "files to process", :default => %w(a b c))
end
describe "#default_value" do
it "is that specified" do
expect(parameter.default_value).to eql %w(a b c)
end
end
describe "#help" do
it "includes the default value" do
expect(parameter.help_rhs).to include("default:")
end
end
describe "#consume" do
it "consumes all the remaining arguments" do
arguments = %w(a b c)
expect(parameter.consume(arguments)).to eql %w(a b c)
expect(arguments).to eql []
end
context "with no arguments" do
it "don't override defaults" do
arguments = []
expect(parameter.consume(arguments)).to eql []
end
end
end
end
end
end
|