public void TestCanRetrieve()
        {
            var textFactory = new TextFactoryServiceFactory.TextFactoryService();
            var tempStorageService = new TemporaryStorageServiceFactory.TemporaryStorageService(textFactory);
            var syntaxTreeStorageService = new SyntaxTreeStorageService();

            var root = CSharp.SyntaxFactory.CompilationUnit();
            var tree = CSharp.SyntaxFactory.SyntaxTree(root);

            Assert.False(syntaxTreeStorageService.CanRetrieve(tree));
        }
        public void TestStore()
        {
            var textFactory = new TextFactoryServiceFactory.TextFactoryService();
            var tempStorageService = new TemporaryStorageServiceFactory.TemporaryStorageService(textFactory);
            var syntaxTreeStorageService = new SyntaxTreeStorageService();

            var root = CSharp.SyntaxFactory.CompilationUnit();
            var tree = CSharp.SyntaxFactory.SyntaxTree(root);

            syntaxTreeStorageService.Store(tree, root, tempStorageService, CancellationToken.None);
            Assert.True(syntaxTreeStorageService.CanRetrieve(tree));
        }
        public async Task TestStoreAsync()
        {
            var textFactory = new TextFactoryServiceFactory.TextFactoryService();
            var tempStorageService = new TemporaryStorageServiceFactory.TemporaryStorageService(textFactory);
            var syntaxTreeStorageService = new SyntaxTreeStorageService();

            var root = CSharp.SyntaxFactory.CompilationUnit();
            var tree = CSharp.SyntaxFactory.SyntaxTree(root);

            await syntaxTreeStorageService.StoreAsync(tree, root, tempStorageService, CancellationToken.None).ConfigureAwait(false);

            Assert.True(syntaxTreeStorageService.CanRetrieve(tree));
        }
        public async Task TestEnqueueStoreAsync()
        {
            var textFactory = new TextFactoryServiceFactory.TextFactoryService();
            var tempStorageService = new TemporaryStorageServiceFactory.TemporaryStorageService(textFactory);
            var syntaxTreeStorageService = new SyntaxTreeStorageService();

            var root = CSharp.SyntaxFactory.CompilationUnit();
            var tree = CSharp.SyntaxFactory.SyntaxTree(root);

            // EnqueueStore and EnqueueStoreAsync is basically same thing. only difference is EnqueueStoreAsync returns Task that can be waited.
            await syntaxTreeStorageService.EnqueueStoreAsync(tree, root, tempStorageService, CancellationToken.None).ConfigureAwait(false);

            Assert.True(syntaxTreeStorageService.CanRetrieve(tree));
        }
        public void TestRetrieve()
        {
            var textFactory = new TextFactoryServiceFactory.TextFactoryService();
            var tempStorageService = new TemporaryStorageServiceFactory.TemporaryStorageService(textFactory);
            var syntaxTreeStorageService = new SyntaxTreeStorageService();

            var tree = CSharp.SyntaxFactory.ParseSyntaxTree("namespace A { class C {");
            var root = tree.GetRoot();

            syntaxTreeStorageService.Store(tree, root, tempStorageService, CancellationToken.None);
            Assert.True(syntaxTreeStorageService.CanRetrieve(tree));

            var syntaxFactoryService = new CSharp.CSharpSyntaxTreeFactoryServiceFactory().CreateLanguageService(provider: null) as ISyntaxTreeFactoryService;
            var newRoot = syntaxTreeStorageService.Retrieve(tree, syntaxFactoryService, CancellationToken.None);

            Assert.True(root.IsEquivalentTo(newRoot));

            // we can't directly compare diagnostics since location in the diagnostic will point to two different trees
            AssertEx.SetEqual(root.GetDiagnostics().Select(d => d.ToString()), newRoot.GetDiagnostics().Select(d => d.ToString()));
        }
        public async Task TestRemoveAsync()
        {
            var textFactory = new TextFactoryServiceFactory.TextFactoryService();
            var tempStorageService = new TemporaryStorageServiceFactory.TemporaryStorageService(textFactory);
            var syntaxTreeStorageService = new SyntaxTreeStorageService();

            var root = CSharp.SyntaxFactory.CompilationUnit();
            var tree = CSharp.SyntaxFactory.SyntaxTree(root);

            var weakReference = new WeakReference(tree);

            syntaxTreeStorageService.Store(tree, root, tempStorageService, CancellationToken.None);
            Assert.True(syntaxTreeStorageService.CanRetrieve(tree));

            // let tree and root go.
            root = null;
            tree = null;

            var count = 0;
            while (weakReference.IsAlive && count < 100)
            {
                GC.Collect();

                await Task.Delay(5).ConfigureAwait(false);
                count++;
            }

            // tree should go away before it reached 100
            Assert.False(count == 100);
        }