示例#1
0
    public void Build_CreatesDefaultCodeTarget()
    {
        // Arrange
        var codeDocument = TestRazorCodeDocument.CreateEmpty();
        var options      = RazorCodeGenerationOptions.CreateDefault();

        var builder = new DefaultCodeTargetBuilder(codeDocument, options);

        var extensions = new ICodeTargetExtension[]
        {
            new MyExtension1(),
            new MyExtension2(),
        };

        for (var i = 0; i < extensions.Length; i++)
        {
            builder.TargetExtensions.Add(extensions[i]);
        }

        // Act
        var result = builder.Build();

        // Assert
        var target = Assert.IsType <DefaultCodeTarget>(result);

        Assert.Equal(extensions, target.Extensions);
    }
        public void Execute_Match_AddsGlobalTargetExtensions()
        {
            // Arrange
            var documentNode = new DocumentIntermediateNode()
            {
                Options = RazorCodeGenerationOptions.CreateDefault(),
            };

            var expected = new ICodeTargetExtension[]
            {
                new MyExtension1(),
                new MyExtension2(),
            };

            var pass = new TestDocumentClassifierPass();

            pass.Engine = RazorProjectEngine.CreateEmpty(b =>
            {
                for (var i = 0; i < expected.Length; i++)
                {
                    b.AddTargetExtension(expected[i]);
                }
            }).Engine;

            ICodeTargetExtension[] extensions = null;

            pass.CodeTargetCallback = (builder) => extensions = builder.TargetExtensions.ToArray();

            // Act
            pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode);

            // Assert
            Assert.Equal(expected, extensions);
        }
示例#3
0
    public void Constructor_CreatesDefensiveCopy()
    {
        // Arrange
        var options = RazorCodeGenerationOptions.CreateDefault();

        var extensions = new ICodeTargetExtension[]
        {
            new MyExtension2(),
            new MyExtension1(),
        };

        // Act
        var target = new DefaultCodeTarget(options, extensions);

        // Assert
        Assert.NotSame(extensions, target);
    }
示例#4
0
    public void HasExtension_ReturnsFalse_WhenExtensionNotFound()
    {
        // Arrange
        var options = RazorCodeGenerationOptions.CreateDefault();

        var extensions = new ICodeTargetExtension[]
        {
            new MyExtension2(),
            new MyExtension2(),
        };

        var target = new DefaultCodeTarget(options, extensions);

        // Act
        var result = target.HasExtension <MyExtension1>();

        // Assert
        Assert.False(result);
    }
示例#5
0
    public void GetExtension_ReturnsExtension_WhenExtensionFound()
    {
        // Arrange
        var options = RazorCodeGenerationOptions.CreateDefault();

        var extensions = new ICodeTargetExtension[]
        {
            new MyExtension2(),
            new MyExtension1(),
        };

        var target = new DefaultCodeTarget(options, extensions);

        // Act
        var result = target.GetExtension <MyExtension1>();

        // Assert
        Assert.Same(extensions[1], result);
    }
示例#6
0
        /// <summary>
        /// Adds the specified <see cref="ICodeTargetExtension"/>.
        /// </summary>
        /// <param name="builder">The <see cref="RazorProjectEngineBuilder"/>.</param>
        /// <param name="extension">The <see cref="ICodeTargetExtension"/> to add.</param>
        /// <returns>The <see cref="RazorProjectEngineBuilder"/>.</returns>
        public static RazorProjectEngineBuilder AddTargetExtension(this RazorProjectEngineBuilder builder, ICodeTargetExtension extension)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (extension == null)
            {
                throw new ArgumentNullException(nameof(extension));
            }

            var targetExtensionFeature = GetTargetExtensionFeature(builder);

            targetExtensionFeature.TargetExtensions.Add(extension);

            return(builder);
        }