public CalculateIntellisenseProvider(ISyntaxTreeBuilderHelper syntaxTreeBuilderHelper)
        {
            _syntaxTreeBuilderHelper = syntaxTreeBuilderHelper;
            IntellisenseProviderType = IntellisenseProviderType.NonDefault;
            IFrameworkRepository<IFunction> functionList = MathOpsFactory.FunctionRepository();
            functionList.Load();
            bool creatingFunctions = false;

            if(_functionNames == null)
            {
                creatingFunctions = true;
                _functionNames = new HashSet<string>(StringComparer.Ordinal);
            }

            IntellisenseResult = functionList.All().Select(currentFunction =>
            {
                string description = currentFunction.Description;
                string dropDownDescription = description;
                if(description != null && description.Length > 80) dropDownDescription = description.Substring(0, 77) + "...";
                if(creatingFunctions) _functionNames.Add(currentFunction.FunctionName);
                IntellisenseProviderResult result = new IntellisenseProviderResult(this, currentFunction.FunctionName, dropDownDescription, description, currentFunction.arguments != null ? currentFunction.arguments.ToArray() : new string[0], currentFunction.ArgumentDescriptions != null ? currentFunction.ArgumentDescriptions.ToArray() : new string[0]);
                return result;
            }).OrderBy(p => p.Name).ToList();
        }
        public void IntellisenseBoxDoesntCrashWhenInsertingResultsGivenAProviderThatThrowsAnException()
        {
            Mock<IIntellisenseProvider> intellisenseProvider = new Mock<IIntellisenseProvider>();
            intellisenseProvider.Setup(
                a => a.PerformResultInsertion(It.IsAny<string>(), It.IsAny<IntellisenseProviderContext>()))
                                .Throws(new Exception());
            intellisenseProvider.Setup(a => a.HandlesResultInsertion).Returns(true);

            IntellisenseProviderResult intellisenseProviderResult =
                new IntellisenseProviderResult(intellisenseProvider.Object, "City", "cake");

            IntellisenseTextBox textBox = new IntellisenseTextBox();
            textBox.CreateVisualTree();
            textBox.InsertItem(intellisenseProviderResult, true);

            // When exepctions are thrown, no results are to be displayed
            Assert.AreEqual(0, textBox.Items.Count, "Expected [ 0 ] But got [ " + textBox.Items.Count + " ]");
            //The desired result is that an exception isn't thrown
        }
        public void InsertItemExpectedTextboxTextChanged_SpaceInFieldName_ErrorStatusUpdated()
        {
            const string ExpectedText = "[[City(). Name]]";
            Mock<IResourceModel> mockResourceModel = new Mock<IResourceModel>();
            mockResourceModel.Setup(model => model.DataList).Returns("<ADL><City><Name></Name></City></ADL>");

            var dataListViewModel = new DataListViewModel();
            dataListViewModel.InitializeDataListViewModel(mockResourceModel.Object);
            DataListSingleton.SetDataList(dataListViewModel);
            Mock<IIntellisenseProvider> intellisenseProvider = new Mock<IIntellisenseProvider>();
            intellisenseProvider.Setup(a => a.HandlesResultInsertion).Returns(true);
            intellisenseProvider.Setup(
                a => a.PerformResultInsertion(It.IsAny<string>(), It.IsAny<IntellisenseProviderContext>())).Returns(ExpectedText);

            IntellisenseProviderResult intellisenseProviderResult =
                new IntellisenseProviderResult(intellisenseProvider.Object, ExpectedText, "cake");

            IntellisenseTextBox textBox = new IntellisenseTextBox();
            textBox.CreateVisualTree();
            textBox.InsertItem(intellisenseProviderResult, true);

            Thread.Sleep(250);
            Thread.Sleep(100);

            Assert.AreEqual(ExpectedText, textBox.Text, "Expected [ " + ExpectedText + " ] But got [ " + textBox.Text + " ]");
            Assert.IsTrue(textBox.HasError, "Expected [ True ] But got [ " + textBox.HasError + " ]");
        }
        public void IntellisenseTextBox_InsertItem_InsertDateTimePartsIn_InsertsCorrectly()
        {
            //------------Setup for test--------------------------            
            Mock<IIntellisenseProvider> intellisenseProvider = new Mock<IIntellisenseProvider>();

            intellisenseProvider.Setup(a => a.HandlesResultInsertion).Returns(false);

            IntellisenseProviderResult intellisenseProviderResult =
                new IntellisenseProviderResult(intellisenseProvider.Object, "DW", "DW");
            //------------Execute Test---------------------------
            IntellisenseTextBox textBox = new IntellisenseTextBox();
            textBox.CreateVisualTree();
            textBox.IsOpen = true;
            textBox.Text = "d YY mm";
            textBox.CaretIndex = 1;
            textBox.InsertItem(intellisenseProviderResult, false);
            //------------Assert Results-------------------------
            Assert.AreEqual("DW YY mm", textBox.Text);
        }
        public void InsertItemExpectedTextboxTextChangedAndErrorStatusUpdated()
        {
            var mockDataListViewModel = new Mock<IDataListViewModel>();
            mockDataListViewModel.Setup(model => model.Resource).Returns(new Mock<IResourceModel>().Object);
            DataListSingleton.SetDataList(mockDataListViewModel.Object);
            const string ExpectedText = "[[City()";
            Mock<IIntellisenseProvider> intellisenseProvider = new Mock<IIntellisenseProvider>();
            intellisenseProvider.Setup(a => a.HandlesResultInsertion).Returns(true);
            intellisenseProvider.Setup(
                a => a.PerformResultInsertion(It.IsAny<string>(), It.IsAny<IntellisenseProviderContext>())).Returns(ExpectedText);

            IntellisenseProviderResult intellisenseProviderResult =
                new IntellisenseProviderResult(intellisenseProvider.Object, ExpectedText, "cake");

            IntellisenseTextBox textBox = new IntellisenseTextBox();
            textBox.CreateVisualTree();
            textBox.InsertItem(intellisenseProviderResult, true);

            Thread.Sleep(250);
            Thread.Sleep(100);

            Assert.AreEqual(ExpectedText, textBox.Text, "Expected [ " + ExpectedText + " ] But got [ " + textBox.Text + " ]");
            Assert.AreEqual(true, textBox.HasError, "Expected [ True ] But got [ " + textBox.HasError + " ]");
        }