add VarSet#include?

This commit is contained in:
Josh Holtrop 2014-03-27 10:39:46 -04:00
parent b2c970c9ca
commit cbd5816b4b
2 changed files with 26 additions and 7 deletions

View File

@ -17,17 +17,25 @@ module Rscons
# Access the value of variable as a particular type # Access the value of variable as a particular type
# @param key [String, Symbol] The variable name. # @param key [String, Symbol] The variable name.
# @return [Object] The variable's value.
def [](key) def [](key)
@vars[key] @vars[key]
end end
# Assign a value to a variable. # Assign a value to a variable.
# @param key [String, Symbol] The variable name. # @param key [String, Symbol] The variable name.
# @param val [Object] The value. # @param val [Object] The value to set.
def []=(key, val) def []=(key, val)
@vars[key] = val @vars[key] = val
end end
# Check if the VarSet contains a variable.
# @param key [String, Symbol] The variable name.
# @return [true, false] Whether the VarSet contains a variable.
def include?(key)
@vars.include?(key)
end
# Add or overwrite a set of variables # Add or overwrite a set of variables
# @param values [VarSet, Hash] New set of variables. # @param values [VarSet, Hash] New set of variables.
def append(values) def append(values)

View File

@ -1,6 +1,6 @@
module Rscons module Rscons
describe VarSet do describe VarSet do
describe '.initialize' do describe '#initialize' do
it "initializes variables from a Hash" do it "initializes variables from a Hash" do
v = VarSet.new({"one" => 1, "two" => :two}) v = VarSet.new({"one" => 1, "two" => :two})
v["one"].should == 1 v["one"].should == 1
@ -20,7 +20,7 @@ module Rscons
end end
end end
describe :[] do describe "#[]" do
v = VarSet.new({"fuz" => "a string", "foo" => 42, "bar" => :baz, v = VarSet.new({"fuz" => "a string", "foo" => 42, "bar" => :baz,
"qax" => [3, 6], "qux" => {a: :b}}) "qax" => [3, 6], "qux" => {a: :b}})
it "allows accessing a variable with its verbatim value if type is not specified" do it "allows accessing a variable with its verbatim value if type is not specified" do
@ -32,7 +32,7 @@ module Rscons
end end
end end
describe :[]= do describe "#[]=" do
it "allows assigning to variables" do it "allows assigning to variables" do
v = VarSet.new("CFLAGS" => ["-Wall", "-O3"]) v = VarSet.new("CFLAGS" => ["-Wall", "-O3"])
v["CPPPATH"] = ["one", "two"] v["CPPPATH"] = ["one", "two"]
@ -41,7 +41,18 @@ module Rscons
end end
end end
describe '.append' do describe "#include?" do
it "returns whether the variable is in the VarSet" do
v = VarSet.new("CFLAGS" => [], :foo => :bar)
expect(v.include?("CFLAGS")).to be_true
expect(v.include?(:CFLAGS)).to be_false
expect(v.include?(:foo)).to be_true
expect(v.include?("foo")).to be_false
expect(v.include?("bar")).to be_false
end
end
describe '#append' do
it "adds values from a Hash to the VarSet" do it "adds values from a Hash to the VarSet" do
v = VarSet.new("LDFLAGS" => "-lgcc") v = VarSet.new("LDFLAGS" => "-lgcc")
v.append("LIBS" => "gcc", "LIBPATH" => ["mylibs"]) v.append("LIBS" => "gcc", "LIBPATH" => ["mylibs"])
@ -56,7 +67,7 @@ module Rscons
end end
end end
describe '.merge' do describe '#merge' do
it "returns a new VarSet merged with the given Hash" do it "returns a new VarSet merged with the given Hash" do
v = VarSet.new("foo" => "yoda") v = VarSet.new("foo" => "yoda")
v2 = v.merge("baz" => "qux") v2 = v.merge("baz" => "qux")
@ -74,7 +85,7 @@ module Rscons
end end
end end
describe '.expand_varref' do describe '#expand_varref' do
v = VarSet.new("CFLAGS" => ["-Wall", "-O2"], v = VarSet.new("CFLAGS" => ["-Wall", "-O2"],
"CC" => "gcc", "CC" => "gcc",
"CPPPATH" => ["dir1", "dir2"], "CPPPATH" => ["dir1", "dir2"],