public void TestCommandTypes(string line, Command.CommandType type, string anchor, string subject, string delim, string target) { Command command = new Command(line); Assert.AreEqual(type, command.CommandIs); Assert.AreEqual(anchor, command.AnchorString); Assert.AreEqual(subject,command.SubjectString); }
public string ApplySingleCommand(string line, Command command) { _pickup.CollectAllPickupsInLine(line, command); if (command.CommandIs == Command.CommandType.isAnchoredTemplate || command.CommandIs == Command.CommandType.isTemplate) return ReplaceMatched(command.SubjectRegex, line, _pickup.ReplacePickupsWithStoredValue(command.ReplacementString)); if (command.CommandIs == Command.CommandType.isAnchoredReplace || command.CommandIs == Command.CommandType.isReplace) return ReplaceFullLine(command.SubjectRegex, line, _pickup.ReplacePickupsWithStoredValue(command.ReplacementString)); return line; }
public void CollectAllPickupsInLine(string line, Command command) { if (command.IsCaptureInSubjectString) { Match m = command.SubjectRegex.Match(line); if ( ! m.Success) return; foreach (int groupNumber in command.SubjectRegex.GetGroupNumbers()) { string name = command.SubjectRegex.GroupNameFromNumber(groupNumber); if (PickupList.ContainsKey(name)) PickupList[name] = m.Groups[groupNumber].Value; else PickupList.Add(name, m.Groups[groupNumber].Value); } } }
public List<Command> GetReplacementList() { // TODO: Use Strategy pattern to only call regex.Replace when regex present, otherwise call String.Replace. kgrepMode = RunningAs.ReplaceAll; String line; while ((line = sr.ReadLine()) != null) { line = line.Trim(); // Remove comment lines. if (line == String.Empty || line.StartsWith(_comment)) { continue; } // Remove any trailing comments. int i = line.IndexOf(_comment); if (i >= 0) line = line.Remove(i); if (line.ToLower().TrimStart().StartsWith("comment")) _comment = GetOption(line, "comment"); else if (line.ToLower().TrimStart().StartsWith("delim")) _delim = GetOption(line, "delim"); else if (line.ToLower().TrimStart().StartsWith("mm")) MaxReplacements = int.Parse(GetOption(line,"mm")); else if (line.ToLower().TrimStart().StartsWith("maxreplacements")) MaxReplacements = int.Parse(GetOption(line, "maxreplacements")); else if (line.ToLower().TrimStart().StartsWith("ofs")) { OFS = GetOption(line, "OFS"); OFS = OFS.Replace("\\n", "\n"); // interpret \n on command line as newline OFS = OFS.Replace("\\t", "\t"); // interpret \t on command line as tab } else { Command command = new Command(line, _delim) {OFS = OFS}; if (command.IsValid()) { CommandList.Add(command); } } } sr.Close(); if (IsScanner()) kgrepMode = RunningAs.Scanner; return CommandList; }
private bool isCandidateForPrinting(string line, Command command) { return Regex.IsMatch(line, command.AnchorString); }
public void WhenNoAnchor_ExpectNoValue() { Command command = new Command(" a~b"); Assert.AreEqual("", command.AnchorString); }
private bool isCandidateForReplacement(string line, Command command) { //if (command.CommandIs != Command.CommandType.isAnchoredReplace && command.CommandIs != Command.CommandType.isAnchoredReplace) // return true; return Regex.IsMatch(line, command.AnchorString); }
public void WhenSameCaptureNameReused_ExpectLastValue() { Command command = new Command(@"^(?<name>[a-z]+).*?(?<name>[0-9]+)"); Pickup pickup = new Pickup(); pickup.CollectAllPickupsInLine("ab c 45", command); var results = pickup.ReplacePickupsWithStoredValue("${name}"); Assert.AreEqual("45", results); }
public void WhenReplacmentSwapsGroups_All_ExpectChange() { ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() }; List<Command> reps = new List<Command>(); Command command = new Command(@"(\w+)\s(\w+)~$2 $1"); Assert.AreEqual("me from you to", engine.ApplySingleCommand("from me to you", command)); }
public void WhenAnchorAndSubjectInPattern() { Command command = new Command("/ hello/ a~b"); Assert.AreEqual("a", command.SubjectString); }
public void WhenUsingTildaInTargetAndTemplateTarget_ExpectSplitOnTemplate() { Command command = new Command("/ hello/ (..) c-> b~dog"); Assert.AreEqual("b~dog", command.ReplacementString); }
public void WhenUsingTildaInSubjectAndTemplateTarget_ExpectSplitOnTilda() { Command command = new Command("/ hello/ a~ c->b"); Assert.AreEqual("a", command.SubjectString); }
public void WhenSubjectInPatternWIthSpaces() { Command command = new Command(" a ~b"); Assert.AreEqual("a", command.SubjectString); }
public void WhenReplacementInTopattern() { Command command = new Command("/ hello/ a~b"); Assert.AreEqual("b", command.ReplacementString); }
public void WhenOnlySubjectInPattern() { Command command = new Command(" a"); Assert.AreEqual("a", command.SubjectString); }
public void WhenAnchorAndPatternMatches_All_ExpectChange() { ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() }; Command command = new Command("/today/ you ~ to"); Assert.AreEqual("to and to today", engine.ApplySingleCommand("you and you today", command)); }
public void WhenAnchorMatchesButPatternDoesNot_All_ExpectNoChange() { ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() }; Command command = new Command("/today/ Joe~to"); Assert.AreEqual("you and you today", engine.ApplySingleCommand("you and you today", command)); }
public void WhenAnchorInTopattern_ExpectAnchor() { Command command = new Command("/ hello/ a~b"); Assert.AreEqual(" hello", command.AnchorString); }
public void WhenSimpleRegex_ExpectSimpleResults(string expected, string line, string input) { ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() }; Command command = new Command(line); Assert.AreEqual(expected, engine.ApplySingleCommand(input, command)); }
public void WhenEnclosedQuotesInAnchor_ExpectQuotesRemoved() { Command command = new Command("/\" AnchorString \"/ from ~ to"); Assert.AreEqual(" AnchorString ", command.AnchorString); }
public void WhenGlobPickup_ExpectHeldValue() { Command command = new Command(@"a{name}d"); Pickup pickup = new Pickup(); pickup.CollectAllPickupsInLine("ab cd",command); string results = pickup.ReplacePickupsWithStoredValue("ab${name}"); Assert.AreEqual("abb c", results); }
public void WhenEnclosedQuotesInFrompattern_ExpectQuotesRemoved() { Command command = new Command("\" from \" ~ to"); Assert.AreEqual(" from ", command.SubjectRegex.ToString()); }
public void WhenTwoCapturesGiven_ExpectTwo() { Command command = new Command("^(?<name>[a-z]+).*?(?<second>[0-9]+)"); Pickup pickup = new Pickup(); pickup.CollectAllPickupsInLine("ab c 45", command); var results = pickup.ReplacePickupsWithStoredValue("${name}"); Assert.AreEqual("ab", results); results = pickup.ReplacePickupsWithStoredValue("${second}"); Assert.AreEqual("45",results); }
public void WhenEnclosedQuotesInTopattern_ExpectQuotesRemoved() { Command command = new Command("this ~ \" with blanks \""); Assert.AreEqual(" with blanks ", command.ReplacementString); }