forked from markrendle/Simple.Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandlerFactoryBuilderTests.cs
155 lines (116 loc) · 4.1 KB
/
HandlerFactoryBuilderTests.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
using Autofac;
using System;
using System.Collections.Generic;
namespace Simple.Web.Autofac.Tests
{
using CodeGeneration;
using System.Reflection;
using Xunit;
public class HandlerFactoryBuilderTests
{
[Fact]
public void CreatesInstanceOfType()
{
var startup = new TestStartup();
startup.Run(SimpleWeb.Configuration, SimpleWeb.Environment);
var target = new HandlerBuilderFactory(SimpleWeb.Configuration);
var actualFunc = target.BuildHandlerBuilder(typeof(TestHandler));
var actual = (TestHandler)actualFunc(
new Dictionary<string, string>
{
{ "TestProperty", "Foo" }
}).Handler;
Assert.Equal(Status.OK, actual.Get());
Assert.Equal("Foo", actual.TestProperty);
}
[Fact]
public void CreatesInstanceOfGenericType()
{
var startup = new TestStartup();
startup.Run(SimpleWeb.Configuration, SimpleWeb.Environment);
var target = new HandlerBuilderFactory(SimpleWeb.Configuration);
var actualFunc = target.BuildHandlerBuilder(typeof(GenericTestHandler));
var actual = (GenericTestHandler)actualFunc(
new Dictionary<string, string>
{
{ "TestProperty", null }
}).Handler;
var status = actual.Patch(new GenericArgument() {Name = "Foo"});
Assert.Equal(Status.Created, status);
Assert.Equal("Foo", actual.TestProperty.Name);
}
[Fact]
public void DisposesInstances()
{
var startup = new TestStartup();
startup.Run(SimpleWeb.Configuration, SimpleWeb.Environment);
var target = new HandlerBuilderFactory(SimpleWeb.Configuration);
var actualFunc = target.BuildHandlerBuilder(typeof(TestHandler));
TestHandler handler;
using (var scopedHandler = actualFunc(new Dictionary<string, string>()))
{
handler = (TestHandler)scopedHandler.Handler;
Assert.Equal(false, handler.IsDisposed);
}
Assert.Equal(true, handler.IsDisposed);
}
}
public class TestStartup : AutofacStartupBase
{
protected internal override IContainer BuildContainer()
{
var builder = new ContainerBuilder();
builder.RegisterHandlersInAssembly(Assembly.GetExecutingAssembly());
builder.Register(c => Status.Created)
.AsSelf();
builder.RegisterType<OkResult>()
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
return builder.Build();
}
}
public class TestHandler : IGet, IDisposable
{
private readonly IResult _result;
public bool IsDisposed { get; set; }
public TestHandler(IResult result)
{
_result = result;
}
public Status Get()
{
return _result.Result;
}
public string TestProperty { get; set; }
public void Dispose()
{
IsDisposed = true;
}
}
public class GenericTestHandler : IPatch<GenericArgument>
{
private readonly Status _status;
public GenericArgument TestProperty { get; set; }
public GenericTestHandler(Status status)
{
_status = status;
}
public Status Patch(GenericArgument input)
{
TestProperty = input;
return _status;
}
}
public interface IResult
{
Status Result { get; }
}
public class OkResult : IResult
{
public Status Result { get { return Status.OK; }}
}
public class GenericArgument
{
public string Name { set; get; }
}
}