Ejemplo n.º 1
0
    public async Task NoDiagnostics_ChainOf3()
    {
        var hostPropertyTypeInfo = new EmptyClassBuilder()
                                   .WithClassName("ClassName")
                                   .WithNamespace("Namespace1")
                                   .WithClassAccess(Accessibility.Public);
        var hostTypeInfo = WhenChangedHostBuilder.Changing()
                           .WithClassName("ClassName")
                           .WithNamespace("Namespace2")
                           .WithInvocation(InvocationKind.MemberAccess, x => x.Child.Child.Value)
                           .WithClassAccess(Accessibility.Public)
                           .WithPropertyType(hostPropertyTypeInfo)
                           .WithPropertyAccess(Accessibility.Public);

        var fixture = await WhenChangedFixture.Create(hostTypeInfo, x => TestContext.WriteLine(x), ("HostProperty.cs", hostPropertyTypeInfo.BuildRoot())).ConfigureAwait(false);

        fixture.RunGenerator(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.Child             = fixture.NewHostInstance();
        host.Child.Child       = fixture.NewHostInstance();
        host.Child.Child.Value = fixture.NewValuePropertyInstance();
        var observable = host.GetWhenChangedObservable(_ => TestContext.WriteLine(fixture.Sources));

        object emittedValue = null;

        observable.Subscribe(x => emittedValue = x);

        var initialValue = host.Child.Child.Value;

        emittedValue.Should().Be(initialValue);
        var previousValue = emittedValue;

        // According to the current design, no emission should occur when the chain is "broken".
        host.Child = null;
        emittedValue.Should().Be(previousValue);
        previousValue = emittedValue;

        host.Child       = fixture.NewHostInstance();
        host.Child.Child = fixture.NewHostInstance();
        emittedValue.Should().Be(previousValue);
        previousValue = emittedValue;

        host.Child.Child.Value = fixture.NewValuePropertyInstance();
        emittedValue.Should().Be(previousValue);
        previousValue = emittedValue;

        host.Child.Child.Value = null;
        emittedValue.Should().Be(previousValue);
    }
Ejemplo n.º 2
0
    public Task NoDiagnostics_ExplicitInvocation_MultiExpression(Accessibility hostTypeAccess, Accessibility propertyTypeAccess, Accessibility propertyAccess)
    {
        var hostPropertyTypeInfo = new EmptyClassBuilder()
                                   .WithClassAccess(propertyTypeAccess);
        var hostTypeInfo = WhenChangedHostBuilder.Changing()
                           .WithClassName("HostClass")
                           .WithClassAccess(hostTypeAccess)
                           .WithPropertyType(hostPropertyTypeInfo)
                           .WithPropertyAccess(propertyAccess);

        return(AssertTestCase_MultiExpression(hostTypeInfo, InvocationKind.Explicit, ("HostPropertyInfo.cs", hostPropertyTypeInfo.BuildRoot())));
    }
Ejemplo n.º 3
0
    public Task NoDiagnostics_Namespaces(string hostTypeNamespace, string hostPropertyTypeNamespace)
    {
        var hostPropertyTypeInfo = new EmptyClassBuilder()
                                   .WithClassAccess(Accessibility.Public)
                                   .WithNamespace(hostPropertyTypeNamespace);
        var hostTypeInfo = WhenChangedHostBuilder.Changing()
                           .WithClassName("HostClass")
                           .WithClassAccess(Accessibility.Public)
                           .WithPropertyType(hostPropertyTypeInfo)
                           .WithPropertyAccess(Accessibility.Public)
                           .WithNamespace(hostTypeNamespace);

        return(AssertTestCase_MultiExpression(hostTypeInfo, InvocationKind.MemberAccess, ("HostPropertyInfo.cs", hostPropertyTypeInfo.BuildRoot())));
    }
Ejemplo n.º 4
0
    public Task NoDiagnostics_AccessModifierCombinations(Accessibility hostContainerTypeAccess, Accessibility hostTypeAccess, Accessibility propertyTypeAccess, Accessibility propertyAccess)
    {
        var hostPropertyTypeInfo = new EmptyClassBuilder()
                                   .WithClassAccess(propertyTypeAccess);
        var hostTypeInfo = WhenChangedHostBuilder.Changing()
                           .WithClassName("HostClass")
                           .WithClassAccess(hostTypeAccess)
                           .AddNestedClass(hostPropertyTypeInfo)
                           .WithPropertyType(hostPropertyTypeInfo)
                           .WithPropertyAccess(propertyAccess);
        var hostContainerTypeInfo = new EmptyClassBuilder()
                                    .WithClassName("HostContainer")
                                    .WithClassAccess(hostContainerTypeAccess)
                                    .AddNestedClass(hostTypeInfo);

        return(AssertTestCase_MultiExpression(hostTypeInfo, InvocationKind.MemberAccess));
    }
Ejemplo n.º 5
0
    public async Task NoDiagnostics_PrivateHostPropertyTypeAndInternalOutputType()
    {
        var hostPropertyTypeInfo = new EmptyClassBuilder()
                                   .WithClassAccess(Accessibility.Private);
        var customType = new EmptyClassBuilder()
                         .WithClassName("Output")
                         .WithClassAccess(Accessibility.Internal);
        var hostTypeInfo = WhenChangedHostBuilder.Changing()
                           .WithClassName("HostClass")
                           .WithInvocation(InvocationKind.MemberAccess, x => x.Child, x => x.Value, "(a, b) => b != null ? new HostContainer.Output() : null")
                           .WithClassAccess(Accessibility.Protected)
                           .AddNestedClass(hostPropertyTypeInfo)
                           .WithPropertyType(hostPropertyTypeInfo)
                           .WithPropertyAccess(Accessibility.Private);
        var hostContainerTypeInfo = new EmptyClassBuilder()
                                    .WithClassName("HostContainer")
                                    .WithClassAccess(Accessibility.Public)
                                    .AddNestedClass(hostTypeInfo)
                                    .AddNestedClass(customType);

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

        fixture.RunGenerator(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.Value = fixture.NewValuePropertyInstance();
        var observable = host.GetWhenChangedObservable(_ => TestContext.WriteLine(fixture.Sources));

        object emittedValue = null;

        observable.Subscribe(x => emittedValue = x);

        // TODO: Better series of checks. Currently can't compare values because of reference
        // equality and we don't have access to the instance that the conversionFunc creates.
        emittedValue.Should().NotBeNull();
        host.Value = null;
        emittedValue.Should().NotBeNull();
        host.Value = fixture.NewValuePropertyInstance();
        emittedValue.Should().BeNull();
    }
Ejemplo n.º 6
0
    public async Task NoDiagnostics_InstanceReceiverKind(InvocationKind invocationKind)
    {
        var receiverPropertyTypeInfo = new EmptyClassBuilder()
                                       .WithClassAccess(Accessibility.Public);
        var externalReceiverTypeInfo = new WhenChangedHostBuilder()
                                       .WithClassName("ReactiveType")
                                       .WithClassAccess(Accessibility.Public)
                                       .WithPropertyType(receiverPropertyTypeInfo)
                                       .WithPropertyAccess(Accessibility.Public)
                                       .WithInvocation("null");
        var hostTypeInfo = WhenChangedHostBuilder.Changing()
                           .WithClassName("HostClass")
                           .WithClassAccess(Accessibility.Public)
                           .WithInvocation(invocationKind, x => x.Value, externalReceiverTypeInfo);

        var fixture = await WhenChangedFixture.Create(hostTypeInfo, externalReceiverTypeInfo, x => TestContext.WriteLine(x), ("Receiver", receiverPropertyTypeInfo.BuildRoot())).ConfigureAwait(false);

        fixture.RunGenerator(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();
        var receiver = fixture.NewReceiverInstance();

        host.Receiver  = receiver;
        receiver.Value = fixture.NewValuePropertyInstance();
        var    observable = host.GetWhenChangedObservable(_ => TestContext.WriteLine(fixture.Sources));
        object value      = null;

        observable.Subscribe(x => value = x);
        receiver.Value.Should().Equals(value);
        receiver.Value = fixture.NewValuePropertyInstance();
        receiver.Value.Should().Equals(value);
        receiver.Value = null;
        receiver.Value.Should().Equals(value);
    }