public RecursiveNuggetParser( NuggetTokens nuggetTokens, NuggetParser.Context context) { m_nuggetTokens = nuggetTokens; m_context = context; m_delimiterWithParameterBeginToken = nuggetTokens.DelimiterToken + nuggetTokens.ParameterBeginToken; m_parameterEndBeforeDelimiterToken = nuggetTokens.ParameterEndToken + nuggetTokens.DelimiterToken; m_parameterEndBeforeEndToken = nuggetTokens.ParameterEndToken + nuggetTokens.EndToken; // Prep the regexes. We escape each token char to ensure it is not misinterpreted. // · Breakdown e.g. "\[\[\[(.+?)(?:\|\|\|(.+?))*(?:\/\/\/(.+?))?\]\]\]" m_tokensRegex = new Regex( string.Format(@"(?:{1}{4})|(?:{5}{1})|(?:{5}{3})|(?:{0})|(?:{1})|(?:{2})|(?:{3})", EscapeString(m_nuggetTokens.BeginToken), EscapeString(m_nuggetTokens.DelimiterToken), EscapeString(m_nuggetTokens.CommentToken), EscapeString(m_nuggetTokens.EndToken), EscapeString(m_nuggetTokens.ParameterBeginToken), EscapeString(m_nuggetTokens.ParameterEndToken)), RegexOptions.CultureInvariant | RegexOptions.Singleline); // RegexOptions.Singleline in fact enable multi-line nuggets. }
private void ParseAndComp(string nuggetString, Nugget rhs, bool equal = true) { // Arrange. NuggetTokens nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing); // Act. Nugget nugget = nuggetParser.BreakdownNugget(nuggetString); // Assert. if (equal) { Assert.AreEqual(nugget, rhs); } else { Assert.AreNotEqual(nugget, rhs); } }
public void NuggetParser_CanParseEntity01() { // Arrange. NuggetTokens nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing); string entity = "<p>[[[hello]]]</p><p>[[[there]]]</p>"; // Act. nuggetParser.ParseString(entity, delegate(string nuggetString, int pos, Nugget nugget1, string entity1) { if (pos == 3) { Assert.AreEqual(nugget1, new Nugget { MsgId = "hello" }); } else if (pos == 21) { Assert.AreEqual(nugget1, new Nugget { MsgId = "there" }); } else { Assert.Fail(); } return null; }); }
// Con public NuggetParser( NuggetTokens nuggetTokens, Context context) { m_nuggetTokens = nuggetTokens; m_context = context; // Prep the regexes. We escape each token char to ensure it is not misinterpreted. // · Breakdown e.g. "\[\[\[(.+?)(?:\|\|\|(.+?))*(?:\/\/\/(.+?))?\]\]\]" m_regexNuggetBreakdown = new Regex( string.Format(@"{0}(.*?)(?:{1}(.{4}?))*(?:{2}(.+?))?{3}", EscapeString(m_nuggetTokens.BeginToken), EscapeString(m_nuggetTokens.DelimiterToken), EscapeString(m_nuggetTokens.CommentToken), EscapeString(m_nuggetTokens.EndToken), m_context == Context.SourceProcessing ? "+" : "*"), RegexOptions.CultureInvariant | RegexOptions.Singleline); // RegexOptions.Singleline in fact enable multi-line nuggets. }
public void NuggetParser_ResponseMode_CanParseEntity_TwoParams_FirstNonEmpty_SecondEmpty() { var nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.ResponseProcessing); var input = "[[[Title: %0, %1|||X|||]]]"; var result = nuggetParser.ParseString(input, (nuggetString, pos, nugget, i_entity) => { Assert.IsTrue(nugget.IsFormatted); return nugget.MsgId; }); Assert.AreEqual("Title: %0, %1", result); }
public void NuggetParser_SourceMode_CanParseEntity_EmptyParam() { var nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing); var input = "[[[Title: %0|||]]]"; var result = nuggetParser.ParseString(input, (nuggetString, pos, nugget, i_entity) => { Assert.IsFalse(nugget.IsFormatted); return nugget.MsgId; }); Assert.AreEqual("Title: %0|||", result); }
public void NuggetParser_ResponseMode_CanParseEntity_EmptyParam() { var nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.ResponseProcessing); var input = "[[[Title: %0|||]]]"; var result = nuggetParser.ParseString(input, (nuggetString, pos, nugget, i_entity) => { Assert.IsTrue(nugget.IsFormatted); var message = NuggetLocalizer.ConvertIdentifiersInMsgId(nugget.MsgId); message = String.Format(message, nugget.FormatItems); return message; }); Assert.AreEqual("Title: ", result); }
public void NuggetParser_CanParseEntity_MultiLineNugget01() { // Arrange. NuggetTokens nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing); string entity = "<p>[[[hello\r\n%0|||{0}]]]</p><p>[[[there]]]</p>"; // Act. int i = 0; nuggetParser.ParseString(entity, delegate(string nuggetString, int pos, Nugget nugget1, string entity1) { switch (i++) { case 0: { Assert.AreEqual(nugget1, new Nugget { MsgId = "hello\r\n%0", FormatItems = new string[] { "{0}" } }); break; } case 1: { Assert.AreEqual(nugget1, new Nugget { MsgId = "there" }); break; } default: Assert.Fail(); break; } return null; }); }
public void NuggetParser_CanParseEntity_CustomNuggetTokens03() { // Arrange. NuggetTokens nuggetTokens = new NuggetTokens("```", "'''", "###", "@@@"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing); string entity = "<p>```hello###{0}'''</p><p>```there###{0}###{1}@@@comment comment comment'''</p>"; // Act. CanParseEntity_CustomNuggetTokens_Act(entity, nuggetParser); }
public void NuggetParser_CanParseEntity_CustomNuggetTokens02() { // Arrange. NuggetTokens nuggetTokens = new NuggetTokens("[[[:", ":]]]", "|||", "///"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing); string entity = "<p>[[[:hello|||{0}:]]]</p><p>[[[:there|||{0}|||{1}///comment comment comment:]]]</p>"; // Act. CanParseEntity_CustomNuggetTokens_Act(entity, nuggetParser); }
public void NuggetParser_CanParseEntity03() { // Arrange. NuggetTokens nuggetTokens = new NuggetTokens("[[[", "]]]", "|||", "///"); NuggetParser nuggetParser = new NuggetParser(nuggetTokens, NuggetParser.Context.SourceProcessing); string entity = "<p>[[[hello|||{0}]]]</p><p>[[[there|||{0}|||{1}///comment comment comment]]]</p>"; // Act. nuggetParser.ParseString(entity, delegate(string nuggetString, int pos, Nugget nugget1, string entity1) { if (pos == 3) { Assert.AreEqual(nugget1, new Nugget { MsgId = "hello", FormatItems = new string[] { "{0}" } }); } else if (pos == 27) { Assert.AreEqual(nugget1, new Nugget { MsgId = "there", FormatItems = new string[] { "{0}", "{1}" }, Comment = "comment comment comment" }); } else { Assert.Fail(); } return null; }); }
// Con public NuggetParser( NuggetTokens nuggetTokens, Context context) { m_nuggetTokens = nuggetTokens; m_context = context; // Prep the regexes. We escape each token char to ensure it is not misinterpreted. // · Breakdown e.g. "\[\[\[(.+?)(?:\|\|\|(.+?))*(?:\/\/\/(.+?))?\]\]\]" m_regexNuggetBreakdown = new Regex( string.Format(@"{0}(.+?)(?:{1}(.{4}?))*(?:{2}(.+?))?{3}", EscapeString(m_nuggetTokens.BeginToken), EscapeString(m_nuggetTokens.DelimiterToken), EscapeString(m_nuggetTokens.CommentToken), EscapeString(m_nuggetTokens.EndToken), m_context == Context.SourceProcessing ? "+" : "*"), RegexOptions.CultureInvariant | RegexOptions.Singleline); // RegexOptions.Singleline in fact enable multi-line nuggets. }