|
1 | 1 | // Copyright (c) Umbraco. |
2 | 2 | // See LICENSE for more details. |
3 | 3 |
|
4 | | -using System.Collections.Generic; |
5 | | -using System.Linq; |
6 | 4 | using System.Text; |
7 | 5 | using NUnit.Framework; |
8 | 6 | using Umbraco.Cms.Core.Configuration.Models; |
@@ -118,6 +116,102 @@ public Type1(IPublishedContent content, IPublishedValueFallback publishedValueFa |
118 | 116 | Assert.AreEqual(expected.ClearLf(), gen); |
119 | 117 | } |
120 | 118 |
|
| 119 | + [Test] |
| 120 | + public void GenerateSimpleType_WithoutVersion() |
| 121 | + { |
| 122 | + // Umbraco returns nice, pascal-cased names. |
| 123 | + var type1 = new TypeModel |
| 124 | + { |
| 125 | + Id = 1, |
| 126 | + Alias = "type1", |
| 127 | + ClrName = "Type1", |
| 128 | + Name = "type1Name", |
| 129 | + ParentId = 0, |
| 130 | + BaseType = null, |
| 131 | + ItemType = TypeModel.ItemTypes.Content, |
| 132 | + }; |
| 133 | + type1.Properties.Add(new PropertyModel |
| 134 | + { |
| 135 | + Alias = "prop1", |
| 136 | + ClrName = "Prop1", |
| 137 | + Name = "prop1Name", |
| 138 | + ModelClrType = typeof(string), |
| 139 | + }); |
| 140 | + |
| 141 | + TypeModel[] types = { type1 }; |
| 142 | + |
| 143 | + var modelsBuilderConfig = new ModelsBuilderSettings { IncludeVersionNumberInGeneratedModels = false }; |
| 144 | + var builder = new TextBuilder(modelsBuilderConfig, types); |
| 145 | + |
| 146 | + var sb = new StringBuilder(); |
| 147 | + builder.Generate(sb, builder.GetModelsToGenerate().First()); |
| 148 | + var gen = sb.ToString(); |
| 149 | + |
| 150 | + var expected = @"//------------------------------------------------------------------------------ |
| 151 | +// <auto-generated> |
| 152 | +// This code was generated by a tool. |
| 153 | +// |
| 154 | +// Umbraco.ModelsBuilder.Embedded |
| 155 | +// |
| 156 | +// Changes to this file will be lost if the code is regenerated. |
| 157 | +// </auto-generated> |
| 158 | +//------------------------------------------------------------------------------ |
| 159 | +
|
| 160 | +using System; |
| 161 | +using System.Linq.Expressions; |
| 162 | +using Umbraco.Cms.Core.Models.PublishedContent; |
| 163 | +using Umbraco.Cms.Core.PublishedCache; |
| 164 | +using Umbraco.Cms.Infrastructure.ModelsBuilder; |
| 165 | +using Umbraco.Cms.Core; |
| 166 | +using Umbraco.Extensions; |
| 167 | +
|
| 168 | +namespace Umbraco.Cms.Web.Common.PublishedModels |
| 169 | +{ |
| 170 | + /// <summary>type1Name</summary> |
| 171 | + [PublishedModel(""type1"")] |
| 172 | + public partial class Type1 : PublishedContentModel |
| 173 | + { |
| 174 | + // helpers |
| 175 | +#pragma warning disable 0109 // new is redundant |
| 176 | + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder.Embedded"", """")] |
| 177 | + public new const string ModelTypeAlias = ""type1""; |
| 178 | + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder.Embedded"", """")] |
| 179 | + public new const PublishedItemType ModelItemType = PublishedItemType.Content; |
| 180 | + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder.Embedded"", """")] |
| 181 | + [return: global::System.Diagnostics.CodeAnalysis.MaybeNull] |
| 182 | + public new static IPublishedContentType GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor) |
| 183 | + => PublishedModelUtility.GetModelContentType(publishedSnapshotAccessor, ModelItemType, ModelTypeAlias); |
| 184 | + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder.Embedded"", """")] |
| 185 | + [return: global::System.Diagnostics.CodeAnalysis.MaybeNull] |
| 186 | + public static IPublishedPropertyType GetModelPropertyType<TValue>(IPublishedSnapshotAccessor publishedSnapshotAccessor, Expression<Func<Type1, TValue>> selector) |
| 187 | + => PublishedModelUtility.GetModelPropertyType(GetModelContentType(publishedSnapshotAccessor), selector); |
| 188 | +#pragma warning restore 0109 |
| 189 | +
|
| 190 | + private IPublishedValueFallback _publishedValueFallback; |
| 191 | +
|
| 192 | + // ctor |
| 193 | + public Type1(IPublishedContent content, IPublishedValueFallback publishedValueFallback) |
| 194 | + : base(content, publishedValueFallback) |
| 195 | + { |
| 196 | + _publishedValueFallback = publishedValueFallback; |
| 197 | + } |
| 198 | +
|
| 199 | + // properties |
| 200 | +
|
| 201 | + ///<summary> |
| 202 | + /// prop1Name |
| 203 | + ///</summary> |
| 204 | + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder.Embedded"", """")] |
| 205 | + [global::System.Diagnostics.CodeAnalysis.MaybeNull] |
| 206 | + [ImplementPropertyType(""prop1"")] |
| 207 | + public virtual string Prop1 => this.Value<string>(_publishedValueFallback, ""prop1""); |
| 208 | + } |
| 209 | +} |
| 210 | +"; |
| 211 | + Console.WriteLine(gen); |
| 212 | + Assert.AreEqual(expected.ClearLf(), gen); |
| 213 | + } |
| 214 | + |
121 | 215 | [Test] |
122 | 216 | public void GenerateSimpleType_Ambiguous_Issue() |
123 | 217 | { |
|
0 commit comments