public static IEnumerable <ISirenField> FieldsByType( this ISirenAction @this, string type ) { return(@this.Fields.Where(field => field.Type.Equals(type))); }
public void SirenAction_Equality_SameAction_ShouldBeEqual() { ISirenAction action = TestHelpers.GetAction(); ISirenAction other = TestHelpers.GetAction(); TestHelpers.BidirectionalEquality(action, other, true); }
public override bool Equals(object obj) { ISirenAction action = obj as ISirenAction; ISirenAction @this = this; return(action != null && @this.Equals(action)); }
public void SirenAction_DeserializesCorrectly() { ISirenAction sirenAction = new SirenAction( name: "foo", href: new Uri("http://example.com"), @class: new [] { "bar" }, method: "GET", title: "Some action", type: "text/html", fields: new [] { new SirenField(name: "field") } ); string serialized = JsonConvert.SerializeObject(sirenAction); ISirenAction action = JsonConvert.DeserializeObject <SirenAction>(serialized); Assert.AreEqual("foo", action.Name); Assert.AreEqual("http://example.com/", action.Href.ToString()); Assert.Contains("bar", action.Class); Assert.AreEqual("GET", action.Method); Assert.AreEqual("Some action", action.Title); Assert.AreEqual("text/html", action.Type); Assert.AreEqual(1, action.Fields.ToList().Count); }
public static IEnumerable <ISirenField> FieldsByClass( this ISirenAction @this, string @class ) { return(@this.Fields.Where(field => field.Class.Contains(@class))); }
public static bool TryGetActionByName( this ISirenEntity @this, string name, out ISirenAction action ) { action = @this.Actions.FirstOrDefault(a => a.Name.Equals(name)); return(action != default(ISirenAction)); }
public static bool TryGetFieldByType( this ISirenAction @this, string type, out ISirenField field ) { field = @this.FieldsByType(type).FirstOrDefault(); return(field != default(ISirenField)); }
public static bool TryGetFieldByName( this ISirenAction @this, string name, out ISirenField field ) { field = @this.Fields.FirstOrDefault(f => f.Name.Equals(name)); return(field != default(ISirenField)); }
public static bool TryGetFieldByClass( this ISirenAction @this, string @class, out ISirenField field ) { field = @this.FieldsByClass(@class).FirstOrDefault(); return(field != default(ISirenField)); }
public static bool TryGetActionByClass( this ISirenEntity @this, string @class, out ISirenAction action ) { action = @this.ActionsByClass(@class).FirstOrDefault(); return(action != default(ISirenAction)); }
int IComparable <ISirenAction> .CompareTo(ISirenAction other) { if (other == null) { return(1); } return(string.CompareOrdinal(m_name, other.Name)); }
public void SirenAction_Equality_MissingAttributes_ShouldNotBeEqual() { ISirenAction action = TestHelpers.GetAction(); ISirenAction other = new SirenAction( name: action.Name, href: action.Href ); TestHelpers.BidirectionalEquality(action, other, false); }
public static bool Matches(ISirenAction expected, ISirenAction actual, out string message) { return(Matches(expected.Name, actual.Name, out message) && Matches(expected.Method, actual.Method, out message) && Matches(expected.Title, actual.Title, out message) && Matches(expected.Type, actual.Type, out message) && Matches(expected.Href, actual.Href, out message) && Matches(expected.Class, actual.Class, out message) && Matches(expected.Fields, actual.Fields, out message)); }
public SirenEntityBuilder AddAction(ISirenAction action) { if (m_actions == null) { m_actions = new List <ISirenAction>(); } m_actions.Add(action); return(this); }
public void SirenEntityBuilder_AddAction_ExpectEntityWithActions() { ISirenAction expectedAction1 = TestHelpers.GetAction("action-1"); ISirenAction expectedAction2 = TestHelpers.GetAction("action-2"); SirenEntityBuilder builder = new SirenEntityBuilder(); builder.AddAction(expectedAction1); builder.AddAction(expectedAction2); ISirenEntity entity = builder.Build(); Assert.AreEqual(2, entity.Actions.Count()); CollectionAssert.Contains(entity.Actions, expectedAction1); CollectionAssert.Contains(entity.Actions, expectedAction2); }
public void SirenAction_Serialized_DoesNotIncludeOptionalParametersIfNull() { ISirenAction sirenAction = new SirenAction( name: "foo", href: new Uri("http://example.com")); string serialized = JsonConvert.SerializeObject(sirenAction); ISirenAction action = JsonConvert.DeserializeObject <SirenAction>(serialized); Assert.AreEqual("foo", action.Name); Assert.AreEqual("http://example.com/", action.Href.ToString()); Assert.IsEmpty(action.Class); Assert.IsNull(action.Method); Assert.IsNull(action.Title); Assert.IsNull(action.Type); Assert.IsEmpty(action.Fields); }
bool IEquatable <ISirenAction> .Equals(ISirenAction other) { if (other == null) { return(false); } bool name = m_name == other.Name; bool @class = m_class.OrderBy(x => x).SequenceEqual(other.Class.OrderBy(x => x)); bool method = m_method == other.Method; bool href = m_href == other.Href; bool title = m_title == other.Title; bool type = m_type == other.Type; bool fields = m_fields.OrderBy(x => x).SequenceEqual(other.Fields.OrderBy(x => x)); return(name && @class && method && href && title && type && fields); }
public void SirenAction_Equality_DifferentFields_ShouldNotBeEqual() { ISirenAction action = TestHelpers.GetAction(); ISirenAction other = new SirenAction( name: action.Name, href: action.Href, @class: action.Class, method: action.Method, title: action.Title, type: action.Type, fields: new[] { new SirenField("fieldName1") } ); TestHelpers.BidirectionalEquality(action, other, false); }
public void SirenAction_Equality_DifferentFieldOrder_ShouldBeEqual() { ISirenAction action = TestHelpers.GetAction(); ISirenAction other = new SirenAction( name: action.Name, href: action.Href, @class: action.Class, method: action.Method, title: action.Title, type: action.Type, fields: new[] { action.Fields.ElementAt(1), action.Fields.ElementAt(2), action.Fields.ElementAt(0) } ); TestHelpers.BidirectionalEquality(action, other, true); }
public void SirenEntityBuilder_AddActions_ExpectEntityWithActions() { ISirenAction expectedAction1 = TestHelpers.GetAction("action-bulk-1"); ISirenAction expectedAction2 = TestHelpers.GetAction("action-bulk-2"); ISirenAction expectedAction3 = TestHelpers.GetAction("action-bulk-3"); IEnumerable <ISirenAction> actions = new[] { expectedAction2, expectedAction3 }; SirenEntityBuilder builder = new SirenEntityBuilder(); builder.AddAction(expectedAction1); builder.AddActions(actions); ISirenEntity entity = builder.Build(); Assert.AreEqual(3, entity.Actions.Count()); CollectionAssert.Contains(entity.Actions, expectedAction1); CollectionAssert.Contains(entity.Actions, expectedAction2); CollectionAssert.Contains(entity.Actions, expectedAction3); }
int IComparable.CompareTo(object obj) { ISirenAction @this = this; return(@this.CompareTo((ISirenAction)obj)); }
public void SirenAction_GetHashCode_NotEqual(ISirenAction action1, ISirenAction action2) { Assert.AreNotEqual(action1.GetHashCode(), action2.GetHashCode()); }
public void SirenAction_GetHashcodeNot0(ISirenAction action) { Assert.AreNotEqual(0, action.GetHashCode()); }
public void MatchingHelpers_MatchesSirenActionArraysCorrectly() { string message; ISirenAction action = TestHelpers.GetAction(); IEnumerable <ISirenAction> expected = null; IEnumerable <ISirenAction> actual = null; Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message)); actual = new[] { action }; Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message)); expected = new[] { new SirenAction( name: action.Name, href: action.Href ) }; actual = new[] { action }; Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message)); expected = new[] { new SirenAction( name: action.Name, href: action.Href, @class: action.Class, method: action.Method, title: action.Title, type: action.Type, fields: new [] { action.Fields.ElementAt(0) } ) }; actual = new[] { action }; Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message)); expected = new[] { new SirenAction( name: action.Name, href: action.Href, @class: action.Class, method: action.Method, title: action.Title, type: action.Type, fields: action.Fields ) }; actual = new[] { action }; Assert.IsTrue(SirenMatchers.Matches(expected, actual, out message)); expected = new[] { new SirenAction( name: action.Name + "-foobar", href: action.Href, @class: action.Class, method: action.Method, title: action.Title, type: action.Type, fields: action.Fields ) }; actual = new[] { action }; Assert.IsFalse(SirenMatchers.Matches(expected, actual, out message)); Assert.AreEqual(message, "Expected action-name-foobar, but was action-name"); expected = new[] { new SirenAction( name: action.Name, href: action.Href, @class: action.Class, method: action.Method, title: action.Title, type: action.Type, fields: new [] { action.Fields.ElementAt(0), action.Fields.ElementAt(1), new SirenField(name: "field3", @class: new [] { "not-class" }, type: SirenFieldType.Range, value: 1) } ) }; actual = new [] { action }; Assert.IsFalse(SirenMatchers.Matches(expected, actual, out message)); Assert.AreEqual(message, "Expected 1, but was "); }