Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using FluentAssertions;
- using NUnit.Framework;
- using ResponseSerialization;
- using ResponseSerialization.Exceptions;
- namespace ResponseSerializationTests
- {
- [TestFixture]
- public class ResponseSerializer_Should
- {
- public class When_Deserializing
- {
- [SetUp]
- public void Setup() => _serializer = new ResponseSerializer();
- [TestCaseSource(nameof(NullAndEmptyValues))]
- public void Empty_Values_Throws_NullOrEmpty(string input)
- {
- Action act = () => _serializer.Deserialize<object>(input);
- act.Should().Throw<NullOrEmpty>();
- }
- [TestCaseSource(nameof(NotJson))]
- public void NotJson_Throws_NotJson(string input)
- {
- Action act = () => _serializer.Deserialize<object>(input);
- act.Should()
- .Throw<NotJson>()
- .Where(x=>x.Content.Equals(input));
- }
- [Test]
- public void KeyWithoutQuotes_Succeeds()
- {
- var result = _serializer.Deserialize<CorrectModel>(JsonExamples.JsonObject_NonStringKey);
- result.Id.Should().Be("Test");
- }
- [Test]
- public void ModelMismatch_Throws_ModelMismatch()
- {
- Action act = () => _serializer.Deserialize<WrongModel>(JsonExamples.JsonObject_NonStringKey);
- act.Should().Throw<ModelMismatch>().WithMessage("*.WrongModel*");
- }
- [Test]
- public void Array_WhenExpectedSingle_Throws_UnexpectedArray()
- {
- Action act = () => _serializer.Deserialize<CorrectModel>(JsonExamples.JsonObject_Array);
- act.Should().Throw<UnexpectedArray>().WithMessage("*.CorrectModel*");
- }
- [Test]
- public void Single_WhenExpectingArray_Throws_UnexpectedObject()
- {
- Action act = () => _serializer.Deserialize<List<CorrectModel>>(JsonExamples.JsonObject_NonStringKey);
- act.Should().Throw<UnexpectedObject>().WithMessage("*.CorrectModel*");
- }
- [Test]
- public void Malformed_Throws_NotJson()
- {
- Action act = () => _serializer.Deserialize<CorrectModel>(JsonExamples.JsonObject_Malformed);
- act.Should().Throw<NotJson>();
- }
- [Test]
- public void Valid_Json_Deserializes()
- {
- var result = _serializer.Deserialize<CorrectModel>(JsonExamples.JsonObject_NonStringKey);
- result.Id.Should().Be("Test");
- }
- [Test]
- public void List_Deserializes()
- {
- List<CorrectModel> results = _serializer.Deserialize<List<CorrectModel>>(JsonExamples.JsonObject_Array);
- results.Count.Should().Be(4);
- results.First().Id.Should().Be("A");
- }
- static string[] NotJson = {"<html><title>SomeHtmlValue</title></html>", "JustARandomString", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>RandomXML</to></note>"};
- static string[] NullAndEmptyValues = {"", null};
- ResponseSerializer _serializer;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement