NamedFormatTests.cs :  » Bloggers » SubText » UnitTests » Subtext » Framework » Text » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » UnitTests » Subtext » Framework » Text » NamedFormatTests.cs
using System;
using MbUnit.Framework;
using Subtext.Framework.Text;

namespace UnitTests.Subtext.Framework.Text{
    [TestFixture]
    public class NamedFormatTests
    {
        [Test]
        [MultipleCulture("en-US,en-NZ,it-IT")]
        public void StringFormat_WithMultipleExpressions_FormatsThemAll()
        {
            //arrange
            var o = new {foo = 123.45, bar = 42, baz = "hello"};

            //act
            string result = "{foo} {foo} {bar}{baz}".NamedFormat(o);

            //assert
            float expectedNum = 123.45f;
            string expected = String.Format("{0} {1} 42hello", expectedNum, expectedNum);
            Assert.AreEqual(expected, result);
        }

        [Test]
        public void StringFormat_WithDoubleEscapedCurlyBraces_DoesNotFormatString()
        {
            //arrange
            var o = new {foo = 123.45};

            //act
            string result = "{{{{foo}}}}".NamedFormat(o);

            //assert
            Assert.AreEqual("{{foo}}", result);
        }

        [Test]
        [MultipleCulture("en-US,en-NZ,it-IT")]
        public void StringFormat_WithFormatSurroundedByDoubleEscapedBraces_FormatsString()
        {
            //arrange
            var o = new {foo = 123.45};

            //act
            string result = "{{{{{foo}}}}}".NamedFormat(o);

            //assert
            float expected = 123.45f;
            Assert.AreEqual("{{" + expected + "}}", result);
        }

        [Test]
        [MultipleCulture("en-US,en-NZ,it-IT")]
        public void Format_WithEscapeSequence_EscapesInnerCurlyBraces()
        {
            var o = new {foo = 123.45};

            //act
            string result = "{{{foo}}}".NamedFormat(o);

            //assert
            float expected = 123.45f;
            Assert.AreEqual("{" + expected + "}", result);
        }

        [Test]
        public void Format_WithEmptyString_ReturnsEmptyString()
        {
            var o = new {foo = 123.45};

            //act
            string result = string.Empty.NamedFormat(o);

            //assert
            Assert.AreEqual(string.Empty, result);
        }

        [Test]
        public void Format_WithNoFormats_ReturnsFormatStringAsIs()
        {
            var o = new {foo = 123.45};

            //act
            string result = "a b c".NamedFormat(o);

            //assert
            Assert.AreEqual("a b c", result);
        }

        [Test]
        [MultipleCulture("en-US,en-NZ,it-IT")]
        public void Format_WithFormatType_ReturnsFormattedExpression()
        {
            var o = new {foo = 123.45};

            //act
            string result = "{foo:#.#}".NamedFormat(o);

            //assert
            float expected = 123.5f;
            Assert.AreEqual(expected.ToString(), result);
        }

        [Test]
        [MultipleCulture("en-US,en-NZ,it-IT")]
        public void Format_WithSubProperty_ReturnsValueOfSubProperty()
        {
            var o = new {foo = new {bar = 123.45}};

            //act
            string result = "{foo.bar:#.#}ms".NamedFormat(o);

            //assert
            float expected = 123.5f;
            Assert.AreEqual(expected + "ms", result);
        }

        [Test]
        public void Format_WithFormatNameNotInObject_ThrowsFormatException()
        {
            //arrange
            var o = new {foo = 123.45};

            //act, assert
            UnitTestHelper.AssertThrows<FormatException>(() => "{bar}".NamedFormat(o));
        }

        [Test]
        public void Format_WithNoEndFormatBrace_ThrowsFormatException()
        {
            //arrange
            var o = new {foo = 123.45};

            //act, assert
            UnitTestHelper.AssertThrows<FormatException>(() => "{bar".NamedFormat(o));
        }

        [Test]
        public void Format_WithEscapedEndFormatBrace_ThrowsFormatException()
        {
            //arrange
            var o = new {foo = 123.45};


            //act, assert
            UnitTestHelper.AssertThrows<FormatException>(() => "{foo}}".NamedFormat(o));
        }

        [Test]
        public void Format_WithDoubleEscapedEndFormatBrace_ThrowsFormatException()
        {
            //arrange
            var o = new {foo = 123.45};

            //act, assert
            UnitTestHelper.AssertThrows<FormatException>(() => "{foo}}}}bar".NamedFormat(o));
        }

        [Test]
        public void Format_WithDoubleEscapedEndFormatBraceWhichTerminatesString_ThrowsFormatException()
        {
            //arrange
            var o = new {foo = 123.45};

            //act, assert
            UnitTestHelper.AssertThrows<FormatException>(() => "{foo}}}}".NamedFormat(o));
        }

        [Test]
        [MultipleCulture("en-US,en-NZ,it-IT")]
        public void Format_WithEndBraceFollowedByEscapedEndFormatBraceWhichTerminatesString_FormatsCorrectly()
        {
            var o = new {foo = 123.45};

            //act
            string result = "{foo}}}".NamedFormat(o);

            //assert
            float expected = 123.45f;
            Assert.AreEqual(expected + "}", result);
        }

        [Test]
        [MultipleCulture("en-US,en-NZ,it-IT")]
        public void Format_WithEndBraceFollowedByEscapedEndFormatBrace_FormatsCorrectly()
        {
            var o = new {foo = 123.45};

            //act
            string result = "{foo}}}bar".NamedFormat(o);

            //assert
            float expected = 123.45f;
            Assert.AreEqual(expected + "}bar", result);
        }

        [Test]
        [MultipleCulture("en-US,en-NZ,it-IT")]
        public void Format_WithEndBraceFollowedByDoubleEscapedEndFormatBrace_FormatsCorrectly()
        {
            var o = new {foo = 123.45};

            //act
            string result = "{foo}}}}}bar".NamedFormat(o);

            //assert
            float expected = 123.45f;
            Assert.AreEqual(expected + "}}bar", result);
        }

        [Test]
        public void Format_WithNullFormatString_ThrowsArgumentNullException()
        {
            //arrange, act, assert
            UnitTestHelper.AssertThrowsArgumentNullException(() => ((string)null).NamedFormat(123));
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.