private static void FlagParsing(ISettingParser parser, TestClass source) { Assert.AreEqual(false, source.FlagValue); parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.FlagValue)}"); Assert.AreEqual(true, source.FlagValue); parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.FlagValue)} false"); Assert.AreEqual(false, source.FlagValue); }
private static void BasicParsing(ISettingParser parser, TestClass source) { parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.StringValue)} StringValueTest"); Assert.AreEqual("StringValueTest", source.StringValue); parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.IntValue)} 1"); Assert.AreEqual(1, source.IntValue); parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.BoolValue)} true"); Assert.AreEqual(true, source.BoolValue); parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.UlongValue)} 18446744073709551615"); Assert.AreEqual(ulong.MaxValue, source.UlongValue); parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.DateTimeValue)} 05/06/2018"); Assert.AreEqual(new DateTime(2018, 5, 6), source.DateTimeValue); }
private static void EmptyParsing(ISettingParser parser, TestClass source) { var expected = parser.GetNeededSettings(source).Count; parser.Parse(source, ""); Assert.AreEqual(expected, parser.GetNeededSettings(source).Count); }
private void ParseSettings() { foreach (Setting setting in _settings.Values) { if (!setting.IsProcessed) { throw new SettingNotProcessedException(setting); } if (setting.Value == null) { ISettingParser parser = _parserResolver.Resolve(setting.Type); try { setting.Value = parser.Parse(setting.Raw, setting.Type); } catch (Exception ex) { string formattedExceptionMessage = String.Format("Cannot parse settting with key:[{0}] and value:[{1}]. Please see inner expection for details.", setting.Key, setting.Value); throw new ConfigurationException(formattedExceptionMessage, ex); } ; } } }
public object Parse(string raw, Type settingType) { Type[] typeGenericArgs = settingType.GetGenericArguments(); IList list = Activator.CreateInstance(typeof(List <>).MakeGenericType(typeGenericArgs)) as IList; if (!raw.IsNullOrEmpty()) { string wrappedRaw = String.Format("<root>{0}</root>", raw); XDocument doc = XDocument.Parse(wrappedRaw); var items = from item in doc.Root.Descendants() select item.Value; ISettingParser elementParser = GetElementParser(settingType, typeGenericArgs[0]); foreach (string item in items) { list.Add(elementParser.Parse(item, typeGenericArgs[0])); } } return(CreateReadOnlyCollection(typeGenericArgs, list)); }
private static void ComplicatedParsing(ISettingParser parser, TestClass source) { var testStr = "Space \"Double deep quote\" Test"; var args = $"{parser.MainPrefix}{nameof(TestClass.StringValue)} \"{testStr}\" " + $"{parser.MainPrefix}{nameof(TestClass.FlagValue2)} " + $"{parser.MainPrefix}{nameof(TestClass.BoolValue)} true " + $"{parser.MainPrefix}{nameof(TestClass.StringValue2)} \"{testStr}\" " + $"{parser.MainPrefix}{nameof(TestClass.UlongValue)} asdf " + $"{parser.MainPrefix}help {nameof(TestClass.FlagValue2)} " + $"{parser.MainPrefix}{nameof(TestClass.DateTimeValue)} \"1/1/2016 12:00:00 PM\" " + $"extra"; var result = parser.Parse(source, args); Assert.AreEqual(5, result.Successes.Count); Assert.AreEqual(1, result.Errors.Count); Assert.AreEqual(1, result.UnusedParts.Count); Assert.AreEqual(1, result.Help.Count); Assert.AreEqual(testStr, source.StringValue); Assert.AreEqual(testStr, source.StringValue2); Assert.AreEqual(true, source.FlagValue2); Assert.AreEqual(true, source.BoolValue); Assert.AreEqual(default, source.UlongValue);