2018-10-23 16:42:18 +00:00
|
|
|
require "../spec_helper"
|
|
|
|
|
|
|
|
private def add_sample_value(values : Spectator::Internals::SampleValues,
|
2018-10-23 16:51:50 +00:00
|
|
|
symbol : Symbol, name : String, value : T) forall T
|
2018-10-23 16:42:18 +00:00
|
|
|
values.add(symbol, name, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def add_sample_value(symbol, name, value : T) forall T
|
|
|
|
add_sample_value(Spectator::Internals::SampleValues.empty, symbol, name, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def add_sample_value(symbol, value : T) forall T
|
|
|
|
add_sample_value(symbol, symbol.to_s, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Spectator::Internals::SampleValues do
|
|
|
|
describe "#add" do
|
|
|
|
it "creates a new set" do
|
|
|
|
original = Spectator::Internals::SampleValues.empty
|
|
|
|
new_set = original.add(:new, "new", 123)
|
|
|
|
new_set.should_not eq(original)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds a new value" do
|
|
|
|
symbol = :new
|
|
|
|
value = 123
|
|
|
|
values = add_sample_value(symbol, value)
|
|
|
|
values.get_value(symbol, typeof(value)).should eq(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#get_wrapper" do
|
|
|
|
it "returns a wrapper for a value" do
|
|
|
|
symbol = :new
|
|
|
|
value = 123
|
|
|
|
values = add_sample_value(symbol, value)
|
|
|
|
wrapper = values.get_wrapper(symbol)
|
|
|
|
wrapper.should be_a(Spectator::Internals::ValueWrapper)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct wrapper" do
|
|
|
|
symbol = :new
|
|
|
|
value = 123
|
|
|
|
values = add_sample_value(symbol, value)
|
|
|
|
wrapper = values.get_wrapper(symbol)
|
|
|
|
wrapper.should be_a(Spectator::Internals::TypedValueWrapper(typeof(value)))
|
|
|
|
wrapper.as(Spectator::Internals::TypedValueWrapper(typeof(value))).value.should eq(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with multiple values" do
|
|
|
|
it "returns the expected value" do
|
|
|
|
symbols = [:one, :two, :three]
|
|
|
|
numbers = [123, 456, 789]
|
|
|
|
values = Spectator::Internals::SampleValues.empty
|
|
|
|
symbols.zip(numbers).each do |symbol, number|
|
|
|
|
values = add_sample_value(values, symbol, symbol.to_s, number)
|
|
|
|
end
|
|
|
|
selected_symbol = symbols[1]
|
|
|
|
selected_number = numbers[1]
|
|
|
|
wrapper = values.get_wrapper(selected_symbol)
|
|
|
|
wrapper.should be_a(Spectator::Internals::TypedValueWrapper(typeof(selected_number)))
|
|
|
|
wrapper.as(Spectator::Internals::TypedValueWrapper(typeof(selected_number))).value.should eq(selected_number)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#get_value" do
|
|
|
|
it "returns a value" do
|
|
|
|
symbol = :new
|
|
|
|
value = 123
|
|
|
|
values = add_sample_value(symbol, value)
|
|
|
|
values.get_value(symbol, typeof(value)).should eq(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with multiple values" do
|
|
|
|
it "returns the expected value" do
|
|
|
|
symbols = [:one, :two, :three]
|
|
|
|
numbers = [123, 456, 789]
|
|
|
|
values = Spectator::Internals::SampleValues.empty
|
|
|
|
symbols.zip(numbers).each do |symbol, number|
|
|
|
|
values = add_sample_value(values, symbol, symbol.to_s, number)
|
|
|
|
end
|
|
|
|
selected_symbol = symbols[1]
|
|
|
|
selected_number = numbers[1]
|
|
|
|
value = values.get_value(selected_symbol, typeof(selected_number))
|
|
|
|
value.should eq(selected_number)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 21:41:22 +00:00
|
|
|
|
|
|
|
describe "#each" do
|
|
|
|
it "yields each entry" do
|
|
|
|
symbols = [:one, :two, :three]
|
|
|
|
numbers = [123, 456, 789]
|
|
|
|
values = Spectator::Internals::SampleValues.empty
|
|
|
|
symbols.zip(numbers).each do |symbol, number|
|
|
|
|
values = add_sample_value(values, symbol, symbol.to_s, number)
|
|
|
|
end
|
|
|
|
|
|
|
|
size = 0
|
|
|
|
values.each do |entry|
|
|
|
|
size += 1
|
|
|
|
symbol = symbols.find { |s| s.to_s == entry.name }
|
|
|
|
symbol.should_not be_nil
|
|
|
|
end
|
|
|
|
size.should eq(symbols.size)
|
|
|
|
end
|
|
|
|
end
|
2018-10-23 16:42:18 +00:00
|
|
|
end
|