dotnet-installer/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Transforms/GivenAnAddPropertyTransform.cs

98 lines
3.4 KiB
C#
Raw Normal View History

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Construction;
2016-08-22 19:24:10 +00:00
using Microsoft.DotNet.ProjectJsonMigration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using FluentAssertions;
2016-08-23 20:50:05 +00:00
using Microsoft.DotNet.ProjectJsonMigration.Transforms;
2016-08-22 19:24:10 +00:00
2016-09-14 22:30:11 +00:00
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
2016-08-22 19:24:10 +00:00
{
public class GivenAnAddPropertyTransform
{
[Fact]
public void It_returns_a_property_with_specified_value()
{
var propertyName = "Property1";
var propertyValue = "Value1";
var propertyTransform = new AddPropertyTransform<string>(propertyName, propertyValue, t=>true);
var property = propertyTransform.Transform("_");
property.Name.Should().Be(propertyName);
property.Value.Should().Be(propertyValue);
}
[Fact]
public void It_returns_a_property_with_computed_value()
{
var propertyName = "Property1";
var propertyValue = "Value1";
var propertyTransform = new AddPropertyTransform<string>(propertyName, t => t.ToUpper(), t => true);
var property = propertyTransform.Transform(propertyValue);
property.Name.Should().Be(propertyName);
property.Value.Should().Be(propertyValue.ToUpper());
}
[Fact]
public void It_returns_null_when_condition_is_false()
{
var propertyName = "Property1";
var propertyValue = "Value1";
var propertyTransform = new AddPropertyTransform<string>(propertyName, propertyValue, t => false);
propertyTransform.Transform(propertyValue).Should().BeNull();
}
[Fact]
public void It_returns_a_property_when_source_is_null_and_propertyValue_is_a_string()
{
var propertyName = "Property1";
var propertyValue = "Value1";
var propertyTransform = new AddPropertyTransform<string>(
propertyName,
propertyValue,
2016-08-22 19:24:10 +00:00
t => true);
var property = propertyTransform.Transform(null);
property.Should().NotBeNull();
property.Value.Should().Be(propertyValue);
}
[Fact]
public void It_returns_a_property_when_source_is_null_and_propertyValue_is_a_Func_that_handles_null()
{
var propertyName = "Property1";
var propertyValue = "Value1";
var propertyTransform = new AddPropertyTransform<string>(
propertyName,
t=> t == null ? propertyValue.ToUpper() : propertyValue.ToLower(),
t => true);
var property = propertyTransform.Transform(null);
property.Value.Should().Be(propertyValue.ToUpper());
}
[Fact]
public void It_throws_when_source_is_null_and_propertyValue_is_a_Func_that_doesnt_handle_null()
{
var propertyName = "Property1";
var propertyTransform = new AddPropertyTransform<string>(
propertyName,
t => t.ToUpper(),
t => true);
Action transform = () => propertyTransform.Transform(null);
transform.ShouldThrow<Exception>();
}
}
}