Exemple #1
0
 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);
 }
Exemple #2
0
 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;
 }
Exemple #3
0
        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);
                }
            }
        }
Exemple #4
0
        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);
 }
Exemple #6
0
 public void WhenNoAnchor_ExpectNoValue()
 {
     Command command = new Command("  a~b");
     Assert.AreEqual("", command.AnchorString);
 }
Exemple #7
0
 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);
 }
Exemple #8
0
 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));
 }
Exemple #10
0
 public void WhenAnchorAndSubjectInPattern()
 {
     Command command = new Command("/ hello/ a~b");
     Assert.AreEqual("a", command.SubjectString);
 }
Exemple #11
0
 public void WhenUsingTildaInTargetAndTemplateTarget_ExpectSplitOnTemplate()
 {
     Command command = new Command("/ hello/ (..) c-> b~dog");
     Assert.AreEqual("b~dog", command.ReplacementString);
 }
Exemple #12
0
 public void WhenUsingTildaInSubjectAndTemplateTarget_ExpectSplitOnTilda()
 {
     Command command = new Command("/ hello/ a~ c->b");
     Assert.AreEqual("a", command.SubjectString);
 }
Exemple #13
0
 public void WhenSubjectInPatternWIthSpaces()
 {
     Command command = new Command(" a ~b");
     Assert.AreEqual("a", command.SubjectString);
 }
Exemple #14
0
 public void WhenReplacementInTopattern()
 {
     Command command = new Command("/ hello/ a~b");
     Assert.AreEqual("b", command.ReplacementString);
 }
Exemple #15
0
 public void WhenOnlySubjectInPattern()
 {
     Command command = new Command(" a");
     Assert.AreEqual("a", command.SubjectString);
 }
Exemple #16
0
 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));
 }
Exemple #17
0
 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));
 }
Exemple #18
0
 public void WhenAnchorInTopattern_ExpectAnchor()
 {
     Command command = new Command("/ hello/ a~b");
     Assert.AreEqual(" hello", command.AnchorString);
 }
Exemple #19
0
 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));
 }
Exemple #20
0
 public void WhenEnclosedQuotesInAnchor_ExpectQuotesRemoved()
 {
     Command command = new Command("/\" AnchorString  \"/ from  ~ to");
     Assert.AreEqual(" AnchorString  ", command.AnchorString);
 }
Exemple #21
0
 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);
 }
Exemple #22
0
 public void WhenEnclosedQuotesInFrompattern_ExpectQuotesRemoved()
 {
     Command command = new Command("\" from  \" ~ to");
     Assert.AreEqual(" from  ", command.SubjectRegex.ToString());
 }
Exemple #23
0
 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);
 }
Exemple #24
0
 public void WhenEnclosedQuotesInTopattern_ExpectQuotesRemoved()
 {
     Command command = new Command("this ~ \" with blanks \"");
     Assert.AreEqual(" with blanks ", command.ReplacementString);
 }