public void SirenLink_Equality_DifferentRel_ShouldNotBeEqual()
        {
            ISirenLink link  = TestHelpers.GetLink();
            ISirenLink other = TestHelpers.GetLink("different-rel");

            TestHelpers.BidirectionalEquality(link, other, false);
        }
        public void SirenLink_Equality_SameLink_ShouldBeEqual()
        {
            ISirenLink link  = TestHelpers.GetLink();
            ISirenLink other = TestHelpers.GetLink();

            TestHelpers.BidirectionalEquality(link, other, true);
        }
Example #3
0
        public override bool Equals(object obj)
        {
            ISirenLink link  = obj as ISirenLink;
            ISirenLink @this = this;

            return(link != null && @this.Equals(link));
        }
Example #4
0
 public static bool Matches(ISirenLink expected, ISirenLink actual, out string message)
 {
     return(Matches(expected.Title, actual.Title, out message) &&
            Matches(expected.Type, actual.Type, out message) &&
            Matches(expected.Href, actual.Href, out message) &&
            Matches(expected.Rel, actual.Rel, out message) &&
            Matches(expected.Class, actual.Class, out message));
 }
 public static bool TryGetLinkByRel(
     this ISirenEntity @this,
     string rel,
     out ISirenLink link
     )
 {
     link = @this.LinksByRel(rel).FirstOrDefault();
     return(link != default(ISirenLink));
 }
 public static bool TryGetLinkByClass(
     this ISirenEntity @this,
     string @class,
     out ISirenLink link
     )
 {
     link = @this.LinksByClass(@class).FirstOrDefault();
     return(link != default(ISirenLink));
 }
 public static bool TryGetLinkByType(
     this ISirenEntity @this,
     string type,
     out ISirenLink link
     )
 {
     link = @this.LinksByType(type).FirstOrDefault();
     return(link != default(ISirenLink));
 }
        public SirenEntityBuilder AddLink(ISirenLink link)
        {
            if (m_links == null)
            {
                m_links = new List <ISirenLink>();
            }

            m_links.Add(link);
            return(this);
        }
        public void SirenLink_Equality_MissingAttributes_ShouldNotBeEqual()
        {
            ISirenLink link  = TestHelpers.GetLink();
            ISirenLink other = new SirenLink(
                rel: new [] { "foo" },
                href: new Uri("http://example.com")
                );

            TestHelpers.BidirectionalEquality(link, other, false);
        }
        public void MatchingHelpers_MatchesSirenLinkArraysCorrectly()
        {
            string     message;
            ISirenLink link = TestHelpers.GetLink();

            IEnumerable <ISirenLink> expected = null;
            IEnumerable <ISirenLink> actual   = null;

            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            actual = new[] { link };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenLink(
                                   rel: null,
                                   href: null
                                   ) };
            actual = new[] { link };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenLink(
                                   rel: new string[] {},
                                   href: null
                                   ) };
            actual = new[] { link };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenLink(
                                   rel: link.Rel,
                                   href: link.Href
                                   ) };
            actual = new[] { link };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenLink(
                                   rel: link.Rel,
                                   href: link.Href,
                                   @class: link.Class,
                                   title: link.Title,
                                   type: link.Type
                                   ) };
            actual = new[] { link };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenLink(
                                   rel: new [] { "different-rel" },
                                   href: link.Href,
                                   @class: link.Class,
                                   title: link.Title,
                                   type: link.Type
                                   ) };
            actual = new[] { link };
            Assert.IsFalse(SirenMatchers.Matches(expected, actual, out message));
            Assert.AreEqual(message, "Expected [different-rel], but was [foo]");
        }
Example #11
0
        int IComparable <ISirenLink> .CompareTo(ISirenLink other)
        {
            if (other == null)
            {
                return(1);
            }

            string first  = string.Join(",", m_rel) + m_href;
            string second = string.Join(",", other.Rel) + other.Href;

            return(string.CompareOrdinal(first, second));
        }
        public void SirenLink_DeserializesCorrectly()
        {
            ISirenLink sirenLink = TestHelpers.GetLink();

            string serialized = JsonConvert.SerializeObject(sirenLink);

            ISirenLink link = JsonConvert.DeserializeObject <SirenLink>(serialized);

            Assert.Contains("foo", link.Rel);
            Assert.AreEqual("http://example.com/", link.Href.ToString());
            Assert.Contains("bar", link.Class);
            Assert.AreEqual("Link title", link.Title);
            Assert.AreEqual("text/html", link.Type);
        }
Example #13
0
        bool IEquatable <ISirenLink> .Equals(ISirenLink other)
        {
            if (other == null)
            {
                return(false);
            }

            bool rel    = m_rel.OrderBy(x => x).SequenceEqual(other.Rel.OrderBy(x => x));
            bool href   = m_href == other.Href;
            bool @class = m_class.OrderBy(x => x).SequenceEqual(other.Class.OrderBy(x => x));
            bool title  = m_title == other.Title;
            bool type   = m_type == other.Type;

            return(rel && href && @class && title && type);
        }
Example #14
0
        public void SirenEntityBuilder_AddLink_ExpectEntityWithLinks()
        {
            ISirenLink expectedLink1 = TestHelpers.GetLink("link-1");
            ISirenLink expectedLink2 = TestHelpers.GetLink("link-2");

            SirenEntityBuilder builder = new SirenEntityBuilder();

            builder.AddLink(expectedLink1);
            builder.AddLink(expectedLink2);
            ISirenEntity entity = builder.Build();

            Assert.AreEqual(2, entity.Links.Count());
            CollectionAssert.Contains(entity.Links, expectedLink1);
            CollectionAssert.Contains(entity.Links, expectedLink2);
        }
Example #15
0
        public void SirenLink_Serialized_DoesNotIncludeOptionalParametersIfNull()
        {
            ISirenLink sirenLink = new SirenLink(
                rel: new[] { "foo" },
                href: new Uri("http://example.com"));

            string serialized = JsonConvert.SerializeObject(sirenLink);

            ISirenLink link = JsonConvert.DeserializeObject <SirenLink>(serialized);

            Assert.AreEqual("foo", link.Rel[0]);
            Assert.AreEqual("http://example.com/", link.Href.ToString());
            Assert.IsEmpty(link.Class);
            Assert.IsNull(link.Type);
            Assert.IsNull(link.Title);
        }
Example #16
0
        public void SirenLink_DeserializesCorrectly()
        {
            ISirenLink sirenLink = new SirenLink(
                rel: new[] { "foo" },
                href: new Uri("http://example.com"),
                @class: new[] { "foo" },
                title: "Link title",
                type: "text/html");

            string serialized = JsonConvert.SerializeObject(sirenLink);

            ISirenLink link = JsonConvert.DeserializeObject <SirenLink>(serialized);

            Assert.Contains("foo", link.Rel);
            Assert.AreEqual("http://example.com/", link.Href.ToString());
            Assert.Contains("foo", link.Class);
            Assert.AreEqual("Link title", link.Title);
            Assert.AreEqual("text/html", link.Type);
        }
Example #17
0
        public void SirenEntityBuilder_AddLinks_ExpectEntityWithLinks()
        {
            ISirenLink expectedLink1       = TestHelpers.GetLink("link-bulk-1");
            ISirenLink expectedLink2       = TestHelpers.GetLink("link-bulk-2");
            ISirenLink expectedLink3       = TestHelpers.GetLink("link-bulk-3");
            IEnumerable <ISirenLink> links = new[] {
                expectedLink1, expectedLink2
            };

            SirenEntityBuilder builder = new SirenEntityBuilder();

            builder.AddLinks(links);
            builder.AddLink(expectedLink3);
            ISirenEntity entity = builder.Build();

            Assert.AreEqual(3, entity.Links.Count());
            CollectionAssert.Contains(entity.Links, expectedLink1);
            CollectionAssert.Contains(entity.Links, expectedLink2);
            CollectionAssert.Contains(entity.Links, expectedLink3);
        }
Example #18
0
 public void SirenLink_GetHashcodeNot0(ISirenLink link)
 {
     Assert.AreNotEqual(0, link.GetHashCode());
 }
Example #19
0
 public void SirenLink_GetHashCode_NotEqual(ISirenLink link1, ISirenLink link2)
 {
     Assert.AreNotEqual(link1.GetHashCode(), link2.GetHashCode());
 }
        public void MatchingHelpers_MatchesSirenEntityArraysCorrectly()
        {
            string       message;
            ISirenEntity entity = TestHelpers.GetEntity();
            ISirenLink   link   = TestHelpers.GetLink();

            IEnumerable <ISirenEntity> expected = null;
            IEnumerable <ISirenEntity> actual   = null;

            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            actual = new[] { entity };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenEntity(
                                   rel: entity.Rel
                                   ) };
            actual = new[] { entity };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenEntity(
                                   rel: entity.Rel,
                                   @class: entity.Class,
                                   entities: entity.Entities,
                                   links: entity.Links,
                                   actions: entity.Actions,
                                   title: entity.Title,
                                   href: entity.Href,
                                   type: entity.Type
                                   ) };
            actual = new[] { entity };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenEntity(
                                   rel: entity.Rel,
                                   @class: entity.Class,
                                   entities: new [] {
                    entity.Entities.ElementAt(0),
                    entity.Entities.ElementAt(1)
                },
                                   links: new [] {
                    entity.Links.ElementAt(0),
                    entity.Links.ElementAt(1)
                },
                                   actions: new [] {
                    entity.Actions.ElementAt(0),
                    entity.Actions.ElementAt(1)
                },
                                   title: entity.Title,
                                   href: entity.Href,
                                   type: entity.Type
                                   ) };
            actual = new[] { entity };
            Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message));

            expected = new[] { new SirenEntity(
                                   entities: new [] {
                    entity.Entities.ElementAt(0),
                    entity.Entities.ElementAt(1),
                    new SirenEntity(rel: new [] { "not-child" }, @class: new [] { "class" }, type: "text/xml", title: "different-title")
                }
                                   ) };
            actual = new[] { entity };
            Assert.IsFalse(SirenMatchers.Matches(expected, actual, out message));
            Assert.AreEqual(message, "Expected different-title, but was entity3");

            expected = new[] { new SirenEntity(
                                   links: new [] {
                    entity.Links.ElementAt(0),
                    entity.Links.ElementAt(1),
                    new SirenLink(rel: new[] { "next" }, href: new Uri("http://example.com"), @class: new [] { "not-class" }, type: "text/html", title: "different-title")
                }
                                   ) };
            actual = new[] { entity };
            Assert.IsFalse(SirenMatchers.Matches(expected, actual, out message));
            Assert.AreEqual(message, "Expected different-title, but was link3");

            expected = new[] { new SirenEntity(
                                   actions: new [] {
                    entity.Actions.ElementAt(0),
                    entity.Actions.ElementAt(1),
                    new SirenAction(name: "different-name", href: new Uri("http://example.com"), @class: new[] { "not-class" })
                }
                                   ) };
            actual = new[] { entity };
            Assert.IsFalse(SirenMatchers.Matches(expected, actual, out message));
            Assert.AreEqual(message, "Expected different-name, but was action3");
        }
Example #21
0
        int IComparable.CompareTo(object obj)
        {
            ISirenLink @this = this;

            return(@this.CompareTo((ISirenLink)obj));
        }