private static Directory BuildDirectory(IEnumerable<object> values) { var directory = new MockDirectory(); foreach (var pair in Enumerable.Range(1, int.MaxValue).Zip(values, Tuple.Create)) directory.Set(pair.Item1, pair.Item2); return directory; }
public void BooleanTests() { Action<Directory, int> assertPresentTrueBoolean = (dictionary, i) => { Assert.True(dictionary.GetBoolean(i)); bool value; Assert.True(dictionary.TryGetBoolean(i, out value)); Assert.True(dictionary.GetBoolean(i)); }; Action<Directory, int> assertPresentFalseBoolean = (dictionary, i) => { Assert.False(dictionary.GetBoolean(i)); bool value; Assert.True(dictionary.TryGetBoolean(i, out value)); Assert.False(dictionary.GetBoolean(i)); }; Action<Directory, int> assertMissingBoolean = (dictionary, i) => { bool value; Assert.False(dictionary.TryGetBoolean(i, out value)); Assert.Throws<MetadataException>(() => dictionary.GetBoolean(i)); }; // NOTE string is not convertible to boolean other than for "true" and "false" Test(BuildDirectory(_singleValues.Where(v => !(v is string))), assertPresentTrueBoolean, assertMissingBoolean); Test(BuildDirectory(_singleZeroValues.Where(v => !(v is string))), assertPresentFalseBoolean, assertMissingBoolean); Test(BuildDirectory(_arraysOfSingleValues.Where(v => !(v is string[]))), assertPresentTrueBoolean, assertMissingBoolean); Test(BuildDirectory(_arraysOfSingleZeroValues.Where(v => !(v is string[]))), assertPresentFalseBoolean, assertMissingBoolean); var directory = new MockDirectory(); directory.Set(1, "True"); directory.Set(2, "true"); directory.Set(3, "False"); directory.Set(4, "false"); Assert.True(directory.GetBoolean(1)); Assert.True(directory.GetBoolean(2)); Assert.False(directory.GetBoolean(3)); Assert.False(directory.GetBoolean(4)); }