diff --git a/src/SmartFormat.Tests/Core/SmartObjectsTests.cs b/src/SmartFormat.Tests/Core/SmartObjectsTests.cs deleted file mode 100644 index ced8cc57..00000000 --- a/src/SmartFormat.Tests/Core/SmartObjectsTests.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.Collections.Generic; -using NUnit.Framework; -using SmartFormat.Core.Extensions; -using SmartFormat.Core.Formatting; -using SmartFormat.Core.Parsing; -using SmartFormat.Core.Settings; -using SmartFormat.Extensions; - -namespace SmartFormat.Tests -{ - [TestFixture] - public class SmartObjectsTests - { - [Test] - public void No_SmartObjects_Nesting() - { - Assert.Throws(() => - { - var so = new SmartObjects(new[] {new SmartObjects()}); - }); - - Assert.Throws(() => - { - var so = new SmartObjects {new SmartObjects()}; - }); - - Assert.Throws(() => - { - var so = new SmartObjects(); - so.AddRange(new[] {new object(), new SmartObjects() }); - }); - } - - [Test] - public void SmartObjects_Add_Regular() - { - Assert.DoesNotThrow(() => - { - new SmartObjects().Add(new SmartSettings()); - }); - } - - [Test] - public void SmartObjects_AddRange_Regular() - { - Assert.DoesNotThrow(() => - { - new SmartObjects().AddRange(new []{ new SmartSettings(), new SmartSettings() } ); - }); - } - - [Test] - public void SmartObjects_Add_Null() - { - Assert.Throws(() => - { - new SmartObjects().Add(default!); - }); - } - - [Test] - public void SmartObjects_AddRange_Null() - { - Assert.Throws(() => - { - new SmartObjects().AddRange(default!); - }); - } - - - [Test] - public void Format_With_SmartObjects() - { - // With SmartObjects - // * all objects used for Smart.Format can be collected in one place as the first argument - // * the format string can be written like each object would be the first argument of Smart.Format - // * there is no need to bother from which argument a value should come from - - var addr = new Extensions.DictionaryFormatterTests.Address(); - var dict1 = new Dictionary { {"dict1key", "dict1 Value"} }; - var dict2 = new Dictionary { { "dict2key", "dict2 Value" } }; - - // format for SmartObjects as argument 1 to Smart.Format - const string format1 = "Name: {Person.FirstName} {Person.LastName}\n" + - "Dictionaries: {dict1key}, {dict2key}"; - - // format for SmartObjects as argument 2 to Smart.Format: - // works although unnecessary and leading to argument references in the format string - const string format2 = "Name: {1.Person.FirstName} {1.Person.LastName}\n" + - "Dictionaries: {1.dict1key}, {1.dict2key}"; - - var expected = $"Name: {addr.Person.FirstName} {addr.Person.LastName}\n" + - $"Dictionaries: {dict1["dict1key"]}, {dict2["dict2key"]}"; - - var formatter = Smart.CreateDefaultSmartFormat(); - var result1 = formatter.Format(format1, new SmartObjects(new object[] { addr, dict1, dict2 })); - var result2 = formatter.Format(format2, default!, new SmartObjects(new object[] { addr, dict1, dict2 })); - - 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); - } - - [Test] - public void Not_Invoked_With_FormattingInfo() - { - Assert.IsFalse(new SmartObjectsSource(new SmartFormatter()).TryEvaluateSelector(new SelectorInfo())); - } - - private class SelectorInfo : ISelectorInfo - { - public object? CurrentValue { get; } - public string? SelectorText { get; } - public int SelectorIndex { get; } - public string? SelectorOperator { get; } - public object? Result { get; set; } - public Placeholder? Placeholder { get; } - public FormatDetails FormatDetails { get; } = (FormatDetails) null!; // dummy - } - } -} diff --git a/src/SmartFormat.Tests/Core/ValueTupleTests.cs b/src/SmartFormat.Tests/Core/ValueTupleTests.cs index 97161e0f..2fa11f22 100644 --- a/src/SmartFormat.Tests/Core/ValueTupleTests.cs +++ b/src/SmartFormat.Tests/Core/ValueTupleTests.cs @@ -52,10 +52,10 @@ public void Nested_Scope() 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 })); + var result = formatter.Format("{Member:choose(null):{Club.Name}|{Name}} - {Hello}", (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 })); + result = formatter.Format("{Member:choose(null):{Club.Name}|{Name}} - {Hello}", (clubNoMember, say)); Assert.AreEqual($"{clubOrMember.Club.Name} - {say.Hello}", result); } diff --git a/src/SmartFormat/Extensions/SmartObjectsSource.cs b/src/SmartFormat/Extensions/SmartObjectsSource.cs deleted file mode 100644 index b2bf2612..00000000 --- a/src/SmartFormat/Extensions/SmartObjectsSource.cs +++ /dev/null @@ -1,61 +0,0 @@ -// -// Copyright (C) axuno gGmbH, Scott Rippey, Bernhard Millauer and other contributors. -// Licensed under the MIT license. -// - -using System; -using SmartFormat.Core.Extensions; -using SmartFormat.Core.Formatting; - -namespace SmartFormat.Extensions -{ - /// - /// processes as arguments to the formatter. - /// - [Obsolete("Depreciated in favor of ValueTupleSource", false)] - public class SmartObjectsSource : ISource - { - private readonly SmartFormatter _formatter; - - /// - /// Creates a new . - /// - /// - [Obsolete("Depreciated in favor of ValueTupleSource", false)] - public SmartObjectsSource(SmartFormatter formatter) - { - _formatter = formatter; - } - - /// - /// Checks, if the current value of the is of type - /// - /// - /// Returns true, if the current value of the is of type . - [Obsolete("Depreciated in favor of ValueTupleSource", false)] - public bool TryEvaluateSelector(ISelectorInfo selectorInfo) - { - if (!(selectorInfo is FormattingInfo formattingInfo)) return false; - if (!(formattingInfo.CurrentValue is SmartObjects smartObjects)) return false; - - var savedCurrentValue = formattingInfo.CurrentValue; - foreach (var obj in smartObjects) - { - foreach (var sourceExtension in _formatter.SourceExtensions) - { - formattingInfo.CurrentValue = obj; - var handled = sourceExtension.TryEvaluateSelector(formattingInfo); - if (handled) - { - formattingInfo.CurrentValue = savedCurrentValue; - return true; - } - } - } - - formattingInfo.CurrentValue = savedCurrentValue; - - return false; - } - } -} \ No newline at end of file diff --git a/src/SmartFormat/Smart.cs b/src/SmartFormat/Smart.cs index 4192e61b..89c1c7db 100644 --- a/src/SmartFormat/Smart.cs +++ b/src/SmartFormat/Smart.cs @@ -67,7 +67,6 @@ public static SmartFormatter CreateDefaultSmartFormat() (ISource) listFormatter, // ListFormatter MUST be first new DictionarySource(formatter), new ValueTupleSource(formatter), - new SmartObjectsSource(formatter), new JsonSource(formatter), new XmlSource(formatter), new ReflectionSource(formatter), diff --git a/src/SmartFormat/SmartObjects.cs b/src/SmartFormat/SmartObjects.cs deleted file mode 100644 index dbb5ca64..00000000 --- a/src/SmartFormat/SmartObjects.cs +++ /dev/null @@ -1,82 +0,0 @@ -// -// Copyright (C) axuno gGmbH, Scott Rippey, Bernhard Millauer and other contributors. -// Licensed under the MIT license. -// - -using System; -using System.Collections.Generic; -using System.Linq; - -namespace SmartFormat -{ - /// - /// Represents a list of objects to be used as a value argument to Smart.Format/>. - /// With SmartObjects - /// * all objects used for Smart.Format can be collected in one place as the first argument - /// * the format string can be written like each object would be the first argument of Smart.Format - /// * there is no need to bother from which argument a value should come from - /// - /// - /// In case more than one object has the same member (or key) name, the value of the first object in the list will - /// prevail. - /// Change the order of objects in the list to change the object priority. - /// - /// - /// var d1 = new Dictionary<string,string> { {"myKey", "myValue"} }; - /// var d2 = new Dictionary<string,string> { {"mySecondKey", "mySecondValue"} }; - /// var smartObj = new SmartObjects(); - /// smartObj.AddRange(new object[] {d1, d2}); - /// Smart.Format("{myKey} - {mySecondKey}", smartSrc); - /// result: "myValue - mySecondValue" - /// - [Obsolete("Depreciated in favor of ValueTuples", false)] - public class SmartObjects : List - { - /// - /// Initializes a new instance of the SmartObjects class. - /// - public SmartObjects() - { - } - - /// - /// Initializes a new instance of the SmartObjects - /// class that contains elements copied from the specified collection. - /// - /// The collection whose elements are copied to the new list. - public SmartObjects(IEnumerable objList) - { - AddRange(objList); - } - - /// - /// Adds an object to the end of list. - /// - /// Any object except types of SmartSource - public new void Add(object obj) - { - if (obj == null) throw new ArgumentNullException($"{nameof(obj)} must not be null.", nameof(obj)); - - if (obj is SmartObjects) - throw new ArgumentException($"Objects of type '{nameof(SmartObjects)}' cannot be nested.", nameof(obj)); - base.Add(obj); - } - - /// - /// Adds the elements of the specified collection to the end of the list. - /// - /// Any list of objects except objects of type SmartSource - public new void AddRange(IEnumerable objList) - { - if (objList == null) - throw new ArgumentNullException($"'{nameof(objList)}' must not be null.", nameof(objList)); - - var objects = objList.ToArray(); - if (objects.Any(o => o is SmartObjects)) - throw new ArgumentException( - $"Objects of type '{nameof(SmartObjects)}' cannot be nested. At least one object in the argument list has type '{nameof(SmartObjects)}'.", - nameof(objList)); - base.AddRange(objects); - } - } -} \ No newline at end of file