public void Can_provide_test_resource_set() { TestResourceSet resourceSet = new TestResourceSet(); resourceSet.Add("foo.bar.baz","foo"); PlainTextResourceManager resourceManager = new PlainTextResourceManager(resourceSet); Assert.AreEqual("foo", resourceManager.GetString("foo.bar.baz", null, "baz")); Assert.AreEqual("baz", resourceManager.GetString("foo.bar", null, "baz")); }
public void Verify_that_default_resource_strings_have_correct_argument_count() { var resourceManager = new PlainTextResourceManager(Utils.Settings.DekiResourcesPath); var resourceMethods = from method in typeof(DekiResources).GetMethods(BindingFlags.Static | BindingFlags.Public) where method.ReturnType == typeof(DekiResource) select method; foreach (var method in resourceMethods) { var parameters = method.GetParameters(); var args = new object[parameters.Length]; for (var i = 0; i < parameters.Length; i++) { var type = parameters[i].ParameterType; if (type == typeof(string)) { args[i] = string.Empty; } else if (type.IsA <DekiResource>()) { // can't test resources that take other resources via this method } else if (type.IsA <MimeType>()) { args[i] = MimeType.TEXT_XML; } else if (type.IsA <SearchQuery>()) { args[i] = new SearchQuery("foo", "bar", new LuceneClauseBuilder(), null); } else if (type.IsA <XUri>()) { args[i] = new XUri("http://foo"); } else { try { args[i] = Activator.CreateInstance(parameters[i].ParameterType, false); } catch (Exception) { Assert.Fail(string.Format("{0}: cannot create argument instance of type '{1}'", method.Name, parameters[i].ParameterType)); } } } var resource = (DekiResource)method.Invoke(null, args); var format = resourceManager.GetString(resource.LocalizationKey, CultureInfo.InvariantCulture, null); Assert.IsNotNull(format, string.Format("{0}: No localization string exists for key '{1}'", method.Name, resource.LocalizationKey)); var paramSet = new HashSet <int>(); var matches = _paramsRegex.Matches(format); for (int i = 0; i < matches.Count; i++) { paramSet.Add(Convert.ToInt32(matches[i].Groups[1].Value)); } Assert.IsTrue(resource.Args.Length >= paramSet.Count, string.Format("{0}: too many parameters in string '{1}' ({2} < {3})", method.Name, format, resource.Args.Length, paramSet.Count)); if (paramSet.Count == 0) { continue; } Assert.AreEqual(paramSet.Count - 1, paramSet.OrderBy(x => x).Last(), string.Format("{0}: incorrect last parameter index '{1}'", method.Name, format)); } }
public void Gets_default_on_missing_key() { string path = Path.GetTempPath(); string resource = Path.Combine(path, "resources.custom.txt"); using(StreamWriter sw = File.CreateText(resource)) { sw.WriteLine("[Test.Section]"); sw.WriteLine(" foo=bar"); } PlainTextResourceManager resourceManager = new PlainTextResourceManager(path); Assert.AreEqual("bar", resourceManager.GetString("Test.Section.foo", null, "baz")); Assert.AreEqual("baz", resourceManager.GetString("Test.Section.blah", null, "baz")); }
public void Verify_that_default_resource_strings_have_correct_argument_count() { var resourceManager = new PlainTextResourceManager(Utils.Settings.DekiResourcesPath); var resourceMethods = from method in typeof(DekiResources).GetMethods(BindingFlags.Static | BindingFlags.Public) where method.ReturnType == typeof(DekiResource) select method; foreach(var method in resourceMethods) { var parameters = method.GetParameters(); var args = new object[parameters.Length]; for(var i = 0; i < parameters.Length; i++) { var type = parameters[i].ParameterType; if(type == typeof(string)) { args[i] = string.Empty; } else if(type.IsA<DekiResource>()) { // can't test resources that take other resources via this method } else if(type.IsA<MimeType>()) { args[i] = MimeType.TEXT_XML; } else if(type.IsA<SearchQuery>()) { args[i] = new SearchQuery("foo", "bar", new LuceneClauseBuilder(), null); } else if(type.IsA<XUri>()) { args[i] = new XUri("http://foo"); } else { try { args[i] = Activator.CreateInstance(parameters[i].ParameterType, false); } catch(Exception) { Assert.Fail(string.Format("{0}: cannot create argument instance of type '{1}'", method.Name, parameters[i].ParameterType)); } } } var resource = (DekiResource)method.Invoke(null, args); var format = resourceManager.GetString(resource.LocalizationKey, CultureInfo.InvariantCulture, null); Assert.IsNotNull(format, string.Format("{0}: No localization string exists for key '{1}'", method.Name, resource.LocalizationKey)); var paramSet = new HashSet<int>(); var matches = _paramsRegex.Matches(format); for(int i = 0; i < matches.Count; i++) { paramSet.Add(Convert.ToInt32(matches[i].Groups[1].Value)); } Assert.IsTrue(resource.Args.Length >= paramSet.Count, string.Format("{0}: too many parameters in string '{1}' ({2} < {3})", method.Name, format, resource.Args.Length, paramSet.Count)); if(paramSet.Count == 0) { continue; } Assert.AreEqual(paramSet.Count - 1, paramSet.OrderBy(x => x).Last(), string.Format("{0}: incorrect last parameter index '{1}'", method.Name, format)); } }