public void Create_CompletionSet_Variable()
        {
            var content = "{% for i in enum %} { {% endfor %}";

            var textBuffer        = new Mock <ITextBuffer>();
            var buffer_properties = new Mock <PropertyCollection>().Object;
            var text_document     = new Mock <ITextDocument>().Object;

            buffer_properties.AddProperty(typeof(ITextDocument), text_document);
            textBuffer.Setup <PropertyCollection>(t => t.Properties).Returns(buffer_properties);
            var current_snapshot = new Mock <ITextSnapshot>();

            current_snapshot.Setup <int>(s => s.Length).Returns(content.Length);
            current_snapshot.Setup <char[]>(s => s.ToCharArray(It.IsAny <int>(), It.IsAny <int>())).Returns((int pos, int len) => content.ToCharArray(pos, len));

            textBuffer.Setup <ITextSnapshot>(t => t.CurrentSnapshot).Returns(current_snapshot.Object);

            var broker = new NodeProviderBroker();

            var source = new Source(broker, textBuffer.Object);

            Assert.IsInstanceOf <Source>(source);

            var session            = new Mock <ICompletionSession>();
            var session_properties = new Mock <PropertyCollection>().Object;

            session_properties.AddProperty(typeof(CompletionContext), CompletionContext.Variable);
            session.Setup <PropertyCollection>(s => s.Properties).Returns(session_properties);

            var tracking_point = new Mock <ITrackingPoint>();

            tracking_point.Setup <SnapshotPoint>(t => t.GetPoint(It.IsAny <ITextSnapshot>())).Returns((ITextSnapshot snapshot) => new SnapshotPoint(snapshot, 21));
            session.Setup <ITrackingPoint>(s => s.GetTriggerPoint(It.IsAny <ITextBuffer>())).Returns(tracking_point.Object);

            var completionSets = new List <CompletionSet>();

            //var node = broker.GetNodeProvider(textBuffer.Object).GetNodes(new SnapshotPoint(current_snapshot.Object, 21), n => true).FindLast(n => true);

            //Assert.IsNotNull(node);

            source.AugmentCompletionSession(session.Object, completionSets);

            Assert.AreEqual(1, completionSets.Count);
        }
        public void Create_CompletionSet_Variable()
        {
            var content = "{% for i in enum %} { {% endfor %}";

            var textBuffer = new Mock<ITextBuffer>();
            var buffer_properties = new Mock<PropertyCollection>().Object;
            var text_document = new Mock<ITextDocument>().Object;
            buffer_properties.AddProperty(typeof(ITextDocument), text_document);
            textBuffer.Setup<PropertyCollection>(t => t.Properties).Returns(buffer_properties);
            var current_snapshot = new Mock<ITextSnapshot>();
            current_snapshot.Setup<int>(s => s.Length).Returns(content.Length);
            current_snapshot.Setup<char[]>(s => s.ToCharArray(It.IsAny<int>(), It.IsAny<int>())).Returns((int pos, int len) => content.ToCharArray(pos, len));

            textBuffer.Setup<ITextSnapshot>(t => t.CurrentSnapshot).Returns(current_snapshot.Object);

            var broker = new NodeProviderBroker();

            var source = new Source(broker, textBuffer.Object);
            Assert.IsInstanceOf<Source>(source);

            var session = new Mock<ICompletionSession>();
            var session_properties = new Mock<PropertyCollection>().Object;
            session_properties.AddProperty(typeof(CompletionContext), CompletionContext.Variable);
            session.Setup<PropertyCollection>(s => s.Properties).Returns(session_properties);

            var tracking_point = new Mock<ITrackingPoint>();
            tracking_point.Setup<SnapshotPoint>(t => t.GetPoint(It.IsAny<ITextSnapshot>())).Returns((ITextSnapshot snapshot) => new SnapshotPoint(snapshot, 21));
            session.Setup<ITrackingPoint>(s => s.GetTriggerPoint(It.IsAny<ITextBuffer>())).Returns(tracking_point.Object);

            var completionSets = new List<CompletionSet>();

            //var node = broker.GetNodeProvider(textBuffer.Object).GetNodes(new SnapshotPoint(current_snapshot.Object, 21), n => true).FindLast(n => true);

            //Assert.IsNotNull(node);

            source.AugmentCompletionSession(session.Object, completionSets);

            Assert.AreEqual(1, completionSets.Count);
        }
        private void CompletionSet_TestBase(string content, CompletionContext context, int position, Type completion_set, string[] display_values, string[] insert_values, string[] builder_values)
        {
            var textBuffer = new Mock <ITextBuffer>();

            // mock current snapshot - always return the content
            var current_snapshot = new Mock <ITextSnapshot>();

            current_snapshot.Setup <int>(s => s.Length).Returns(content.Length);
            current_snapshot.Setup <char[]>(s => s.ToCharArray(It.IsAny <int>(), It.IsAny <int>())).Returns((int pos, int len) => content.ToCharArray(pos, len));
            current_snapshot.Setup(s => s.GetText()).Returns(content);
            current_snapshot.Setup(s => s.CreateTrackingSpan(It.IsAny <int>(), It.IsAny <int>(), SpanTrackingMode.EdgeInclusive))
            .Returns((int start, int length, SpanTrackingMode tracking_mode) => new TrackingSpan(textBuffer.Object, start, length, tracking_mode));
            current_snapshot.Setup <ITextBuffer>(s => s.TextBuffer).Returns(textBuffer.Object);
            textBuffer.Setup <ITextSnapshot>(t => t.CurrentSnapshot).Returns(current_snapshot.Object);

            // mock snapshot lines - test templates are all single line
            var line   = new Mock <ITextSnapshotLine>();
            var extent = new SnapshotSpan(current_snapshot.Object, 0, current_snapshot.Object.Length);

            line.Setup(l => l.LineNumber).Returns(0);
            line.Setup(l => l.Snapshot).Returns(current_snapshot.Object);
            line.Setup(l => l.Start).Returns(new SnapshotPoint(current_snapshot.Object, 0));
            line.Setup(l => l.End).Returns(new SnapshotPoint(current_snapshot.Object, current_snapshot.Object.Length));
            line.Setup(l => l.Extent).Returns(extent);

            // build a list of lines into snapshot
            var lines = new List <ITextSnapshotLine>();

            lines.Add(line.Object);
            current_snapshot.Setup(s => s.Lines).Returns(lines);
            current_snapshot.Setup(s => s.LineCount).Returns(lines.Count);
            current_snapshot.Setup(s => s.GetLineFromPosition(It.IsAny <int>())).Returns(line.Object);

            Assert.IsInstanceOf <ITemplateManager>(parser);

            // mock handler
            var handler = new Mock <IProjectHandler>();

            handler.Setup <ITextSnapshot>(h => h.GetSnapshot(It.IsAny <string>())).Returns((string t) => current_snapshot.Object);
            handler.Setup <Microsoft.FSharp.Collections.FSharpList <INodeImpl> >(h => h.ParseTemplate(It.IsAny <string>(), (NDjango.TypeResolver.ITypeResolver)It.IsAny <TestTypeResolver>()))
            .Returns((string t, NDjango.TypeResolver.ITypeResolver resolver)
                     => parser.GetTemplate(t, resolver, new NDjango.TypeResolver.ModelDescriptor(new List <NDjango.TypeResolver.IDjangoType>())).Nodes);

            var provider = new NodeProvider(handler.Object, "temp://" + content, new TestTypeResolver());

            var parsing_completed = false;

            provider.NodesChanged += delegate(SnapshotSpan span)
            {
                parsing_completed = true;
            };

            while (!parsing_completed)
            {
                System.Threading.Thread.Sleep(1);
            }

            var source = new Source(provider, textBuffer.Object);

            // mock session properties
            var session_properties = new Mock <PropertyCollection>().Object;

            session_properties.AddProperty(typeof(CompletionContext), context);

            // mock tracking point
            var tracking_point = new Mock <ITrackingPoint>();

            tracking_point.Setup <SnapshotPoint>(t => t.GetPoint(It.IsAny <ITextSnapshot>())).Returns((ITextSnapshot snapshot) => new SnapshotPoint(snapshot, position));

            var session = new Mock <ICompletionSession>();

            session.Setup <PropertyCollection>(s => s.Properties).Returns(session_properties);
            session.Setup <ITrackingPoint>(s => s.GetTriggerPoint(It.IsAny <ITextBuffer>())).Returns(tracking_point.Object);

            var completionSets = new List <CompletionSet>();

            source.AugmentCompletionSession(session.Object, completionSets);

            Assert.AreEqual(1, completionSets.Count);
            var c = completionSets[0];

            Assert.IsInstanceOf(completion_set, c);
            Assert.AreEqual(display_values.Length, c.Completions.Count);
            Assert.AreEqual(builder_values.Length, c.CompletionBuilders.Count);
            for (int i = 0; i < display_values.Length; i++)
            {
                Assert.AreEqual(display_values[i], c.Completions[i].DisplayText);
            }
            for (int i = 0; i < insert_values.Length; i++)
            {
                Assert.AreEqual(insert_values[i], c.Completions[i].InsertionText);
            }
            for (int i = 0; i < builder_values.Length; i++)
            {
                Assert.AreEqual(builder_values[i], c.CompletionBuilders[i].DisplayText);
            }
        }
        private void CompletionSet_TestBase(string content, CompletionContext context, int position, Type completion_set, string[] display_values, string[] insert_values,string[] builder_values)
        {
            var textBuffer = new Mock<ITextBuffer>();

            // mock current snapshot - always return the content
            var current_snapshot = new Mock<ITextSnapshot>();
            current_snapshot.Setup<int>(s => s.Length).Returns(content.Length);
            current_snapshot.Setup<char[]>(s => s.ToCharArray(It.IsAny<int>(), It.IsAny<int>())).Returns((int pos, int len) => content.ToCharArray(pos, len));
            current_snapshot.Setup(s => s.GetText()).Returns(content);
            current_snapshot.Setup(s => s.CreateTrackingSpan(It.IsAny<int>(), It.IsAny<int>(), SpanTrackingMode.EdgeInclusive))
                .Returns((int start, int length, SpanTrackingMode tracking_mode) => new TrackingSpan(textBuffer.Object, start, length, tracking_mode));
            current_snapshot.Setup<ITextBuffer>(s => s.TextBuffer).Returns(textBuffer.Object);
            textBuffer.Setup<ITextSnapshot>(t => t.CurrentSnapshot).Returns(current_snapshot.Object);

            // mock snapshot lines - test templates are all single line
            var line = new Mock<ITextSnapshotLine>();
            var extent = new SnapshotSpan(current_snapshot.Object, 0, current_snapshot.Object.Length);
            line.Setup(l => l.LineNumber).Returns(0);
            line.Setup(l => l.Snapshot).Returns(current_snapshot.Object);
            line.Setup(l => l.Start).Returns(new SnapshotPoint(current_snapshot.Object, 0));
            line.Setup(l => l.End).Returns(new SnapshotPoint(current_snapshot.Object, current_snapshot.Object.Length));
            line.Setup(l => l.Extent).Returns(extent);

            // build a list of lines into snapshot
            var lines = new List<ITextSnapshotLine>();
            lines.Add(line.Object);
            current_snapshot.Setup(s => s.Lines).Returns(lines);
            current_snapshot.Setup(s => s.LineCount).Returns(lines.Count);
            current_snapshot.Setup(s => s.GetLineFromPosition(It.IsAny<int>())).Returns(line.Object);

            Assert.IsInstanceOf<ITemplateManager>(parser);

            // mock handler
            var handler = new Mock<IProjectHandler>();
            handler.Setup<ITextSnapshot>(h => h.GetSnapshot(It.IsAny<string>())).Returns((string t) => current_snapshot.Object);
            handler.Setup<Microsoft.FSharp.Collections.FSharpList<INodeImpl>>(h => h.ParseTemplate(It.IsAny<string>(), (NDjango.TypeResolver.ITypeResolver)It.IsAny<TestTypeResolver>()))
                .Returns((string t, NDjango.TypeResolver.ITypeResolver resolver)
                    => parser.GetTemplate(t, resolver, new NDjango.TypeResolver.ModelDescriptor(new List<NDjango.TypeResolver.IDjangoType>())).Nodes);

            var provider = new NodeProvider(handler.Object, "temp://" + content, new TestTypeResolver());

            var parsing_completed = false;

            provider.NodesChanged += delegate(SnapshotSpan span)
            {
                parsing_completed = true;
            };

            while (!parsing_completed)
                System.Threading.Thread.Sleep(1);

            var source = new Source(provider, textBuffer.Object);

            // mock session properties
            var session_properties = new Mock<PropertyCollection>().Object;
            session_properties.AddProperty(typeof(CompletionContext), context);

            // mock tracking point
            var tracking_point = new Mock<ITrackingPoint>();
            tracking_point.Setup<SnapshotPoint>(t => t.GetPoint(It.IsAny<ITextSnapshot>())).Returns((ITextSnapshot snapshot) => new SnapshotPoint(snapshot, position));

            var session = new Mock<ICompletionSession>();
            session.Setup<PropertyCollection>(s => s.Properties).Returns(session_properties);
            session.Setup<ITrackingPoint>(s => s.GetTriggerPoint(It.IsAny<ITextBuffer>())).Returns(tracking_point.Object);

            var completionSets = new List<CompletionSet>();
            source.AugmentCompletionSession(session.Object, completionSets);

            Assert.AreEqual(1, completionSets.Count);
            var c = completionSets[0];
            Assert.IsInstanceOf(completion_set, c);
            Assert.AreEqual(display_values.Length, c.Completions.Count);
            Assert.AreEqual(builder_values.Length,c.CompletionBuilders.Count);
            for (int i = 0; i<display_values.Length; i++)
                Assert.AreEqual(display_values[i], c.Completions[i].DisplayText);
            for (int i = 0; i < insert_values.Length; i++)
                Assert.AreEqual(insert_values[i], c.Completions[i].InsertionText);
            for (int i = 0; i < builder_values.Length; i++)
                Assert.AreEqual(builder_values[i], c.CompletionBuilders[i].DisplayText);
        }