Added Exception.Throws overload to take multiple possible messages to match

This way tests can pass in multiple possible string matches which the runtime may use as a message depending on the given platform (e.g.: MS-Runtime vs Mono)
This commit is contained in:
Redth
2014-09-09 09:39:05 -04:00
parent aa6c474999
commit 831e32809e
5 changed files with 77 additions and 34 deletions
@@ -791,10 +791,12 @@ third line", jsonTextReader.Value);
[Test] [Test]
public void NullTextReader() public void NullTextReader()
{ {
ExceptionAssert.Throws<ArgumentNullException>( ExceptionAssert.Throws<ArgumentNullException> (
@"Value cannot be null. () => { new JsonTextReader (null); },
Parameter name: reader", new string[] {
() => { new JsonTextReader(null); }); "Value cannot be null." + Environment.NewLine + "Parameter name: reader",
"Argument cannot be null." + Environment.NewLine + "Parameter name: reader" // Mono
});
} }
[Test] [Test]
@@ -141,8 +141,12 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test] [Test]
public void SerializeDefaultArray() public void SerializeDefaultArray()
{ {
ExceptionAssert.Throws<NullReferenceException>("Object reference not set to an instance of an object.", () => ExceptionAssert.Throws<NullReferenceException>(
JsonConvert.SerializeObject(default(ImmutableArray<int>), Formatting.Indented)); () => JsonConvert.SerializeObject(default(ImmutableArray<int>), Formatting.Indented),
new [] {
"Object reference not set to an instance of an object.",
"Object reference not set to an instance of an object" // mono
});
} }
#endregion #endregion
@@ -2975,8 +2975,11 @@ Path '', line 1, position 1.",
string json = @"[]"; string json = @"[]";
ExceptionAssert.Throws<InvalidCastException>( ExceptionAssert.Throws<InvalidCastException>(
@"Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.", () => { JsonConvert.DeserializeObject<JObject>(json); },
() => { JsonConvert.DeserializeObject<JObject>(json); }); new [] {
"Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.",
"Cannot cast from source type to destination type." // mono
});
} }
[Test] [Test]
@@ -6304,18 +6307,22 @@ To fix this error either change the environment to be fully trusted, change the
public void DeserializeDoubleFromNullString() public void DeserializeDoubleFromNullString()
{ {
ExceptionAssert.Throws<ArgumentNullException>( ExceptionAssert.Throws<ArgumentNullException>(
@"Value cannot be null. () => { JsonConvert.DeserializeObject<double>(null); },
Parameter name: value", new [] {
() => { JsonConvert.DeserializeObject<double>(null); }); "Value cannot be null." + Environment.NewLine + "Parameter name: value",
"Argument cannot be null." + Environment.NewLine + "Parameter name: value" // mono
});
} }
[Test] [Test]
public void DeserializeFromNullString() public void DeserializeFromNullString()
{ {
ExceptionAssert.Throws<ArgumentNullException>( ExceptionAssert.Throws<ArgumentNullException>(
@"Value cannot be null. () => { JsonConvert.DeserializeObject(null); },
Parameter name: value", new [] {
() => { JsonConvert.DeserializeObject(null); }); "Value cannot be null." + Environment.NewLine + "Parameter name: value",
"Argument cannot be null." + Environment.NewLine + "Parameter name: value" // mono
});
} }
[Test] [Test]
@@ -193,5 +193,24 @@ namespace Newtonsoft.Json.Tests
throw new Exception(string.Format("Exception of type {0} expected; got exception of type {1}.", typeof(TException).Name, ex.GetType().Name), ex); throw new Exception(string.Format("Exception of type {0} expected; got exception of type {1}.", typeof(TException).Name, ex.GetType().Name), ex);
} }
} }
public static void Throws<TException>(Action action, params string[] possibleMessages)
where TException : Exception
{
try
{
action();
Assert.Fail("Exception of type {0} expected; got none exception", typeof(TException).Name);
}
catch (TException ex)
{
Assert.Contains (ex.Message, possibleMessages, "Unexpected exception message." + Environment.NewLine + "Expected One of: " + string.Join (Environment.NewLine, possibleMessages) + Environment.NewLine + "Got: " + ex.Message + Environment.NewLine + Environment.NewLine + ex);
}
catch (Exception ex)
{
throw new Exception(string.Format("Exception of type {0} expected; got exception of type {1}.", typeof(TException).Name, ex.GetType().Name), ex);
}
}
} }
} }
@@ -107,13 +107,15 @@ namespace Newtonsoft.Json.Tests.Utilities
[Test] [Test]
public void DefaultConstructor_Abstract() public void DefaultConstructor_Abstract()
{ {
ExceptionAssert.Throws<Exception>("Cannot create an abstract class.", ExceptionAssert.Throws<Exception>(
() => () => {
{
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Type)); Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Type));
create(); create();
}); }, new [] {
"Cannot create an abstract class.",
"Cannot create an abstract class 'System.Type'." // mono
});
} }
[Test] [Test]
@@ -277,24 +279,26 @@ namespace Newtonsoft.Json.Tests.Utilities
[Test] [Test]
public void CreateGetWithBadObjectTarget() public void CreateGetWithBadObjectTarget()
{ {
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.", ExceptionAssert.Throws<InvalidCastException>(
() => () => {
{ Person p = new Person();
Person p = new Person(); p.Name = "Hi";
p.Name = "Hi";
Func<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name")); Func<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
setter(p); setter(p);
}); },
new [] {
"Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
"Cannot cast from source type to destination type." // mono
});
} }
[Test] [Test]
public void CreateSetWithBadObjectTarget() public void CreateSetWithBadObjectTarget()
{ {
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.", ExceptionAssert.Throws<InvalidCastException>(
() => () => {
{
Person p = new Person(); Person p = new Person();
Movie m = new Movie(); Movie m = new Movie();
@@ -307,21 +311,28 @@ namespace Newtonsoft.Json.Tests.Utilities
setter(p, "Hi"); setter(p, "Hi");
Assert.AreEqual(p.Name, "Hi"); Assert.AreEqual(p.Name, "Hi");
}); },
new [] {
"Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
"Cannot cast from source type to destination type." // mono
});
} }
[Test] [Test]
public void CreateSetWithBadObjectValue() public void CreateSetWithBadObjectValue()
{ {
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.", ExceptionAssert.Throws<InvalidCastException>(
() => () => {
{
Movie m = new Movie(); Movie m = new Movie();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name")); Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, new Version("1.1.1.1")); setter(m, new Version("1.1.1.1"));
}); }, new [] {
"Unable to cast object of type 'System.Version' to type 'System.String'.",
"Cannot cast from source type to destination type." //mono
});
} }
[Test] [Test]