示例#1
0
        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);
        }
示例#2
0
        public void LoadsFromDictionary()
        {
            var source = new Dictionary <string, string> {
                { "foo", "bar" },
                { "xyz", "123" }
            };

            var target = new FormValueMultimap(source, settings);

            Assert.Equal(source, target);
        }
示例#3
0
        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);
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        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);
        }
示例#8
0
        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);
        }
示例#9
0
        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);
        }
示例#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);
        }
示例#11
0
        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);
        }
示例#12
0
        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);
        }
示例#13
0
        public void EmptyIfNullPassedIn()
        {
            var target = new FormValueMultimap(null, settings);

            Assert.Empty(target);
        }