Example #1
0
        public void FeatureMatcherGeneratesCorrectTypeNameInDescription()
        {
            var sut = Describe.Object <AnotherFlatClass>()
                      .Property(x => x.Id, new FakeMatcher <Guid>(true, "", i => ""));

            sut.ShouldHaveDescription(Starts.With("a(n) AnotherFlatClass where:"));
        }
        public void It_Can_Handle_Recursive_Rules()
        {
            IRuleParser ListParser()
            {
                var list = new List <string>();

                return(Starts.With
                       (
                           A.Word(list.Add),
                           One.Of
                           (
                               The.Symbol("}"),
                               A.Rule <List <string> >(ListParser, rest => list.AddRange(rest))
                           )
                       ).ReturnsNode(list));
            }

            var    parser = ListParser();
            string src    = "a b c d e f g}";
            var    tokens = new Lexer().ToTokens(src);

            var resultList = parser
                             .FeedAll(tokens)
                             .AssertComplete <List <string> >();

            var expectedList = new string[] { "a", "b", "c", "d", "e", "f", "g" };

            Assert.Equal(expectedList, resultList);
        }
Example #3
0
        public void FeatureMatcherGeneratesCorrectTypeNameInMismatchDescription()
        {
            var sut = Describe.Object <AnotherFlatClass>()
                      .Property(x => x.Id, new FakeMatcher <Guid>(false, "", i => ""));

            sut.ShouldHaveMismatchDescriptionForValue(new AnotherFlatClass(), Starts.With("was a(n) AnotherFlatClass where:"));
        }
        public void No_match_if_string_does_not_end_with_substring()
        {
            var matcher = Starts.With("bob");

            var matches = matcher.Matches("the cat sat on the mat");

            Assert.That(matches, Is.False());
        }
        public void Describe_to()
        {
            var matcher     = Starts.With("bob");
            var description = new StringDescription();

            matcher.DescribeTo(description);

            Assert.That(description.ToString(), Is.EqualTo("a string starting with \"bob\""));
        }
        public void Describe_mismatch()
        {
            var matcher     = Starts.With("bob");
            var description = new StringDescription();

            matcher.DescribeMismatch("the cat sat on the mat", description);

            Assert.That(description.ToString(), Is.EqualTo("was \"the cat sat on the mat\""));
        }
Example #7
0
        public void Describe_mismatch_if_thrown_exception_does_not_match_predicate()
        {
            var matcher     = new ThrowsMatcher <ArgumentNullException>().With(e => e.Message == "something else");
            var description = new StringDescription();

            matcher.DescribeMismatch(DoIt, description);

            NHAssert.That(description.ToString(), Starts.With("the exception was of the correct type, but did not match the predicate"));
        }
Example #8
0
        public void Describe_mismatch_if_action_throws_different_exception()
        {
            var matcher     = new ThrowsMatcher <NullReferenceException>();
            var description = new StringDescription();

            matcher.DescribeMismatch(DoIt, description);

            NHAssert.That(description.ToString(), Starts.With("an exception of type System.ArgumentNullException was thrown"));
        }
        public void You_Can_Compose_Multiple_Rules()
        {
            var funcDef  = new FunctionDefinition();
            var classDef = new ClassDefinition();

            classDef.Functions = new List <FunctionDefinition>();

            var funcParser = Starts
                             .With
                             (
                The.Keyword("function"),
                A.Word(n => funcDef.Name = n),
                The.Symbol("("),
                The.Symbol(")"),
                The.Symbol("{"),
                The.Symbol("}")
                             )
                             .ReturnsNode(funcDef);

            var classParser = Starts
                              .With
                              (
                The.Keyword("class"),
                A.Word(n => classDef.Name = n),
                The.Symbol("{"),
                A.Rule <FunctionDefinition>(() => funcParser, classDef.Functions.Add),
                The.Symbol("}")
                              )
                              .ReturnsNode(classDef);

            // Generate some tokens to parse with the parser we just made
            string src =
                @"
        class FooBar
        {
          function DoThing() {}
        }
      ";
            var tokens = new Lexer().ToTokens(src).ToArray();
            var result = classParser.FeedAll(tokens);

            // Assert that it correctly did stuff.
            result.AssertComplete();

            Assert.Equal(classDef, result.node);
            Assert.Equal("FooBar", classDef.Name);

            Assert.Contains(funcDef, classDef.Functions);
            Assert.Equal("DoThing", funcDef.Name);
        }
        public void One_Of_Works()
        {
            IRuleParser AlphabetParser()
            {
                return(Starts
                       .With
                       (
                           The.Word("a"),
                           The.Word("b"),
                           The.Word("c"),
                           The.Word("d")
                       )
                       .ReturnsNode("abcd"));
            }

            IRuleParser AberahamParser()
            {
                return(Starts
                       .With
                       (
                           The.Word("a"),
                           The.Word("b"),
                           The.Word("e"),
                           The.Word("raham") // laziness
                       )
                       .ReturnsNode("aberaham"));
            }

            var node        = new TestNode();
            var oneOfParser = Starts
                              .With
                              (
                One.Of
                (
                    A.Rule <string>(AlphabetParser, t => node.name = t),
                    A.Rule <string>(AberahamParser, t => node.name = t)
                )
                              )
                              .ReturnsNode(node);

            string src    = "a b e raham";
            var    tokens = new Lexer().ToTokens(src);
            var    result = oneOfParser.FeedAll(tokens);

            var resultNode = result.AssertComplete <TestNode>();

            Assert.Equal(node, resultNode);
            Assert.Equal("aberaham", resultNode.name);
        }
        public void You_Can_Express_An_Empty_Class()
        {
            var classDef = new ClassDefinition();

            var classParser = Starts
                              .With
                              (
                The.Keyword("class"),
                A.Word(name => classDef.Name = name),
                The.Symbol("{"),
                The.Symbol("}")
                              )
                              .ReturnsNode(classDef);

            // Generate some tokens to parse with the parser we just made
            string src    = "class FooBar {}";
            var    tokens = new Lexer().ToTokens(src);
            var    result = classParser.FeedAll(tokens);

            // Assert that it correctly did stuff.
            result.AssertComplete();
            Assert.Equal(classDef, result.node);
            Assert.Equal("FooBar", classDef.Name);
        }
 public void Match_if_string_ends_with_substring()
 {
     Assert.That("the cat sat on the mat", Starts.With("the"));
 }
 public void Case_insensitive_match_if_string_ends_with_substring()
 {
     Assert.That("the cat sat on the mat", Starts.With("The").CaseInsensitive());
 }