Exemplo n.º 1
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleRow printDelimiter
        public static void print()
        {
            const String input = "Socialism,Communism,Fascism";

            using (Pnyx p = new Pnyx())
            {
                p.readString(input);
                p.parseCsv();
                p.print("Failed Systems: $1, $2, and $3");
                p.writeStdout();
            }
            // outputs: Failed Systems: Socialism, Communism, and Fascism
        }
Exemplo n.º 2
0
        public void csvInOut()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS);
                p.parseCsv();
                actual = p.processToString();
            }

            Assert.Equal(PLANETS_GODS, actual);
        }
Exemplo n.º 3
0
 public static void helloWorld()
 {
     using (Pnyx p = new Pnyx())
     {
         p.readString("Hello World.");
         p.sed("World", "World, with love from Pnyx.."); // transforms each line
         p.grep("world", caseSensitive: false);          // filters each line
         p.writeStdout();
         p.compile();                                    // Builds processors and wires filters together
         p.process();                                    // Runs processors (All IO is done here)
     }
     // outputs: Hello World, with love from Pnyx...
 }
Exemplo n.º 4
0
        public void columnDefinition(bool maxWidth, bool hasHeaderRow, bool minWidth, bool nullable, String expected)
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(ECONOMIC_FREEDOM);
                p.parseTab(hasHeader: hasHeaderRow);
                p.columnDefinition(null, maxWidth, hasHeaderRow, minWidth, nullable, swapRowsAndColumns: false);
                actual = p.processToString();
            }
            Assert.Equal(expected, actual);
        }
Exemplo n.º 5
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleRow printDelimiter
        public static void printDelimiter()
        {
            const String input = "col1,\"Column, zwei\"";

            using (Pnyx p = new Pnyx())
            {
                p.readString(input);
                p.parseCsv();
                p.printDelimiter("|");
                p.writeStdout();
            }
            // outputs: col1|Column, zwei
        }
Exemplo n.º 6
0
        public void count(String source, int expected)
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(source);
                p.countLines();
                actual = p.processToString();
            }

            Assert.Equal(expected, int.Parse(actual));
        }
Exemplo n.º 7
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleLine embeddedNewlineParseCsv
        public static void embeddedNewlineAutoAsCsv()
        {
            const String input = "a,\"Long\nText\n\"";

            using (Pnyx p = new Pnyx())
            {
                p.readString(input); // CsvStreamToRowProcessor (auto-wired)
                p.parseCsv(strict: false);
                p.selectColumns(2, 1);
                p.writeStdout();
            }
            // "Long\nText\n",a
        }
Exemplo n.º 8
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleLine embeddedNewlineAsCsv
        public static void embeddedNewlineAsCsv()
        {
            const String input = "a,\"Long\nText\n\"";

            using (Pnyx p = new Pnyx())
            {
                p.asCsv(p2 => p.readString(input)); // CsvStreamToRowProcessor
                p.selectColumns(2, 1);
                p.writeStdout();
            }
            // outputs:
            // "Long\nText\n",a
        }
Exemplo n.º 9
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleLine rowConverter
        public static void rowConverter()
        {
            const String input = "set x := (set == 0 ? 0 : 100 / set)";

            using (Pnyx p = new Pnyx())
            {
                p.readString(input);
                p.lineToRow(new ExampleRowConverter());
                p.withColumns(p2 => p2.sed("set[ ]*", "var ", "i"), 1); // replace set from first column
                p.writeStdout();                                        // internally converts back to line
            }
            // outputs: var x := (set == 0 ? 0 : 100 / set)
        }
Exemplo n.º 10
0
        public void setEndRow()
        {
            TestEndRow processor = new TestEndRow();

            using (Pnyx p = new Pnyx())
            {
                p.readString("a,1\nb,2\nc,3");
                p.parseCsv();
                p.endRow(processor);
            }

            Assert.Equal("a|1\nb|2\nc|3\nEOF\n", processor.ToString());
        }
Exemplo n.º 11
0
        public void csvInOutFixFormatting()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS_FORMAT_ISSUES);
                p.parseCsv(strict: false);
                actual = p.processToString();
            }

            Assert.Equal(PLANETS_GODS, actual); // verifies that output is formatted properly, even if input is loose
        }
Exemplo n.º 12
0
        public void countRow()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS);
                p.parseCsv();
                p.countLines();
                actual = p.processToString();
            }

            Assert.Equal("7,7,7", actual.TrimEnd());
        }
Exemplo n.º 13
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleSettings instance
        public static void instance()
        {
            const String input = "line one\nline two";

            using (Pnyx p = new Pnyx())
            {
                p.setSettings(stdIoDefault: true);
                p.readString(input);
                p.process(); // automatically writes to STD-OUT
            }
            // outputs STD-OUT:
            // line one
            // line two
        }
Exemplo n.º 14
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleLine parseDelimiter
        public static void parseDelimiter()
        {
            const String input = "a|b|c|d|e|f|g";

            using (Pnyx p = new Pnyx())
            {
                p.readString(input);
                p.sed("[aceg]", @"\0\0", "gi");     // duplicates every other char
                p.parseDelimiter("|");
                p.print("$1,$3,$5,$7|$2,$4,$6");
                p.writeStdout();
            }
            // outputs: aa,cc,ee,gg|b,d,f
        }
Exemplo n.º 15
0
        public void rowFilterShimOr()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS_TITANS);
                p.parseCsv();
                p.grep("u");                        // U needs to be present in any column
                actual = p.processToString();
            }

            Assert.Equal(PLANETS_GODS_TITANS, actual);
        }
Exemplo n.º 16
0
        public void tabInOutExplicit()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(ECONOMIC_FREEDOM);
                p.parseTab();
                p.printTab();
                actual = p.processToString();
            }

            Assert.Equal(ECONOMIC_FREEDOM, actual);
        }
Exemplo n.º 17
0
        public void lineTransform()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(EARTH);
                p.sed(",", "\t", "g");
                actual = p.processToString();
            }

            const String expected = "Gaia\tTerra\t\"Mother goddess of the earth\"";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 18
0
        public void printLine()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString("Logic");
                p.print("$0 $0");
                actual = p.processToString();
            }

            const String expected = "Logic Logic";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 19
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleRow filterFunc
        public static void filterFunc()
        {
            const String input = @"Line one,KEEPER
Line two,Loser
";

            using (Pnyx p = new Pnyx())
            {
                p.readString(input);
                p.parseCsv();
                p.rowFilterFunc(x => TextUtil.isUpperCase(x[1]));
                p.writeStdout();
            }
            // outputs:
            // "Line one",KEEPER
        }
Exemplo n.º 20
0
        public void lineFilter()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(MAGNA_CARTA);
                p.grep("god", caseSensitive: false);
                actual = p.processToString();
            }

            const String expected = @"KNOW THAT BEFORE GOD, for the health of our soul and those of our ancestors and heirs, 
to the honour of God, the exaltation of the holy Church, and the better ordering of our";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 21
0
        public void columnToLine()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(EARTH);
                p.parseCsv();
                p.printColumn(2);
                actual = p.processToString();
            }

            const String expected = "Terra";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 22
0
        public void columns()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(EARTH);
                p.parseCsv();
                p.selectColumns(3, 2, 1);
                actual = p.processToString();
            }

            const String expected = @"""Mother goddess of the earth"",Terra,Gaia";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 23
0
        public void printRow()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(EARTH);
                p.parseCsv();
                p.print("$3,$2,$1");
                actual = p.processToString();
            }

            const String expected = "Mother goddess of the earth,Terra,Gaia";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 24
0
        public void rowTransform()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(EARTH);
                p.parseCsv();
                p.sed("Ter.*", "Forma", "g");
                actual = p.processToString();
            }

            const String expected = "Gaia,Forma,\"Mother goddess of the earth\"";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 25
0
        public void rowFilter()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS);
                p.parseCsv();
                p.grep("war");
                actual = p.processToString();
            }

            const String expected = "Ares,Mars,\"Hated god of war\"\r\n";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 26
0
        public void teeServile()
        {
            MemoryStream capture = new MemoryStream();

            Pnyx teeP = null;
            Pnyx p    = new Pnyx();

            p.readString(PLANETS_GODS_TITANS);
            p.tee(pn =>
            {
                teeP = pn;
                pn.writeStream(capture);
            });

            Assert.Equal(FluentState.CompiledServile, teeP.state);
            Assert.Throws <IllegalStateException>(() => teeP.process());
        }
Exemplo n.º 27
0
        public void or()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS);
                p.or(pn =>
                {
                    pn.grep("Cronus");
                    pn.grep("Uranus");
                });
                actual = p.processToString();
            }

            Assert.Equal(PLANETS_GODS_TITANS, actual);
        }
Exemplo n.º 28
0
        public void tabHasHeader()
        {
            const String source = "headerA\theaderB\nvalueA1\tvalueB1";
            String       actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(source);
                p.parseTab(hasHeader: true);
                p.sed("[AB]", "X");
                actual = p.processToString();
            }

            const String expected = "headerA\theaderB\nvalueX1\tvalueX1";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 29
0
        public void lineToRow()
        {
            String       actual;
            const String tabSource = "1) Be fruitful and multiply\t2) and fill the earth and subdue it";

            using (Pnyx p = new Pnyx())
            {
                p.readString(tabSource);
                p.sed("[\t]", ",", "g");
                p.parseCsv(strict: false);
                actual = p.processToString();
            }

            const String expected = @"""1) Be fruitful and multiply"",""2) and fill the earth and subdue it""";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 30
0
        public void lineBuffering()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.setSettings(defaultNewline: StreamInformation.newlineString(NewLineEnum.Windows));
                p.readString(EARTH);
                p.sedAppendLine("The Lord is my shepherd");
                actual = p.processToString();
            }

            const String expected = @"Gaia,Terra,""Mother goddess of the earth""
The Lord is my shepherd";

            Assert.Equal(expected, actual);
        }