Skip to content

Commit 1fb5418

Browse files
committed
Changed convension for content type aliases to be pascal cased, like Umbraco recommends.
Added nontracked files added for annotation.
1 parent 5937515 commit 1fb5418

16 files changed

+368
-22
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using NUnit.Framework;
7+
using Umbraco.CodeGen.Configuration;
8+
using Umbraco.CodeGen.Definitions;
9+
using Umbraco.CodeGen.Generators;
10+
using Umbraco.CodeGen.Generators.Annotated;
11+
using Umbraco.CodeGen.Tests.TestHelpers;
12+
13+
namespace Umbraco.CodeGen.Tests
14+
{
15+
[TestFixture]
16+
public class AnnotatedCodeGeneratorAcceptanceTests
17+
{
18+
[Test]
19+
public void BuildCode_GeneratesCodeForDocumentType()
20+
{
21+
TestBuildCode("SomeAnnotatedDocumentType", "SomeDocumentType", "DocumentType");
22+
}
23+
24+
[Test]
25+
public void BuildCode_GeneratesCodeForMediaType()
26+
{
27+
TestBuildCode("SomeAnnotatedMediaType", "SomeMediaType", "MediaType");
28+
}
29+
30+
private void TestBuildCode(string classFileName, string xmlFileName, string contentTypeName)
31+
{
32+
ContentType contentType;
33+
var expectedOutput = "";
34+
using (var inputReader = File.OpenText(@"..\..\TestFiles\" + xmlFileName + ".xml"))
35+
{
36+
contentType = new ContentTypeSerializer().Deserialize(inputReader);
37+
}
38+
using (var goldReader = File.OpenText(@"..\..\TestFiles\" + classFileName + ".cs"))
39+
{
40+
expectedOutput = goldReader.ReadToEnd();
41+
}
42+
43+
var configuration = new CodeGeneratorConfiguration();
44+
configuration.TypeMappings.Add(new TypeMapping("1413afcb-d19a-4173-8e9a-68288d2a73b8", "Int32"));
45+
var typeConfig = configuration.Get(contentTypeName);
46+
typeConfig.BaseClass = "Umbraco.Core.Models.TypedModelBase";
47+
typeConfig.Namespace = "Umbraco.CodeGen.Models";
48+
49+
var sb = new StringBuilder();
50+
var writer = new StringWriter(sb);
51+
52+
var dataTypeProvider = new TestDataTypeProvider();
53+
var generator = new CodeGenerator(typeConfig, dataTypeProvider, new AnnotatedCodeGeneratorFactory());
54+
55+
generator.Generate(contentType, writer);
56+
57+
writer.Flush();
58+
Console.WriteLine(sb.ToString());
59+
60+
Assert.AreEqual(expectedOutput, sb.ToString());
61+
}
62+
}
63+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.CodeDom;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
using Umbraco.CodeGen.Generators.Bcl;
5+
6+
namespace Umbraco.CodeGen.Tests.Generators.Bcl
7+
{
8+
[TestFixture]
9+
public class ImportsGeneratorTests
10+
{
11+
[Test]
12+
public void Generate_AddsImports()
13+
{
14+
var ns = new CodeNamespace("ANamespace");
15+
var generator = new ImportsGenerator(null);
16+
generator.Generate(ns, null);
17+
Assert.That(
18+
new[]
19+
{
20+
"System",
21+
"System.ComponentModel",
22+
"System.ComponentModel.DataAnnotations",
23+
"Umbraco.Core.Models",
24+
"Umbraco.Web"
25+
}.SequenceEqual(
26+
ns.Imports.Cast<CodeNamespaceImport>()
27+
.Select(import => import.Namespace)
28+
));
29+
}
30+
}
31+
}

Umbraco.CodeGen.Tests/Generators/ClassGeneratorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void Generate_Master_WhenNullOrEmpty_IsConfiguredBaseClass(string master)
5656
[Test]
5757
public void Generate_Master_WhenNotEmpty_IsBaseClassPascalCased()
5858
{
59-
const string expectedMaster = "aBaseClass";
59+
const string expectedMaster = "ABaseClass";
6060
info.Master = expectedMaster;
6161
Generate();
6262
Assert.AreEqual(expectedMaster.PascalCase(), Type.BaseTypes[0].BaseType);
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
using NUnit.Framework;
2+
using Umbraco.CodeGen.Configuration;
3+
using Umbraco.CodeGen.Definitions;
4+
using Umbraco.CodeGen.Parsers.Annotated;
5+
6+
namespace Umbraco.CodeGen.Tests.Parsers.Annotated
7+
{
8+
[TestFixture]
9+
public class CommonInfoParserTests : ContentTypeCodeParserTestBase
10+
{
11+
[SetUp]
12+
public void SetUp()
13+
{
14+
Configuration = new CodeGeneratorConfiguration().MediaTypes;
15+
Parser = new CommonInfoParser(Configuration);
16+
ContentType = new MediaType();
17+
Info = ContentType.Info;
18+
}
19+
20+
[Test]
21+
public void Parse_Name_WhenPureClassName_IsSplitPascal()
22+
{
23+
const string code = @"
24+
public class ItsAName {}
25+
";
26+
Parse(code);
27+
Assert.AreEqual("Its A Name", Info.Name);
28+
}
29+
30+
[Test]
31+
public void Parse_Name_WhenDisplayNameArgument_IsAttributeValue()
32+
{
33+
const string code = @"
34+
[MediaType(DisplayName=""It's another name"")]
35+
public class ItsAName {}
36+
";
37+
Parse(code);
38+
Assert.AreEqual("It's another name", Info.Name);
39+
}
40+
41+
[Test]
42+
public void Parse_Description_WhenDescriptionArgument_IsAttributeValue()
43+
{
44+
const string code = @"
45+
[MediaType(Description=""It's a description"")]
46+
public class AClass {}
47+
";
48+
Parse(code);
49+
Assert.AreEqual("It's a description", Info.Description);
50+
}
51+
52+
[Test]
53+
public void Parse_Description_WhenArgumentMissing_IsNull()
54+
{
55+
Parse(EmptyClass);
56+
Assert.IsNull(Info.Description);
57+
}
58+
59+
[Test]
60+
public void Parse_Alias_IsPascalCase()
61+
{
62+
const string code = @"
63+
public class ItShouldBePascal{}
64+
";
65+
Parse(code);
66+
Assert.AreEqual("ItShouldBePascal", Info.Alias);
67+
}
68+
69+
[Test]
70+
public void Parse_Master_WhenNoBaseClass_IsNull()
71+
{
72+
Parse(EmptyClass);
73+
Assert.IsNull(Info.Master);
74+
}
75+
76+
[Test]
77+
public void Parse_Master_WhenRootBaseClass_IsNull()
78+
{
79+
const string rootBaseClass = "RootBaseClass";
80+
const string code = @"
81+
public class AClass : RootBaseClass {}
82+
";
83+
Configuration.BaseClass = rootBaseClass;
84+
Parse(code);
85+
Assert.IsNull(Info.Master);
86+
}
87+
88+
[Test]
89+
public void Parse_Master_WhenNonRootBaseClass_IsBaseClassPascalCased()
90+
{
91+
const string expectedBaseClass = "ExpectedBaseClass";
92+
const string code = @"
93+
public class AClass : ExpectedBaseClass {}
94+
";
95+
Parse(code);
96+
Assert.AreEqual(expectedBaseClass.PascalCase(), Info.Master);
97+
}
98+
99+
[Test]
100+
public void Parse_AllowAtRoot_WhenTrueArgument_IsTrue()
101+
{
102+
const string code = @"
103+
[MediaType(AllowAtRoot=true)]
104+
public class AClass {
105+
}
106+
";
107+
Parse(code);
108+
Assert.IsTrue(Info.AllowAtRoot);
109+
}
110+
111+
[Test]
112+
public void Parse_AllowAtRoot_WhenFalseArgument_IsFalse()
113+
{
114+
const string code = @"
115+
[MediaType(AllowAtRoot=false)]
116+
public class AClass {
117+
}
118+
";
119+
Parse(code);
120+
Assert.IsFalse(Info.AllowAtRoot);
121+
}
122+
123+
[Test]
124+
public void Parse_AllowAtRoot_WhenMissing_IsFalse()
125+
{
126+
Parse(EmptyClass);
127+
Assert.IsFalse(Info.AllowAtRoot);
128+
}
129+
130+
[Test]
131+
[TestCase("Icon", "anIcon.gif")]
132+
[TestCase("Thumbnail", "folder.png")]
133+
public void Parse_Argument_HasValue(
134+
string memberName,
135+
string memberValue
136+
)
137+
{
138+
var code = @"
139+
[MediaType(" + memberName + @" = """ + memberValue + @""")]
140+
public class AClass {
141+
}
142+
";
143+
Parse(code);
144+
var value = typeof(DocumentTypeInfo).GetProperty(memberName).GetValue(Info, null);
145+
Assert.AreEqual(memberValue, value);
146+
}
147+
148+
[Test]
149+
[TestCase("Icon", "folder.gif")]
150+
[TestCase("Thumbnail", "folder.png")]
151+
public void Parse_Argument_IsNullOrMissing_HasDefaultValue(
152+
string memberName,
153+
object expectedValue
154+
)
155+
{
156+
var code = new[]{
157+
EmptyClass
158+
, @"
159+
[MediaType(" + memberName + @" = null)]
160+
public class AClass {
161+
}
162+
"};
163+
foreach (var snippet in code)
164+
{
165+
Parse(snippet);
166+
var value = PropertyValue(memberName);
167+
Assert.AreEqual(expectedValue, value);
168+
}
169+
}
170+
}
171+
}

Umbraco.CodeGen.Tests/Parsers/Annotated/StructureParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class AClass {
3232
Parse(code);
3333
Console.WriteLine(String.Join(", ", ContentType.Structure));
3434
Assert.That(
35-
new[] { "anotherClass", "differentClass" }
35+
new[] { "AnotherClass", "DifferentClass" }
3636
.SequenceEqual(ContentType.Structure)
3737
);
3838
}

Umbraco.CodeGen.Tests/Parsers/Bcl/CommonInfoParserTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public void Parse_Description_WhenAttributeMissing_IsNull()
5757
}
5858

5959
[Test]
60-
public void Parse_Alias_IsCamelCase()
60+
public void Parse_Alias_IsPascalCase()
6161
{
6262
const string code = @"
63-
public class ItShouldBeCamel{}
63+
public class ItShouldBePascal{}
6464
";
6565
Parse(code);
66-
Assert.AreEqual("itShouldBeCamel", Info.Alias);
66+
Assert.AreEqual("ItShouldBePascal", Info.Alias);
6767
}
6868

6969
[Test]
@@ -86,14 +86,14 @@ public class AClass : RootBaseClass {}
8686
}
8787

8888
[Test]
89-
public void Parse_Master_WhenNonRootBaseClass_IsBaseClassCascalCased()
89+
public void Parse_Master_WhenNonRootBaseClass_IsBaseClassPascalCased()
9090
{
9191
const string expectedBaseClass = "ExpectedBaseClass";
9292
const string code = @"
9393
public class AClass : ExpectedBaseClass {}
9494
";
9595
Parse(code);
96-
Assert.AreEqual(expectedBaseClass.CamelCase(), Info.Master);
96+
Assert.AreEqual(expectedBaseClass.PascalCase(), Info.Master);
9797
}
9898

9999
[Test]

Umbraco.CodeGen.Tests/Parsers/Bcl/StructureParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class AClass {
3030

3131
Parse(code);
3232
Assert.That(
33-
new[]{"anotherClass", "differentClass"}
33+
new[]{"AnotherClass", "DifferentClass"}
3434
.SequenceEqual(ContentType.Structure)
3535
);
3636
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace Umbraco.CodeGen.Models
2+
{
3+
using System;
4+
using Umbraco.CodeGen.Annotations;
5+
using Umbraco.Core.Models;
6+
using Umbraco.Web;
7+
8+
[DocumentType(Description="A description of some document type", Icon="privateMemberIcon.gif", Thumbnail="privateMemberThumb.png", AllowAtRoot=true, DefaultTemplate="ATemplate", AllowedTemplates=new String[] {
9+
"ATemplate",
10+
"AnotherTemplate"}, Structure=new System.Type[] {
11+
typeof(SomeOtherDocType)})]
12+
public partial class SomeDocumentType : Umbraco.Core.Models.TypedModelBase
13+
{
14+
public SomeDocumentType(IPublishedContent content) :
15+
base(content)
16+
{
17+
}
18+
[GenericProperty(Description="A description", Definition="RTE", Tab="A tab", Mandatory=true, Validation="[a-z]")]
19+
public virtual String SomeProperty
20+
{
21+
get
22+
{
23+
return Content.GetPropertyValue<String>("someProperty");
24+
}
25+
}
26+
[GenericProperty(Description="Another description", Definition="RTE", Tab="A tab")]
27+
public virtual String AnotherProperty
28+
{
29+
get
30+
{
31+
return Content.GetPropertyValue<String>("anotherProperty");
32+
}
33+
}
34+
[GenericProperty(Definition="Numeric")]
35+
public virtual Int32 TablessProperty
36+
{
37+
get
38+
{
39+
return Content.GetPropertyValue<Int32>("tablessProperty");
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)