Skip to content

Commit 8d926bb

Browse files
author
Joao Fernandes
committed
Remove client-side arguments checks
Also took the chance to prettify the code and cleanup tests. Issue #545
1 parent c543140 commit 8d926bb

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

lib/redis.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,8 +1440,6 @@ def zadd(key, *args) #, options
14401440
xx = options[:xx]
14411441
zadd_options << "XX" if xx
14421442

1443-
raise ArgumentError, "XX and NX options at the same time are not compatible" if nx && xx
1444-
14451443
ch = options[:ch]
14461444
zadd_options << "CH" if ch
14471445

@@ -1451,12 +1449,11 @@ def zadd(key, *args) #, options
14511449

14521450
synchronize do |client|
14531451
if args.size == 1 && args[0].is_a?(Array)
1454-
# Variadic: return integer
1455-
raise ArgumentError, "only one score-element pair can be specified with the :incr option" if incr
1456-
client.call([:zadd, key] + zadd_options + args[0])
1452+
# Variadic: return float if INCR, integer if !INCR
1453+
client.call([:zadd, key] + zadd_options + args[0], &(incr ? _floatify : _identity))
14571454
elsif args.size == 2
14581455
# Single pair: return float if INCR, boolean if !INCR
1459-
client.call([:zadd, key] + zadd_options + [args[0], args[1]], &(incr ? _floatify : _boolify))
1456+
client.call([:zadd, key] + zadd_options + args, &(incr ? _floatify : _boolify))
14601457
else
14611458
raise ArgumentError, "wrong number of arguments"
14621459
end
@@ -2647,6 +2644,12 @@ def _pairify(array)
26472644
array.each_slice(2).to_a
26482645
end
26492646

2647+
def _identity
2648+
lambda { |value|
2649+
value
2650+
}
2651+
end
2652+
26502653
def _subscription(method, channels, block)
26512654
return @client.call([method] + channels) if subscribed?
26522655

test/lint/sorted_sets.rb

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,14 @@ def test_zadd
3737
r.del "foo"
3838

3939
# INCR option
40-
rv = r.zadd("foo", 1, "s1", :incr => true)
41-
assert_equal 1.0, rv
42-
43-
rv = r.zadd("foo", 10, "s1", :incr => true)
44-
assert_equal 11.0, rv
45-
46-
rv = r.zadd("bar", "-inf", "s1", :incr => true)
47-
assert_equal(-Infinity, rv)
48-
49-
rv = r.zadd("bar", "+inf", "s2", :incr => true)
50-
assert_equal(+Infinity, rv)
40+
assert_equal 1.0, r.zadd("foo", 1, "s1", :incr => true)
41+
assert_equal 11.0, r.zadd("foo", 10, "s1", :incr => true)
42+
assert_equal -Infinity, r.zadd("bar", "-inf", "s1", :incr => true)
43+
assert_equal +Infinity, r.zadd("bar", "+inf", "s2", :incr => true)
5144
r.del "foo", "bar"
5245

53-
# Incompatible options combinations
54-
assert_raise(ArgumentError) { r.zadd("foo", 1, "s1", :xx => true, :nx => true) }
46+
# Incompatible options combination
47+
assert_raise(Redis::CommandError) { r.zadd("foo", 1, "s1", :xx => true, :nx => true) }
5548
end
5649
end
5750

@@ -106,10 +99,15 @@ def test_variadic_zadd
10699
r.del "foo"
107100

108101
# INCR option
109-
assert_raise(ArgumentError) { r.zadd("foo", [1, "s1"], :incr => true) }
102+
assert_equal 1.0, r.zadd("foo", [1, "s1"], :incr => true)
103+
assert_equal 11.0, r.zadd("foo", [10, "s1"], :incr => true)
104+
assert_equal -Infinity, r.zadd("bar", ["-inf", "s1"], :incr => true)
105+
assert_equal +Infinity, r.zadd("bar", ["+inf", "s2"], :incr => true)
106+
assert_raise(Redis::CommandError) { r.zadd("foo", [1, "s1", 2, "s2"], :incr => true) }
107+
r.del "foo", "bar"
110108

111-
# Incompatible options combinations
112-
assert_raise(ArgumentError) { r.zadd("foo", [1, "s1"], :xx => true, :nx => true) }
109+
# Incompatible options combination
110+
assert_raise(Redis::CommandError) { r.zadd("foo", [1, "s1"], :xx => true, :nx => true) }
113111
end
114112
end
115113

0 commit comments

Comments
 (0)