Пример #1
0
 public void TemplateWithPickupOnlyNoPrinting()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile("/Today (?<date>..)/;(.)->Date ${date}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "Today is the 5th.", "The 6th is tomorrow." });
     Assert.AreEqual("Date is\nDate is\n", results);
 }
Пример #2
0
 public void WhenGlobPickupAtEndOfLine_ExpectReplacedValue()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(@"{num=[0-9]+}{title};->${title}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "900  the end"});
     Assert.AreEqual("  the end\n", results);
 }
Пример #3
0
 public void WhenDifferentNamedPickups_ExpectReplacedValue()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(@"(?<name>[a-z]+) (?<name2>[a-z]+);(?<digit>[0-9]+)~${name}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "ab cd", "89ab" });
     Assert.AreEqual("ab cd\nabab\n", results);
 }
Пример #4
0
 public void WhenEmbeddedDelim_ExpectChangedDelim()
 {
     ParseCommandFile rf = new ParseCommandFile("delim=,; a,bc;   hello,world  ;  third , fourth ");
     List<Command> reps = rf.CommandList;
     Assert.AreEqual((new Regex("third".Trim(), RegexOptions.Compiled)).ToString(), reps[2].SubjectRegex.ToString());
     Assert.AreEqual("fourth", reps[2].ReplacementString);
 }
 public void WhenOFSUsed_ExpectDelimitedOutput(string input, string pattern, string expected)
 {
     PrintTokensInSourceFiles engine = new PrintTokensInSourceFiles() { sw = new WriteToString() };
         ParseCommandFile commands = new ParseCommandFile(pattern);
         string result = engine.ApplyCommandsToInputFileList(commands, new List<string> { input });
         Assert.AreEqual(expected, result);
 }
Пример #6
0
 public void WhenGlobPickupIsTooShort_ExpectNoValue()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(@"<{opentag}>;abc~${opentag}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "<>def", "abc here" });
     Assert.AreEqual("<>def\n here\n", results);
 }
 public void WhenNonRegexTokenGiven_ExpectSimpleOutput(string expected, string scantoken, string input)
 {
     PrintTokensInSourceFiles engine = new PrintTokensInSourceFiles() { sw = new WriteToString() };
         ParseCommandFile commands = new ParseCommandFile(scantoken);
         string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { input });
         Assert.AreEqual(expected, results);
 }
 public void WhenNoScanTokenGiven_ExpectNoChange()
 {
     PrintTokensInSourceFiles engine = new PrintTokensInSourceFiles { sw = new WriteToString() };
         ParseCommandFile commands = new ParseCommandFile("bye");
         string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "today bye bye birdie" });
         Assert.AreEqual("bye\nbye\n", results);
 }
 public void WhenNoMatchFound_ExpectNoOutput()
 {
     PrintTokensInSourceFiles engine = new PrintTokensInSourceFiles() { sw = new WriteToString() };
         ParseCommandFile commands = new ParseCommandFile("abc");
         string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "def" });
         Assert.AreEqual("", results);
 }
 public void WhenInvalidTokenGiven_ExpectException()
 {
     PrintTokensInSourceFiles engine = new PrintTokensInSourceFiles() { sw = new WriteToString() };
         ParseCommandFile commands = new ParseCommandFile("a[bc");
         string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "def" });
         Assert.AreEqual("", results);
 }
 public void WhenAnchorMatchFound_ExpectOutput()
 {
     PrintTokensInSourceFiles engine = new PrintTokensInSourceFiles() { sw = new WriteToString() };
         ParseCommandFile commands = new ParseCommandFile("/world/ [0-9]+");
         string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "def", "89 hello world", "hello 09182 worlds" });
         Assert.AreEqual("89\n09182\n", results);
 }
Пример #12
0
 public void LimitMatchesToDefault()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile("a~b;b~c;c~d;d~e");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "abcde" });
     Assert.AreEqual("eeeee\n", results);
 }
Пример #13
0
 public void WhenEmbeddedComment_ExpectCommentIgnored()
 {
     ParseCommandFile rf = new ParseCommandFile("#comment; a~bc");
     List<Command> reps = rf.CommandList;
     Assert.AreEqual((new Regex("a".Trim(), RegexOptions.Compiled)).ToString(), reps[0].SubjectRegex.ToString());
     Assert.AreEqual("bc", reps[0].ReplacementString);
 }
Пример #14
0
        public void WhenEnclosedQuotes_ExpectTrailingSpacesRetained()
        {
            ParseCommandFile rf = new ParseCommandFile(" \"a \" ~ b ");
            List<Command> reps = rf.CommandList;

            string result = reps[0].SubjectRegex.ToString();
            Assert.AreEqual("a ", result);
        }
Пример #15
0
 public void WhenEmbeddedDelim_ExpectAllReplaces()
 {
     ParseCommandFile rf = new ParseCommandFile("delim=,; a,b; b,c");
     List<Command> reps = rf.CommandList;
     Assert.IsTrue(reps.Count == 2);
     Assert.AreEqual((new Regex("b".Trim(), RegexOptions.Compiled)).ToString(), reps[1].SubjectRegex.ToString());
     Assert.AreEqual("c", reps[1].ReplacementString);
 }
Пример #16
0
 public void TemplateWithPickup()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile("(?<date>[0-9]+);(.)->Date is ${date}");
     commands.MaxReplacements = 3;
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "Today is the 5th.", "The 6th is tomorrow." });
     Assert.AreEqual("Date is 5\nDate is 6\n", results);
 }
Пример #17
0
 public void SetLimitMatchViaCommand()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(" MM = 1 ;a~b;b~c;c~d;d~e");
     Assert.AreEqual(1,commands.MaxReplacements);
     commands = new ParseCommandFile("MaxReplacements= 8 ;a~b;b~c;c~d;d~e");
     Assert.AreEqual(8,commands.MaxReplacements);
 }
Пример #18
0
 public void LimitMatchesResetsWithEachFile()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile("a~b;b~c;c~d;d~e");
     commands.MaxReplacements = 1;  // this override value should be used for each file read. Don't use value from previous file scan.
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "abcde", "kcde" });
     Assert.AreEqual("bbcde\nkdde\n", results);
 }
Пример #19
0
 public IFileAction GetFileAction(ParseCommandFile.RunningAs runas)
 {
     switch (runas) {
         case ParseCommandFile.RunningAs.Scanner:
             return new PrintTokensInSourceFiles();
         default:
             return new ReplaceTokens();
     }
 }
Пример #20
0
        static void Main(string[] args)
        {
            ParseCommandLine commandLine = new ParseCommandLine();
            commandLine.Init(args);
            if (commandLine.InputSourceList.Count == 0)
                Usage("No input sources given/recognized.");

            ParseCommandFile commands = new ParseCommandFile(commandLine.ReplacementFileName);
            IFileAction engine = (new FileActionFactory()).GetFileAction(commands.kgrepMode);
            engine.ApplyCommandsToInputFileList(commands, commandLine.InputSourceList);
        }
Пример #21
0
 public void ApplyCommandsToFile(ParseCommandFile commandFile, string filename)
 {
     IHandleInput sr = (new ReadFileFactory()).GetSource((filename));
     _maxReplacements = commandFile.MaxReplacements;
     string line;
     while ((line = sr.ReadLine()) != null) {
         string alteredLine = ApplyCommandsToLine(line, commandFile.CommandList);
         if (!String.IsNullOrEmpty(alteredLine)) sw.Write(alteredLine);
     }
     sr.Close();
 }
Пример #22
0
 public string ApplyCommandsToInputFileList(ParseCommandFile rf, List<string> inputFilenames)
 {
     try {
         foreach (string filename in inputFilenames) {
             ApplyCommandsToFile(rf, filename);
         }
     } catch (Exception e) {
         Console.WriteLine("{0}", e.Message);
     }
     return sw.Close();
 }
Пример #23
0
 private void ApplyCommandsToFile(ParseCommandFile rf, string filename)
 {
     IHandleInput sr = (new ReadFileFactory()).GetSource((filename));
     string line;
     while ((line = sr.ReadLine()) != null) {
         foreach (Command command in rf.CommandList) {
             if (isCandidateForPrinting(line, command))
                 sw.Write(ScanForTokens(line, command.SubjectRegex, command.OFS));
         }
     }
     sr.Close();
 }
Пример #24
0
 public void WhenGlobPickupsSpanThreeLines_ExpectReplacedValue()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(@"<title>{title}</title>; <isbn>{isbn}$; report ~ ${isbn} ${title}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "<isbn>97809", "<title>Answers</title>", "report" });
     Assert.AreEqual("<isbn>97809\n<title>Answers</title>\n97809 Answers\n", results);
 }
Пример #25
0
 public void WhenPickupAndRegexGiven_ExpectBothReplaced()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(@"^(\w)(.);..(.)~$1${2}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "abc" });
     Assert.AreEqual("cb\n", results);
 }
Пример #26
0
 public void WhenValidFieldContent_ExpectValidCommand(string line)
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(line);
     Assert.AreEqual(1, commands.CommandList.Count);
 }
Пример #27
0
 public void WhenTwoPickupSpansLines_ExpectReplacedValue()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(@"^(?<name>[a-z]+)~blue ${name};(?<digit>[0-9]+) ~${name}; blue~ ${name}${digit}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> {  "ab 89 cd" });
     Assert.AreEqual("ab89 ab ab cd\n", results);
 }
Пример #28
0
 public void WhenTwoPickupSpansLinesWithMultipleReferences_ExpectReplacedValue()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(@"^<chap=(?<tag>[a-z]+?)>~; end~chapter=${tag}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "<chap=a><last=z> end" });
     Assert.AreEqual("<last=z> chapter=a\n", results);
 }
Пример #29
0
 public void WhenReplacementPickupPresent_ExpectResults()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile("my bye~hi ${title}");
     Assert.IsTrue(commands.CommandList[0].IsPickupInReplacementString);
 }
Пример #30
0
 public void WhenPickupRepeats_ExpectReplacedValues()
 {
     ReplaceTokens engine = new ReplaceTokens() { sw = new WriteToString() };
     ParseCommandFile commands = new ParseCommandFile(@"^(?<name>[a-z]+)~c${name}${name}");
     string results = engine.ApplyCommandsToInputFileList(commands, new List<string> { "a" });
     Assert.AreEqual("caa\n", results);
 }