public void ResolvableToStringNullArguments() { string[] codes = new string[] {"code1", "code2"}; string defaultMessage = "defaultMessage"; DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(codes, null, defaultMessage); Assert.AreEqual(getResolvableStringNull(), dmr.ToString(), "to string"); }
public void InstantiationWithASingleCodeDefaultsToEmptyDefaultMessage() { DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable("foo"); Assert.AreEqual(string.Empty, dmr.DefaultMessage, "Not defaulting to non null empty string value (it must)."); }
public void GetMessageWithNoParentMessageSourceAndNullDefaultMessageSourceResolvableAndCulture() { IMessageSourceResolvable resolver = new DefaultMessageSourceResolvable( new string[] { "foo" }, new object[] {}, string.Empty); DelegatingMessageSource source = new DelegatingMessageSource(); source.GetMessage(resolver, CultureInfo.InvariantCulture); }
public void ResolvableToString() { string[] codes = new string[] {"code1", "code2"}; object[] arguments = new object[] {"argument1", "argument2"}; string defaultMessage = "defaultMessage"; DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(codes, arguments, defaultMessage); Assert.AreEqual(getResolvableString(), dmr.ToString(), "to string"); }
public void GetMessageWithNoParentMessageSourceAndNullDefaultMessageSourceResolvableAndCulture() { IMessageSourceResolvable resolver = new DefaultMessageSourceResolvable( new string[] { "foo" }, new object[] {}, string.Empty); DelegatingMessageSource source = new DelegatingMessageSource(); Assert.Throws <NoSuchMessageException>(() => source.GetMessage(resolver, CultureInfo.InvariantCulture)); }
public void MessageResolveableGetMessage() { string[] codes = { "field.firstname" }; DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(codes, null); messageSource.ResourceManagers = resourceManagerList; Assert.AreEqual(messageSource.GetMessage("error.required", CultureInfo.CurrentCulture, dmr, "dude!"), "First name is required dude!", "message not as expected"); }
public void ResolvableToStringNullArguments() { string[] codes = new string[] { "code1", "code2" }; string defaultMessage = "defaultMessage"; DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(codes, null, defaultMessage); Assert.AreEqual(getResolvableStringNull(), dmr.ToString(), "to string"); }
public void ResolvableToString() { string[] codes = new string[] { "code1", "code2" }; object[] arguments = new object[] { "argument1", "argument2" }; string defaultMessage = "defaultMessage"; DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(codes, arguments, defaultMessage); Assert.AreEqual(getResolvableString(), dmr.ToString(), "to string"); }
public void DefaultResolvableFromExistingResolvable() { IMessageSourceResolvable res = (IMessageSourceResolvable)mocks.CreateMock(typeof(IMessageSourceResolvable)); Expect.Call(res.DefaultMessage).Return("defaultMessageFromMock").Repeat.AtLeastOnce(); Expect.Call(res.GetCodes()).Return(new string[] { "code1FromMock" }); Expect.Call(res.GetArguments()).Return(new object[] { "ArgumentFromMock" }).Repeat.AtLeastOnce(); mocks.ReplayAll(); DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(res); Assert.AreEqual("defaultMessageFromMock", dmr.DefaultMessage, "default"); Assert.AreEqual("code1FromMock", dmr.LastCode, "codes"); Assert.AreEqual("ArgumentFromMock", (dmr.GetArguments())[0], "arguments"); mocks.VerifyAll(); }
public void DefaultResolvableFromExistingResolvable() { IMessageSourceResolvable res = A.Fake <IMessageSourceResolvable>(); A.CallTo(() => res.DefaultMessage).Returns("defaultMessageFromMock"); A.CallTo(() => res.GetCodes()).Returns(new string[] { "code1FromMock" }); A.CallTo(() => res.GetArguments()).Returns(new object[] { "ArgumentFromMock" }); DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(res); Assert.AreEqual("defaultMessageFromMock", dmr.DefaultMessage, "default"); Assert.AreEqual("code1FromMock", dmr.LastCode, "codes"); Assert.AreEqual("ArgumentFromMock", (dmr.GetArguments())[0], "arguments"); }
public void DefaultResolvableFromExistingResolvable() { IMessageSourceResolvable res = mocks.StrictMock <IMessageSourceResolvable>(); Expect.Call(res.DefaultMessage).Return("defaultMessageFromMock").Repeat.AtLeastOnce(); Expect.Call(res.GetCodes()).Return(new string[] { "code1FromMock" }); Expect.Call(res.GetArguments()).Return(new object[] { "ArgumentFromMock" }).Repeat.AtLeastOnce(); mocks.ReplayAll(); DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(res); Assert.AreEqual("defaultMessageFromMock", dmr.DefaultMessage, "default"); Assert.AreEqual("code1FromMock", dmr.LastCode, "codes"); Assert.AreEqual("ArgumentFromMock", (dmr.GetArguments())[0], "arguments"); mocks.VerifyAll(); }
public void DoTestMessageAccess(bool hasParentContext, bool useCodeAsDefaultMessage) { StaticApplicationContext ac = new StaticApplicationContext(); if (hasParentContext) { StaticApplicationContext parent = new StaticApplicationContext(); parent.Refresh(); ac.ParentContext = parent; } MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add("resourceManagers", resourceManagerList); if (useCodeAsDefaultMessage) { pvs.Add("UseCodeAsDefaultMessage", true); } ac.RegisterSingleton("messageSource", typeof(ResourceSetMessageSource), pvs); ac.Refresh(); // Localizaiton fallbacks GetMessageLocalizationFallbacks(ac); // MessageSourceAccessor functionality MessageSourceAccessor accessor = new MessageSourceAccessor(ac); Assert.AreEqual("message3", accessor.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null)); // IMessageSourceResolveable Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null)); IMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("code3"); Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" }); Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null)); resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" }); Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); object[] arguments = new object[] { "Hello", new DefaultMessageSourceResolvable(new string[] { "code1" }) }; Assert.AreEqual("Hello, message1", ac.GetMessage("hello", CultureInfo.CurrentUICulture, arguments)); // test default message without and with args Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, null)); Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, arguments)); /* not supported Assert.AreEqual("{0}, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, null)); */ Assert.AreEqual("Hello, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, arguments)); // test resolvable with default message, without and with args resolvable = new DefaultMessageSourceResolvable(null, null, "default"); Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); resolvable = new DefaultMessageSourceResolvable(null, arguments, "default"); Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); /* not supported resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default"); Assert.AreEqual("{0}, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); */ resolvable = new DefaultMessageSourceResolvable(null, arguments, "{0}, default"); Assert.AreEqual("Hello, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); // test message args Assert.AreEqual("Arg1, Arg2", ac.GetMessage("hello", CultureInfo.CurrentUICulture, new object[] { "Arg1", "Arg2" })); /* not supported Assert.AreEqual("{0}, {1}", ac.GetMessage("hello", CultureInfo.CurrentUICulture, null)); */ /* not supported Assert.AreEqual("Hello\nWorld", ac.GetMessage("escaped")); } else { Assert.AreEqual("Hello\\nWorld", ac.GetMessage("escaped")); } */ try { Assert.AreEqual("MyNonExistantMessage", ac.GetMessage("MyNonExistantMessage")); if (!useCodeAsDefaultMessage) { Assert.Fail("Should have thrown NoSuchMessagException"); } } catch (NoSuchMessageException e) { if (useCodeAsDefaultMessage) { Assert.Fail("Should have returned code as default message."); e.ToString(); } } }
public void NullLastCode() { DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(null, null); Assert.IsNull(dmr.LastCode); }
/// <summary> /// Retrieve various localized messages from the application context. /// </summary> /// <param name="ctx"></param> public static void DemoMessageLocalization(IApplicationContext ctx) { CultureInfo spanishCultureInfo = new CultureInfo("es"); write("----------------------------------"); write("Resolve message key 'HelloMessage'"); write("----------------------------------"); string msg = ctx.GetMessage(Keys.HELLO_MESSAGE, CultureInfo.CurrentCulture, "Mr.", "Anderson"); write("Current culture resolved message = " + msg); string esMsg = ctx.GetMessage(Keys.HELLO_MESSAGE, spanishCultureInfo, "Mr.", "Anderson"); write("Spanish culture resolved message = " + esMsg); write("--------------------------------------------------------------------------"); write("Now using message argument that itself implements IMessageSourceResolvable"); write("--------------------------------------------------------------------------"); string[] codes = {Keys.FEMALE_GREETING}; DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(codes, null); msg = ctx.GetMessage(Keys.HELLO_MESSAGE, CultureInfo.CurrentCulture, dmr, "Anderson"); write("Current culture resolved message = " + msg); esMsg = ctx.GetMessage(Keys.HELLO_MESSAGE, spanishCultureInfo, dmr, "Anderson"); write("Spanish culture resolved message = " + esMsg); Person p = new Person(); write("------------------------"); write("Appling Person resources"); write("------------------------"); ctx.ApplyResources(p, "person", CultureInfo.CurrentUICulture); write(p.ToString()); }
public void LastCode() { DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(new string[] {"code1"}, null); Assert.AreEqual("code1", dmr.LastCode); }
public void LastCode() { DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(new string[] { "code1" }, null); Assert.AreEqual("code1", dmr.LastCode); }
public void GetMessageWithNoParentMessageSourceAndNullDefaultMessageSourceResolvableWithNoCodesAndCulture() { IMessageSourceResolvable resolver = new DefaultMessageSourceResolvable( new string[] {}, new object[] {}, string.Empty); DelegatingMessageSource source = new DelegatingMessageSource(); source.GetMessage(resolver, CultureInfo.InvariantCulture); }
public void DoTestMessageAccess(bool hasParentContext, bool useCodeAsDefaultMessage) { StaticApplicationContext ac = new StaticApplicationContext(); if (hasParentContext) { StaticApplicationContext parent = new StaticApplicationContext(); parent.Refresh(); ac.ParentContext = parent; } MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add("resourceManagers", resourceManagerList); if (useCodeAsDefaultMessage) { pvs.Add("UseCodeAsDefaultMessage", true); } ac.RegisterSingleton("messageSource", typeof(ResourceSetMessageSource), pvs); ac.Refresh(); // Localizaiton fallbacks GetMessageLocalizationFallbacks(ac); // MessageSourceAccessor functionality MessageSourceAccessor accessor = new MessageSourceAccessor(ac); Assert.AreEqual("message3", accessor.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null)); // IMessageSourceResolveable Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null)); IMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("code3"); Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" }); Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null)); resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" }); Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); object[] arguments = new object[] { "Hello", new DefaultMessageSourceResolvable(new string[] { "code1" }) }; Assert.AreEqual("Hello, message1", ac.GetMessage("hello", CultureInfo.CurrentUICulture, arguments)); // test default message without and with args Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, null)); Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, arguments)); /* not supported * Assert.AreEqual("{0}, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, null)); */ Assert.AreEqual("Hello, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, arguments)); // test resolvable with default message, without and with args resolvable = new DefaultMessageSourceResolvable(null, null, "default"); Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); resolvable = new DefaultMessageSourceResolvable(null, arguments, "default"); Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); /* not supported * resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default"); * Assert.AreEqual("{0}, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); */ resolvable = new DefaultMessageSourceResolvable(null, arguments, "{0}, default"); Assert.AreEqual("Hello, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture)); // test message args Assert.AreEqual("Arg1, Arg2", ac.GetMessage("hello", CultureInfo.CurrentUICulture, new object[] { "Arg1", "Arg2" })); /* not supported * Assert.AreEqual("{0}, {1}", ac.GetMessage("hello", CultureInfo.CurrentUICulture, null)); */ /* not supported * Assert.AreEqual("Hello\nWorld", ac.GetMessage("escaped")); * } * else * { * Assert.AreEqual("Hello\\nWorld", ac.GetMessage("escaped")); * } */ try { Assert.AreEqual("MyNonExistantMessage", ac.GetMessage("MyNonExistantMessage")); if (!useCodeAsDefaultMessage) { Assert.Fail("Should have thrown NoSuchMessagException"); } } catch (NoSuchMessageException e) { if (useCodeAsDefaultMessage) { Assert.Fail("Should have returned code as default message."); e.ToString(); } } }
public void GetMessageWithNoParentMessageSourceAndNullDefaultMessageSourceResolvableAndCulture() { IMessageSourceResolvable resolver = new DefaultMessageSourceResolvable( new string[] {"foo"}, new object[] {}, string.Empty); DelegatingMessageSource source = new DelegatingMessageSource(); Assert.Throws<NoSuchMessageException>(() => source.GetMessage(resolver, CultureInfo.InvariantCulture)); }