Commit ed5fa1cd authored by Jeremy Meng's avatar Jeremy Meng

Simplify Assert.Throws usage.

parent 2ea6ff97
......@@ -74,31 +74,28 @@ public void PingMany(bool preserveOrder)
}
[Test]
// [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"A null key is not valid in this context")]
public void GetWithNullKey()
{
using (var muxer = Create())
{
var db = muxer.GetDatabase();
string key = null;
//db.StringGet(key);
ArgumentException ex = Assert.Throws(typeof(ArgumentException), delegate { db.StringGet(key); }) as ArgumentException;
Assert.That(ex.Message.Equals( @"A null key is not valid in this context"));
}
Assert.Throws<ArgumentException>(
() => db.StringGet(key),
"A null key is not valid in this context");
}
}
[Test]
// [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"A null key is not valid in this context")]
public void SetWithNullKey()
{
using (var muxer = Create())
{
var db = muxer.GetDatabase();
string key = null, value = "abc";
// db.StringSet(key, value);
ArgumentException ex = Assert.Throws(typeof(ArgumentException), delegate { db.StringSet(key, value); }) as ArgumentException;
Assert.That(ex.Message.Equals(@"A null key is not valid in this context"));
Assert.Throws<ArgumentException>(
() => db.StringSet(key, value),
"A null key is not valid in this context");
}
}
......@@ -308,7 +305,6 @@ public void GetWithExpiry(bool exists, bool hasExpiry)
}
}
[Test]
// [ExpectedException(typeof(RedisServerException), ExpectedMessage = @"A null key is not valid in this context")]
public void GetWithExpiryWrongTypeAsync()
{
using (var conn = Create())
......@@ -317,25 +313,26 @@ public void GetWithExpiryWrongTypeAsync()
RedisKey key = Me();
db.KeyDelete(key);
db.SetAdd(key, "abc");
try
{
var async = db.Wait(db.StringGetWithExpiryAsync(key));
}
catch(AggregateException ex)
Assert.Throws<RedisServerException>(() =>
{
//throw ex.InnerExceptions[0];
Assert.That(ex.GetType().Equals(typeof(RedisServerException)));
Assert.That(ex.InnerExceptions[0].Equals(@"A null key is not valid in this context"));
}
Assert.Fail();
try
{
var async = db.Wait(db.StringGetWithExpiryAsync(key));
}
catch (AggregateException ex)
{
throw ex.InnerExceptions[0];
}
Assert.Fail();
},
"A null key is not valid in this context");
}
}
[Test]
// [ExpectedException(typeof(RedisServerException), ExpectedMessage = "WRONGTYPE Operation against a key holding the wrong kind of value")]
public void GetWithExpiryWrongTypeSync()
{
Exception ex = Assert.Throws(typeof(RedisServerException), delegate
Assert.Throws<RedisServerException>(() =>
{
using (var conn = Create())
{
......@@ -346,8 +343,8 @@ public void GetWithExpiryWrongTypeSync()
db.StringGetWithExpiry(key);
Assert.Fail();
}
});
Assert.That(ex.Message.Equals("WRONGTYPE Operation against a key holding the wrong kind of value"));
},
"WRONGTYPE Operation against a key holding the wrong kind of value");
}
#if FEATURE_BOOKSLEEVE
......
......@@ -202,10 +202,10 @@ public void IntentionalWrongServer()
}
[Test]
//[ExpectedException(typeof(RedisCommandException), ExpectedMessage = "Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot")]
public void TransactionWithMultiServerKeys()
{
Exception ex = Assert.Throws(typeof(RedisCommandException), delegate {
Assert.Throws<RedisCommandException>(() =>
{
using (var muxer = Create())
{
// connect
......@@ -254,15 +254,14 @@ public void TransactionWithMultiServerKeys()
//Assert.IsFalse(cluster.Wait(existsX), "x exists");
//Assert.IsFalse(cluster.Wait(existsY), "y exists");
}
});
Assert.That(ex.Message.Equals("Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot"));
},
"Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot");
}
[Test]
//[ExpectedException(typeof(RedisCommandException), ExpectedMessage = "Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot")]
public void TransactionWithSameServerKeys()
{
Exception ex = Assert.Throws(typeof(RedisCommandException), delegate
Assert.Throws<RedisCommandException>(() =>
{
using (var muxer = Create())
{
......@@ -311,8 +310,8 @@ public void TransactionWithSameServerKeys()
//Assert.IsTrue(cluster.Wait(existsX), "x exists");
//Assert.IsTrue(cluster.Wait(existsY), "y exists");
}
});
Assert.That(ex.Message.Equals("Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot"));
},
"Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot");
}
[Test]
......
......@@ -125,13 +125,16 @@ public void ClientName()
}
[Test]
// [ExpectedException(typeof(RedisCommandException), ExpectedMessage = "This operation has been disabled in the command-map and cannot be used: CONFIG")]
public void ReadConfigWithConfigDisabled()
{
using (var muxer = Create(allowAdmin: true, disabledCommands: new[] { "config", "info" }))
{
var conn = GetServer(muxer);
var all = conn.ConfigGet();
Assert.Throws<RedisCommandException>(() =>
{
var all = conn.ConfigGet();
},
"This operation has been disabled in the command-map and cannot be used: CONFIG");
}
}
[Test]
......@@ -161,7 +164,7 @@ public void ReadConfig()
}
[Test]
public async void TestConfigureAsync()
public async System.Threading.Tasks.Task TestConfigureAsync()
{
using(var muxer = Create())
{
......
......@@ -290,10 +290,9 @@ public void KeyPersist()
}
[Test]
//[ExpectedException(typeof(NotSupportedException))]
public void KeyRandom()
{
Assert.Throws(typeof(NotSupportedException), delegate { wrapper.KeyRandom(); });
Assert.Throws<NotSupportedException>(() => wrapper.KeyRandom());
}
[Test]
......
......@@ -24,22 +24,20 @@ public void UnkonwnKeywordHandling_Ignore()
var options = ConfigurationOptions.Parse("ssl2=true", true);
}
[Test]
//, ExpectedException(typeof(ArgumentException), ExpectedMessage = "Keyword 'ssl2' is not supported")]
public void UnkonwnKeywordHandling_ExplicitFail()
{
Exception ex = Assert.Throws(typeof(ArgumentException), delegate {
Assert.Throws<ArgumentException>(() => {
var options = ConfigurationOptions.Parse("ssl2=true", false);
});
Assert.That(ex.Message.Equals("Keyword 'ssl2' is not supported"));
},
"Keyword 'ssl2' is not supported");
}
[Test]
//, ExpectedException(typeof(ArgumentException), ExpectedMessage = "Keyword 'ssl2' is not supported")]
public void UnkonwnKeywordHandling_ImplicitFail()
{
Exception ex = Assert.Throws(typeof(ArgumentException), delegate {
Assert.Throws<ArgumentException>(() => {
var options = ConfigurationOptions.Parse("ssl2=true");
});
Assert.That(ex.Message.Equals("Keyword 'ssl2' is not supported"));
},
"Keyword 'ssl2' is not supported");
}
}
}
......@@ -15,10 +15,9 @@ protected override string GetConfiguration()
}
[Test]
//, ExpectedException(typeof(RedisCommandException), ExpectedMessage = "Command cannot be issued to a slave: FLUSHDB")]
public void CannotFlushSlave()
{
Exception ex = Assert.Throws(typeof(RedisCommandException), delegate {
Assert.Throws<RedisCommandException>(() => {
ConfigurationOptions config = GetMasterSlaveConfig();
using (var conn = ConnectionMultiplexer.Connect(config))
{
......@@ -26,9 +25,8 @@ public void CannotFlushSlave()
var slave = servers.First(x => x.IsSlave);
slave.FlushDatabase();
}
});
Assert.That(ex.Message.Equals("Command cannot be issued to a slave: FLUSHDB"));
},
"Command cannot be issued to a slave: FLUSHDB");
}
[Test]
......
......@@ -68,17 +68,16 @@ public void Connect()
[Test]
[TestCase("wrong")]
[TestCase("")]
//[ExpectedException(typeof(RedisConnectionException), ExpectedMessage = "No connection is available to service this operation: PING")]
public void ConnectWithWrongPassword(string password)
{
Exception ex = Assert.Throws(typeof(RedisConnectionException), delegate {
Assert.Throws<RedisConnectionException>(() => {
SetExpectedAmbientFailureCount(-1);
using (var server = Create(password: password, checkConnect: false))
{
server.GetDatabase().Ping();
}
});
Assert.That(ex.Message.Equals("No connection is available to service this operation: PING"));
},
"No connection is available to service this operation: PING");
}
}
}
......@@ -29,10 +29,9 @@ public void BlankPrefixYieldsSame_String()
}
}
[Test]
//, ExpectedException(typeof(ArgumentNullException))]
public void NullPrefixIsError_Bytes()
{
Assert.Throws(typeof(ArgumentNullException), delegate {
Assert.Throws<ArgumentNullException>(() => {
using (var conn = Create())
{
var raw = conn.GetDatabase(1);
......@@ -41,10 +40,9 @@ public void NullPrefixIsError_Bytes()
});
}
[Test]
//, ExpectedException(typeof(ArgumentNullException))]
public void NullPrefixIsError_String()
{
Assert.Throws(typeof(ArgumentNullException), delegate {
Assert.Throws<ArgumentNullException>(() => {
using (var conn = Create())
{
var raw = conn.GetDatabase(1);
......@@ -54,13 +52,12 @@ public void NullPrefixIsError_String()
}
[Test]
//, ExpectedException(typeof(ArgumentNullException))]
[TestCase("abc")]
[TestCase("")]
[TestCase(null)]
public void NullDatabaseIsError(string prefix)
{
Assert.Throws(typeof(ArgumentNullException), delegate {
Assert.Throws<ArgumentNullException>(() => {
IDatabase raw = null;
var prefixed = raw.WithKeyPrefix(prefix);
});
......
......@@ -259,10 +259,9 @@ public void KeyPersistAsync()
}
[Test]
//[ExpectedException(typeof(NotSupportedException))]
public void KeyRandomAsync()
{
Assert.Throws(typeof(NotSupportedException), delegate {
Assert.Throws<NotSupportedException>(() => {
wrapper.KeyRandomAsync();
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment