-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
EnvironmentMockBuilder.cs
75 lines (62 loc) · 2.26 KB
/
EnvironmentMockBuilder.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
namespace Microsoft.Extensions.DependencyModel.Tests
{
public class EnvironmentMockBuilder
{
private Dictionary<string, string> _variables = new Dictionary<string, string>();
private Dictionary<string, object> _appContextData = new Dictionary<string, object>();
private bool _isWindows;
internal static IEnvironment Empty { get; } = Create().Build();
public static EnvironmentMockBuilder Create()
{
return new EnvironmentMockBuilder();
}
public EnvironmentMockBuilder AddVariable(string name, string value)
{
_variables.Add(name, value);
return this;
}
public EnvironmentMockBuilder AddAppContextData(string name, object value)
{
_appContextData.Add(name, value);
return this;
}
public EnvironmentMockBuilder SetIsWindows(bool value)
{
_isWindows = value;
return this;
}
internal IEnvironment Build()
{
return new EnvironmentMock(_variables, _appContextData, _isWindows);
}
private class EnvironmentMock : IEnvironment
{
private Dictionary<string, string> _variables;
private Dictionary<string, object> _appContextData;
private bool _isWindows;
public EnvironmentMock(Dictionary<string, string> variables, Dictionary<string, object> appContextData, bool isWindows)
{
_variables = variables;
_appContextData = appContextData;
_isWindows = isWindows;
}
public string GetEnvironmentVariable(string name)
{
_variables.TryGetValue(name, out string value);
return value;
}
public object GetAppContextData(string name)
{
_appContextData.TryGetValue(name, out object value);
return value;
}
public bool IsWindows()
{
return _isWindows;
}
}
}
}