public void Test_Serialize_Implicitly() { /* Scenarios to check: * - explicit by default, no override * + implicit by default, override both types * - explicit by default, override outer type * + implicit by default, override inner type * - explicit by default, override inner type * + implicit by default, override outer type * - explicit by default, override both types * + implicit by default, no override */ IEnumerable <string> GetLines(bool outerIsExplicit, bool innerIsExplicit) { if (!outerIsExplicit) { yield return("- a = 3"); } yield return("- b = 4"); if (!outerIsExplicit) { yield return("- subClass :"); if (!innerIsExplicit) { yield return("-- a = 5"); } yield return("-- b = 6"); } } ImplicitConfig configToSerialize = new ImplicitConfig(3, 4, new ImplicitConfig.SubClass(5, 6)); void RunTest(bool outerIsExplicit, bool innerIsExplicit, ConfigOptions configOptions) { ConfigSerializer configDeserializer = new ConfigSerializer(configOptions); string[] resultLines = configDeserializer.Serialize(configToSerialize).ToArray(); string[] expectedLines = GetLines(outerIsExplicit, innerIsExplicit).ToArray(); bool areEqual = resultLines.Length == expectedLines.Length && !resultLines.Except(expectedLines).Any(); Assert.True(areEqual, "Expected lines: '{0}', Received Lines: '{1}'", string.Join("', '", expectedLines), string.Join("', '", resultLines)); } Type outerType = typeof(ImplicitConfig); Type innerType = typeof(ImplicitConfig.SubClass); RunTest(true, true, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Explicit }); RunTest(true, true, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Implicit, explicitTypes = new List <Type> { outerType, innerType } }); RunTest(false, true, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Explicit, implicitTypes = new List <Type> { outerType } }); RunTest(false, true, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Implicit, explicitTypes = new List <Type> { innerType } }); RunTest(true, false, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Explicit, implicitTypes = new List <Type> { innerType } }); RunTest(true, false, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Implicit, explicitTypes = new List <Type> { outerType } }); RunTest(false, false, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Explicit, implicitTypes = new List <Type> { outerType, innerType } }); RunTest(false, false, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Implicit }); }
public void Test_Deserialize_Implicitly() { /* Scenarios to check: * - explicit by default, no override * + implicit by default, override both types * - explicit by default, override outer type * + implicit by default, override inner type * - explicit by default, override inner type * + implicit by default, override outer type * - explicit by default, override both types * + implicit by default, no override */ IEnumerable <string> GetLines(bool outerIsExplicit, bool innerIsExplicit) { if (!outerIsExplicit) { yield return("- a = 3"); } yield return("- b = 4"); if (!outerIsExplicit) { yield return("- subClass :"); if (!innerIsExplicit) { yield return("-- a = 5"); } yield return("-- b = 6"); } } void RunTest(bool outerIsExplicit, bool innerIsExplicit, ConfigOptions configOptions) { ConfigDeserializer <ImplicitConfig> configDeserializer = new ConfigDeserializer <ImplicitConfig>( DeserializerUtils.Tokenize(new LineReader(GetLines(outerIsExplicit, innerIsExplicit))).ToList(), configOptions); ImplicitConfig result = configDeserializer.Deserialize(); if (outerIsExplicit) { Assert.AreEqual(0, result.a); Assert.AreEqual(4, result.c); Assert.AreEqual(null, result.subClass); } else { Assert.AreEqual(3, result.a); Assert.AreEqual(4, result.c); Assert.NotNull(result.subClass); Assert.AreEqual(innerIsExplicit ? 0 : 5, result.subClass.a); Assert.AreEqual(6, result.subClass.c); } } Type outerType = typeof(ImplicitConfig); Type innerType = typeof(ImplicitConfig.SubClass); RunTest(true, true, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Explicit, verifyAllKeysSet = true }); RunTest(true, true, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Implicit, explicitTypes = new List <Type> { outerType, innerType }, verifyAllKeysSet = true }); RunTest(false, true, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Explicit, implicitTypes = new List <Type> { outerType }, verifyAllKeysSet = true }); RunTest(false, true, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Implicit, explicitTypes = new List <Type> { innerType }, verifyAllKeysSet = true }); RunTest(true, false, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Explicit, implicitTypes = new List <Type> { innerType }, verifyAllKeysSet = true }); RunTest(true, false, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Implicit, explicitTypes = new List <Type> { outerType }, verifyAllKeysSet = true }); RunTest(false, false, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Explicit, implicitTypes = new List <Type> { outerType, innerType }, verifyAllKeysSet = true }); RunTest(false, false, new ConfigOptions { fieldSelectorOption = EFieldSelectorOption.Implicit, verifyAllKeysSet = true }); }