-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTuples.cs
185 lines (154 loc) · 3.51 KB
/
Tuples.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
using System;
using Xunit;
namespace CsharpSeven
{
public class Tuples
{
[Fact]
public void Works_For_Simple_Tuple()
{
(int Added, int Multiplied) Calculate(int first, int second)
{
return (first + second, first * second);
}
/* Setup */
const int four = 4;
const int five = 5;
/* Test */
var result = Calculate(four, five);
/* Assert */
Assert.Equal(result.Added, 9);
Assert.Equal(result.Multiplied, 20);
}
[Fact]
public void Works_As_Class_Member()
{
/* Setup */
var person = new Person
{
FirstName = "Darth",
LastName = "Vader"
};
/* Test */
var fullName = person.FullName;
/* Assert */
Assert.Equal(fullName.First, person.FirstName);
Assert.Equal(fullName.Last, person.LastName);
}
[Fact]
public void Should_Survive_Type_Casting()
{
/* Setup */
var person = new Person
{
FirstName = "Darth",
LastName = "Vader"
};
var tupple = person.FullName;
var tuppleAsObj = tupple as object;
/* Test */
var backAgain = (ValueTuple<string, string>)tuppleAsObj;
/* Assert */
Assert.Equal(backAgain.Item1, person.FirstName);
Assert.Equal(backAgain.Item2, person.LastName);
}
[Fact]
public void Works_With_Generic_Arguments()
{
(T1 FirstArgument, T2 SecondArgument) Create<T1, T2>(T1 t1, T2 t2)
{
return (t1, t2);
}
/* Setup */
const int one = 1;
const string hello = "hello";
/* Test */
var tupple = Create(one, hello);
/* Assert */
Assert.Equal(tupple.FirstArgument, one);
Assert.Equal(tupple.SecondArgument, hello);
}
[Fact]
public void Can_Be_Deconstructed()
{
(int Added, int Multiplied) Calculate(int first, int second)
{
return (first + second, first * second);
}
/* Setup */
const int four = 4;
const int five = 5;
/* Test */
(var added, var multipled) = Calculate(four, five);
/* Assert */
Assert.Equal(added, 9);
Assert.Equal(multipled, 20);
}
[Fact]
public void Can_Deconstruct_Objects()
{
/* Setup */
var person = new Person
{
FirstName = "Obi-Wan",
LastName = "Kenobi"
};
/* Test */
(var first, var last) = person;
/* Assert */
Assert.Equal(first, person.FirstName);
Assert.Equal(last, person.LastName);
}
[Fact]
public void Can_Deconstruct_Objects_With_Discard_Operator()
{
/* Setup */
var person = new Person
{
FirstName = "Obi-Wan",
LastName = "Kenobi"
};
/* Test */
(var first, _) = person;
/* Assert */
Assert.Equal(first, person.FirstName);
}
[Fact]
public void Can_Deconstruct_From_Extension_Method()
{
/* Setup */
var xmas = new DateTime(2017, 12, 24);
/* Test */
(var year, var month, var day) = xmas;
/* Assert */
Assert.Equal(xmas.Year, year);
Assert.Equal(xmas.Month, month);
Assert.Equal(xmas.Day, day);
}
}
internal class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public (string First, string Last) FullName => (FirstName, LastName);
public void Deconstruct(out string firstName, out string lastName)
{
firstName = FirstName;
lastName = LastName;
}
}
internal static class DateTimeDeconstructExtension
{
public static void Deconstruct(this DateTime datetime, out int year, out int month, out int day)
{
year = datetime.Year;
month = datetime.Month;
day = datetime.Day;
}
public static void Deconstruct(this DateTime datetime, out int year, out int month)
{
year = datetime.Year;
month = datetime.Month;
}
}
}