Skip to content

Commit

Permalink
Fixes #116
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Feb 8, 2019
1 parent f5da3f5 commit bddbfee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/SmartFormat.Tests/Core/SmartObjectsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using SmartFormat.Core.Settings;

namespace SmartFormat.Tests
{
Expand Down Expand Up @@ -58,5 +59,21 @@ public void Format_With_SmartObjects()
Assert.AreEqual(expected, result1);
Assert.AreEqual(expected, result2);
}

[Test]
public void Nested_Scope()
{
var clubOrMember = new { Member = new { Name = "Joe" }, Club = new { Name = "The Strikers" } };
var clubNoMember = new { Member = default(object), Club = new { Name = "The Strikers" } };
var say = new { Hello = "Good morning" };
var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.ParseErrorAction = formatter.Settings.FormatErrorAction = ErrorAction.ThrowError;

var result = formatter.Format("{Member:choose(null):{Club.Name}|{Name}} - {Hello}", new SmartObjects(new object[] { clubOrMember, say }));
Assert.AreEqual($"{clubOrMember.Member.Name} - {say.Hello}", result);

result = formatter.Format("{Member:choose(null):{Club.Name}|{Name}} - {Hello}", new SmartObjects(new object[] { clubNoMember, say }));
Assert.AreEqual($"{clubOrMember.Club.Name} - {say.Hello}", result);
}
}
}
4 changes: 2 additions & 2 deletions src/SmartFormat/SmartFormat.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>A string composition library written in C# that can format data into a string with a minimal, intuitive syntax. It uses extensions to provide named placeholders, pluralization, gender conjugation, and time and list formatting. Project hosted on GitHub https://github.com/axuno/SmartFormat.NET</Description>
<Copyright>Copyright 2011-2019 axuno gGmbH, Scott Rippey, Bernhard Millauer and other contributors.</Copyright>
<AssemblyTitle>SmartFormat</AssemblyTitle>
<Version>2.4.1.0</Version>
<Version>2.4.2.0</Version>
<Authors>axuno gGmbH, Scott Rippey, Bernhard Millauer and other contributors.</Authors>
<TargetFrameworks>netstandard1.6;netstandard2.0;net45</TargetFrameworks>
<DefineConstants>TRACE;DEBUG</DefineConstants>
Expand All @@ -22,7 +22,7 @@ https://github.com/axuno/SmartFormat.NET/blob/master/CHANGES.md</PackageReleaseN
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<RepositoryUrl>https://github.com/axuno/SmartFormat.NET</RepositoryUrl>
<AssemblyVersion>2.4.1.0</AssemblyVersion>
<AssemblyVersion>2.4.2.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
16 changes: 9 additions & 7 deletions src/SmartFormat/SmartFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public ErrorAction ErrorAction
/// </summary>
/// <param name="format">A composite format string.</param>
/// <param name="args">The object to format.</param>
/// <returns>Returns the formated input with items replaced with their string representation.</returns>
/// <returns>Returns the formatted input with items replaced with their string representation.</returns>
public string Format(string format, params object[] args)
{
return Format(null, format, args ?? new object[] {null});
Expand All @@ -156,7 +156,7 @@ public string Format(string format, params object[] args)
/// <param name="provider">The <see cref="IFormatProvider" /> to use.</param>
/// <param name="format">A composite format string.</param>
/// <param name="args">The object to format.</param>
/// <returns>Returns the formated input with items replaced with their string representation.</returns>
/// <returns>Returns the formatted input with items replaced with their string representation.</returns>
public string Format(IFormatProvider provider, string format, params object[] args)
{
args = args ?? new object[] {null};
Expand Down Expand Up @@ -222,8 +222,7 @@ public void Format(FormattingInfo formattingInfo)
CheckForExtensions();
foreach (var item in formattingInfo.Format.Items)
{
var literalItem = item as LiteralText;
if (literalItem != null)
if (item is LiteralText literalItem)
{
formattingInfo.Write(literalItem.ToString());
continue;
Expand Down Expand Up @@ -332,15 +331,18 @@ private bool InvokeSourceExtensions(FormattingInfo formattingInfo)
// if the current value is of type SmartObjects
// then try to find the right source extension for each of the objects in SmartObjects
// Note: SmartObjects cannot be nested, so this can be the case only once.
var smartObjects = formattingInfo.CurrentValue as SmartObjects;
if (smartObjects != null)
if (formattingInfo.CurrentValue is SmartObjects smartObjects)
{
var savedCurrentValue = formattingInfo.CurrentValue;
foreach (var obj in smartObjects)
{
formattingInfo.CurrentValue = obj;
var handled = sourceExtension.TryEvaluateSelector(formattingInfo);
if (handled) return true;
if (handled)
{
formattingInfo.CurrentValue = savedCurrentValue;
return true;
}
}

formattingInfo.CurrentValue = savedCurrentValue;
Expand Down

0 comments on commit bddbfee

Please sign in to comment.