public void SetAsGameConfigFile() { Game = GameHandlerConfig; District = DistrictHandlerConfig; Action = ActionHandlerConfig; CheckCorrectInformation(); }
public void TestLiquification() { ActionHandlerConfig config = new ActionHandlerConfig { LineRegex = @"!{%nick%} {%channel%} {%user%}", LineAction = this.MessageFunction }; ActionHandler uut = new ActionHandler( config ); string expectedMessage = string.Format( "!{0} {1} {2}", this.ircConfig.Nick, this.ircConfig.Channels[0], remoteUser ); string ircString = this.GenerateMessage(remoteUser, this.ircConfig.Channels[0], expectedMessage); uut.HandleEvent(this.ConstructArgs(ircString)); Assert.IsNotNull(this.responseReceived); Assert.AreEqual(this.ircConfig.Channels[0], this.responseReceived.Channel); Assert.AreEqual(remoteUser, this.responseReceived.User); Assert.AreEqual(expectedMessage, this.responseReceived.Message); }
public void CloneTest() { ActionHandlerConfig config1 = new ActionHandlerConfig(); ActionHandlerConfig clone = config1.Clone(); Assert.AreNotSame(config1, clone); }
public void BlacklistTest() { ActionHandlerConfig config = new ActionHandlerConfig { LineRegex = @"!bot\s+help", LineAction = this.MessageFunction }; ActionHandler uut = new ActionHandler( config ); const string channel = "#blacklist"; const string expectedMessage = "!bot help"; List <string> blackList = new List <string>() { channel }; string ircString = this.GenerateMessage(remoteUser, channel, expectedMessage); HandlerArgs args = this.ConstructArgs(ircString); args.BlackListedChannels = blackList; uut.HandleEvent(args); Assert.IsNull(this.responseReceived); }
public void DefaultValueTest() { ActionHandlerConfig uut = new ActionHandlerConfig(); Assert.AreEqual(0, uut.CoolDown); Assert.AreEqual(RegexOptions.None, uut.RegexOptions); Assert.AreEqual(ResponseOptions.ChannelAndPms, uut.ResponseOption); Assert.IsFalse(uut.RespondToSelf); }
/// <summary> /// Converts an XML file to a list of handlers. /// </summary> /// <param name="rng"> /// Leave this null to use the default RNG, otherwise pass this in if you want to use your own (e.g. with a different seed) /// </param> public static IList <IIrcHandler> LoadXmlBotConfig(string file, IIrcConfig ircConfig, Random rng = null) { ArgumentChecker.IsNotNull(ircConfig, nameof(ircConfig)); if (File.Exists(file) == false) { throw new FileNotFoundException("Could not find XmlBotConfig file '" + file + '"'); } List <IIrcHandler> handlers = new List <IIrcHandler>(); XmlDocument doc = new XmlDocument(); doc.Load(file); XmlNode rootNode = doc.DocumentElement; if (rootNode.Name != rootXmlElementName) { throw new XmlException( "Root XML node should be named \"" + rootXmlElementName + "\". Got: " + rootNode.Name ); } foreach (XmlNode messageNode in rootNode.ChildNodes) { IIrcHandler handler = null; if (messageNode.Name.EqualsIgnoreCase("message")) { MessageHandlerConfig config = new MessageHandlerConfig(); config.Deserialize(messageNode, ircConfig, rng); handler = new MessageHandler(config); } else if (messageNode.Name.EqualsIgnoreCase("action")) { ActionHandlerConfig config = new ActionHandlerConfig(); config.Deserialze(messageNode, ircConfig, rng); handler = new ActionHandler(config); } if (handler != null) { handlers.Add(handler); } } return(handlers); }
public void ConstructionTest() { ActionHandlerConfig config = new ActionHandlerConfig { LineRegex = @"!bot\s+help", LineAction = this.MessageFunction }; ActionHandler uut = new ActionHandler( config ); // Keep Handling should be true by default. Assert.IsTrue(uut.KeepHandling); }
/// <summary> /// Converts an XML node to a config object. /// </summary> /// <param name="rng"> /// Leave this null to use the default RNG, otherwise pass this in if you want to use your own (e.g. with a different seed) /// </param> public static void Deserialze( this ActionHandlerConfig actionConfig, XmlNode handlerNode, IIrcConfig ircConfig, Random rng = null ) { IReadOnlyList <string> responses = DeserializeBase(actionConfig, handlerNode); // I can not for the life of me figure out how to make this generic between this // and message... maybe we can't? ActionHandlerAction action = delegate(ActionHandlerArgs args) { HandleResponse(args, responses, ircConfig, rng); }; actionConfig.LineAction = action; }
public void TestMisMatchedMessage() { ActionHandlerConfig config = new ActionHandlerConfig { LineRegex = @"!bot\s+help", LineAction = this.MessageFunction }; ActionHandler uut = new ActionHandler( config ); // Does not match pattern. No response expected. const string expectedMessage = "hello world!"; string ircString = this.GenerateMessage(remoteUser, this.ircConfig.Channels[0], expectedMessage); uut.HandleEvent(this.ConstructArgs(ircString)); Assert.IsNull(this.responseReceived); }
// ---------------- Test Helpers ---------------- private void DoGoodMessageTest(string user, string channel) { ActionHandlerConfig config = new ActionHandlerConfig { LineRegex = @"!bot\s+help", LineAction = this.MessageFunction }; ActionHandler uut = new ActionHandler( config ); const string expectedMessage = "!bot help"; string ircString = this.GenerateMessage(user, channel, expectedMessage); uut.HandleEvent(this.ConstructArgs(ircString)); Assert.AreEqual(channel, this.responseReceived.Channel); Assert.AreEqual(user, this.responseReceived.User); Assert.AreEqual(expectedMessage, this.responseReceived.Message); }
public void IgnoreMessageTest() { const string expectedMessage = "!bot help"; ActionHandlerConfig config = new ActionHandlerConfig { LineRegex = @".+", LineAction = this.MessageFunction, RespondToSelf = false }; ActionHandler uut = new ActionHandler( config ); // Instead of action string, create a message string. string ircString = TestHelpers.ConstructMessageString(remoteUser, this.ircConfig.Channels[0], expectedMessage); uut.HandleEvent(this.ConstructArgs(ircString)); Assert.IsNull(this.responseReceived); }
public void ValidateTest() { ActionHandlerConfig config = new ActionHandlerConfig(); // No action should not validate. config.LineAction = null; config.LineRegex = @"!bot\s+help"; Assert.Throws <ListedValidationException>(() => config.Validate()); // Empty regex should not validate. config.LineAction = delegate(ActionHandlerArgs args) { }; config.LineRegex = string.Empty; Assert.Throws <ListedValidationException>(() => config.Validate()); // Null regex should not validate. config.LineAction = delegate(ActionHandlerArgs args) { }; config.LineRegex = null; Assert.Throws <ListedValidationException>(() => config.Validate()); // Cooldown can not be less than 0. config.LineAction = delegate(ActionHandlerArgs args) { }; config.LineRegex = @"!bot\s+help"; config.CoolDown = -1; Assert.Throws <ListedValidationException>(() => config.Validate()); // This should validate. config.LineAction = delegate(ActionHandlerArgs args) { }; config.LineRegex = @"!bot\s+help"; config.CoolDown = 1; Assert.DoesNotThrow(() => config.Validate()); }