Mentions légales du service

Skip to content
Snippets Groups Projects
Commit fdc21ae7 authored by Jérémie Gaidamour's avatar Jérémie Gaidamour
Browse files

[dev] Replaced <a-b> syntax by [a-b]

parent e87e6bbd
No related branches found
No related tags found
No related merge requests found
......@@ -30,22 +30,22 @@ class ::Hash
self.merge(other_hash, &merger)
end
# Merge keys that match "PREFIX-<a-b>" with others keys that begins by "PREFIX-"
# Merge keys that match "PREFIX-[a-b]" with others keys that begins by "PREFIX-"
# and that ends with x, where a<=x<=b.
# - This is done recursively (for this Hash and every Hashes it may contain).
# - PREFIX-<a-b> values have lower priority on existing PREFIX-x keys.
# - "a" and/or "b" may be omited (ie. "PREFIX-<a->", "PREFIX-<-b>" or "PREFIX-<->"), meaning that there are no lower and/or upper bound for x.
# - PREFIX-[a-b] values have lower priority on existing PREFIX-x keys.
# - "a" and/or "b" may be omited (ie. "PREFIX-[a-]", "PREFIX-[-b]" or "PREFIX-[-]"), meaning that there are no lower and/or upper bound for x.
# * If only a is omited, a == 1.
# * If b is omited, only existing keys are modified (no keys are created). Otherwise, PREFIX-<a> to PREFIX-<b> entries are created (if missing).
# * If b is omited, only existing keys are modified (no keys are created). Otherwise, PREFIX-[a] to PREFIX-[b] entries are created (if missing).
# Example:
# {"foo-1": {a: 0}, "foo-2": {a: 0}, "foo-3": {a: 0}, "foo-<2->": {b: 1}}.expand_angle_brackets()
# {"foo-1": {a: 0}, "foo-2": {a: 0}, "foo-3": {a: 0}, "foo-[2-]": {b: 1}}.expand_angle_brackets()
# -> {"foo-1": {a: 0}, "foo-2": {a: 0, b:1}, "foo-3": {a: 0, b: 0}}
def expand_angle_brackets()
dup = self.clone # because can't add a new key into hash during iteration
# Looking up for PREFIX-<a-b> keys
# Looking up for PREFIX-[a-b] keys
dup.each { |key_ab, value_ab|
prefix, a, b = key_ab.to_s.scan(/^(.*)-<(\d*)-(\d*)>$/).first
prefix, a, b = key_ab.to_s.scan(/^(.*)-\[(\d*)-(\d*)\]$/).first
next if not a and not b # not found
a != "" ? a = a.to_i : a = 1
b != "" ? b = b.to_i : b
......@@ -74,7 +74,7 @@ class ::Hash
}
end
# Delete entry "PREFIX-<a-b>"
# Delete entry "PREFIX-[a-b]"
self.delete(key_ab)
}
......
......@@ -56,7 +56,7 @@ class TestInputLoader < Test::Unit::TestCase
"foo-1": {a: 0},
"foo-2": {a: 0},
"foo-3": {a: 0},
"foo-<2->": {b: 1}
"foo-[2-]": {b: 1}
}
expected_expanded_hash = {
......@@ -70,8 +70,8 @@ class TestInputLoader < Test::Unit::TestCase
# The 'a' parameter
def test__expand_angle_brackets__a_values
assert_equal_expanded_hash({"foo-<-3>": 0}, {"foo-1": 0, "foo-2": 0, "foo-3": 0}) # Default 'a' value is 1
assert_equal_expanded_hash({"foo-<2-3>": 0}, {"foo-2": 0, "foo-3": 0}) # Simply check if the value of 'a' is taken into account
assert_equal_expanded_hash({"foo-[-3]": 0}, {"foo-1": 0, "foo-2": 0, "foo-3": 0}) # Default 'a' value is 1
assert_equal_expanded_hash({"foo-[2-3]": 0}, {"foo-2": 0, "foo-3": 0}) # Simply check if the value of 'a' is taken into account
end
def test__expand_angle_brackets__create_keys
......@@ -80,26 +80,26 @@ class TestInputLoader < Test::Unit::TestCase
#
# With symbol keys and numeric values
assert_equal_expanded_hash({"foo-<2-3>": 0}, {"foo-2": 0, "foo-3": 0})
assert_equal_expanded_hash({"foo-<-2>": 0}, {"foo-1": 0, "foo-2": 0})
assert_equal_expanded_hash({"foo-[2-3]": 0}, {"foo-2": 0, "foo-3": 0})
assert_equal_expanded_hash({"foo-[-2]": 0}, {"foo-1": 0, "foo-2": 0})
# With symbol keys and hash values
assert_equal_expanded_hash({"foo-<2-3>": {a: 0}}, {"foo-2": {a: 0}, "foo-3": {a: 0}})
assert_equal_expanded_hash({"foo-<-2>": {a: 0}}, {"foo-1": {a: 0}, "foo-2": {a: 0}})
assert_equal_expanded_hash({"foo-[2-3]": {a: 0}}, {"foo-2": {a: 0}, "foo-3": {a: 0}})
assert_equal_expanded_hash({"foo-[-2]": {a: 0}}, {"foo-1": {a: 0}, "foo-2": {a: 0}})
# With string keys and numeric values
assert_equal_expanded_hash({"foo-<2-3>" => 0}, {"foo-2" => 0, "foo-3" => 0})
assert_equal_expanded_hash({"foo-<-2>" => 0}, {"foo-1" => 0, "foo-2" => 0})
assert_equal_expanded_hash({"foo-[2-3]" => 0}, {"foo-2" => 0, "foo-3" => 0})
assert_equal_expanded_hash({"foo-[-2]" => 0}, {"foo-1" => 0, "foo-2" => 0})
# With string keys and hash values
assert_equal_expanded_hash({"foo-<2-3>" => {a: 0}}, {"foo-2" => {a: 0}, "foo-3" => {a: 0}})
assert_equal_expanded_hash({"foo-<-2>" => {a: 0}}, {"foo-1" => {a: 0}, "foo-2" => {a: 0}})
assert_equal_expanded_hash({"foo-[2-3]" => {a: 0}}, {"foo-2" => {a: 0}, "foo-3" => {a: 0}})
assert_equal_expanded_hash({"foo-[-2]" => {a: 0}}, {"foo-1" => {a: 0}, "foo-2" => {a: 0}})
#
# If 'b' is not given, do not create any new key
#
assert_equal_expanded_hash({"foo-<->": 0}, {}) # All
assert_equal_expanded_hash({"foo-<2->": 0}, {})
assert_equal_expanded_hash({"foo-[-]": 0}, {}) # All
assert_equal_expanded_hash({"foo-[2-]": 0}, {})
end
......@@ -109,17 +109,17 @@ class TestInputLoader < Test::Unit::TestCase
#
[0, {h: 0}].each { |v|
assert_equal_expanded_hash({"foo-<1-3>": v, "foo-2": 1}, {"foo-1": v, "foo-2": 1, "foo-3": v}) # b given
assert_equal_expanded_hash({"foo-<2->": v, "foo-2": 1}, {"foo-2": 1}) # b not given
assert_equal_expanded_hash({"foo-[1-3]": v, "foo-2": 1}, {"foo-1": v, "foo-2": 1, "foo-3": v}) # b given
assert_equal_expanded_hash({"foo-[2-]": v, "foo-2": 1}, {"foo-2": 1}) # b not given
}
end
# Some tests with nil values (=> nil values are created, existing nil values are overriden)
def test__expand_angle_brackets__nil
assert_equal_expanded_hash({"foo-<2-3>": nil}, {"foo-2": nil, "foo-3": nil}) # PREFIX-<a-b> is nil
assert_equal_expanded_hash({"foo-<2-3>": 0, "foo-2": nil}, {"foo-2": 0, "foo-3": 0}) # PREFIX-x is nil
assert_equal_expanded_hash({"foo-<2->": nil, "foo-3": 0}, {"foo-3": 0}) # PREFIX-<a-> is nil
assert_equal_expanded_hash({"foo-<2->": 0, "foo-3": nil}, {"foo-3": 0}) # PREFIX-x is nil
assert_equal_expanded_hash({"foo-[2-3]": nil}, {"foo-2": nil, "foo-3": nil}) # PREFIX-[a-b] is nil
assert_equal_expanded_hash({"foo-[2-3]": 0, "foo-2": nil}, {"foo-2": 0, "foo-3": 0}) # PREFIX-x is nil
assert_equal_expanded_hash({"foo-[2-]": nil, "foo-3": 0}, {"foo-3": 0}) # PREFIX-[a-] is nil
assert_equal_expanded_hash({"foo-[2-]": 0, "foo-3": nil}, {"foo-3": 0}) # PREFIX-x is nil
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment