mirror of https://github.com/Squidex/squidex.git
13 changed files with 215 additions and 5 deletions
@ -0,0 +1,15 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
|
|||
namespace Squidex.Web |
|||
{ |
|||
public sealed class ExposedConfiguration : Dictionary<string, string> |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Web |
|||
{ |
|||
public sealed class ExposedValues : Dictionary<string, string> |
|||
{ |
|||
public ExposedValues() |
|||
{ |
|||
} |
|||
|
|||
public ExposedValues(ExposedConfiguration configured, IConfiguration configuration) |
|||
{ |
|||
Guard.NotNull(configured, nameof(configured)); |
|||
Guard.NotNull(configuration, nameof(configuration)); |
|||
|
|||
foreach (var kvp in configured) |
|||
{ |
|||
var value = configuration.GetValue<string>(kvp.Value); |
|||
|
|||
if (!string.IsNullOrWhiteSpace(value)) |
|||
{ |
|||
this[kvp.Key] = value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
public override string ToString() |
|||
{ |
|||
var sb = new StringBuilder(); |
|||
|
|||
foreach (var kvp in this) |
|||
{ |
|||
if (sb.Length > 0) |
|||
{ |
|||
sb.Append(", "); |
|||
} |
|||
|
|||
sb.Append(kvp.Key); |
|||
sb.Append(": "); |
|||
sb.Append(kvp.Value); |
|||
} |
|||
|
|||
return sb.ToString(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using FakeItEasy; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Web |
|||
{ |
|||
public class ExposedValuesTests |
|||
{ |
|||
[Fact] |
|||
public void Should_create_from_configuration() |
|||
{ |
|||
var source = new ExposedConfiguration |
|||
{ |
|||
["name1"] = "config1", |
|||
["name2"] = "config2", |
|||
["name3"] = "config3", |
|||
}; |
|||
|
|||
var configuration = A.Fake<IConfiguration>(); |
|||
|
|||
SetupConfiguration(configuration, "config1", "value1"); |
|||
SetupConfiguration(configuration, "config2", "value2"); |
|||
|
|||
var values = new ExposedValues(source, configuration); |
|||
|
|||
Assert.Equal(2, values.Count); |
|||
Assert.Equal("value1", values["name1"]); |
|||
Assert.Equal("value2", values["name2"]); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_format_empty_values() |
|||
{ |
|||
var values = new ExposedValues(); |
|||
|
|||
Assert.Empty(values.ToString()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_format_from_single_value() |
|||
{ |
|||
var values = new ExposedValues |
|||
{ |
|||
["name1"] = "value1" |
|||
}; |
|||
|
|||
Assert.Equal("name1: value1", values.ToString()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_format_from_multiple_values() |
|||
{ |
|||
var values = new ExposedValues |
|||
{ |
|||
["name1"] = "value1", |
|||
["name2"] = "value2" |
|||
}; |
|||
|
|||
Assert.Equal("name1: value1, name2: value2", values.ToString()); |
|||
} |
|||
|
|||
private static void SetupConfiguration(IConfiguration configuration, string key, string value) |
|||
{ |
|||
var configSection = A.Fake<IConfigurationSection>(); |
|||
|
|||
A.CallTo(() => configSection.Value).Returns(value); |
|||
A.CallTo(() => configuration.GetSection(key)).Returns(configSection); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue