File: StringWithQualityHeaderValueComparerTests.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (156 lines) | stat: -rw-r--r-- 6,558 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Net.Http.Formatting
{
    public class StringWithQualityHeaderValueComparerTests
    {
        public static IEnumerable<object[]> EqualValues
        {
            get
            {
                yield return new[] { "value", "value" };
                yield return new[] { "value", "VALUE" };
                yield return new[] { "value", "value;q=1" };
                yield return new[] { "value", "value; q=1" };
                yield return new[] { "value", "value;q=1.0" };
                yield return new[] { "value", "value; q=1.0" };
                yield return new[] { "value", "value; q=1.00000" };
                yield return new[] { "value; q=0.5", "value; q=0.5" };
                yield return new[] { "value; q=1.0", "value; q=1.0" };
                yield return new[] { "*", "*" };
                yield return new[] { "*", "*;q=1" };
                yield return new[] { "*", "*; q=1" };
                yield return new[] { "*", "*;q=1.0" };
                yield return new[] { "*", "*; q=1.0" };
                yield return new[] { "*; q=0.5", "*; q=0.5" };
                yield return new[] { "*; q=1.0", "*; q=1.0" };
                yield return new[] { "value1", "value2" };
                yield return new[] { "value1", "value2;q=1" };
                yield return new[] { "value1", "value2; q=1" };
                yield return new[] { "value1", "value2;q=1.0" };
                yield return new[] { "value1", "value2; q=1.0" };
            }
        }

        public static IEnumerable<object[]> NonEqualValues
        {
            get
            {
                yield return new[] { "value; q=0.5", "value; q=1.0" };
                yield return new[] { "value1; q=0.5", "value2; q=1.0" };
                yield return new[] { "*", "value1" };
                yield return new[] { "*;q=1", "value1" };
                yield return new[] { "*; q=1", "value1" };
                yield return new[] { "*;q=1.0", "value1" };
                yield return new[] { "*; q=1.0", "value1" };
                yield return new[] { "*; q=0.5", "value1; q=0.5" };
                yield return new[] { "*; q=1.0", "value1; q=1.0" };
            }
        }

        public static IEnumerable<object[]> BeforeAfterSortedValues
        {
            get
            {
                yield return new[]
                {
                    new List<string>
                    {
                        "text",
                        "text;q=1.0",
                        "text",
                        "text;q=0",
                        "*;q=0.8",
                        "*;q=1",
                        "text;q=0.8",
                        "*;q=0.6",
                        "text;q=1.0",
                        "*;q=0.4",
                        "text;q=0.6",
                    }, 
                    new List<string>
                    {
                        "text",
                        "text;q=1.0",
                        "text",
                        "text;q=1.0",
                        "*;q=1",
                        "text;q=0.8",
                        "*;q=0.8",
                        "text;q=0.6",
                        "*;q=0.6",
                        "*;q=0.4",
                        "text;q=0",
                    }, 
                };
            }
        }

        [Fact]
        public void StaticComparerReturnsSameInstance()
        {
            StringWithQualityHeaderValueComparer comparer1 = StringWithQualityHeaderValueComparer.QualityComparer;
            StringWithQualityHeaderValueComparer comparer2 = StringWithQualityHeaderValueComparer.QualityComparer;

            Assert.NotNull(comparer1);
            Assert.Same(comparer1, comparer2);
        }

        [Theory]
        [PropertyData("EqualValues")]
        public void ComparerReturnsZeroForEqualValues(string stringWithQuality1, string stringWithQuality2)
        {
            // Arrange
            StringWithQualityHeaderValueComparer comparer = StringWithQualityHeaderValueComparer.QualityComparer;

            // Act
            StringWithQualityHeaderValue stringWithQualityHeaderValue1 = StringWithQualityHeaderValue.Parse(stringWithQuality1);
            StringWithQualityHeaderValue stringWithQualityHeaderValue2 = StringWithQualityHeaderValue.Parse(stringWithQuality2);

            // Assert
            Assert.Equal(0, comparer.Compare(stringWithQualityHeaderValue1, stringWithQualityHeaderValue2));
            Assert.Equal(0, comparer.Compare(stringWithQualityHeaderValue2, stringWithQualityHeaderValue1));
        }

        [Theory]
        [PropertyData("NonEqualValues")]
        public void ComparerReturnsNonZeroForNonEqualValues(string stringWithQuality1, string stringWithQuality2)
        {
            // Arrange
            StringWithQualityHeaderValueComparer comparer = StringWithQualityHeaderValueComparer.QualityComparer;

            // Act
            StringWithQualityHeaderValue stringWithQualityHeaderValue1 = StringWithQualityHeaderValue.Parse(stringWithQuality1);
            StringWithQualityHeaderValue stringWithQualityHeaderValue2 = StringWithQualityHeaderValue.Parse(stringWithQuality2);

            // Assert
            Assert.Equal(-1, comparer.Compare(stringWithQualityHeaderValue1, stringWithQualityHeaderValue2));
            Assert.Equal(1, comparer.Compare(stringWithQualityHeaderValue2, stringWithQualityHeaderValue1));
        }

        [Theory]
        [PropertyData("BeforeAfterSortedValues")]
        public void ComparerSortsListCorrectly(List<string> unsorted, List<string> expectedSorted)
        {
            // Arrange
            IEnumerable<StringWithQualityHeaderValue> unsortedValues =
                unsorted.Select(u => StringWithQualityHeaderValue.Parse(u));

            IEnumerable<StringWithQualityHeaderValue> expectedSortedValues =
                expectedSorted.Select(u => StringWithQualityHeaderValue.Parse(u));

            // Act
            IEnumerable<StringWithQualityHeaderValue> actualSorted = unsortedValues.OrderByDescending(m => m, StringWithQualityHeaderValueComparer.QualityComparer);

            // Assert
            Assert.True(expectedSortedValues.SequenceEqual(actualSorted));
        }
    }
}