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 */ }
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()); }
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 });
public void TestS() => Combinators.S <int, int, int>(x => y => x + y)(x => x * x)(10).ShouldBe(110);
public void TestW() => Combinators.W <int, int>(x => y => x * y)(6).ShouldBe(36);
public void TestK() => Combinators.K <string, int>("x")(0).ShouldBe("x");
public void TestC() { Combinators.C <string, int, string[]>(x => y => Enumerable.Repeat(x, y).ToArray())(3)(null) .ShouldBe(new string[] { null, null, null }); }
public void TestB() => Combinators.B <int, string, int>(int.Parse)(x => x.ToString())(10).ShouldBe(10);
public void TestI() => Combinators.I("id").ShouldBe("id");
public void TestB() => Combinators.B <int, string, int>(int.Parse)(ToString <int>(true))(10).ShouldBe(10);