-
Notifications
You must be signed in to change notification settings - Fork 3
/
PropertyTests.cs
72 lines (57 loc) · 2.45 KB
/
PropertyTests.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
using System;
using System.Reflection;
using Shouldly;
using Xunit;
namespace Ryder.Tests
{
/// <summary>
/// <see cref="PropertyRedirection"/> tests.
/// </summary>
public class PropertyTests
{
/// <summary>
/// Returns a random <see cref="DateTime"/> between 1970-01-01 00:00:00
/// and 2000-01-01 00:00:00.
/// </summary>
public static DateTime Random => new DateTime(1970, 01, 01).AddSeconds(new Random().NextDouble() * 946_684_800);
/// <summary>
/// Trims milliseconds from the given <see cref="DateTime"/> in order to
/// accept a margin of error up to a second in the following tests.
/// </summary>
public static DateTime Trim(DateTime dt) => dt.Millisecond == 0 ? dt : dt.AddMilliseconds(-dt.Millisecond);
[Fact]
public void TestStaticProperties()
{
PropertyInfo randomProperty = typeof(PropertyTests)
.GetProperty(nameof(Random), BindingFlags.Static | BindingFlags.Public);
PropertyInfo nowProperty = typeof(DateTime)
.GetProperty(nameof(DateTime.Now), BindingFlags.Static | BindingFlags.Public);
DateTime.Now.ShouldNotBe(Random, tolerance: TimeSpan.FromMilliseconds(100));
using (Redirection.Redirect(randomProperty, nowProperty))
{
DateTime.Now.ShouldBe(Random, tolerance: TimeSpan.FromMilliseconds(100));
}
DateTime.Now.ShouldNotBe(Random, tolerance: TimeSpan.FromMilliseconds(100));
}
public virtual int Value => 1;
[Fact]
public void TestInstanceProperties()
{
PropertyInfo baseValueProperty = typeof(PropertyTests)
.GetProperty(nameof(Value), BindingFlags.Instance | BindingFlags.Public);
PropertyInfo overrideValueProperty = typeof(OverridePropertyTests)
.GetProperty(nameof(Value), BindingFlags.Instance | BindingFlags.Public);
OverridePropertyTests overriden = new OverridePropertyTests();
Value.ShouldNotBe(overriden.Value);
using (Redirection.Redirect(baseValueProperty, overrideValueProperty))
{
Value.ShouldBe(overriden.Value);
}
Value.ShouldNotBe(overriden.Value);
}
private sealed class OverridePropertyTests : PropertyTests
{
public override int Value => 2;
}
}
}