public void Formatter_Ignores_Exceptions() { var formatter = Smart.CreateDefaultSmartFormat(); formatter.ErrorAction = ErrorAction.Ignore; formatter.Test("--{0}--{0:ZZZZ}--", errorArgs, "------"); }
public void Formatter_Outputs_Exceptions() { var formatter = Smart.CreateDefaultSmartFormat(); formatter.ErrorAction = ErrorAction.OutputErrorInResult; formatter.Test("--{0}--{0:ZZZZ}--", errorArgs, "--ERROR!--ERROR!--"); }
private SmartFormatter GetCustomFormatter() { var testFormatter = Smart.CreateDefaultSmartFormat(); testFormatter.AddExtensions(new TestExtension1(), new TestExtension2(), new DefaultFormatter()); testFormatter.AddExtensions(new DefaultSource(testFormatter)); testFormatter.Settings.FormatErrorAction = ErrorAction.ThrowError; return(testFormatter); }
public void Test_Get_Property_From_Base_Class() { var derived = new DerivedMiscObject(); var formatter = Smart.CreateDefaultSmartFormat(); formatter.Settings.CaseSensitivity = CaseSensitivityType.CaseInsensitive; Assert.AreEqual(string.Format($"{derived.Property}"), formatter.Format("{Property}", derived)); }
public void Test_DateTimeOffset_Dates(string format, string expected) { // only the date part will be compared var args = new object[] { SystemTime.OffsetNow().AddDays(-1), SystemTime.OffsetNow(), SystemTime.OffsetNow().AddDays(1) }; var smart = Smart.CreateDefaultSmartFormat(); smart.Test(format, args, expected); }
public void Escaped_Curly_Braces_At_Begin_And_End_Should_Work() { var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { StringFormatCompatibility = true }); var result = formatter.Format("{{{0}}}", 99999); Assert.That(result, Is.EqualTo($"{{{99999}}}")); }
public IsMatchFormatterTests() { m_Formatter = Smart.CreateDefaultSmartFormat(); var ismatch = new IsMatchFormatter(); ismatch.RegexOptions = RegexOptions.CultureInvariant; m_Formatter.AddExtensions(ismatch); m_Formatter.Settings.FormatErrorAction = ErrorAction.ThrowError; }
public void Setup() { m_Formatter = Smart.CreateDefaultSmartFormat(); m_GlobalVariablesSource = new GlobalVariablesSource(m_Formatter); m_Formatter.AddExtensions(m_GlobalVariablesSource); m_NestedGroup1 = ScriptableObject.CreateInstance <GlobalVariablesGroup>(); m_NestedGroup2 = ScriptableObject.CreateInstance <GlobalVariablesGroup>(); m_Group1 = ScriptableObject.CreateInstance <GlobalVariablesGroup>(); m_GlobalVariablesSource.Add("global", m_Group1); m_Group1.Add("myInt", new IntGlobalVariable { Value = 123 }); m_Group1.Add("my-Int-var", new IntGlobalVariable()); m_Group1.Add("apple-count", new IntGlobalVariable { Value = 10 }); m_Group1.Add("myFloat", new FloatGlobalVariable { Value = 1.23f }); m_Group1.Add("some-float-value", new FloatGlobalVariable()); m_Group1.Add("time", new FloatGlobalVariable { Value = 1.234f }); m_Group1.Add("door-state", new BoolGlobalVariable { Value = true }); m_Group1.Add("max", new CharacterDetailsNoReflection()); m_Group2 = ScriptableObject.CreateInstance <GlobalVariablesGroup>(); m_GlobalVariablesSource.Add("npc", m_Group2); m_Group2.Add("emily", new CharacterDetails { FirstName = "Emily", Surname = "Kaldwin", Age = 20, Alive = true }); m_Group2.Add("guy", new CharacterDetails { FirstName = "Guy", Surname = "Threepwood", Alive = false }); // Nested groups m_Group1.Add("nested", new NestedGlobalVariablesGroup { Value = m_NestedGroup1 }); m_NestedGroup1.Add("further-nested", new NestedGlobalVariablesGroup { Value = m_NestedGroup2 }); m_NestedGroup1.Add("nested-float", new FloatGlobalVariable { Value = 0.12345f }); m_NestedGroup2.Add("my-nested-int", new IntGlobalVariable { Value = 1111 }); m_NestedGroup2.Add("my-nested-string", new StringGlobalVariable { Value = "I am nested deep" }); }
public void NamedPlaceholder_DecimalCurrencyArg() { var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { StringFormatCompatibility = false }); var pricePerOunce = 17.36m; var format = "The current price is {0:C2} per ounce."; Assert.That(formatter.Format(format, pricePerOunce), Is.EqualTo(string.Format(format, pricePerOunce))); }
/********* ** Protected methods *********/ /// <summary>Construct a formatter instance with the natural time plugins registered.</summary> /// <param name="errorAction">How to handle format errors.</param> public SmartFormatter GetFormatter(ErrorAction?errorAction = null) { SmartFormatter formatter = Smart.CreateDefaultSmartFormat().AddExtensionsForNaturalTime(); if (errorAction.HasValue) { formatter.ErrorAction = errorAction.Value; } return(formatter); }
public void Templates_are_case_sensitive(string format) { var smart = Smart.CreateDefaultSmartFormat(new SmartSettings { Formatter = new FormatterSettings { ErrorAction = FormatErrorAction.ThrowError } }); Assert.Throws <FormattingException>(() => smart.Format(format, 5)); }
public void Test_With_Changed_SplitChar(string format, int arg, string expected) { var smart = Smart.CreateDefaultSmartFormat(); // Set SplitChar from | to ~, so we can use | for the output string smart.GetFormatterExtension <ConditionalFormatter>() !.SplitChar = '~'; var result = smart.Format(format, arg); Assert.That(result, Is.EqualTo(expected)); }
public void Should_Process_Signed_And_Unsigned_Numbers() { var smart = Smart.CreateDefaultSmartFormat(); foreach (var number in new object[] { (long)123, (ulong)123, (short)123, (ushort)123, (int)123, (uint)123 }) { Assert.That(smart.Format("{0:cond:=123?yes|no}", number), Is.EqualTo("yes")); } }
public void Test_ComplexCondition(string format, string expected) { // ConditionalFormatter name "cond" must be included, because // otherwise PluralLocalizationFormatter would be invoked var args = new object[] { -5, 0, 0.5, 1.0, 1.5, 5.0, 11.0M, 14.0f, 18, 22, 45, 60, 101 }; var smart = Smart.CreateDefaultSmartFormat(); smart.Test(format, args, expected); }
public void Templates_can_be_case_insensitive_and_overwrite_each_other(string format, string expected) { this.smart = Smart.CreateDefaultSmartFormat(); this.smart.Settings.CaseSensitivity = CaseSensitivityType.CaseInsensitive; RegisterTemplates(this.smart); TestWithScottRippey(format, expected); // Reset: this.SetupSmart(); }
public void Simple_List() { var smart = Smart.CreateDefaultSmartFormat(); var items = new[] { "one", "two", "three" }; // Important: You cannot use "items" as an indexed parameter directly, // as it would be used as arg0="one", arg1="two", arg2="three" var result = smart.Format("{0:list:{}|, |, and }", (System.Collections.IList)items); Assert.AreEqual("one, two, and three", result); }
public void Choose_With_Changed_SplitChar() { var smart = Smart.CreateDefaultSmartFormat(); // Set SplitChar from | to ~, so we can use | for the output string smart.GetFormatterExtension <ChooseFormatter>() !.SplitChar = '~'; var result = smart.Format("{0:choose(1~2~3):|one|~|two|~|three|}", 2); Assert.That(result, Is.EqualTo("|two|")); }
public void Nesting_can_access_outer_scopes_no_blanks(string format, string expectedOutput) { // Removing the spaces from Nesting_can_access_outer_scopes requires alternative escaping of { and }! var sf = Smart.CreateDefaultSmartFormat(); sf.Parser.UseAlternativeEscapeChar('\\'); var actual = sf.Format(format, data); Assert.AreEqual(expectedOutput, actual); }
public void Choose_throws_when_choices_are_too_few_or_too_many(string format, object arg0) { var smart = Smart.CreateDefaultSmartFormat(new SmartSettings { Formatter = new FormatterSettings { ErrorAction = FormatErrorAction.ThrowError } }); Assert.Throws <FormattingException>(() => smart.Format(format, arg0)); }
public void UnsupportedCharacterLiteralEscapeSequence(string format, string exMessage) { var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { Parser = new ParserSettings { ConvertCharacterStringLiterals = true } }); Assert.That(() => formatter.Format(format, 123), Throws.ArgumentException.And.Message.Contains(exMessage)); }
public void UnicodeEscapeSequenceIsParsed(string format, string expectedOutput) { var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { Parser = new ParserSettings { ConvertCharacterStringLiterals = true } }); Assert.AreEqual(expectedOutput, formatter.Format(format, 123)); }
public void IndexPlaceholderDateTimeHHmmss() { var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { StringFormatCompatibility = true }); // columns in the time part must not be recognized as delimiters of a named placeholder var fmt = "It is now {0:yyyy/MM/dd HH:mm:ss}"; var now = DateTime.Now; Assert.That(formatter.Format(fmt, now), Is.EqualTo(string.Format(fmt, now))); }
public void Format_SingleLevelXml_TemplateWithCurlyBraces_Escaped() { var sf = Smart.CreateDefaultSmartFormat(); // arrange var xmlEl = XElement.Parse(OneLevelXml); // act var res = sf.Format("Mr. {{{LastName}}}", xmlEl); // assert Assert.AreEqual("Mr. {Doe}", res); }
public void Simple_List_Changed_SplitChar() { var smart = Smart.CreateDefaultSmartFormat(); // Set SplitChar from | to ~, so we can use | for the output string smart.GetFormatterExtension <ListFormatter>() !.SplitChar = '~'; var items = new[] { "one", "two", "three" }; var result = smart.Format("{0:list:{}~|~|}", (System.Collections.IList)items); Assert.AreEqual("one|two|three", result); }
public void Parser_Throws_Exceptions(string format) { // Let's set the "ErrorAction" to "Throw": var formatter = Smart.CreateDefaultSmartFormat(); formatter.Settings.ParseErrorAction = ErrorAction.ThrowError; var args = new object[] { TestFactory.GetPerson() }; Assert.Throws <ParsingErrors>(() => formatter.Test(format, args, "Error")); }
public void FormatDelegate_WithCulture() { var smart = Smart.CreateDefaultSmartFormat(); var amount = (decimal)123.456; var c = new CultureInfo("fr-FR"); // Only works for indexed placeholders var formatDelegate = new FormatDelegate((text, culture) => $"{text}: {amount.ToString(c)}"); Assert.That(smart.Format("{0:The amount is}", formatDelegate) , Is.EqualTo($"The amount is: {amount.ToString(c)}")); }
public void NamedPlaceholderDateTime() { var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { StringFormatCompatibility = true }); var now = new DateTime(2021, 12, 22, 14, 18, 12); var smartFmt = "It is now {Date:d} at {Date:t}"; var stringFmt = $"It is now {now.Date:d} at {now.Date:t}"; Assert.That(formatter.Format(smartFmt, now), Is.EqualTo(stringFmt)); }
public void EscapingDoubleBraces() { var args = new object[] { "Zero", "One", "Two", "Three" }; var format = "{0} {{0}} {{{0}}}"; var expected = "Zero {0} {Zero}"; var actual = Smart.CreateDefaultSmartFormat().Format(format, args); Assert.AreEqual(expected, actual); }
public void SmartFormat_With_Three_Arguments() { var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { StringFormatCompatibility = true }); var args = new Dictionary <string, object> { { "key1", "value1" }, { "key2", "value2" }, { "key3", "value3" } }; Assert.That(formatter.Format("{0} {1} {2}", args["key1"], args["key2"], args["key3"]), Is.EqualTo($"{args["key1"]} {args["key2"]} {args["key3"]}")); }
public void NamedPlaceholderAlignment() { var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { StringFormatCompatibility = true }); var yearAmount = new Tuple <long, long>(2017, 1025632); var smartFmt = "Year: {Item1,-6} Amount: {Item2,15:N0}"; var stringFmt = $"Year: {yearAmount.Item1,-6} Amount: {yearAmount.Item2,15:N0}"; Assert.That(formatter.Format(smartFmt, yearAmount), Is.EqualTo(stringFmt)); }