Exemplo n.º 1
0
        public static void Equal(Error expected, Error actual)
        {
            if (expected == null)
            {
                Assert.Null(actual);
                return;
            }
            Assert.NotNull(actual);

            // Id
            Assert.Equal(expected.Id, actual.Id);

            // Status
            Assert.Equal(expected.Status, actual.Status);

            // Code
            Assert.Equal(expected.Code, actual.Code);

            // Title
            Assert.Equal(expected.Title, actual.Title);

            // Detail
            Assert.Equal(expected.Detail, actual.Detail);

            // Source
            ClrObjectAssert.Equal(expected.Source, actual.Source);

            // Links
            LinksAssert.Equal(expected.Links, actual.Links);

            // Meta
            MetaAssert.Equal(expected.Meta, actual.Meta);
        }
Exemplo n.º 2
0
        public static void Equal(ResourceIdentifier expected, JToken actualJToken)
        {
            // Handle when 'expected' is null.
            if (expected == null)
            {
                ClrObjectAssert.IsNull(actualJToken);
                return;
            }

            // Handle when 'expected' is not null.
            Assert.NotNull(actualJToken);

            var actualJTokenType = actualJToken.Type;

            Assert.Equal(JTokenType.Object, actualJTokenType);

            var actualJObject = (JObject)actualJToken;

            Assert.Equal(expected.Type, (string)actualJObject.SelectToken(Keywords.Type));
            Assert.Equal(expected.Id, (string)actualJObject.SelectToken(Keywords.Id));

            var actualMetaJToken = actualJObject.SelectToken(Keywords.Meta);

            ClrObjectAssert.Equal(expected.Meta, actualMetaJToken);
        }
Exemplo n.º 3
0
        public static void Equal(ApiProperty expected, ApiProperty actual)
        {
            if (expected == null)
            {
                Assert.Null(actual);
                return;
            }
            Assert.NotNull(actual);

            Assert.Equal(expected.Name, actual.Name);
            ClrObjectAssert.Equal(expected.ValueAsObject(), actual.ValueAsObject());
        }
        public static void Equal(object expected, JObject actualJObject)
        {
            // Handle when 'expected' is null.
            if (expected == null)
            {
                ClrObjectAssert.IsNull(actualJObject);
                return;
            }

            // Handle when 'expected' is not null.
            Assert.NotNull(actualJObject);

            var expectedTypeInfo = expected.GetType().GetTypeInfo();

            // .. Handle special case when expected type is 'Resource'
            if (expectedTypeInfo == ResourceTypeInfo)
            {
                var expectedResource = (Resource)expected;
                ResourceAssert.Equal(expectedResource, actualJObject);
            }
            // .. Handle special case when expected type is 'ResourceIdentifier'
            else if (expectedTypeInfo == ResourceIdentifierTypeInfo)
            {
                var expectedResourceIdentifier = (ResourceIdentifier)expected;
                ResourceIdentifierAssert.Equal(expectedResourceIdentifier, actualJObject);
            }
            // .. Handle special case when expected type is 'JObject'
            else if (expectedTypeInfo == JObjectTypeInfo)
            {
                var expectedJObject = (JObject)expected;
                Assert.Equal(expectedJObject, actualJObject);
            }
            // .. Handle normal case when expected type is an object that is
            // .. not 'Resource' or 'ResourceIdentifier'.
            else
            {
                var expectedProperties = expectedTypeInfo.GetProperties();
                foreach (var expectedProperty in expectedProperties)
                {
                    // ReSharper disable once UnusedVariable
                    var expectedPropertyName  = GetDotNetPropertyName(expectedProperty);
                    var expectedPropertyValue = expectedProperty.GetValue(expected, null);

                    var actualPropertyName = GetJsonApiPropertyName(expectedProperty);
                    var actualJToken       = actualJObject.SelectToken(actualPropertyName);

                    ClrObjectAssert.Equal(expectedPropertyValue, actualJToken);
                }
            }
        }
Exemplo n.º 5
0
        public static void Equal(Error expected, JToken actualJToken)
        {
            // Handle when 'expected' is null.
            if (expected == null)
            {
                ClrObjectAssert.IsNull(actualJToken);
                return;
            }

            // Handle when 'expected' is not null.
            Assert.NotNull(actualJToken);

            var actualJTokenType = actualJToken.Type;

            Assert.Equal(JTokenType.Object, actualJTokenType);

            var actualJObject = (JObject)actualJToken;

            // Id
            Assert.Equal(expected.Id, (string)actualJObject.SelectToken(Keywords.Id));

            // Status
            Assert.Equal(expected.Status, (string)actualJObject.SelectToken(Keywords.Status));

            // Code
            Assert.Equal(expected.Code, (string)actualJObject.SelectToken(Keywords.Code));

            // Title
            Assert.Equal(expected.Title, (string)actualJObject.SelectToken(Keywords.Title));

            // Detail
            Assert.Equal(expected.Detail, (string)actualJObject.SelectToken(Keywords.Detail));

            // Source
            var actualSourceJToken = actualJObject.SelectToken(Keywords.Source);

            ClrObjectAssert.Equal(expected.Source, actualSourceJToken);

            // Links
            var actualLinksJToken = actualJObject.SelectToken(Keywords.Links);

            LinksAssert.Equal(expected.Links, actualLinksJToken);

            // Meta
            var actualMetaJToken = actualJObject.SelectToken(Keywords.Meta);

            MetaAssert.Equal(expected.Meta, actualMetaJToken);
        }
        public static void Equal(IReadOnlyList <object> expectedCollection, IReadOnlyList <object> actualCollection)
        {
            if (expectedCollection == null)
            {
                Assert.Null(actualCollection);
                return;
            }

            Assert.Equal(expectedCollection.Count, actualCollection.Count);

            var count = expectedCollection.Count;

            for (var index = 0; index < count; ++index)
            {
                var expected = expectedCollection[index];
                var actual   = actualCollection[index];

                ClrObjectAssert.Equal(expected, actual);
            }
        }
Exemplo n.º 7
0
        // PUBLIC METHODS ///////////////////////////////////////////////////
        #region Assert Methods
        public static void Equal(ApiProperty expected, JToken actualJToken)
        {
            // Handle when 'expected' is null.
            if (expected == null)
            {
                ClrObjectAssert.IsNull(actualJToken);
                return;
            }

            // Handle when 'expected' is not null.
            Assert.NotNull(actualJToken);

            var actualJTokenType = actualJToken.Type;

            Assert.Equal(JTokenType.Property, actualJTokenType);

            var actualJProperty = (JProperty)actualJToken;

            Assert.Equal(expected.Name, actualJProperty.Name);
            ClrObjectAssert.Equal(expected.ValueAsObject(), actualJProperty.Value);
        }
        public static void Equal(object expected, object actual)
        {
            // Handle when 'expected' is null.
            if (expected == null)
            {
                if (actual == null)
                {
                    return;
                }

                var actualType1 = actual.GetType();
                if (actualType1.IsSubclassOf(typeof(JToken)))
                {
                    var actualJToken = (JToken)actual;
                    ClrObjectAssert.IsNull(actualJToken);
                    return;
                }
            }

            // Handle when 'expected' is not null.
            Assert.NotNull(actual);

            var actualType2 = actual.GetType();

            if (actualType2.IsSubclassOf(typeof(JToken)))
            {
                var actualJToken = (JToken)actual;
                ClrObjectAssert.Equal(expected, actualJToken);
            }
            else
            {
                // Use FluentAssertions to compare for attribute equality
                // instead of reference equality in Assert.Equal(expectetd, actual)
                actual.ShouldBeEquivalentTo(expected);
            }
        }
Exemplo n.º 9
0
        public static void Equal(Link expected, JToken actualJToken)
        {
            // Handle when 'expected' is null.
            if (expected == null)
            {
                ClrObjectAssert.IsNull(actualJToken);
                return;
            }

            // Handle when 'expected' is not null.
            var expectedHRef = expected.HRef;
            var expectedMeta = expected.Meta;

            Assert.NotNull(actualJToken);

            var actualJTokenType = actualJToken.Type;

            switch (actualJTokenType)
            {
            case JTokenType.String:
            {
                var actualHRef = (string)actualJToken;
                Assert.Equal(expectedHRef, actualHRef);

                Assert.Null(expectedMeta);
            }
            break;

            case JTokenType.Object:
            {
                var actualJObject = (JObject)actualJToken;

                // HRef String
                var actualHRefJToken = actualJObject.SelectToken(Keywords.HRef);
                if (expectedHRef == null)
                {
                    if (actualHRefJToken == null)
                    {
                        return;
                    }

                    var actualHRefJTokenType = actualHRefJToken.Type;
                    Assert.Equal(JTokenType.Null, actualHRefJTokenType);
                }
                else
                {
                    Assert.Equal(JTokenType.String, actualHRefJToken.Type);

                    var actualHRef = (string)actualHRefJToken;
                    Assert.Equal(expectedHRef, actualHRef);
                }

                // Meta Object
                var actualMetaJToken = actualJObject.SelectToken(Keywords.Meta);
                ClrObjectAssert.Equal(expectedMeta, actualMetaJToken);
            }
            break;

            case JTokenType.None:
            case JTokenType.Null:
            {
                Assert.Null(expectedHRef);
                Assert.Null(expectedMeta);
            }
            break;

            default:
                Assert.True(false, String.Format("Invalid JToken [type={0}] for link.", actualJTokenType));
                return;
            }
        }
Exemplo n.º 10
0
        // PUBLIC METHODS ///////////////////////////////////////////////////
        #region Assert Methods
        public static void Equal(object expected, JToken actualJToken)
        {
            // Handle when 'expected' is null.
            if (expected == null)
            {
                ClrObjectAssert.IsNull(actualJToken);
                return;
            }

            // Handle when 'expected' is not null.
            Assert.NotNull(actualJToken);

            var actualJTokenType = actualJToken.Type;

            switch (actualJTokenType)
            {
            case JTokenType.Array:
            {
                var expectedArray = ((IEnumerable <object>)expected).ToList();
                var actualJArray  = (JArray)actualJToken;

                Assert.Equal(expectedArray.Count, actualJArray.Count);
                var count = expectedArray.Count;
                for (var i = 0; i < count; ++i)
                {
                    var expectedItem     = expectedArray[i];
                    var actualJTokenItem = actualJArray[i];
                    ClrObjectAssert.Equal(expectedItem, actualJTokenItem);
                }
            }
            break;

            case JTokenType.Object:
            {
                var actualJObject = (JObject)actualJToken;
                ClrObjectAssert.Equal(expected, actualJObject);
            }
            break;

            case JTokenType.Integer:
            {
                var expectedInteger = Convert.ToInt64(expected);
                var actualInteger   = (long)actualJToken;
                Assert.Equal(expectedInteger, actualInteger);
            }
            break;

            case JTokenType.Float:
            {
                var expectedFloat = Convert.ToDecimal(expected);
                var actualFloat   = (decimal)actualJToken;
                Assert.Equal(expectedFloat, actualFloat);
            }
            break;

            case JTokenType.String:
            {
                // Special case if expected is or derives from JToken.
                var expectedType = expected.GetType();
                if (typeof(JToken).IsAssignableFrom(expectedType))
                {
                    Assert.Equal(expected, actualJToken);
                    return;
                }

                var actualString = (string)actualJToken;
                var actual       = TypeConverter.Convert(actualString, expectedType);
                Assert.Equal(expected, actual);
            }
            break;

            case JTokenType.Boolean:
            {
                var expectedBool = Convert.ToBoolean(expected);
                var actualBool   = (bool)actualJToken;
                Assert.Equal(expectedBool, actualBool);
            }
            break;

            case JTokenType.Date:
            {
                var expectedType = expected.GetType();
                if (expectedType == typeof(DateTime))
                {
                    var expectedDateTime       = (DateTime)expected;
                    var jsonSerializerSettings = new JsonSerializerSettings
                    {
                        DateParseHandling = DateParseHandling.DateTime
                    };
                    var jsonSerializer = JsonSerializer.Create(jsonSerializerSettings);
                    var actualDateTime = actualJToken.ToObject <DateTime>(jsonSerializer);
                    Assert.Equal(expectedDateTime, actualDateTime);
                }
                else if (expectedType == typeof(DateTimeOffset))
                {
                    var expectedDateTimeOffset = (DateTimeOffset)expected;
                    var jsonSerializerSettings = new JsonSerializerSettings
                    {
                        DateParseHandling = DateParseHandling.DateTimeOffset
                    };
                    var jsonSerializer       = JsonSerializer.Create(jsonSerializerSettings);
                    var actualDateTimeOffset = actualJToken.ToObject <DateTimeOffset>(jsonSerializer);
                    Assert.Equal(expectedDateTimeOffset, actualDateTimeOffset);
                }
                else
                {
                    Assert.True(false, "Expected date type is not DateTime or DateTimeOffset.");
                }
            }
            break;

            default:
                Assert.True(false, String.Format("Unknown actual JTokenType [value={0}] in expected object to actual JToken assert method.", actualJTokenType));
                break;
            }
        }
Exemplo n.º 11
0
        public static void Equal(Document expected, Document actual)
        {
            if (expected == null)
            {
                Assert.Null(actual);
                return;
            }
            Assert.NotNull(actual);

            // JsonApiVersion
            JsonApiVersionAssert.Equal(expected.JsonApiVersion, actual.JsonApiVersion);

            // Meta
            MetaAssert.Equal(expected.Meta, actual.Meta);

            // Links
            LinksAssert.Equal(expected.Links, actual.Links);

            // Data

            // Document types can be the following:
            // 1. Document
            // 2. EmptyDocument
            // 3. ErrorsDocument
            // 4. NullDocument
            // 5. ResourceDocument
            // 6. ResourceIdentifierDocument
            // 7. ResourceCollectionDocument
            // 8. ResourceIdentifierCollectionDocument
            var expectedDocumentTypeInfo = expected.GetType().GetTypeInfo();

            if (expectedDocumentTypeInfo == DocumentTypeInfo)
            {
                // NOOP
            }
            else if (expectedDocumentTypeInfo == EmptyDocumentTypeInfo)
            {
                var expectedData = expected.GetData();
                var actualData   = actual.GetData();

                ClrObjectAssert.Equal(expectedData, actualData);
            }
            else if (expectedDocumentTypeInfo == ErrorsDocumentTypeInfo)
            {
                var expectedErrors = expected.GetErrors();
                var actualErrors   = actual.GetErrors();

                ErrorAssert.Equal(expectedErrors, actualErrors);
            }
            else if (expectedDocumentTypeInfo == NullDocumentTypeInfo)
            {
                var expectedData = expected.GetData();
                var actualData   = actual.GetData();

                ClrObjectAssert.Equal(expectedData, actualData);
            }
            else if (expectedDocumentTypeInfo == ResourceDocumentTypeInfo)
            {
                var expectedResourceDocument = (ResourceDocument)expected;
                var expectedResource         = expectedResourceDocument.Data;
                if (expectedResource == null)
                {
                    Assert.True(actual.IsNullDocument() || (actual.IsResourceDocument() && actual.IsDataNullOrEmpty()));
                    return;
                }

                var actualResourceDocument = (ResourceDocument)actual;
                var actualResource         = actualResourceDocument.Data;

                ResourceAssert.Equal(expectedResource, actualResource);

                // Included
                var expectedIncluded = expected.GetIncludedResources();
                var actualIncluded   = actual.GetIncludedResources();
                ResourceAssert.Equal(expectedIncluded, actualIncluded);
            }
            else if (expectedDocumentTypeInfo == ResourceIdentifierDocumentTypeInfo)
            {
                var expectedResourceIdentifierDocument = (ResourceIdentifierDocument)expected;
                var expectedResourceIdentifier         = expectedResourceIdentifierDocument.Data;
                if (expectedResourceIdentifier == null)
                {
                    Assert.True(actual.IsNullDocument() || (actual.IsResourceIdentifierDocument() && actual.IsDataNullOrEmpty()));
                    return;
                }

                var actualResourceIdentifierDocument = (ResourceIdentifierDocument)actual;
                var actualResourceIdentifier         = actualResourceIdentifierDocument.Data;

                ResourceIdentifierAssert.Equal(expectedResourceIdentifier, actualResourceIdentifier);
            }
            else if (expectedDocumentTypeInfo == ResourceCollectionDocumentTypeInfo)
            {
                var expectedResourceCollectionDocument = (ResourceCollectionDocument)expected;
                var expectedResourceCollection         = expectedResourceCollectionDocument.Data;
                Assert.NotNull(expectedResourceCollection);

                if (!expectedResourceCollection.Any())
                {
                    Assert.True(actual.IsEmptyDocument() || (actual.IsResourceCollectionDocument() && actual.IsDataNullOrEmpty()));
                    return;
                }

                var actualResourceCollectionDocument = (ResourceCollectionDocument)actual;
                var actualResourceCollection         = actualResourceCollectionDocument.Data;

                ResourceAssert.Equal(expectedResourceCollection, actualResourceCollection);

                // Included
                var expectedIncludedResources = expected.GetIncludedResources();
                var actualIncludedResources   = actual.GetIncludedResources();
                ResourceAssert.Equal(expectedIncludedResources, actualIncludedResources);
            }
            else if (expectedDocumentTypeInfo == ResourceIdentifierCollectionDocumentTypeInfo)
            {
                var expectedResourceIdentifierCollectionDocument = (ResourceIdentifierCollectionDocument)expected;
                var expectedResourceIdentifierCollection         = expectedResourceIdentifierCollectionDocument.Data;
                Assert.NotNull(expectedResourceIdentifierCollection);

                if (!expectedResourceIdentifierCollection.Any())
                {
                    Assert.True(actual.IsEmptyDocument() || (actual.IsResourceIdentifierCollectionDocument() && actual.IsDataNullOrEmpty()));
                    return;
                }

                var actualResourceIdentifierCollectionDocument = (ResourceIdentifierCollectionDocument)actual;
                var actualResourceIdentifierCollection         = actualResourceIdentifierCollectionDocument.Data;

                ResourceIdentifierAssert.Equal(expectedResourceIdentifierCollection, actualResourceIdentifierCollection);
            }
            else
            {
                Assert.True(false, String.Format("Unknown document type={0}", expectedDocumentTypeInfo));
            }
        }