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]
public void NullTextReader()
{
ExceptionAssert.Throws<ArgumentNullException>(
@"Value cannot be null.
Parameter name: reader",
() => { new JsonTextReader(null); });
ExceptionAssert.Throws<ArgumentNullException> (
() => { new JsonTextReader (null); },
new string[] {
"Value cannot be null." + Environment.NewLine + "Parameter name: reader",
"Argument cannot be null." + Environment.NewLine + "Parameter name: reader" // Mono
});
}
[Test]
@@ -141,8 +141,12 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeDefaultArray()
{
ExceptionAssert.Throws<NullReferenceException>("Object reference not set to an instance of an object.", () =>
JsonConvert.SerializeObject(default(ImmutableArray<int>), Formatting.Indented));
ExceptionAssert.Throws<NullReferenceException>(
() => 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
@@ -2975,8 +2975,11 @@ Path '', line 1, position 1.",
string json = @"[]";
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]
@@ -6304,18 +6307,22 @@ To fix this error either change the environment to be fully trusted, change the
public void DeserializeDoubleFromNullString()
{
ExceptionAssert.Throws<ArgumentNullException>(
@"Value cannot be null.
Parameter name: value",
() => { JsonConvert.DeserializeObject<double>(null); });
() => { JsonConvert.DeserializeObject<double>(null); },
new [] {
"Value cannot be null." + Environment.NewLine + "Parameter name: value",
"Argument cannot be null." + Environment.NewLine + "Parameter name: value" // mono
});
}
[Test]
public void DeserializeFromNullString()
{
ExceptionAssert.Throws<ArgumentNullException>(
@"Value cannot be null.
Parameter name: value",
() => { JsonConvert.DeserializeObject(null); });
() => { JsonConvert.DeserializeObject(null); },
new [] {
"Value cannot be null." + Environment.NewLine + "Parameter name: value",
"Argument cannot be null." + Environment.NewLine + "Parameter name: value" // mono
});
}
[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);
}
}
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]
public void DefaultConstructor_Abstract()
{
ExceptionAssert.Throws<Exception>("Cannot create an abstract class.",
() =>
{
ExceptionAssert.Throws<Exception>(
() => {
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Type));
create();
});
}, new [] {
"Cannot create an abstract class.",
"Cannot create an abstract class 'System.Type'." // mono
});
}
[Test]
@@ -277,24 +279,26 @@ namespace Newtonsoft.Json.Tests.Utilities
[Test]
public void CreateGetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
p.Name = "Hi";
ExceptionAssert.Throws<InvalidCastException>(
() => {
Person p = new Person();
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]
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();
Movie m = new Movie();
@@ -307,21 +311,28 @@ namespace Newtonsoft.Json.Tests.Utilities
setter(p, "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]
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();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
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]