-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
427 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
Chaskis/ChaskisCore/Handlers/SendAction/SendActionEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
namespace Chaskis.Core | ||
{ | ||
/// <summary> | ||
/// Arguments that are passed into <see cref="SendActionEventHandler"/> | ||
/// </summary> | ||
public sealed class SendActionEventArgs : BaseSendMessageEventArgs | ||
{ | ||
// ---------------- Fields ---------------- | ||
|
||
internal const string XmlRootName = "chaskis_sendaction_event"; | ||
|
||
// ---------------- Constructor ---------------- | ||
|
||
public SendActionEventArgs() : | ||
base() | ||
{ | ||
} | ||
|
||
// ---------------- Properties ---------------- | ||
|
||
protected override string XmlElementName => XmlRootName; | ||
} | ||
|
||
internal static class SendActionEventArgsExtensions | ||
{ | ||
public static SendActionEventArgs FromXml( string xmlString, IIrcWriter writer ) | ||
{ | ||
SendActionEventArgs args = new SendActionEventArgs(); | ||
|
||
BaseSendMessageEventArgsExtensions.FromXml( args, xmlString, writer ); | ||
|
||
return args; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Chaskis/ChaskisCore/Handlers/SendAction/SendActionEventConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Chaskis.Core | ||
{ | ||
/// <summary> | ||
/// Configuration for setting up <see cref="SendActionEventHandler"/>. | ||
/// </summary> | ||
public sealed class SendActionEventConfig : | ||
BaseCoreEvent<SendActionEventConfig, SendActionEventHandlerAction, SendActionEventArgs> | ||
{ | ||
// ---------------- Constructor ---------------- | ||
|
||
public SendActionEventConfig() | ||
{ | ||
} | ||
|
||
// ---------------- Functions ---------------- | ||
|
||
public override SendActionEventConfig Clone() | ||
{ | ||
return (SendActionEventConfig)this.MemberwiseClone(); | ||
} | ||
|
||
protected override IEnumerable<string> ValidateChild() | ||
{ | ||
// Nothing to validate. | ||
return null; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
Chaskis/ChaskisCore/Handlers/SendAction/SendActionEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using System.Text.RegularExpressions; | ||
|
||
namespace Chaskis.Core | ||
{ | ||
public delegate void SendActionEventHandlerAction( SendActionEventArgs args ); | ||
|
||
/// <summary> | ||
/// Event that is fired when the bot sends an action to | ||
/// the IRC Server. | ||
/// </summary> | ||
public sealed class SendActionEventHandler : BaseCoreEventHandler<SendActionEventConfig> | ||
{ | ||
// ---------------- Fields ---------------- | ||
|
||
private static readonly Regex regex = new Regex( | ||
$@"^<{SendActionEventArgs.XmlRootName}>.+</{SendActionEventArgs.XmlRootName}>", | ||
RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.IgnoreCase | ||
); | ||
|
||
// ---------------- Constructor ---------------- | ||
|
||
public SendActionEventHandler( SendActionEventConfig config ) : | ||
base( config, regex ) | ||
{ | ||
} | ||
|
||
// ---------------- Functions ---------------- | ||
|
||
protected override void HandleEventInternal( HandlerArgs args, Match match ) | ||
{ | ||
SendActionEventArgs eventArgs = SendActionEventArgsExtensions.FromXml( args.Line, args.IrcWriter ); | ||
this.config.LineAction( eventArgs ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
Chaskis/UnitTests/CoreTests/Handlers/SendAction/SendActionEventArgsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using System; | ||
using Chaskis.Core; | ||
using Moq; | ||
using NUnit.Framework; | ||
using SethCS.Exceptions; | ||
|
||
namespace Chaskis.UnitTests.CoreTests.Handlers.SendAction | ||
{ | ||
[TestFixture] | ||
public class SendActionEventArgsTests | ||
{ | ||
// ---------------- Fields ---------------- | ||
|
||
private const string channel = "#somechannel"; | ||
private const string message = "Some Message"; | ||
private const string server = "irc.somewhere.net"; | ||
private const ChaskisEventProtocol protocol = ChaskisEventProtocol.IRC; | ||
|
||
private Mock<IIrcWriter> mockWriter; | ||
|
||
// ---------------- Setup / Teardown ---------------- | ||
|
||
[SetUp] | ||
public void TestSetup() | ||
{ | ||
this.mockWriter = new Mock<IIrcWriter>( MockBehavior.Strict ); | ||
} | ||
|
||
[TearDown] | ||
public void TestTeardown() | ||
{ | ||
this.mockWriter = null; | ||
} | ||
|
||
// ---------------- Tests ---------------- | ||
|
||
[Test] | ||
public void XmlRoundTripTest() | ||
{ | ||
SendActionEventArgs uut = new SendActionEventArgs | ||
{ | ||
Protocol = protocol, | ||
Server = server, | ||
Writer = mockWriter.Object, | ||
|
||
ChannelOrUser = channel, | ||
Message = message | ||
}; | ||
|
||
DoRoundTripTest( uut ); | ||
} | ||
|
||
[Test] | ||
public void InvalidXmlRootName() | ||
{ | ||
string xmlString = $"<lol><server>{server}</server><protocol>{protocol}</protocol><channel>{channel}</channel><message>{message}</message></lol>"; | ||
|
||
Assert.Throws<ValidationException>( | ||
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object ) | ||
); | ||
} | ||
|
||
[Test] | ||
public void MissingServerDuringXmlParsing() | ||
{ | ||
string xmlString = $"<chaskis_sendaction_event><protocol>{protocol}</protocol><channel>{channel}</channel><message>{message}</message></chaskis_sendaction_event>"; | ||
|
||
Assert.Throws<ValidationException>( | ||
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object ) | ||
); | ||
} | ||
|
||
[Test] | ||
public void MissingProtocolDuringXmlParsing() | ||
{ | ||
string xmlString = $"<chaskis_sendaction_event><server>{server}</server><channel>{channel}</channel><message>{message}</message></chaskis_sendaction_event>"; | ||
|
||
Assert.Throws<ValidationException>( | ||
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object ) | ||
); | ||
} | ||
|
||
[Test] | ||
public void MissingChannelDuringXmlParsing() | ||
{ | ||
string xmlString = $"<chaskis_sendaction_event><server>{server}</server><protocol>{protocol}</protocol><message>{message}</message></chaskis_sendaction_event>"; | ||
|
||
Assert.Throws<ValidationException>( | ||
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object ) | ||
); | ||
} | ||
|
||
[Test] | ||
public void MissingMessageDuringXmlParsing() | ||
{ | ||
string xmlString = $"<chaskis_sendaction_event><server>{server}</server><protocol>{protocol}</protocol><channel>{channel}</channel></chaskis_sendaction_event>"; | ||
|
||
Assert.Throws<ValidationException>( | ||
() => SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object ) | ||
); | ||
} | ||
|
||
// ---------------- Test Helpers ---------------- | ||
|
||
private void DoRoundTripTest( SendActionEventArgs uut ) | ||
{ | ||
string xmlString = uut.ToXml(); | ||
SendActionEventArgs postXml = SendActionEventArgsExtensions.FromXml( xmlString, mockWriter.Object ); | ||
|
||
Console.WriteLine( xmlString ); | ||
|
||
Assert.AreEqual( uut.Server, postXml.Server ); | ||
Assert.AreEqual( uut.Protocol, postXml.Protocol ); | ||
Assert.AreEqual( uut.ChannelOrUser, postXml.ChannelOrUser ); | ||
Assert.AreEqual( uut.Message, postXml.Message ); | ||
Assert.AreSame( uut.Writer, postXml.Writer ); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
Chaskis/UnitTests/CoreTests/Handlers/SendAction/SendActionEventConfigTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using Chaskis.Core; | ||
using NUnit.Framework; | ||
using SethCS.Exceptions; | ||
|
||
namespace Chaskis.UnitTests.CoreTests.Handlers.SendAction | ||
{ | ||
[TestFixture] | ||
public class SendActionEventConfigTests | ||
{ | ||
// ---------------- Tests ---------------- | ||
|
||
[Test] | ||
public void ValidateTest() | ||
{ | ||
SendActionEventConfig config = new SendActionEventConfig | ||
{ | ||
LineAction = null | ||
}; | ||
Assert.Throws<ListedValidationException>( () => config.Validate() ); | ||
|
||
config.LineAction = delegate ( SendActionEventArgs args ) | ||
{ | ||
}; | ||
|
||
Assert.DoesNotThrow( () => config.Validate() ); | ||
} | ||
|
||
[Test] | ||
public void CloneTest() | ||
{ | ||
SendActionEventConfig config = new SendActionEventConfig(); | ||
SendActionEventConfig clone = config.Clone(); | ||
|
||
Assert.AreNotSame( config, clone ); | ||
} | ||
} | ||
} |
Oops, something went wrong.