示例#1
0
        public void ExampleIsValid(ExampleData example)
        {
            var resourceTypeRegistrar = new ResourceTypeRegistrar(new AzResourceTypeProvider());
            var compilation           = new Compilation(resourceTypeRegistrar, SyntaxFactory.CreateFromText(example.BicepContents));
            var emitter = new TemplateEmitter(compilation.GetSemanticModel());

            using var stream = new MemoryStream();
            var result = emitter.Emit(stream);

            result.Diagnostics.Should().BeEmpty();
            result.Status.Should().Be(EmitStatus.Succeeded);

            stream.Position = 0;
            var generated = new StreamReader(stream).ReadToEnd();

            var actual = JToken.Parse(generated);

            FileHelper.SaveResultFile(this.TestContext !, $"{example.BicepFileName}_Compiled_Actual.json", actual.ToString(Formatting.Indented));

            var expected = JToken.Parse(example.JsonContents !);

            FileHelper.SaveResultFile(this.TestContext !, $"{example.BicepFileName}_Compiled_Expected.json", expected.ToString(Formatting.Indented));

            JsonAssert.AreEqual(expected, actual, this.TestContext !, $"{example.BicepFileName}_Compiled_Delta.json");
        }
示例#2
0
文件: Program.cs 项目: visnema/bicep
        private void BuildManyFilesToStdOut(IDiagnosticLogger logger, string[] bicepPaths)
        {
            using var writer = new JsonTextWriter(this.outputWriter)
                  {
                      Formatting = Formatting.Indented
                  };

            if (bicepPaths.Length > 1)
            {
                writer.WriteStartArray();
            }
            foreach (var bicepPath in bicepPaths)
            {
                string text       = ReadFile(bicepPath);
                var    lineStarts = TextCoordinateConverter.GetLineStarts(text);

                var resourceTypeRegistrar = new ResourceTypeRegistrar(new AzResourceTypeProvider());
                var compilation           = new Compilation(resourceTypeRegistrar, SyntaxFactory.CreateFromText(text));

                var emitter = new TemplateEmitter(compilation.GetSemanticModel());

                var result = emitter.Emit(writer);

                foreach (var diagnostic in result.Diagnostics)
                {
                    logger.LogDiagnostic(bicepPath, diagnostic, lineStarts);
                }
            }
            if (bicepPaths.Length > 1)
            {
                writer.WriteEndArray();
            }
        }
示例#3
0
        public Compilation(ResourceTypeRegistrar resourceTypeRegistrar, ProgramSyntax programSyntax)
        {
            this.resourceTypeRegistrar = resourceTypeRegistrar;
            this.ProgramSyntax         = programSyntax;

            this.lazySemanticModel = new Lazy <SemanticModel>(this.GetSemanticModelInternal, LazyThreadSafetyMode.PublicationOnly);
        }
示例#4
0
        public void BuildRegistration_sets_up_correct_attribute_for_to_one_complex_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttributeHelper(reg, "to-one-complex-type-field",
                                  new JObject {
                { "intProp", 32 }, { "StringProp", "qux" }
            },
                                  grabBag =>
            {
                grabBag.ToOneComplexTypeField.Should().NotBeNull();
                grabBag.ToOneComplexTypeField.IntProp.Should().Be(32);
                grabBag.ToOneComplexTypeField.StringProp.Should().Be("qux");
            },
                                  token =>
            {
                ((int)token["intProp"]).Should().Be(32);
                ((string)token["StringProp"]).Should().Be("qux");
            });
            AssertAttribute(reg, "to-one-complex-type-field", null, null, (SampleComplexType)null, g => g.ToOneComplexTypeField);
        }
        private DefaultSortingTransformer GetTransformer()
        {
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));
            var registry  = new ResourceTypeRegistry();

            registry.AddRegistration(registrar.BuildRegistration(typeof(Dummy), "dummies"));
            registry.AddRegistration(registrar.BuildRegistration(typeof(Dummy2), "dummy2s"));
            return(new DefaultSortingTransformer(registry));
        }
示例#6
0
        public void BuildRegistration_sets_up_correct_attribute_for_string_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "string-field", "asdf", "asdf", "asdf", g => g.StringField);
            AssertAttribute(reg, "string-field", null, null, (string)null, g => g.StringField);
        }
示例#7
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_enum_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "nullable-enum-field", (int)SampleEnum.Value1, SampleEnum.Value1, (int)SampleEnum.Value1, g => g.NullableEnumField);
            AssertAttribute(reg, "nullable-enum-field", null, null, (SampleEnum?)null, g => g.NullableEnumField);
        }
示例#8
0
        public void Cant_register_type_with_two_properties_with_the_same_name()
        {
            // Arrange
            var  registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));
            Type saladType = typeof(Salad);

            // Act
            Action action = () => registrar.BuildRegistration(saladType);

            // Assert
            action.ShouldThrow <InvalidOperationException>().Which.Message.Should()
            .Be("Failed to register type `Salad` because contains multiple properties that would serialize as `salad-type`.");
        }
示例#9
0
文件: Server.cs 项目: visnema/bicep
        private static void RegisterServices(IServiceCollection services)
        {
            // We don't want to recreate this every compilation.
            // I'm not using DI for it here, because I think it makes sense to set this up for the CLI at the same time.
            var resourceTypeRegistrar = new ResourceTypeRegistrar(new AzResourceTypeProvider());

            // using type based registration so dependencies can be injected automatically
            // without manually constructing up the graph
            services.AddSingleton <ICompilationManager, BicepCompilationManager>();
            services.AddSingleton <ICompilationProvider>(provider => new BicepCompilationProvider(resourceTypeRegistrar));
            services.AddSingleton <ISymbolResolver, BicepSymbolResolver>();
            services.AddSingleton <ICompletionProvider, BicepCompletionProvider>();
        }
示例#10
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_boolean_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "nullable-boolean-field", false, false, false, g => g.NullableBooleanField);
            AssertAttribute(reg, "nullable-boolean-field", true, true, true, g => g.NullableBooleanField);
            AssertAttribute(reg, "nullable-boolean-field", null, null, (Boolean?)null, g => g.NullableBooleanField);
        }
示例#11
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_UInt64_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "nullable-uint64-field", 0, (UInt64?)0, (UInt64?)0, g => g.NullableUint64Field);
            AssertAttribute(reg, "nullable-uint64-field", 20000000000, (UInt64?)20000000000, (UInt64?)20000000000, g => g.NullableUint64Field);
            AssertAttribute(reg, "nullable-uint64-field", null, null, (UInt64?)null, g => g.NullableUint64Field);
        }
示例#12
0
        public void Cant_register_type_with_non_id_property_called_id()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            Action action = () => registrar.BuildRegistration(typeof(Continent));

            // Assert
            action.ShouldThrow <InvalidOperationException>()
            .Which.Message.Should()
            .Be("Failed to register type `Continent` because it contains a non-id property that would serialize as \"id\".");
        }
示例#13
0
        public void Cant_register_type_with_missing_id()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            Action action = () => registrar.BuildRegistration(typeof(InvalidModel));

            // Assert
            action.ShouldThrow <InvalidOperationException>()
            .Which.Message.Should()
            .Be("Unable to determine Id property for type `InvalidModel`.");
        }
示例#14
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_Decimal_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "nullable-decimal-field", "0", 0m, "0", g => g.NullableDecimalField);
            AssertAttribute(reg, "nullable-decimal-field", "20000000000.1234", 20000000000.1234m, "20000000000.1234", g => g.NullableDecimalField);
            AssertAttribute(reg, "nullable-decimal-field", null, null, (string)null, g => g.NullableDecimalField);
        }
示例#15
0
        public void BuildRegistration_sets_up_correct_attribute_for_Double_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "double-field", 0d, 0d, 0d, g => g.DoubleField);
            AssertAttribute(reg, "double-field", 20000000000.1234d, 20000000000.1234d, 20000000000.1234d, g => g.DoubleField);
            AssertAttribute(reg, "double-field", null, 0d, 0d, g => g.DoubleField);
        }
        private DefaultFilteringTransformer GetTransformer()
        {
            var pluralizationService = new PluralizationService(new Dictionary <string, string>
            {
                { "Dummy", "Dummies" }
            });
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(pluralizationService));
            var registry  = new ResourceTypeRegistry();

            registry.AddRegistration(registrar.BuildRegistration(typeof(Dummy)));
            registry.AddRegistration(registrar.BuildRegistration(typeof(RelatedItemWithId)));
            return(new DefaultFilteringTransformer(registry));
        }
示例#17
0
        public void BuildRegistration_sets_up_correct_attribute_for_UInt16_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "uint16-field", 0, 0, 0, g => g.Uint16Field);
            AssertAttribute(reg, "uint16-field", 4000, 4000, 4000, g => g.Uint16Field);
            AssertAttribute(reg, "uint16-field", null, 0, 0, g => g.Uint16Field);
        }
示例#18
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_DateTime_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "nullable-date-time-field", "1776-07-04", new DateTime(1776, 07, 04, 0, 0, 0, DateTimeKind.Utc), "1776-07-04T00:00:00", g => g.NullableDateTimeField);
            AssertAttribute(reg, "nullable-date-time-field", "1776-07-04T00:00:00", new DateTime(1776, 07, 04, 0, 0, 0, DateTimeKind.Utc), "1776-07-04T00:00:00", g => g.NullableDateTimeField);
            AssertAttribute(reg, "nullable-date-time-field", null, null, (DateTime?)null, g => g.NullableDateTimeField);
        }
示例#19
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_guid_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            var guid = new Guid("6566f9b4-5245-40de-890d-98b40a4ad656");

            AssertAttribute(reg, "nullable-guid-field", "6566f9b4-5245-40de-890d-98b40a4ad656", guid, "6566f9b4-5245-40de-890d-98b40a4ad656", g => g.NullableGuidField);
            AssertAttribute(reg, "nullable-guid-field", null, null, (Guid?)null, g => g.NullableGuidField);
        }
示例#20
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_Single_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "nullable-single-field", 0f, 0f, 0f, g => g.NullableSingleField);
            AssertAttribute(reg, "nullable-single-field", 20000000000.1234f, 20000000000.1234f, (Int64?)20000000000.1234f, g => g.NullableSingleField);
            AssertAttribute(reg, "nullable-single-field", -20000000000.1234f, -20000000000.1234f, -20000000000.1234f, g => g.NullableSingleField);
            AssertAttribute(reg, "nullable-single-field", null, null, (Single?)null, g => g.NullableSingleField);
        }
示例#21
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_SByte_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttribute(reg, "nullable-sbyte-field", 0, (SByte?)0, (SByte?)0, g => g.NullableSbyteField);
            AssertAttribute(reg, "nullable-sbyte-field", 12, (SByte?)12, (SByte?)12, g => g.NullableSbyteField);
            AssertAttribute(reg, "nullable-sbyte-field", -12, (SByte?)-12, (SByte?)-12, g => g.NullableSbyteField);
            AssertAttribute(reg, "nullable-sbyte-field", null, null, (SByte?)null, g => g.NullableSbyteField);
        }
示例#22
0
        public void BuildRegistration_sets_up_correct_attribute_for_nullable_DateTimeOffset_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            var testDateTimeOffset = new DateTimeOffset(new DateTime(1776, 07, 04), TimeSpan.FromHours(-5));

            AssertAttribute(reg, "nullable-date-time-offset-field", "1776-07-04T00:00:00-05:00", testDateTimeOffset, "1776-07-04T00:00:00.0000000-05:00",
                            g => g.NullableDateTimeOffsetField);
            AssertAttribute(reg, "nullable-date-time-offset-field", null, null, (DateTimeOffset?)null,
                            g => g.NullableDateTimeOffsetField);
        }
示例#23
0
        public void BuildRegistration_sets_up_correct_attribute_for_Decimal_field_non_en_US()
        {
            // Set up non US culture
            var culture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = new CultureInfo("se-SE");

            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            AssertAttribute(reg, "decimal-field", "20000000000.1234", 20000000000.1234m, "20000000000.1234", g => g.DecimalField);

            Thread.CurrentThread.CurrentCulture = culture;
        }
示例#24
0
文件: Program.cs 项目: visnema/bicep
        private static void BuildSingleFile(IDiagnosticLogger logger, string bicepPath, string outputPath)
        {
            string text       = ReadFile(bicepPath);
            var    lineStarts = TextCoordinateConverter.GetLineStarts(text);

            var resourceTypeRegistrar = new ResourceTypeRegistrar(new AzResourceTypeProvider());
            var compilation           = new Compilation(resourceTypeRegistrar, SyntaxFactory.CreateFromText(text));

            var emitter = new TemplateEmitter(compilation.GetSemanticModel());

            using var outputStream = CreateFileStream(outputPath);
            var result = emitter.Emit(outputStream);

            foreach (var diagnostic in result.Diagnostics)
            {
                logger.LogDiagnostic(bicepPath, diagnostic, lineStarts);
            }
        }
示例#25
0
        public void BuildRegistration_sets_up_correct_attribute_for_DateTimeOffset_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            var testDateTimeOffset1 = new DateTimeOffset(new DateTime(1776, 07, 04), TimeSpan.FromHours(-5));
            var testDateTimeOffset2 = new DateTimeOffset(new DateTime(1776, 07, 04, 12, 30, 0), TimeSpan.FromHours(0));
            var testDateTimeOffset3 = new DateTimeOffset(new DateTime(2015, 03, 11, 04, 31, 0), TimeSpan.FromHours(0));

            AssertAttribute(reg, "date-time-offset-field", "1776-07-04T00:00:00-05:00", testDateTimeOffset1, "1776-07-04T00:00:00.0000000-05:00", g => g.DateTimeOffsetField);
            AssertAttribute(reg, "date-time-offset-field", "1776-07-04T12:30:00+00:00", testDateTimeOffset2, "1776-07-04T12:30:00.0000000+00:00", g => g.DateTimeOffsetField);
            AssertAttribute(reg, "date-time-offset-field", "2015-03-11T04:31:00.0000000+00:00", testDateTimeOffset3, "2015-03-11T04:31:00.0000000+00:00", g => g.DateTimeOffsetField);
            AssertAttribute(reg, "date-time-offset-field", null, new DateTimeOffset(), "0001-01-01T00:00:00.0000000+00:00", g => g.DateTimeOffsetField);
        }
示例#26
0
        public void BuildRegistration_sets_up_correct_attribute_for_to_many_complex_field()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var reg = registrar.BuildRegistration(typeof(AttributeGrabBag));

            // Assert
            AssertAttributeHelper(reg, "to-many-complex-type-field",
                                  new JArray
            {
                new JObject {
                    { "intProp", 49 }, { "StringProp", "blue" }
                },
                new JObject {
                    { "intProp", 67 }, { "StringProp", "orange" }
                }
            },
                                  grabBag =>
            {
                var result = grabBag.ToManyComplexTypeField.ToArray();
                result.Length.Should().Be(2);
                result[0].IntProp.Should().Be(49);
                result[0].StringProp.Should().Be("blue");
                result[1].IntProp.Should().Be(67);
                result[1].StringProp.Should().Be("orange");
            },
                                  token =>
            {
                var jarray = (JArray)token;
                jarray.Count.Should().Be(2);
                ((int)jarray[0]["intProp"]).Should().Be(49);
                ((string)jarray[0]["StringProp"]).Should().Be("blue");
                ((int)jarray[1]["intProp"]).Should().Be(67);
                ((string)jarray[1]["StringProp"]).Should().Be("orange");
            });
            AssertAttribute(reg, "to-many-complex-type-field", null, null, (SampleComplexType[])null, g => g.ToManyComplexTypeField);
        }
示例#27
0
        public void BuildRegistration_sets_up_registration_correctly()
        {
            // Arrange
            var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService()));

            // Act
            var postReg = registrar.BuildRegistration(typeof(Post));

            // Assert
            postReg.IdProperty.Should().BeSameAs(typeof(Post).GetProperty("Id"));
            postReg.ResourceTypeName.Should().Be("posts");
            postReg.Attributes.Length.Should().Be(1);
            postReg.Attributes.First().Property.Should().BeSameAs(typeof(Post).GetProperty("Title"));
            postReg.Relationships.Length.Should().Be(2);
            postReg.Relationships[0].IsToMany.Should().BeFalse();
            postReg.Relationships[0].Property.Should().BeSameAs(typeof(Post).GetProperty("Author"));
            postReg.Relationships[0].SelfLinkTemplate.Should().BeNull();
            postReg.Relationships[0].RelatedResourceLinkTemplate.Should().BeNull();
            postReg.Relationships[1].IsToMany.Should().BeTrue();
            postReg.Relationships[1].Property.Should().BeSameAs(typeof(Post).GetProperty("Comments"));
            postReg.Relationships[1].SelfLinkTemplate.Should().Be("/posts/{1}/relationships/comments");
            postReg.Relationships[1].RelatedResourceLinkTemplate.Should().Be("/posts/{1}/comments");
        }
示例#28
0
        private static Compilation GetCompilation(string text)
        {
            var resourceTypeRegistrar = new ResourceTypeRegistrar(new AzResourceTypeProvider());

            return(new Compilation(resourceTypeRegistrar, SyntaxFactory.CreateFromText(text)));
        }
示例#29
0
 public BicepCompilationProvider(ResourceTypeRegistrar resourceTypeRegistrar)
 {
     this.resourceTypeRegistrar = resourceTypeRegistrar;
 }