-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCategoryThemeTest.cs
190 lines (158 loc) · 6.94 KB
/
CategoryThemeTest.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using NUnit.Framework;
using SharpMap.Data;
using SharpMap.Styles;
namespace SharpMap.Rendering.Thematics.CategoryThemeTest
{
public class CategoryThemeTest
{
[Test]
public void TestCategoryRangeItemInt32()
{
// arrange
var ri = new CategoryThemeRangeItem<int> { LowerBound = 5, UpperBound = 10, Style = VectorStyle.CreateRandomStyle()};
// act
// assert
Assert.That(ri, Is.Not.Null);
Assert.That(ri.Title, Is.EqualTo("5 - 10"));
ri.Title = "[5, 10[";
Assert.That(ri.Title, Is.EqualTo("[5, 10["));
Assert.That(ri.Matches(4), Is.False);
Assert.That(ri.Matches(5), Is.True);
Assert.That(ri.Matches(6), Is.True);
Assert.That(ri.Matches(9), Is.True);
Assert.That(ri.Matches(10), Is.False);
Assert.That(ri.Matches(11), Is.False);
}
[Test]
public void TestCategoryRangeItemDouble()
{
// arrange
var ri = new CategoryThemeRangeItem<double>
{
LowerBound = 5.01, UpperBound = 9.99,
Style = VectorStyle.CreateRandomStyle()
};
// act
// assert
Assert.That(ri, Is.Not.Null);
Assert.That(ri.Title, Is.EqualTo(string.Format("{0} - {1}", 5.01, 9.99)));
ri.Title = "[5.01 - 9.99[";
Assert.That(ri.Title, Is.EqualTo("[5.01 - 9.99["));
Assert.That(ri.Matches(5), Is.False);
Assert.That(ri.Matches(5.01), Is.True);
Assert.That(ri.Matches(6), Is.True);
Assert.That(ri.Matches(9), Is.True);
Assert.That(ri.Matches(9.99), Is.False);
Assert.That(ri.Matches(10), Is.False);
Assert.That(ri.Style != null, Is.True);
}
[Test]
public void TestCategoryValuesItem()
{
// arrange
var vi = new CategoryThemeValuesItem<string>
{
Values = new List<string> { "A", "B", "C", "D"},
Style = VectorStyle.CreateRandomStyle()
};
// act
// assert
Assert.That(vi, Is.Not.Null);
Assert.That(vi.Title, Is.EqualTo(string.Join(",", vi.Values.ToArray())));
vi.Title = "A, B, C or D";
Assert.That(vi.Title, Is.EqualTo("A, B, C or D"));
Assert.That(vi.Matches("A"), Is.True);
Assert.That(vi.Matches("B"), Is.True);
Assert.That(vi.Matches("C"), Is.True);
Assert.That(vi.Matches("D"), Is.True);
Assert.That(vi.Matches("0"), Is.False);
Assert.That(vi.Matches("1"), Is.False);
Assert.That(vi.Style != null, Is.True);
}
[Test]
public void TestCategoryThemeWithRange()
{
// Arrange
var ct = new CategoryTheme<double>();
ct.ColumnName = "Value";
ct.Default = VectorStyle.CreateRandomStyle();
// Add unordered
ct.Add(new CategoryThemeRangeItem<double> { LowerBound = 0d, UpperBound = 5d, Style = VectorStyle.CreateRandomStyle()});
ct.Add(new CategoryThemeRangeItem<double> { LowerBound = 10d, UpperBound = 15d, Style = VectorStyle.CreateRandomStyle() });
ct.Add(new CategoryThemeRangeItem<double> { LowerBound = 5d, UpperBound = 10d, Style = VectorStyle.CreateRandomStyle() });
ct.Add(new CategoryThemeRangeItem<double> { LowerBound = 20d, UpperBound = 25d, Style = VectorStyle.CreateRandomStyle() });
// act & assert
Assert.That(ct, Is.Not.Null);
Assert.That(ct.ItemsAsReadOnly(), Is.Not.Null);
Assert.That(ct.ItemsAsReadOnly().Count, Is.EqualTo(4));
var fdt = CreateTable("test", ct.ColumnName, typeof(double), 1, 7, 12, 18, 24);
DoTest(ct, fdt);
}
[Test]
public void TestCategoryThemeWithValues()
{
// Arrange
var ct = new CategoryTheme<string>();
ct.ColumnName = "Value";
ct.Default = VectorStyle.CreateRandomStyle();
// Add unordered
ct.Add(new CategoryThemeValuesItem<string> { Values = new List<string> { "A", "B"} , Style = VectorStyle.CreateRandomStyle() });
ct.Add(new CategoryThemeValuesItem<string> { Values = new List<string> { "C", "D" }, Style = VectorStyle.CreateRandomStyle() });
ct.Add(new CategoryThemeValuesItem<string> { Values = new List<string> { "E", "F" }, Style = VectorStyle.CreateRandomStyle() });
ct.Add(new CategoryThemeValuesItem<string> { Values = new List<string> { "G", "H" }, Style = VectorStyle.CreateRandomStyle() });
// act & assert
Assert.That(ct, Is.Not.Null);
Assert.That(ct.ItemsAsReadOnly(), Is.Not.Null);
Assert.That(ct.ItemsAsReadOnly().Count, Is.EqualTo(4));
var fdt = CreateTable("test", ct.ColumnName, typeof(string), "A", "B", "C", "D", "E", "F", "G", "H", "I");
DoTest(ct, fdt);
}
private static void DoTest<T>(CategoryTheme<T> ct, FeatureDataTable fdt) where T : IComparable<T>
{
var ctis = ct.ItemsAsReadOnly();
Assert.That(ctis, Is.Not.Null);
var i = 0;
foreach (FeatureDataRow dataRow in fdt.Rows)
{
i++;
IStyle style = null;
Assert.DoesNotThrow(() => style = ct.GetStyle(dataRow));
if (dataRow.IsNull(ct.ColumnName))
{
if (ct.UseDefaultStyleForDbNull)
Assert.That(style, Is.SameAs(ct.Default));
else
Assert.That(style, Is.Null);
}
else
{
var cti = ctis.FirstOrDefault(t => t.Matches((T)Convert.ChangeType(dataRow[1], typeof(T))));
if (cti != null)
Assert.That(style, Is.SameAs(cti.Style));
else
Assert.That(style, Is.SameAs(ct.Default));
}
}
}
private static FeatureDataTable CreateTable(string tableName, string columnName, Type columnType,
params object[] values)
{
var res = new FeatureDataTable();
res.TableName = tableName;
res.Columns.Add("FID", typeof(int));
res.Columns.Add(columnName, columnType);
res.PrimaryKey = new DataColumn[] { res.Columns[0]};
res.BeginLoadData();
var i = 1;
for (; i < values.Length; i++)
res.LoadDataRow(new [] {i, Convert.ChangeType(values[i], columnType)}, true);
res.LoadDataRow(new object[] {i, DBNull.Value}, true);
res.EndLoadData();
return res;
}
}
}