예제 #1
0
        public void CanAddComponentReferenceCaptureInsideComponent()
        {
            // Arrange
            var             builder  = new RenderTreeBuilder(new TestRenderer());
            Action <object> myAction = elementRef => { };

            // Act
            builder.OpenComponent <TestComponent>(0);               //  0: <TestComponent
            builder.AddAttribute(1, "attribute2", 123);             //  1:     attribute2=intExpression123>
            builder.AddComponentReferenceCapture(2, myAction);      //  2:     # capture: myAction
            builder.AddContent(3, "some text");                     //  3:     some text
            builder.CloseComponent();                               //     </TestComponent>

            // Assert
            Assert.Collection(builder.GetFrames(),
                              frame => AssertFrame.Component <TestComponent>(frame, 4, 0),
                              frame => AssertFrame.Attribute(frame, "attribute2", 123, 1),
                              frame => AssertFrame.ComponentReferenceCapture(frame, myAction, 2),
                              frame => AssertFrame.Text(frame, "some text", 3));
        }
        public void Render_GenericComponent_TypeInference_WithRef_Recursive()
        {
            // Arrange
            AdditionalSyntaxTrees.Add(GenericContextComponent);

            var assembly = CompileToAssembly("Test.cshtml", @"
@addTagHelper *, TestAssembly
@typeparam TItem
<GenericContext Items=""@MyItems"" ref=""_my"" />

@functions {
    [Parameter] List<TItem> MyItems { get; set; }
    GenericContext<TItem> _my;
    void Foo() { GC.KeepAlive(_my); }
}");

            var componentType = assembly.Assembly.DefinedTypes
                                .Where(t => t.Name == "Test`1")
                                .Single()
                                .MakeGenericType(typeof(int));
            var component = (IComponent)Activator.CreateInstance(componentType);

            // Act
            var frames = GetRenderTree(component);

            // Assert
            var genericComponentType = assembly.Assembly.DefinedTypes
                                       .Where(t => t.Name == "GenericContext`1")
                                       .Single()
                                       .MakeGenericType(typeof(int));

            Assert.Collection(
                frames,
                frame => AssertFrame.Component(frame, genericComponentType.FullName, 3, 0),
                frame => AssertFrame.Attribute(frame, "Items", 1),
                frame => AssertFrame.ComponentReferenceCapture(frame, 2));
        }
예제 #3
0
        public void CanAddMultipleReferenceCapturesToSameComponent()
        {
            // There won't be any way of doing this from Razor because there's no known use
            // case for it. However it's harder to *not* support it than to support it, and
            // there's no known reason to prevent it, so here's test coverage to show it
            // just works.

            // Arrange
            var             builder = new RenderTreeBuilder(new TestRenderer());
            Action <object> referenceCaptureAction1 = elementRef => { };
            Action <object> referenceCaptureAction2 = elementRef => { };

            // Act
            builder.OpenComponent <TestComponent>(0);
            builder.AddComponentReferenceCapture(0, referenceCaptureAction1);
            builder.AddComponentReferenceCapture(0, referenceCaptureAction2);
            builder.CloseComponent();

            // Assert
            Assert.Collection(builder.GetFrames(),
                              frame => AssertFrame.Component <TestComponent>(frame, 3),
                              frame => AssertFrame.ComponentReferenceCapture(frame, referenceCaptureAction1),
                              frame => AssertFrame.ComponentReferenceCapture(frame, referenceCaptureAction2));
        }