/// <summary> /// Runs a single JsonReaderTestCaseDescriptor test. /// </summary> /// <param name="testCase">The test case descriptor to run.</param> /// <param name="testConfiguration">The test configuration to use.</param> /// <param name="jsonValueComparer">The comparer to use to compare JSON OMs.</param> /// <param name="assert">The assertion handler.</param> public static void ReadAndVerifyJson( JsonReaderTestCaseDescriptor testCase, JsonReaderTestConfiguration testConfiguration, IJsonValueComparer jsonValueComparer, AssertionHandler assert, IExceptionVerifier exceptionVerifier) { TextReader testReader = new TestTextReader(new StringReader(testCase.JsonText)) { FailOnPeek = true, FailOnSingleCharacterRead = true, ReadSizesEnumerator = testConfiguration.ReadSizes.EndLessLoop() }; JsonValue actualJsonResult = null; assert.ExpectedException(() => { JsonReader jsonReader = testConfiguration.JsonReaderCreatorFunc(testReader, assert); actualJsonResult = ReadJson(jsonReader, assert); }, testCase.ExpectedException, exceptionVerifier); if (testCase.ExpectedException == null) { if (testCase.FragmentExtractor != null) { actualJsonResult = testCase.FragmentExtractor(actualJsonResult); } jsonValueComparer = new JsonValueComparer(); jsonValueComparer.Compare(testCase.ExpectedJson, actualJsonResult); } }
/// <summary> /// Runs a single JsonReaderTestCaseDescriptor test. /// </summary> /// <param name="testCase">The test case descriptor to run.</param> /// <param name="testConfiguration">The test configuration to use.</param> /// <param name="jsonValueComparer">The comparer to use to compare JSON OMs.</param> /// <param name="assert">The assertion handler.</param> public static void ReadAndVerifyJson( JsonReaderTestCaseDescriptor testCase, JsonReaderTestConfiguration testConfiguration, IJsonValueComparer jsonValueComparer, AssertionHandler assert, IExceptionVerifier exceptionVerifier) { TextReader testReader = new TestTextReader(new StringReader(testCase.JsonText)) { FailOnPeek = true, FailOnSingleCharacterRead = true, ReadSizesEnumerator = testConfiguration.ReadSizes.EndLessLoop() }; JsonValue actualJsonResult = null; assert.ExpectedException(() => { JsonReader jsonReader = testConfiguration.JsonReaderCreatorFunc(testReader, assert); actualJsonResult = ReadJson(jsonReader, assert); }, testCase.ExpectedException, exceptionVerifier); if (testCase.ExpectedException == null) { if (testCase.FragmentExtractor != null) { actualJsonResult = testCase.FragmentExtractor(actualJsonResult); } jsonValueComparer = new JsonValueComparer(); jsonValueComparer.Compare(testCase.ExpectedJson, actualJsonResult); } }
private static void RunHeaderTest( Func <IEnumerable <KeyValuePair <string, string> > > getHeadersFunc, bool writing, Func <string, string> getHeaderFunc, Action <string, string> setHeaderAction, AssertionHandler assert, IExceptionVerifier exceptionVerifier) { assert.IsNotNull(getHeadersFunc(), "Non-null headers expected."); assert.AreEqual(0, getHeadersFunc().Count(), "Empty header collection expected."); assert.IsNull(getHeaderFunc("a"), "Unexpectedly found header."); ExpectedException expectedException = writing ? null : ODataExpectedExceptions.ODataException("ODataMessage_MustNotModifyMessage"); TestExceptionUtils.ExpectedException( assert, () => { setHeaderAction("a", "b"); assert.AreEqual(1, getHeadersFunc().Count(), "One header expected."); assert.AreEqual("b", getHeaderFunc("a"), "Header not found or invalid header value."); List <KeyValuePair <string, string> > expectedHeaders = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("a", "b") }; VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("a", "c"); assert.AreEqual(1, getHeadersFunc().Count(), "One header expected."); assert.AreEqual("c", getHeaderFunc("a"), "Header not found or invalid header value."); expectedHeaders[0] = new KeyValuePair <string, string>("a", "c"); VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("d", "e"); assert.AreEqual(2, getHeadersFunc().Count(), "Two headers expected."); assert.AreEqual("c", getHeaderFunc("a"), "Header not found or invalid header value."); assert.AreEqual("e", getHeaderFunc("d"), "Header not found or invalid header value."); expectedHeaders.Add(new KeyValuePair <string, string>("d", "e")); VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("d", null); setHeaderAction("a", null); assert.AreEqual(0, getHeadersFunc().Count(), "Empty header collection expected."); assert.IsNull(getHeaderFunc("a"), "Unexpectedly found header."); }, expectedException, exceptionVerifier); }
private static void RunHeaderTest( Func<IEnumerable<KeyValuePair<string, string>>> getHeadersFunc, bool writing, Func<string, string> getHeaderFunc, Action<string, string> setHeaderAction, AssertionHandler assert, IExceptionVerifier exceptionVerifier) { assert.IsNotNull(getHeadersFunc(), "Non-null headers expected."); assert.AreEqual(0, getHeadersFunc().Count(), "Empty header collection exptected."); assert.IsNull(getHeaderFunc("a"), "Unexpectedly found header."); ExpectedException expectedException = writing ? null : ODataExpectedExceptions.ODataException("ODataMessage_MustNotModifyMessage"); TestExceptionUtils.ExpectedException( assert, () => { setHeaderAction("a", "b"); assert.AreEqual(1, getHeadersFunc().Count(), "One header expected."); assert.AreEqual("b", getHeaderFunc("a"), "Header not found or invalid header value."); List<KeyValuePair<string, string>> expectedHeaders = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("a", "b") }; VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("a", "c"); assert.AreEqual(1, getHeadersFunc().Count(), "One header expected."); assert.AreEqual("c", getHeaderFunc("a"), "Header not found or invalid header value."); expectedHeaders[0] = new KeyValuePair<string, string>("a", "c"); VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("d", "e"); assert.AreEqual(2, getHeadersFunc().Count(), "Two headers expected."); assert.AreEqual("c", getHeaderFunc("a"), "Header not found or invalid header value."); assert.AreEqual("e", getHeaderFunc("d"), "Header not found or invalid header value."); expectedHeaders.Add(new KeyValuePair<string, string>("d", "e")); VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("d", null); setHeaderAction("a", null); assert.AreEqual(0, getHeadersFunc().Count(), "Empty header collection expected."); assert.IsNull(getHeaderFunc("a"), "Unexpectedly found header."); }, expectedException, exceptionVerifier); }
/// <summary> /// Asserts that the given action throws the specified exception type. /// </summary> /// <param name="action">Action to try.</param> /// <param name="expectedException">The expected exception.</param> /// <param name="exceptionVerifier">The exception verifier.</param> public static void ExpectedException(this AssertionHandler assert, Action action, ExpectedException expectedException, IExceptionVerifier exceptionVerifier) { ExceptionUtilities.CheckArgumentNotNull(assert, "assert"); ExceptionUtilities.CheckArgumentNotNull(action, "action"); ExceptionUtilities.CheckArgumentNotNull(exceptionVerifier, "exceptionVerifier"); Exception exception = RunCatching(action); if (exception == null && expectedException == null) { return; } else if (exception == null) { assert.IsNotNull(exception, "Expected exception of type '{0}' with message resource ID '{1}' but none was thrown.", expectedException.ExpectedExceptionType.ToString(), expectedException.ExpectedMessage == null ? "<null>" : expectedException.ExpectedMessage.ResourceIdentifier); } else if (expectedException == null) { assert.IsNotNull(expectedException, "Did not expect an exception but an exception of type '{0}' with message '{1}' was thrown.", exception.GetType().ToString(), exception.Message); } exception = TestExceptionUtils.UnwrapAggregateException(exception, assert); exceptionVerifier.VerifyExceptionResult(expectedException, exception); }