Example #1
0
 public static ObjectNode CreateAccessibilityNode(Options options, Mode mode)
 {
     var value = new SimpleValue(new MemberAccessibilityEnumeration(), 
         typeof(MemberAccessibilityEnumeration).ToCachedType());
     return new ObjectNode(new Context(options ?? Options.Create(), 
         mode, "xml"), null, value, null, null);
 }
Example #2
0
        public override void Encode(Stream stream, Encoding encoding = null)
        {
            var context = new Context(_options, Mode.Deserialize, NodeFormat);
            var header = new SimpleValue(_type.GenericEnumerableType);
            var columns = new ObjectNode(context, null, header, null, null)
                .Select(x => x.Name).ToArray();

            if (!columns.Any()) return;

            var writer = new StreamWriter(stream, encoding ?? UTF8Encoding.NoBOM);
            var qualifier = _options.CsvQualifier;

            Action<string[]> writeLine = x => writer
                .Write(x.Select(y =>
                    $"{qualifier}{y?.Replace(qualifier, qualifier + qualifier)}{qualifier}")
                .Aggregate((a, i) => $"{a}{_options.CsvDelimiter}{i}") + _options.CsvNewLine);

            writeLine(columns);

            foreach (var row in _rows)
            {
                var fields = new string[columns.Length];
                row.Where(x => columns.Contains(x.Name))
                    .ForEach(x => fields[columns.IndexOf(x.Name)] = x.Value?.ToString());
                writeLine(fields);
            }
                
            writer.Flush();
        }
Example #3
0
 public void should_get_inner_value()
 {
     var value = new SimpleValue("hai", typeof(string).ToCachedType());
     var node = new Node(value);
     node.Value.ShouldBeSameAs(value.Instance);
     node.SpecifiedType.Type.ShouldBe<String>();
     node.ActualType.Type.ShouldBe<String>();
 }
Example #4
0
        public void should_return_specified_type_as_actual_type_when_not_initialized()
        {
            var factory = Substitute.For<Func<IList>>();
            var value = new SimpleValue(typeof(IList).ToCachedType());
            var lazy = new LazyValue(value, factory);

            lazy.SpecifiedType.Type.ShouldBe<IList>();
            lazy.ActualType.Type.ShouldBe<IList>();
            factory.DidNotReceiveWithAnyArgs().Invoke();
        }
Example #5
0
        public void should_set_correct_defaults_in_type_overload()
        {
            var type = typeof(object);
            var value = new SimpleValue(type.ToCachedType());

            value.Instance.ShouldBeNull();
            value.SpecifiedType.Type.ShouldEqual(type);
            value.ActualType.Type.ShouldEqual(type);
            value.IsReadonly.ShouldBeFalse();
        }
Example #6
0
        public void should_set_correct_defaults_in_value_overload(bool @readonly)
        {
            var type = typeof(object);
            var @object = new Tuple<string>("yada");
            var value = new SimpleValue(@object, type.ToCachedType(), @readonly);

            value.Instance.ShouldEqual(@object);
            value.SpecifiedType.Type.ShouldEqual(type);
            value.ActualType.Type.ShouldBe<Tuple<string>>();
            value.IsReadonly.ShouldEqual(@readonly);
        }
Example #7
0
        public void should_create_lazy_value()
        {
            var type = typeof(int);
            var backingValue = new SimpleValue(type.ToCachedType());
            var value = ValueFactory.Create(backingValue, () => 5);

            value.Instance.ShouldEqual(5);
            value.SpecifiedType.Type.ShouldEqual(type);
            value.ActualType.Type.ShouldEqual(type);
            value.IsReadonly.ShouldBeFalse();
        }
Example #8
0
        public void should_get_actual_and_specified_type_when_initialized()
        {
            var factory = Substitute.For<Func<IList>>();
            factory.Invoke().Returns(new List<string>());
            var value = new SimpleValue(typeof(IList).ToCachedType());
            var lazy = new LazyValue(value, factory);

            var instance = lazy.Instance;

            lazy.SpecifiedType.Type.ShouldBe<IList>();
            lazy.ActualType.Type.ShouldBe<List<string>>();
            factory.Received(1);
        }
Example #9
0
        public Encoder(Stream output, Encoding encoding, 
            Options options, string format, CachedType type)
        {
            if (type.IsGenericEnumerable && options.Serialization
                .SerializationType == SerializationType.SpecifiedType)
            {
                var header = new SimpleValue(type.GenericEnumerableType);
                var context = new Context(options, Mode.Deserialize, format);
                _columns = new ObjectNode(context, null, header, null, null)
                    .Select(x => x.Name).ToArray();
            }

            _header = options.CsvHeader;
            _qualifier = options.CsvQualifier;
            _delimiter = options.CsvDelimiter;
            _newLine = options.CsvNewLine;
            _writer = new Lazy<StreamWriter>(() => new StreamWriter(output, encoding));
        }
Example #10
0
        public void should_deterministically_create_value()
        {
            var factory = Substitute.For<Func<string>>();
            factory.Invoke().Returns("hai");
            var value = new SimpleValue(typeof(string).ToCachedType());
            var lazy = new LazyValue(value, factory);

            value.Instance.ShouldBeNull();

            for (var i = 0; i < 5; i++)
            {
                lazy.EnsureValue();
                lazy.SpecifiedType.Type.ShouldBe<string>();
                lazy.ActualType.Type.ShouldBe<string>();
                value.Instance.ShouldEqual("hai");
            }

            factory.Received(1);
        }
Example #11
0
        public void should_set_and_get_property_value()
        {
            var model = new Model();
            var value = new SimpleValue(model, ModelType);
            var member = new MemberValue(value, new CachedMember(ModelType.Type.GetProperty("Property")), x => false);

            member.Instance.ShouldBeNull();
            member.SpecifiedType.Type.ShouldBe<string>();
            member.ActualType.Type.ShouldBe<string>();

            member.Instance = "oh";
            member.Instance.ShouldEqual("oh");
            member.ActualType.Type.ShouldBe<string>();
            member.SpecifiedType.Type.ShouldBe<string>();
            model.Property.ShouldEqual("oh");

            model.Property = "hai";
            member.Instance.ShouldEqual("hai");
            member.ActualType.Type.ShouldBe<string>();
            member.SpecifiedType.Type.ShouldBe<string>();
            model.Property.ShouldEqual("hai");
        }
Example #12
0
 public void should_set_null_on_reference_types(Type type)
 {
     var value = new SimpleValue(typeof(string).ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize), null, value, null, null).Value = null;
     value.Instance.ShouldBeNull();
 }
Example #13
0
 public void should_convert_simple_numeric_types_to_decimal(Type type, object value)
 {
     var result = new SimpleValue(typeof(decimal).ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize), null, result, null, null).Value = value;
     result.Instance.ShouldEqual(Convert.ChangeType(value, typeof(decimal)));
 }
Example #14
0
 public void should_convert_simple_types_to_string(Type type, object value)
 {
     var result = new SimpleValue(typeof(string).ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize), null, result, null, null).Value = value;
     result.Instance.ShouldEqual(value.ToString());
 }
Example #15
0
 public void should_set_decimal_enum_type()
 {
     var result = new SimpleValue(typeof(UriFormat).ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize), null, result, null, null).Value = 3.0;
     result.Instance.ShouldEqual(UriFormat.SafeUnescaped);
 }
Example #16
0
 public void should_return_object_if_it_implements_ilist()
 {
     var list = new List<string>();
     IValue value = new SimpleValue(list, typeof(List<string>).ToCachedType());
     ArrayAdapter.Create(value).ShouldBeSameAs(list);
 }
Example #17
0
 public void should_set_enum_value_case_insensitively_when_not_configured()
 {
     var result = new SimpleValue(typeof(UriFormat).ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize), null, result,
         null, null).Value = "safeunescaped";
     result.Instance.ShouldEqual(UriFormat.SafeUnescaped);
 }
Example #18
0
 public void should_get_enum_numeric_value()
 {
     var result = new SimpleValue(UriFormat.SafeUnescaped,
         typeof(UriFormat).ToCachedType());
     new ValueNode(CreateContext(Mode.Serialize, Options.Create(x =>
         x.Serialization(y => y.EnumValuesAsNumeric()))), null, result,
         null, null).Value.ShouldEqual(3);
 }
Example #19
0
 public void should_fail_to_set_enum_value_case_sensitively_when_configured()
 {
     var result = new SimpleValue(typeof(UriFormat).ToCachedType());
     Assert.Throws<ValueParseException> (() => new ValueNode(CreateContext(Mode.Deserialize,
         Options.Create(x => x.Deserialization(y => y.WithCaseSensitiveEnumValues()))),
             null, result, null, null).Value = "safeunescaped")
         .FriendlyMessage.ShouldEqual("Option 'safeunescaped' is not valid.");
 }
Example #20
0
        public void should_return_path_for_dictionary()
        {
            var instance = new SimpleValue(new Dictionary<string, object>
            {
                { "one", "hai" },
                { "two", new List<object>
                {
                    "hai",
                    new List<string>(),
                    new Dictionary<string, object>(),
                    new object()
                } },
                { "three", new Dictionary<string, object>
                {
                    { "one", "hai" },
                    { "two", new List<string>() },
                    { "three", new Dictionary<string, string>() },
                    { "four", new object() }
                } },
                { "four", new Object
                {
                    ValueNode = "hai",
                    EnumerableNode = new List<object>(),
                    DictionaryNode = new Dictionary<string, object>(),
                    ObjectNode = new Object()
                } }
            }, typeof(Dictionary<string, object>).ToCachedType());
            var node = Bender.Nodes.Object.NodeFactory.CreateSerializable("yada", instance,
                null, new Context(Options.Create(), Mode.Serialize, "yada"));

            const string baseName = "System.Collections.Generic.Dictionary<System.String, System.Object>";

            node.Path.ShouldEqual(baseName);

            var valueNode = node.First();
            valueNode.ShouldBeType<ValueNode>();
            valueNode.Path.ShouldEqual(baseName + "[\"one\"]");

            var enumerableNode = node.Skip(1).First();
            enumerableNode.ShouldBeType<EnumerableNode>();
            enumerableNode.Path.ShouldEqual(baseName + "[\"two\"]");
            enumerableNode.First().Path.ShouldEqual(baseName + "[\"two\"][0]");
            enumerableNode.Skip(1).First().Path.ShouldEqual(baseName + "[\"two\"][1]");
            enumerableNode.Skip(2).First().Path.ShouldEqual(baseName + "[\"two\"][2]");
            enumerableNode.Skip(3).First().Path.ShouldEqual(baseName + "[\"two\"][3]");

            var dictionaryNode = node.Skip(2).First();
            dictionaryNode.ShouldBeType<DictionaryNode>();
            dictionaryNode.Path.ShouldEqual(baseName + "[\"three\"]");
            dictionaryNode.GetNode("one").Path.ShouldEqual(baseName + "[\"three\"][\"one\"]");
            dictionaryNode.GetNode("two").Path.ShouldEqual(baseName + "[\"three\"][\"two\"]");
            dictionaryNode.GetNode("three").Path.ShouldEqual(baseName + "[\"three\"][\"three\"]");
            dictionaryNode.GetNode("four").Path.ShouldEqual(baseName + "[\"three\"][\"four\"]");

            var objectNode = node.Skip(3).First();
            objectNode.ShouldBeType<ObjectNode>();
            objectNode.Path.ShouldEqual(baseName + "[\"four\"]");
            objectNode.GetNode("ValueNode").Path.ShouldEqual(baseName + "[\"four\"].ValueNode");
            objectNode.GetNode("EnumerableNode").Path.ShouldEqual(baseName + "[\"four\"].EnumerableNode");
            objectNode.GetNode("DictionaryNode").Path.ShouldEqual(baseName + "[\"four\"].DictionaryNode");
            objectNode.GetNode("ObjectNode").Path.ShouldEqual(baseName + "[\"four\"].ObjectNode");
        }
Example #21
0
 public void should_set_simple_types(Type type, object value)
 {
     var result = new SimpleValue(type.ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize), null, result, null, null).Value = value;
     result.Instance.ShouldEqual(value);
 }
Example #22
0
 public void should_set_enum_with_custom_naming_convention()
 {
     var result = new SimpleValue(typeof(UriFormat).ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize, Options.Create(
         x => x.UseEnumSnakeCaseNaming())), null, result,
         null, null).Value = "Safe_Unescaped";
     result.Instance.ShouldEqual(UriFormat.SafeUnescaped);
 }
Example #23
0
 public void should_set_null_on_nullable_types_when_empty_string_is_passed(Type type)
 {
     var value = new SimpleValue(type.ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize), null,
         value, null, null).Value = "";
     value.Instance.ShouldBeNull();
 }
Example #24
0
 public void should_set_enum_numeric_value_when_configured(
     [Values(3, "3")] object enumValue)
 {
     var result = new SimpleValue(typeof(UriFormat).ToCachedType());
     new ValueNode(CreateContext(Mode.Deserialize),
         null, result, null, null).Value = enumValue;
     result.Instance.ShouldEqual(UriFormat.SafeUnescaped);
 }
Example #25
0
        public void should_allow_value_override()
        {
            var factory = Substitute.For<Func<string>>();
            factory.Invoke().Returns("oh");
            var value = new SimpleValue(typeof(string).ToCachedType());
            var lazy = new LazyValue(value, factory);

            value.Instance.ShouldBeNull();

            lazy.Instance = "hai";

            for (var i = 0; i < 5; i++)
            {
                lazy.Instance.ShouldEqual("hai");
                lazy.SpecifiedType.Type.ShouldBe<string>();
                lazy.ActualType.Type.ShouldBe<string>();
                value.Instance.ShouldEqual("hai");
            }

            factory.DidNotReceiveWithAnyArgs();
        }