mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-15 09:35:37 -05:00
- TSqlFormatterService with support for formatting document and text range inside document - Settings support for all formatting options. - Extensibility support so that the service can be initialized using MEF extensibility, and can find all necessary TSqlFormatters using the same process Fix Initialize request error on startup - Messages were being read from the input channel before all request handlers were registered - In particular, the Initialize request which is key for any server to talk to the client was getting lost because the message reader thread begins consuming, and we take an extra few hundred milliseconds due to MEF startup before we register the handler - The solution is to initialize the message handler so request handlers can register, but not actually start processing incoming messages until all handers are ready. This is a safer way to go and should improve reliability overall Improvements from internal prototype: - Normalizing baselines to handle the line ending differences on Mac & Linux vs. Windows - Significantly shortened most lines by implementing base class methods to wrap common objects from Visitor.Context and removing unnecessary "this." syntax - Refactored the SqlCommonTableExpressionFormatter and related classes to reduce code count significantly. This provides a pattern to follow when refactoring other classes for similar clarity. It's likely a lot of common logic could be found and reused across these. - Reduced overall code size by adding utility methods
89 lines
3.2 KiB
C#
89 lines
3.2 KiB
C#
//
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
//
|
|
|
|
using System;
|
|
using System.Composition;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Microsoft.SqlTools.ServiceLayer.Extensibility;
|
|
using Microsoft.SqlTools.ServiceLayer.Formatter;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.Test.Extensibility
|
|
{
|
|
public class ExtensionTests
|
|
{
|
|
|
|
[Fact]
|
|
public void CreateAssemblyStoreShouldFindTypesInAssembly()
|
|
{
|
|
// Given a store for MyExportType
|
|
ExtensionStore store = ExtensionStore.CreateAssemblyStore<MyExportType>(GetType().GetTypeInfo().Assembly);
|
|
// Then should get any export for this type and subtypes
|
|
Assert.Equal(2, store.GetExports<MyExportType>().Count());
|
|
|
|
// But for a different type, expect throw as the store only contains MyExportType
|
|
Assert.Throws<InvalidCastException>(() => store.GetExports<MyOtherType>().Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateDefaultLoaderShouldOnlyFindTypesInMainAssembly()
|
|
{
|
|
// Given a store created using CreateDefaultLoader
|
|
// Then should not find exports from a different assembly
|
|
ExtensionStore store = ExtensionStore.CreateDefaultLoader<MyExportType>();
|
|
Assert.Equal(0, store.GetExports<MyExportType>().Count());
|
|
|
|
// But should find exports that are defined in the main assembly
|
|
store = ExtensionStore.CreateDefaultLoader<ASTNodeFormatterFactory>();
|
|
Assert.NotEmpty(store.GetExports<ASTNodeFormatterFactory>());
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void CreateDefaultServiceProviderShouldOnlyFindTypesInMainAssembly()
|
|
{
|
|
// Given a default ExtensionServiceProvider
|
|
// Then should not find exports from a different assembly
|
|
ExtensionServiceProvider serviceProvider = ExtensionServiceProvider.CreateDefaultServiceProvider();
|
|
Assert.Empty(serviceProvider.GetServices<MyExportType>());
|
|
|
|
// But should find exports that are defined in the main assembly
|
|
Assert.NotEmpty(serviceProvider.GetServices<ASTNodeFormatterFactory>());
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateStoreForCurrentDirectoryShouldFindExportsInDirectory()
|
|
{
|
|
// Given stores created for types in different assemblies
|
|
ExtensionStore myStore = ExtensionStore.CreateStoreForCurrentDirectory<MyExportType>();
|
|
ExtensionStore querierStore = ExtensionStore.CreateStoreForCurrentDirectory<ASTNodeFormatterFactory>();
|
|
|
|
// When I query exports
|
|
// Then exports for all assemblies should be found
|
|
Assert.Equal(2, myStore.GetExports<MyExportType>().Count());
|
|
Assert.NotEmpty(querierStore.GetExports<ASTNodeFormatterFactory>());
|
|
}
|
|
}
|
|
|
|
// Note: in order for the MEF lookup to succeed, one class must have
|
|
[Export(typeof(MyExportType))]
|
|
public class MyExportType
|
|
{
|
|
|
|
}
|
|
|
|
public class MyExportSubType : MyExportType
|
|
{
|
|
|
|
}
|
|
|
|
public class MyOtherType
|
|
{
|
|
|
|
}
|
|
}
|
|
|