public static async Task <BindFixture> Create(BindHostBuilder hostTypeInfo, Action <string> testOutputHelper)
    {
        var bindFixture = new BindFixture(hostTypeInfo, testOutputHelper);
        await bindFixture.Initialize().ConfigureAwait(false);

        return(bindFixture);
    }
예제 #2
0
    public async Task NoDiagnostics_TwoWayBind(Accessibility hostContainerTypeAccess, Accessibility hostTypeAccess, Accessibility hostPropertiesAccess, Accessibility vmTypeAccess, Accessibility vmPropertiesAccess)
    {
        var viewModelHostDetails = new WhenChangedHostBuilder()
                                   .WithClassAccess(vmTypeAccess)
                                   .WithInvocation(InvocationKind.MemberAccess, x => x.Value)
                                   .WithPropertyType("string")
                                   .WithPropertyAccess(vmPropertiesAccess)
                                   .WithClassName("ViewModelHost");

        var hostTypeInfo = new BindHostBuilder()
                           .WithClassName("Host")
                           .WithTwoWayInvocation(InvocationKind.MemberAccess, ReceiverKind.This, x => x.Value, x => x.Value)
                           .WithClassAccess(hostTypeAccess)
                           .WithPropertyType("string")
                           .WithPropertyAccess(hostPropertiesAccess)
                           .WithViewModelPropertyAccess(hostPropertiesAccess)
                           .WithViewModelPropertyType(viewModelHostDetails)
                           .AddNestedClass(viewModelHostDetails);

        var hostContainerTypeInfo = new EmptyClassBuilder()
                                    .WithClassName("HostContainer")
                                    .WithClassAccess(hostContainerTypeAccess)
                                    .AddNestedClass(hostTypeInfo);

        var fixture = await BindFixture.Create(hostTypeInfo, x => TestContext.WriteLine(x)).ConfigureAwait(false);

        fixture.RunGenerator(hostTypeInfo, out var compilationDiagnostics, out var generatorDiagnostics);

        generatorDiagnostics.Where(x => x.Severity >= DiagnosticSeverity.Warning).Should().BeEmpty();
        compilationDiagnostics.Where(x => x.Severity >= DiagnosticSeverity.Warning).Should().BeEmpty();

        var host = fixture.NewHostInstance();

        host.ViewModel = fixture.NewViewModelPropertyInstance();
        var disposable = host.GetTwoWayBindSubscription(_ => TestContext.WriteLine(fixture.Sources));

        var    viewModelObservable = host.GetViewModelWhenChangedObservable(_ => TestContext.WriteLine(fixture.Sources));
        var    viewObservable      = host.GetWhenChangedObservable(_ => TestContext.WriteLine(fixture.Sources));
        object viewModelValue      = null;
        object viewValue           = null;

        viewModelObservable.Subscribe(x => viewModelValue = x);
        viewObservable.Subscribe(x => viewValue           = x);

        host.Value = "test";
        host.ViewModel.Value.Should().Be("test");

        host.Value = "Test2";
        host.ViewModel.Value.Should().Be("Test2");

        host.ViewModel.Value = "Test3";
        host.Value.Should().Be("Test3");
    }
 public void RunGenerator(BindHostBuilder hostTypeInfo, out ImmutableArray <Diagnostic> compilationDiagnostics, out ImmutableArray <Diagnostic> generatorDiagnostics, bool writeOutput = false, [CallerMemberName] string callerMemberName = null)
 {
     var sources = new[] { ("HostBuilder.cs", hostTypeInfo.BuildRoot()) };
 private BindFixture(BindHostBuilder hostTypeInfo, Action <string> testOutputHelper)
 {
     _hostTypeInfo     = hostTypeInfo;
     _testOutputHelper = testOutputHelper;
     _compilation      = new(x => _testOutputHelper(x));
 }