Skip to content

Commit

Permalink
Smartsettings initialization in unit tests (#190)
Browse files Browse the repository at this point in the history
Assign all SmartSettings properties in unit tests when creating the instance
  • Loading branch information
axunonb committed Aug 27, 2021
1 parent dab5c1e commit 0d6f514
Show file tree
Hide file tree
Showing 33 changed files with 198 additions and 201 deletions.
8 changes: 5 additions & 3 deletions src/Demo.NetFramework/SmartFormatDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ private void SmartFormatDemo_Load(object sender, EventArgs e)
args.Xml = XElement.Parse(XmlSourceTest.TwoLevelXml);
propertyGrid1.SelectedObject = args;


Smart.Default.Settings.Formatter.ErrorAction = FormatErrorAction.OutputErrorInResult;
Smart.Default.Settings.Parser.ErrorAction = ParseErrorAction.ThrowError;
Smart.Default = Smart.CreateDefaultSmartFormat(new SmartSettings
{
Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.OutputErrorInResult},
Parser = new ParserSettings {ErrorAction = ParseErrorAction.ThrowError}
});

LoadExamples();
}
Expand Down
8 changes: 5 additions & 3 deletions src/Demo/SmartFormatDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ private void SmartFormatDemo_Load(object sender, EventArgs e)
args.Xml = XElement.Parse(XmlSourceTest.TwoLevelXml);
propertyGrid1.SelectedObject = args;


Smart.Default.Settings.Formatter.ErrorAction = FormatErrorAction.OutputErrorInResult;
Smart.Default.Settings.Parser.ErrorAction = ParseErrorAction.ThrowError;
Smart.Default = Smart.CreateDefaultSmartFormat(new SmartSettings
{
Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.OutputErrorInResult},
Parser = new ParserSettings {ErrorAction = ParseErrorAction.ThrowError}
});

LoadExamples();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Copyright>Copyright 2011-2021 axuno gGmbH, Scott Rippey, Bernhard Millauer and other contributors.</Copyright>
<RepositoryUrl>https://github.com/axuno/SmartFormat.git</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<AssemblyVersion>2.7.0</AssemblyVersion> <!--only update AssemblyVersion with major releases -->
<AssemblyVersion>3.0.0</AssemblyVersion> <!--only update AssemblyVersion with major releases -->
<LangVersion>latest</LangVersion>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
Expand Down
9 changes: 5 additions & 4 deletions src/SmartFormat.Tests/Core/AlignmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using NUnit.Framework;
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Settings;
using SmartFormat.Extensions;
using SmartFormat.Utilities;

Expand All @@ -14,9 +15,9 @@ namespace SmartFormat.Tests.Core
[TestFixture]
public class AlignmentTests
{
private SmartFormatter GetSimpleFormatter()
private SmartFormatter GetSimpleFormatter(SmartSettings? settings = null)
{
var formatter = new SmartFormatter();
var formatter = new SmartFormatter(settings ?? new SmartSettings());
formatter.AddExtensions(new DefaultFormatter());
formatter.AddExtensions(new ReflectionSource(), new DefaultSource());
return formatter;
Expand Down Expand Up @@ -45,8 +46,8 @@ public void Formatter_PostAlign_Custom_AlignmentChar()
{
const string name = "Joe";
var obj = new { name };
var smart = GetSimpleFormatter();
smart.Settings.Formatter.AlignmentFillCharacter = '.'; // dot instead of space
// fill with dot instead of space
var smart = GetSimpleFormatter(new SmartSettings{Formatter = new FormatterSettings {AlignmentFillCharacter = '.'}});
var result = smart.Format("Name: {name,-10}|", obj);
Assert.That(result, Is.EqualTo("Name: Joe.......|"));
}
Expand Down
5 changes: 2 additions & 3 deletions src/SmartFormat.Tests/Core/FormatterExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Formatters_AutoDetection_Should_Not_Throw()
{
foreach (var formatter in GetAllFormatters().Where(f => f.CanAutoDetect))
{
Assert.That(() => formatter.TryEvaluateFormat(FormattingInfo.Create("", new List<object>() {new object()})),
Assert.That(() => formatter.TryEvaluateFormat(FormattingInfo.Create("", new List<object> {new()})),
Throws.Nothing);
}
}
Expand Down Expand Up @@ -159,10 +159,9 @@ public void Implicit_formatters_require_an_isDefaultFormatter_flag(string format

private static SmartFormatter GetCustomFormatter()
{
var testFormatter = new SmartFormatter();
var testFormatter = new SmartFormatter(new SmartSettings {Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.ThrowError}});
testFormatter.AddExtensions(new TestExtension1(), new TestExtension2(), new DefaultFormatter());
testFormatter.AddExtensions(new DefaultSource());
testFormatter.Settings.Formatter.ErrorAction = FormatErrorAction.ThrowError;
return testFormatter;
}

Expand Down
47 changes: 18 additions & 29 deletions src/SmartFormat.Tests/Core/FormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,39 @@ public void Formatter_With_IList_Objects()
[Test]
public void Formatter_Throws_Exceptions()
{
var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Formatter.ErrorAction = FormatErrorAction.ThrowError;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings{Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.ThrowError}});

Assert.Throws<FormattingException>(() => formatter.Test("--{0}--", _errorArgs, "--ERROR!--ERROR!--"));
}

[Test]
public void Formatter_Outputs_Exceptions()
{
var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Formatter.ErrorAction = FormatErrorAction.OutputErrorInResult;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings{Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.OutputErrorInResult}});

formatter.Test("--{0}--{0:ZZZZ}--", _errorArgs, "--ERROR!--ERROR!--");
}

[Test]
public void Formatter_Ignores_Exceptions()
{
var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Formatter.ErrorAction = FormatErrorAction.Ignore;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings{Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.Ignore}});

formatter.Test("--{0}--{0:ZZZZ}--", _errorArgs, "------");
}

[Test]
public void Formatter_Maintains_Tokens()
{
var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Formatter.ErrorAction = FormatErrorAction.MaintainTokens;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings{Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.MaintainTokens}});

formatter.Test("--{0}--{0:ZZZZ}--", _errorArgs, "--{0}--{0:ZZZZ}--");
}

[Test]
public void Formatter_Maintains_Object_Tokens()
{
var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Formatter.ErrorAction = FormatErrorAction.MaintainTokens;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings {Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.MaintainTokens}});
formatter.Test("--{Object.Thing}--", _errorArgs, "--{Object.Thing}--");
}

Expand Down Expand Up @@ -144,28 +139,20 @@ public void Formatter_NotifyFormattingError()
var obj = new { Name = "some name" };
var badPlaceholder = new List<string>();

var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Formatter.ErrorAction = FormatErrorAction.Ignore;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings {Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.Ignore}});
formatter.OnFormattingFailure += (o, args) => badPlaceholder.Add(args.Placeholder);
var res = formatter.Format("{NoName} {Name} {OtherMissing}", obj);
Assert.That(badPlaceholder.Count == 2 && badPlaceholder[0] == "{NoName}" && badPlaceholder[1] == "{OtherMissing}");
}

[Test]
public void LeadingBackslashMustNotEscapeBraces()
[TestCase("\\{Test}", "\\Hello", false)]
[TestCase(@"\\{Test}",@"\Hello", true)]
public void LeadingBackslashMustNotEscapeBraces(string format, string expected, bool convertCharacterStringLiterals)
{
var smart = Smart.CreateDefaultSmartFormat();
smart.Settings.Parser.ConvertCharacterStringLiterals = false;
smart.Settings.StringFormatCompatibility = true;

var expected = "\\Hello";
var actual = smart.Format("\\{Test}", new { Test = "Hello" });
Assert.AreEqual(expected, actual);

smart.Settings.Parser.ConvertCharacterStringLiterals = true;
var smart = Smart.CreateDefaultSmartFormat(new SmartSettings
{StringFormatCompatibility = true, Parser = new ParserSettings {ConvertCharacterStringLiterals = convertCharacterStringLiterals}});

expected = @"\Hello";
actual = smart.Format(@"\\{Test}", new { Test = "Hello" }); // double backslash means escaping the backslash
var actual = smart.Format(format, new { Test = "Hello" });
Assert.AreEqual(expected, actual);
}

Expand All @@ -184,10 +171,12 @@ public void SmartFormatter_FormatDetails()
var args = new object[] {new Dictionary<string, string> {{"Greeting", "Hello"}} };
var format = "{Greeting}";
var output = new StringOutput();
var formatter = new SmartFormatter();
formatter.Settings.CaseSensitivity = CaseSensitivityType.CaseInsensitive;
formatter.Settings.Formatter.ErrorAction = FormatErrorAction.OutputErrorInResult;
formatter.Settings.Parser.ErrorAction = ParseErrorAction.OutputErrorInResult;
var formatter = new SmartFormatter(new SmartSettings
{
CaseSensitivity = CaseSensitivityType.CaseInsensitive,
Formatter = new FormatterSettings {ErrorAction = FormatErrorAction.OutputErrorInResult},
Parser = new ParserSettings {ErrorAction = ParseErrorAction.OutputErrorInResult}
});
var formatParsed = formatter.Parser.ParseFormat(format);
var formatDetails = new FormatDetails(formatter, formatParsed, args, null, output);

Expand Down
18 changes: 7 additions & 11 deletions src/SmartFormat.Tests/Core/LiteralTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using SmartFormat.Core.Settings;

namespace SmartFormat.Tests.Core
{
Expand All @@ -20,8 +21,7 @@ public void FormatCharacterLiteralsAsString()
const string formatWithFileBehavior = @"No carriage return\r, no line feed\n";
const string formatWithCodeBehavior = "With carriage return\r, and with line feed\n";

var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Parser.ConvertCharacterStringLiterals = false;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings {Parser = new ParserSettings {ConvertCharacterStringLiterals = false}});

var result = formatter.Format(formatWithFileBehavior);
Assert.AreEqual(string.Format(formatWithFileBehavior), result);
Expand All @@ -35,9 +35,8 @@ public void AllSupportedCharacterLiteralsAsUnicode()
{
const string formatWithFileBehavior = @"All supported literal characters: \' \"" \\ \a \b \f \n \r \t \v \0 \u2022!";
const string formatWithCodeBehavior = "All supported literal characters: \' \" \\ \a \b \f \n \r \t \v \0 \u2022!";

var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Parser.ConvertCharacterStringLiterals = true;

var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings {Parser = new ParserSettings {ConvertCharacterStringLiterals = true}});

var result = formatter.Format(formatWithFileBehavior);
Assert.AreEqual(formatWithCodeBehavior, result);
Expand All @@ -48,8 +47,7 @@ public void AllSupportedCharacterLiteralsAsUnicode()
[TestCase(@"\u2010 {0} \u2015", "\u2010 123 \u2015")]
public void UnicodeEscapeSequenceIsParsed(string format, string expectedOutput)
{
var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Parser.ConvertCharacterStringLiterals = true;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings {Parser = new ParserSettings {ConvertCharacterStringLiterals = true}});
Assert.AreEqual(expectedOutput, formatter.Format(format, 123));
}

Expand All @@ -60,17 +58,15 @@ public void UnicodeEscapeSequenceIsParsed(string format, string expectedOutput)
[TestCase(@"Some text \uuuuu {0}", @"\uuuuu")]
public void UnsupportedCharacterLiteralEscapeSequence(string format, string exMessage)
{
var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.Parser.ConvertCharacterStringLiterals = true;
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings {Parser = new ParserSettings {ConvertCharacterStringLiterals = true}});

Assert.That( () => formatter.Format(format, 123), Throws.ArgumentException.And.Message.Contains(exMessage));
}

[Test]
public void ConvertCharacterLiteralsToUnicodeWithListFormatter()
{
var smart = Smart.CreateDefaultSmartFormat();
smart.Settings.Parser.ConvertCharacterStringLiterals = true;
var smart = Smart.CreateDefaultSmartFormat(new SmartSettings {Parser = new ParserSettings {ConvertCharacterStringLiterals = true}});
// a useful practical test case: separate members of a list by a new line
var items = new[] { "one", "two", "three" };
// Note the @ before the format string will switch off conversion of \n by the compiler
Expand Down
Loading

0 comments on commit 0d6f514

Please sign in to comment.