-
Notifications
You must be signed in to change notification settings - Fork 0
/
With.Tests
102 lines (80 loc) · 3.55 KB
/
With.Tests
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
using System;
using System.Immutable;
using System.Security.Cryptography.X509Certificates;
namespace Test
{
public class Employee : IImmutable
{
public string FirstName { get; }
public readonly string LastName;
public int Age { get; }
public Employee(string firstName) : this(firstName, "", 0) { }
[WithConstructor]
public Employee(string firstName, string lastName, int age) { FirstName = firstName; LastName = lastName; Age = age; }
}
public class Department : IImmutable
{
public string Title { get; }
public Employee Manager { get; }
public DateTime Created { get; }
public Department() : this("", new Employee("", "", 0)) { }
public Department(string title, int manager) : this(title, new Employee("", "", 0)) { }
public Department(string title) : this(title, new Employee("", "", 0)) { }
// With will choose this ctor
public Department(string title, Employee manager)
{
Title = title;
Manager = manager;
Created = DateTime.Now;
}
}
public class Organization : IImmutable
{
public string Name { get; }
public Department Sales { get; }
public Organization(string name)
{
Name = name;
Sales = new Department();
}
public Organization(string name, Department sales)
{
Name = name;
Sales = sales;
}
}
public class Manager : Employee
{
public int Rank { get; }
public Manager(string firstName, string lastName, int age, int rank) : base(firstName, lastName, age) { this.Rank = rank; }
}
class Program
{
static void Main(string[] _)
{
var employee = new Employee("firstname", "lastname", 40);
var mutatedEmployee = employee.With(x => x.FirstName, "mutatedFirstName");
var mutatedEmployee1 = employee.With(x => x.FirstName, "2mutatedFirstName");
var mutatedEmployee2 = employee.With((x => x.FirstName, "mutatedFirstName"), (x => x.LastName, "xxx"));
var mutatedEmployee3 = employee.With((x => x.FirstName, "mutatedFirstName"), (x => x.LastName, "xxx"));
var mutatedEmployee4 = employee.With((x => x.LastName, "xxx"), (x => x.FirstName, "mutatedFirstName"));
var mutatedEmployee5 = employee.With((x => x.FirstName, "mutatedFirstName"), (x => x.Age, 5));
var original = new Organization("Organization", new Department("Development Department", new Employee("John", "Doe", 50)));
var mutated = original.With(x => x.Sales.Manager.FirstName, "mutatedFirstName");
var mutated2 = original.With((x=>x.Name, "mutatedOrg"), (x => x.Sales.Manager.FirstName, "Foo"));
var mutated3 = original.With((x => x.Name, "mutatedOrg"), (x => x.Name, "zzz"));
Console.WriteLine($"{original.Sales.Manager.FirstName} -> {mutated.Sales.Manager.FirstName}");
Console.WriteLine(Object.ReferenceEquals(original, mutated)); // false
Console.WriteLine(Object.ReferenceEquals(original.Sales, mutated.Sales)); // false
Console.WriteLine(Object.ReferenceEquals(original.Sales.Manager, mutated.Sales.Manager)); // false
Console.WriteLine(Object.ReferenceEquals(original.Name, mutated.Name)); // true
Console.WriteLine(Object.ReferenceEquals(original.Sales.Title, original.Sales.Title)); // true
var mutated1 = original.With(x => x.Sales.Manager.FirstName, "Foo");
Console.WriteLine(Object.ReferenceEquals(mutated, mutated1)); // false
var manager = new Manager("Big", "Boss", 20, 1);
var manager1 = manager.With(x => x.Rank, 3);
Console.WriteLine($"{manager.Rank} -> {manager1.Rank}");
Console.ReadKey(true);
}
}
}