Skip to content

Commit

Permalink
fixes - tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Feb 19, 2024
1 parent 029d115 commit d6bfdd2
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 18 deletions.
22 changes: 16 additions & 6 deletions src/Api/PubnubApi/EventEngine/Presence/Common/PresenceInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ namespace PubnubApi.EventEngine.Presence.Common
{
public class PresenceInput

Check warning on line 6 in src/Api/PubnubApi/EventEngine/Presence/Common/PresenceInput.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

'PresenceInput' overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
public IEnumerable<string> Channels { get; set; }
public IEnumerable<string> ChannelGroups { get; set; }
public IEnumerable<string> Channels { get; set; } = Enumerable.Empty<string>();
public IEnumerable<string> ChannelGroups { get; set; } = Enumerable.Empty<string>();

public static PresenceInput operator +(PresenceInput a, PresenceInput b)
{
return new PresenceInput
{
Channels = a.Channels?.Union(b.Channels ?? new string[0]),
ChannelGroups = a.ChannelGroups?.Union(b.ChannelGroups ?? new string[0]),
Channels = a.Channels?.Union(b.Channels ?? new string[0]).ToArray(),
ChannelGroups = a.ChannelGroups?.Union(b.ChannelGroups ?? new string[0]).ToArray(),
};
}

public static PresenceInput operator -(PresenceInput a, PresenceInput b)
{
return new PresenceInput
{
Channels = a.Channels?.Except(b.Channels ?? new string[0]),
ChannelGroups = a.ChannelGroups?.Except(b.ChannelGroups ?? new string[0]),
Channels = a.Channels?.Except(b.Channels ?? new string[0]).ToArray(),
ChannelGroups = a.ChannelGroups?.Except(b.ChannelGroups ?? new string[0]).ToArray(),
};
}

Expand All @@ -32,5 +32,15 @@ public bool IsEmpty()
|| ((Channels != null && Channels.Count() == 0)
&& (ChannelGroups != null && ChannelGroups.Count() == 0));
}

public override bool Equals(object obj)
{
if (obj is null || obj is not PresenceInput)
return false;

var typedObj = obj as PresenceInput;
return this.Channels.SequenceEqual(typedObj.Channels)
&& this.ChannelGroups.SequenceEqual(typedObj.ChannelGroups);
}
}
}
10 changes: 9 additions & 1 deletion src/Api/PubnubApi/EventEngine/Presence/States/APresenceState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace PubnubApi.EventEngine.Presence.States
{
public abstract class APresenceState : Core.State

Check warning on line 7 in src/Api/PubnubApi/EventEngine/Presence/States/APresenceState.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

'APresenceState' overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
public PresenceInput Input { get; set; }
public PresenceInput Input { get; set; } = new PresenceInput(); // empty by default

public bool IsEmpty()
{
Expand All @@ -26,5 +26,13 @@ protected TransitionResult HandleLeftEvent(Events.LeftEvent e)

return state.With(new LeaveInvocation(){ Input = e.Input });
}

public override bool Equals(object obj)
{
if (obj is null || obj is not APresenceState)
return false;

return this.Input.Equals(((APresenceState)obj).Input);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PubnubApi.EventEngine.Presence.Invocations;
using PubnubApi.EventEngine.Presence.Common;
using PubnubApi.EventEngine.Core;
using System.Collections.Generic;
using System;
Expand All @@ -9,7 +10,7 @@ public class InactiveState : APresenceState
{
public InactiveState()
{
Input = null;
Input = new PresenceInput();
}

// TODO: Dummy Invocation until we have real ones
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using PubnubApi.EventEngine.Presence.Events;
using PubnubApi.EventEngine.Presence.States;
using PubnubApi.EventEngine.Presence.Invocations;
using System.Linq;
using System.Security.Policy;
using System.Collections.Generic;

namespace PubnubApi.Tests.EventEngine.Presence
{
Expand Down Expand Up @@ -61,7 +64,7 @@ internal class CooldownTransitions
new CooldownState(),
new DisconnectEvent(),
new StoppedState(),
new IEffectInvocation[] { new LeaveInvocation() { Input = new PresenceInput() { Channels = new string[] { } } } }
new IEffectInvocation[] { new LeaveInvocation() { Input = new PresenceInput() } }
},
new object[] {
new CooldownState(),
Expand All @@ -72,15 +75,34 @@ internal class CooldownTransitions
};

[TestCaseSource(nameof(testCases))]
public void TestTransition(State @sut, IEvent @ev, State @expected, IEffectInvocation[] @_)
public void TestTransition(APresenceState @sut, IEvent @ev, APresenceState @expected, IEffectInvocation[] @_)
{
Assert.AreEqual(@expected.GetType(), @sut.Transition(@ev).State.GetType());
var result = @sut.Transition(@ev);

if (result == null && expected == null)
{
// it's expected result
return;
}

Assert.AreEqual(@expected, result.State);
}

[TestCaseSource(nameof(testCases))]
public void TestReturnedInvocations(State @sut, IEvent @ev, State @_, IEffectInvocation[] @expected)
public void TestReturnedInvocations(State @sut, IEvent @ev, State @_, IEffectInvocation[] @expected)
{
CollectionAssert.AreEqual(@expected, @sut.Transition(@ev).Invocations);
var result = @sut.Transition(@ev);

if (result == null && expected == null)
{
// it's expected result
return;
}

foreach (var item in result.Invocations)
{
Assert.True(expected.Select(i => i.GetType()).Contains(item.GetType()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ internal class FailedStateTransitions
[TestCaseSource(nameof(testCases))]
public void TestTransition(State @sut, IEvent @ev, State @expected)
{
Assert.AreEqual(@expected.GetType(), @sut.Transition(@ev).State.GetType());
var result = @sut.Transition(@ev);

if (result == null && expected == null)
{
// it's expected result
return;
}

Assert.AreEqual(@expected.GetType(), result.State.GetType());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ internal class HeartbeatingStateTransitions
[TestCaseSource(nameof(testCases))]
public void TestTransition(State @sut, IEvent @ev, State @expected)
{
Assert.AreEqual(@expected.GetType(), @sut.Transition(@ev).State.GetType());
var result = @sut.Transition(@ev);

if (result == null && expected == null)
{
// it's expected result
return;
}

Assert.AreEqual(@expected.GetType(), result.State.GetType());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ internal class InactiveStateTransitions
[TestCaseSource(nameof(testCases))]
public void TestTransition(State @sut, IEvent @ev, State @expected)
{
Assert.AreEqual(@expected.GetType(), @sut.Transition(@ev).State.GetType());
var result = @sut.Transition(@ev);

if (result == null && expected == null)
{
// it's expected result
return;
}

Assert.AreEqual(@expected.GetType(), result.State.GetType());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ internal class ReconnectingStateTransitions
[TestCaseSource(nameof(testCases))]
public void TestTransition(State @sut, IEvent @ev, State @expected)
{
Assert.AreEqual(@expected.GetType(), @sut.Transition(@ev).State.GetType());
var result = @sut.Transition(@ev);

if (result == null && expected == null)
{
// it's expected result
return;
}

Assert.AreEqual(@expected.GetType(), result.State.GetType());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ internal class StoppedStateTransitions
[TestCaseSource(nameof(testCases))]
public void TestTransition(State @sut, IEvent @ev, State @expected)
{
Assert.AreEqual(@expected.GetType(), @sut.Transition(@ev).State.GetType());
var result = @sut.Transition(@ev);

if (result == null && expected == null)
{
// it's expected result
return;
}

Assert.AreEqual(@expected.GetType(), result.State.GetType());
}
}
}

0 comments on commit d6bfdd2

Please sign in to comment.