public void NullValueProducesStringContainingNull()
        {
            var mockExtensionPoints = MockRepository.GenerateStub <IExtensionPoints>();
            var formatter           = new RuleBasedFormatter(mockExtensionPoints, EmptyArray <IFormattingRule> .Instance);

            Assert.AreEqual("null", formatter.Format(null));
        }
        public void Custom_formatting()
        {
            var extentionPoints = new DefaultExtensionPoints();
            var formatter       = new RuleBasedFormatter(extentionPoints, EmptyArray <IFormattingRule> .Instance);

            extentionPoints.CustomFormatters.Register <Foo>(x => String.Format("Foo's value is {0}.", x.Value));
            string output = formatter.Format(new Foo(123));

            Assert.AreEqual("Foo's value is 123.", output);
        }
        public void FormatterSubstitutesNameOfTypeIfRuleThrows()
        {
            var mockRule = MockRepository.GenerateMock <IFormattingRule>();

            mockRule.Expect(x => x.GetPriority(typeof(int))).Return(0);
            mockRule.Expect(x => x.Format(null, null)).IgnoreArguments().Throw(new ApplicationException("Boom!"));
            var formatter = new RuleBasedFormatter(new DefaultExtensionPoints(), new[] { mockRule });

            Assert.AreEqual("{System.Int32}", formatter.Format(42));
            mockRule.VerifyAllExpectations();
        }
        public void FormatterSubstitutesNameOfTypeIfRuleReturnsNullOrEmpty(bool useNull)
        {
            var mockRule = MockRepository.GenerateMock <IFormattingRule>();

            mockRule.Expect(x => x.GetPriority(typeof(int))).Return(0);
            mockRule.Expect(x => x.Format(null, null)).IgnoreArguments().Return(useNull ? null : String.Empty);
            var formatter = new RuleBasedFormatter(new DefaultExtensionPoints(), new[] { mockRule });

            Assert.AreEqual("{System.Int32}", formatter.Format(42));
            mockRule.VerifyAllExpectations();
        }
        public void FormatterChoosesTheBestRuleAndCachesLookups()
        {
            var mockRule1 = MockRepository.GenerateMock <IFormattingRule>();
            var mockRule2 = MockRepository.GenerateMock <IFormattingRule>();

            mockRule1.Expect(x => x.GetPriority(typeof(int))).Return(0);
            mockRule2.Expect(x => x.GetPriority(typeof(int))).Return(1);
            mockRule2.Expect(x => x.Format(null, null)).IgnoreArguments().Repeat.Once().Return("42");
            mockRule2.Expect(x => x.Format(null, null)).IgnoreArguments().Repeat.Once().Return("53");
            var formatter = new RuleBasedFormatter(new DefaultExtensionPoints(), new[] { mockRule1, mockRule2 });

            Assert.AreEqual("42", formatter.Format(42));
            Assert.AreEqual("53", formatter.Format(53));
            mockRule1.VerifyAllExpectations();
            mockRule2.VerifyAllExpectations();
        }