Migrated stylesheet and template tests into new project and builder pattern.

This commit is contained in:
Andy Butland
2020-04-04 09:25:50 +02:00
parent 59997252a2
commit 58a1bc0a8f
9 changed files with 357 additions and 110 deletions

View File

@@ -1,116 +0,0 @@
using System.Diagnostics;
using System.Linq;
using Newtonsoft.Json;
using NUnit.Framework;
using Umbraco.Core.Models;
using Umbraco.Core.Serialization;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Models
{
[TestFixture]
public class StylesheetTests
{
[SetUp]
public virtual void Initialize()
{
SettingsForTests.Reset();
}
[Test]
public void Can_Create_Stylesheet()
{
// Arrange
var stylesheet = new Stylesheet("/css/styles.css");
stylesheet.Content = @"body { color:#000; } .bold {font-weight:bold;}";
// Assert
Assert.That(stylesheet.Name, Is.EqualTo("styles.css"));
Assert.That(stylesheet.Alias, Is.EqualTo("styles"));
}
[Test]
public void Can_Add_Property()
{
// Arrange
var stylesheet = new Stylesheet("/css/styles.css") {Content = @"body { color:#000; } .bold {font-weight:bold;}"};
stylesheet.AddProperty(new StylesheetProperty("Test", "p", "font-weight:bold; font-family:Arial;"));
// Assert
Assert.AreEqual(1, stylesheet.Properties.Count());
Assert.AreEqual("Test", stylesheet.Properties.Single().Name);
Assert.AreEqual("p", stylesheet.Properties.Single().Alias);
Assert.AreEqual("font-weight:bold;\r\nfont-family:Arial;", stylesheet.Properties.Single().Value);
}
[Test]
public void Can_Remove_Property()
{
// Arrange
var stylesheet = new Stylesheet("/css/styles.css") { Content = @"body { color:#000; } /**umb_name:Hello*/p{font-size:2em;} .bold {font-weight:bold;}" };
Assert.AreEqual(1, stylesheet.Properties.Count());
stylesheet.RemoveProperty("Hello");
Assert.AreEqual(0, stylesheet.Properties.Count());
Assert.AreEqual(@"body { color:#000; } .bold {font-weight:bold;}", stylesheet.Content);
}
[Test]
public void Can_Update_Property()
{
// Arrange
var stylesheet = new Stylesheet("/css/styles.css")
{
Content = @"body { color:#000; } /**umb_name:Hello*/p{font-size:2em;} .bold {font-weight:bold;}"
};
var prop = stylesheet.Properties.Single();
prop.Alias = "li";
prop.Value = "font-size:5em;";
//re-get
prop = stylesheet.Properties.Single();
Assert.AreEqual("li", prop.Alias);
Assert.AreEqual("font-size:5em;", prop.Value);
Assert.AreEqual("body { color:#000; } /**umb_name:Hello*/\r\nli {\r\n\tfont-size:5em;\r\n} .bold {font-weight:bold;}", stylesheet.Content);
}
[Test]
public void Can_Get_Properties_From_Css()
{
// Arrange
var stylesheet = new Stylesheet("/css/styles.css");
stylesheet.Content = @"body { color:#000; } .bold {font-weight:bold;} /**umb_name:Hello */ p { font-size: 1em; } /**umb_name:testing123*/ li:first-child {padding:0px;}";
// Act
var properties = stylesheet.Properties;
Assert.AreEqual(2, properties.Count());
Assert.AreEqual("Hello", properties.First().Name);
Assert.AreEqual("font-size: 1em;", properties.First().Value);
Assert.AreEqual("p", properties.First().Alias);
Assert.AreEqual("testing123", properties.Last().Name);
Assert.AreEqual("padding:0px;", properties.Last().Value);
Assert.AreEqual("li:first-child", properties.Last().Alias);
}
[Test]
public void Can_Serialize_Without_Error()
{
var stylesheet = new Stylesheet("/css/styles.css");
stylesheet.Content = @"@media screen and (min-width: 600px) and (min-width: 900px) {
.class {
background: #666;
}
}";
var json = JsonConvert.SerializeObject(stylesheet);
Debug.Print(json);
}
}
}

View File

@@ -1,81 +0,0 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using NUnit.Framework;
using Umbraco.Core.Models;
using Umbraco.Core.Serialization;
using Umbraco.Tests.Testing;
namespace Umbraco.Tests.Models
{
[TestFixture]
public class TemplateTests : UmbracoTestBase
{
[Test]
public void Can_Deep_Clone()
{
var item = new Template(ShortStringHelper, "Test", "test")
{
Id = 3,
CreateDate = DateTime.Now,
Key = Guid.NewGuid(),
UpdateDate = DateTime.Now,
Content = "blah",
Path = "-1,3",
IsMasterTemplate = true,
MasterTemplateAlias = "master",
MasterTemplateId = new Lazy<int>(() => 88)
};
var clone = (Template)item.DeepClone();
Assert.AreNotSame(clone, item);
Assert.AreEqual(clone, item);
Assert.AreEqual(clone.Path, item.Path);
Assert.AreEqual(clone.IsMasterTemplate, item.IsMasterTemplate);
Assert.AreEqual(clone.CreateDate, item.CreateDate);
Assert.AreEqual(clone.Alias, item.Alias);
Assert.AreEqual(clone.Id, item.Id);
Assert.AreEqual(clone.Key, item.Key);
Assert.AreEqual(clone.MasterTemplateAlias, item.MasterTemplateAlias);
Assert.AreEqual(clone.MasterTemplateId.Value, item.MasterTemplateId.Value);
Assert.AreEqual(clone.Name, item.Name);
Assert.AreEqual(clone.UpdateDate, item.UpdateDate);
// clone.Content should be null but getting it would lazy-load
var type = clone.GetType();
var contentField = type.BaseType.GetField("_content", BindingFlags.Instance | BindingFlags.NonPublic);
var value = contentField.GetValue(clone);
Assert.IsNull(value);
// this double verifies by reflection
// need to exclude content else it would lazy-load
var allProps = clone.GetType().GetProperties();
foreach (var propertyInfo in allProps.Where(x => x.Name != "Content"))
{
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(item, null));
}
}
[Test]
public void Can_Serialize_Without_Error()
{
var item = new Template(ShortStringHelper, "Test", "test")
{
Id = 3,
CreateDate = DateTime.Now,
Key = Guid.NewGuid(),
UpdateDate = DateTime.Now,
Content = "blah",
MasterTemplateAlias = "master",
MasterTemplateId = new Lazy<int>(() => 88)
};
var json = JsonConvert.SerializeObject(item);
Debug.Print(json);
}
}
}

View File

@@ -300,7 +300,6 @@
<Compile Include="Models\PropertyGroupTests.cs" />
<Compile Include="Models\PropertyTypeTests.cs" />
<Compile Include="Models\RelationTests.cs" />
<Compile Include="Models\TemplateTests.cs" />
<Compile Include="Models\LightEntityTest.cs" />
<Compile Include="Models\UserTests.cs" />
<Compile Include="Web\Mvc\RenderModelBinderTests.cs" />
@@ -402,7 +401,6 @@
<Compile Include="PropertyEditors\PropertyEditorValueConverterTests.cs" />
<Compile Include="Models\ContentTests.cs" />
<Compile Include="Models\ContentXmlTest.cs" />
<Compile Include="Models\StylesheetTests.cs" />
<Compile Include="Persistence\SqlCeTableByTableTest.cs" />
<Compile Include="Persistence\DatabaseContextTests.cs" />
<Compile Include="Persistence\Mappers\ContentMapperTest.cs" />