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 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);
        }