|
2 | 2 | // |
3 | 3 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
| 5 | +using System; |
5 | 6 | using System.Collections.Generic; |
6 | 7 | using CefSharp.ModelBinding; |
7 | 8 | using Xunit; |
@@ -86,5 +87,64 @@ public void BindsDoublesWithoutPrecisionLoss() |
86 | 87 |
|
87 | 88 | Assert.Equal(2.0, result); |
88 | 89 | } |
| 90 | + |
| 91 | + [Theory] |
| 92 | + [InlineData(0, typeof(int))] |
| 93 | + [InlineData(0d, typeof(double))] |
| 94 | + [InlineData(false, typeof(bool))] |
| 95 | + public void NullToValueTypeTheory(object excepectedResult, Type conversionType) |
| 96 | + { |
| 97 | + var binder = new DefaultBinder(); |
| 98 | + |
| 99 | + var actualResult = binder.Bind(null, conversionType); |
| 100 | + |
| 101 | + Assert.Equal(excepectedResult, actualResult); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void BindArrayWithNullElementToIntArray() |
| 106 | + { |
| 107 | + var arrayType = typeof(int[]); |
| 108 | + |
| 109 | + var binder = new DefaultBinder(); |
| 110 | + var obj = new List<object> { 10, 20, null, 30 }; |
| 111 | + var result = binder.Bind(obj, arrayType); |
| 112 | + |
| 113 | + Assert.NotNull(result); |
| 114 | + Assert.Equal(arrayType, result.GetType()); |
| 115 | + |
| 116 | + var arr = (int[])result; |
| 117 | + Assert.Equal(obj.Count, arr.Length); |
| 118 | + |
| 119 | + for (int i = 0; i < obj.Count; i++) |
| 120 | + { |
| 121 | + var expected = obj[i] ?? 0; |
| 122 | + var actual = arr[i]; |
| 123 | + Assert.Equal(expected, actual); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + public void BindListOfNumbersToDoubleArray() |
| 129 | + { |
| 130 | + var doubleArrayType = typeof(double[]); |
| 131 | + |
| 132 | + var binder = new DefaultBinder(); |
| 133 | + var obj = new List<object> { 10, 20, 1.23 }; |
| 134 | + var result = binder.Bind(obj, doubleArrayType); |
| 135 | + |
| 136 | + Assert.NotNull(result); |
| 137 | + Assert.Equal(doubleArrayType, result.GetType()); |
| 138 | + |
| 139 | + var arr = (double[])result; |
| 140 | + Assert.Equal(obj.Count, arr.Length); |
| 141 | + |
| 142 | + for (int i = 0; i < obj.Count; i++) |
| 143 | + { |
| 144 | + var expected = Convert.ToDouble(obj[i]); |
| 145 | + var actual = arr[i]; |
| 146 | + Assert.Equal(expected, actual); |
| 147 | + } |
| 148 | + } |
89 | 149 | } |
90 | 150 | } |
0 commit comments