Files
Umbraco-CMS/tests/Umbraco.Tests.Common/Builders/MacroBuilder.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

129 lines
3.5 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.Builders.Interfaces;
namespace Umbraco.Cms.Tests.Common.Builders
{
public class MacroBuilder
: BuilderBase<Macro>,
IWithIdBuilder,
IWithKeyBuilder,
IWithAliasBuilder,
IWithNameBuilder
{
private readonly List<MacroPropertyBuilder> _propertyBuilders = new List<MacroPropertyBuilder>();
private int? _id;
private Guid? _key;
private string _alias;
private string _name;
private bool? _useInEditor;
private int? _cacheDuration;
private bool? _cacheByPage;
private bool? _cacheByMember;
private bool? _dontRender;
private string _macroSource;
public MacroBuilder WithUseInEditor(bool useInEditor)
{
_useInEditor = useInEditor;
return this;
}
public MacroBuilder WithCacheDuration(int cacheDuration)
{
_cacheDuration = cacheDuration;
return this;
}
public MacroBuilder WithCacheByPage(bool cacheByPage)
{
_cacheByPage = cacheByPage;
return this;
}
public MacroBuilder WithCacheByMember(bool cacheByMember)
{
_cacheByMember = cacheByMember;
return this;
}
public MacroBuilder WithDontRender(bool dontRender)
{
_dontRender = dontRender;
return this;
}
public MacroBuilder WithSource(string macroSource)
{
_macroSource = macroSource;
return this;
}
public MacroPropertyBuilder AddProperty()
{
var builder = new MacroPropertyBuilder(this);
_propertyBuilders.Add(builder);
return builder;
}
public override Macro Build()
{
var id = _id ?? 1;
var name = _name ?? Guid.NewGuid().ToString();
var alias = _alias ?? name.ToCamelCase();
Guid key = _key ?? Guid.NewGuid();
var useInEditor = _useInEditor ?? false;
var cacheDuration = _cacheDuration ?? 0;
var cacheByPage = _cacheByPage ?? false;
var cacheByMember = _cacheByMember ?? false;
var dontRender = _dontRender ?? false;
var macroSource = _macroSource ?? string.Empty;
var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());
var macro = new Macro(shortStringHelper, id, key, useInEditor, cacheDuration, alias, name, cacheByPage, cacheByMember, dontRender, macroSource);
foreach (IMacroProperty property in _propertyBuilders.Select(x => x.Build()))
{
macro.Properties.Add(property);
}
return macro;
}
int? IWithIdBuilder.Id
{
get => _id;
set => _id = value;
}
Guid? IWithKeyBuilder.Key
{
get => _key;
set => _key = value;
}
string IWithAliasBuilder.Alias
{
get => _alias;
set => _alias = value;
}
string IWithNameBuilder.Name
{
get => _name;
set => _name = value;
}
}
}