Exemplo n.º 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);
 }
Exemplo n.º 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;
 }
Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 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;
        }
Exemplo n.º 5
0
 private bool isCandidateForPrinting(string line, Command command)
 {
     return Regex.IsMatch(line, command.AnchorString);
 }
Exemplo n.º 6
0
 public void WhenNoAnchor_ExpectNoValue()
 {
     Command command = new Command("  a~b");
     Assert.AreEqual("", command.AnchorString);
 }
Exemplo n.º 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);
 }
Exemplo n.º 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);
 }
Exemplo n.º 9
0
 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));
 }
Exemplo n.º 10
0
 public void WhenAnchorAndSubjectInPattern()
 {
     Command command = new Command("/ hello/ a~b");
     Assert.AreEqual("a", command.SubjectString);
 }
Exemplo n.º 11
0
 public void WhenUsingTildaInTargetAndTemplateTarget_ExpectSplitOnTemplate()
 {
     Command command = new Command("/ hello/ (..) c-> b~dog");
     Assert.AreEqual("b~dog", command.ReplacementString);
 }
Exemplo n.º 12
0
 public void WhenUsingTildaInSubjectAndTemplateTarget_ExpectSplitOnTilda()
 {
     Command command = new Command("/ hello/ a~ c->b");
     Assert.AreEqual("a", command.SubjectString);
 }
Exemplo n.º 13
0
 public void WhenSubjectInPatternWIthSpaces()
 {
     Command command = new Command(" a ~b");
     Assert.AreEqual("a", command.SubjectString);
 }
Exemplo n.º 14
0
 public void WhenReplacementInTopattern()
 {
     Command command = new Command("/ hello/ a~b");
     Assert.AreEqual("b", command.ReplacementString);
 }
Exemplo n.º 15
0
 public void WhenOnlySubjectInPattern()
 {
     Command command = new Command(" a");
     Assert.AreEqual("a", command.SubjectString);
 }
Exemplo n.º 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));
 }
Exemplo n.º 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));
 }
Exemplo n.º 18
0
 public void WhenAnchorInTopattern_ExpectAnchor()
 {
     Command command = new Command("/ hello/ a~b");
     Assert.AreEqual(" hello", command.AnchorString);
 }
Exemplo n.º 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));
 }
Exemplo n.º 20
0
 public void WhenEnclosedQuotesInAnchor_ExpectQuotesRemoved()
 {
     Command command = new Command("/\" AnchorString  \"/ from  ~ to");
     Assert.AreEqual(" AnchorString  ", command.AnchorString);
 }
Exemplo n.º 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);
 }
Exemplo n.º 22
0
 public void WhenEnclosedQuotesInFrompattern_ExpectQuotesRemoved()
 {
     Command command = new Command("\" from  \" ~ to");
     Assert.AreEqual(" from  ", command.SubjectRegex.ToString());
 }
Exemplo n.º 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);
 }
Exemplo n.º 24
0
 public void WhenEnclosedQuotesInTopattern_ExpectQuotesRemoved()
 {
     Command command = new Command("this ~ \" with blanks \"");
     Assert.AreEqual(" with blanks ", command.ReplacementString);
 }