public void CheckContractIsOther(string contract, ContractKind kind)
        {
            var root = Syntax.ParseExpression(contract);
              var req = new CodeContractCollector(kind, Categories.SemanticCategories);

              req.Visit(root);

              Assert.IsTrue(req.Labels.Count == 1, "Expected a single contract clause for contract {0}", contract);
              Assert.IsFalse(
            req.Labels[0].Labels.Any(),
            string.Format("Contract '{0}' miscategorized. Label(s): {1}",
              contract,
              string.Join(",", req.Labels[0].Labels)));
        }
        public void CheckContract(string contract, ContractKind kind, Category expected)
        {
            var root = Syntax.ParseExpression(contract);
              var collector = new CodeContractCollector(kind, Categories.SemanticCategories);

              collector.Visit(root);

              Assert.AreEqual(1, collector.Labels.Count, "Expected a single top-level contract clause");

              var labels = collector.Labels[0].Labels.ToList();

              Assert.IsTrue(labels.Count > 0, string.Format("Contract {0} not labeled", contract));
              Assert.AreEqual(1, labels.Count, "Expected a single label for the contract");
              Assert.AreEqual(expected.Name, labels[0], string.Format("Contract {0} miscategorized", contract));
        }
        public void Combined_bounds_check_test()
        {
            // This test fails because the categorization is considering each top-level clause separately
              var root = Syntax.ParseExpression("Contract.Requires(idx >= 0 && idx < array.Length);");
              var req = new CodeContractCollector(ContractKind.Requires, Categories.SemanticCategories);

              req.Visit(root);

              Assert.AreEqual(2, req.Labels.Count(), "Expected categorization for both clauses");

              Assert.AreEqual(1, req.Labels[0].Labels.Count(), "Expected first clause to have a single label");
              Assert.AreEqual(1, req.Labels[1].Labels.Count(), "Expected second clause to have a single label");

              Assert.AreEqual(Categories.BoundsCheck.Name, req.Labels[0].Labels.First());
              Assert.AreEqual(Categories.BoundsCheck.Name, req.Labels[1].Labels.First());
        }
        public void Top_level_enumerable_clauses_tests()
        {
            var root = Syntax.ParseExpression("Contract.Requires(Contract.ForAll(xs, x => x >= 0 && x < array.Length));");
              var req = new CodeContractCollector(ContractKind.Requires, Categories.SemanticCategories);

              req.Visit(root);

              Assert.AreEqual(2, req.Labels.Count(), "Expected categorization for both top-level clauses");

              Assert.AreEqual(1, req.Labels[0].Labels.Count(), "Expected first clause to have a single label");
              Assert.AreEqual(1, req.Labels[1].Labels.Count(), "Expected second clause to have a single label");
        }
        public void Top_level_cojunct_and_enumerable_clauses_tests()
        {
            var root = Syntax.ParseExpression("Contract.Requires(y != null && Contract.ForAll(xs, x => x >= 0 && x < array.Length));");
              var req = new CodeContractCollector(ContractKind.Requires, Categories.SemanticCategories);

              req.Visit(root);

              Assert.AreEqual(3, req.Labels.Count(), "Expected categorization for both top-level clauses");
        }
        public void Top_level_clause_tests()
        {
            var root = Syntax.ParseExpression("Contract.Requires(x > 5 && y > 1);");
              var req = new CodeContractCollector(ContractKind.Requires, Categories.SemanticCategories);

              req.Visit(root);

              Assert.AreEqual(2, req.Labels.Count(), "Expected categorization for both clauses");

              Assert.AreEqual(1, req.Labels[0].Labels.Count(), "Expected first clause to have a single label");
              Assert.AreEqual(1, req.Labels[1].Labels.Count(), "Expected second clause to have a single label");
        }
        public void Three_clause_tests()
        {
            var root = Syntax.ParseExpression("Contract.Requires(x != null && (y != null && (z != null)));");
              var req = new CodeContractCollector(ContractKind.Requires, Categories.SemanticCategories);

              req.Visit(root);

              Assert.AreEqual(3, req.Labels.Count(), "Expected categorization for all three clauses");
        }
        public void Invalid_contract_tests()
        {
            var root = Syntax.ParseExpression("Contract.Requires(false);");
              var req = new CodeContractCollector(ContractKind.Requires, Categories.SemanticCategories);

              req.Visit(root);

              Assert.AreEqual(0, req.Labels.Count, "Invalid requires contract should be skipped");
        }