public void LoadFromObjectWithCollections()
        {
            var source = new ObjectWithRepeatedFieldsTestClass
            {
                A = new List <int> {
                    1, 2
                },
                B = new HashSet <string> {
                    "set1", "set2"
                },
                C = new HashSet <int> {
                    1, 2
                },
                D = new List <double> {
                    0.1, 1.0
                },
                E = new List <bool> {
                    true, false
                }
            };
            var expected = new List <KeyValuePair <string, string> > {
                new KeyValuePair <string, string>("A", "01"),
                new KeyValuePair <string, string>("A", "02"),
                new KeyValuePair <string, string>("B", "set1,set2"),
                new KeyValuePair <string, string>("C", "01 02"),
                new KeyValuePair <string, string>("D", "0.10\t1.00"),

                // The default behavior is to capitalize booleans. This is not a requirement.
                new KeyValuePair <string, string>("E", "True|False")
            };

            var actual = new FormValueMultimap(source, settings);

            Assert.Equal(expected, actual);
        }
        public void LoadsFromDictionary()
        {
            var source = new Dictionary <string, string> {
                { "foo", "bar" },
                { "xyz", "123" }
            };

            var target = new FormValueMultimap(source, settings);

            Assert.Equal(source, target);
        }
        public void SkipsNullValuesFromDictionary()
        {
            var source = new Dictionary <string, string> {
                { "foo", "bar" },
                { "xyz", null }
            };

            var target = new FormValueMultimap(source, settings);

            Assert.Single(target);
            Assert.Contains("foo", target.Keys);
        }
        public void UsesJsonPropertyAttribute()
        {
            var source = new AliasingTestClass
            {
                Bar = "xyz"
            };

            var target = new FormValueMultimap(source, settings);

            Assert.DoesNotContain("Bar", target.Keys);
            Assert.Contains("b", target.Keys);
            Assert.Equal("xyz", target.FirstOrDefault(entry => entry.Key == "b").Value);
        }
        public void UsesQueryPropertyAttribute()
        {
            var source = new AliasingTestClass
            {
                Frob = 4
            };

            var target = new FormValueMultimap(source, settings);

            Assert.DoesNotContain("Bar", target.Keys);
            Assert.Contains("prefix-fr", target.Keys);
            Assert.Equal("4.0", target.FirstOrDefault(entry => entry.Key == "prefix-fr").Value);
        }
        public void UsesAliasAsAttribute()
        {
            var source = new AliasingTestClass
            {
                Foo = "abc"
            };

            var target = new FormValueMultimap(source, settings);

            Assert.DoesNotContain("Foo", target.Keys);
            Assert.Contains("f", target.Keys);
            Assert.Equal("abc", target.FirstOrDefault(entry => entry.Key == "f").Value);
        }
        public void GivesPrecedenceToAliasAs()
        {
            var source = new AliasingTestClass
            {
                Baz = "123"
            };

            var target = new FormValueMultimap(source, settings);

            Assert.DoesNotContain("Bar", target.Keys);
            Assert.DoesNotContain("z", target.Keys);
            Assert.Contains("a", target.Keys);
            Assert.Equal("123", target.FirstOrDefault(entry => entry.Key == "a").Value);
        }
        public void ExcludesPropertiesWithInaccessibleGetters()
        {
            var source = new ClassWithInaccessibleGetters
            {
                A = "Foo",
                B = "Bar"
            };
            var expected = new Dictionary <string, string>
            {
                { "C", "FooBar" }
            };

            var actual = new FormValueMultimap(source, settings);

            Assert.Equal(expected, actual);
        }
        public void LoadsFromObject()
        {
            var source = new ObjectTestClass
            {
                A = "1",
                B = "2"
            };
            var expected = new Dictionary <string, string>
            {
                { "A", "1" },
                { "B", "2" },
            };

            var actual = new FormValueMultimap(source, settings);

            Assert.Equal(expected, actual);
        }
Beispiel #10
0
        public void DefaultCollectionFormatCanBeSpecifiedInSettings()
        {
            var settingsWithCollectionFormat = new RefitSettings
            {
                CollectionFormat = CollectionFormat.Multi
            };
            var source = new ObjectWithRepeatedFieldsTestClass
            {
                // Members have explicit CollectionFormat
                A = new List <int> {
                    1, 2
                },
                B = new HashSet <string> {
                    "set1", "set2"
                },
                C = new HashSet <int> {
                    1, 2
                },
                D = new List <double> {
                    0.1, 1.0
                },
                E = new List <bool> {
                    true, false
                },

                // Member has no explicit CollectionFormat
                F = new[] { 1, 2, 3 }
            };
            var expected = new List <KeyValuePair <string, string> > {
                new KeyValuePair <string, string>("A", "01"),
                new KeyValuePair <string, string>("A", "02"),
                new KeyValuePair <string, string>("B", "set1,set2"),
                new KeyValuePair <string, string>("C", "01 02"),
                new KeyValuePair <string, string>("D", "0.10\t1.00"),
                new KeyValuePair <string, string>("E", "True|False"),
                new KeyValuePair <string, string>("F", "1"),
                new KeyValuePair <string, string>("F", "2"),
                new KeyValuePair <string, string>("F", "3"),
            };

            var actual = new FormValueMultimap(source, settingsWithCollectionFormat);

            Assert.Equal(expected, actual);
        }
        public void SerializesEnumWithEnumMemberAttribute()
        {
            var source = new Dictionary <string, EnumWithEnumMember>()
            {
                { "A", EnumWithEnumMember.A },
                { "B", EnumWithEnumMember.B }
            };

            var expected = new Dictionary <string, string>
            {
                { "A", "A" },
                { "B", "b" }
            };


            var actual = new FormValueMultimap(source, settings);

            Assert.Equal(expected, actual);
        }
        public void LoadsFromAnonymousType()
        {
            var source = new
            {
                foo = "bar",
                xyz = 123
            };

            var expected = new Dictionary <string, string>
            {
                { "foo", "bar" },
                { "xyz", "123" }
            };

            var actual = new FormValueMultimap(source, settings);


            Assert.Equal(expected, actual);
        }
        public void EmptyIfNullPassedIn()
        {
            var target = new FormValueMultimap(null, settings);

            Assert.Empty(target);
        }