예제 #1
0
파일: Tests.cs 프로젝트: TyOverby/TyParse
        public void AndCombinator()
        {
            Parser <bool> lightsAreOnParser = new Switch("lights");
            Parser <bool> fanIsOnParser     = new Switch("fan");
            Parser <(bool lights, bool fan)> lightsAndFanParser = Combinators.And(lightsAreOnParser, fanIsOnParser);

            // command line: --lights --fan
            var both = lightsAndFanParser.AssumeGoodParse("--lights", "--fan");

            Console.WriteLine($"lights : {both.lights} | fan : {both.fan}");
            both.lights.Should().BeTrue(); /* hide */
            both.fan.Should().BeTrue();    /* hide */

            // command line: --lights
            both = lightsAndFanParser.AssumeGoodParse("--lights");
            Console.WriteLine($"lights : {both.lights} | fan : {both.fan}");
            both.lights.Should().BeTrue(); /* hide */
            both.fan.Should().BeFalse();   /* hide */

            // command line: <empty>
            both = lightsAndFanParser.AssumeGoodParse(/* no args! */);
            Console.WriteLine($"lights : {both.lights} | fan : {both.fan}");
            both.lights.Should().BeFalse(); /* hide */
            both.fan.Should().BeFalse();    /* hide */
        }
예제 #2
0
        public void AnyTest()
        {
            var parser = Combinators.Any(Chars.Char('I'), Chars.Char('J'), Chars.Char('K'));

            Assert.IsTrue(parser("I".AsPlainCharStream()).Success());
            Assert.IsTrue(parser("J".AsPlainCharStream()).Success());
            Assert.IsTrue(parser("K".AsPlainCharStream()).Success());
            Assert.IsFalse(parser("L".AsPlainCharStream()).Success());
            Assert.IsFalse(parser("M".AsPlainCharStream()).Success());
            Assert.IsFalse(parser("".AsPlainCharStream()).Success());
        }
예제 #3
0
 public void TestY() =>
 Enumerable.Range(0, 5)
 .Select(x => (int)Math.Pow(2, x))
 .Select(Combinators.Y <int, int>(factorial => n => n == 0 ? 1 : n * factorial(n - 1)))
 .ShouldBe(new[] { 1, 2, 24, 40320, 2004189184 });
예제 #4
0
 public void TestS() =>
 Combinators.S <int, int, int>(x => y => x + y)(x => x * x)(10).ShouldBe(110);
예제 #5
0
 public void TestW() =>
 Combinators.W <int, int>(x => y => x * y)(6).ShouldBe(36);
예제 #6
0
 public void TestK() =>
 Combinators.K <string, int>("x")(0).ShouldBe("x");
예제 #7
0
 public void TestC()
 {
     Combinators.C <string, int, string[]>(x => y => Enumerable.Repeat(x, y).ToArray())(3)(null)
     .ShouldBe(new string[] { null, null, null });
 }
예제 #8
0
 public void TestB() =>
 Combinators.B <int, string, int>(int.Parse)(x => x.ToString())(10).ShouldBe(10);
예제 #9
0
 public void TestI() =>
 Combinators.I("id").ShouldBe("id");
예제 #10
0
 public void TestB() =>
 Combinators.B <int, string, int>(int.Parse)(ToString <int>(true))(10).ShouldBe(10);