Skip to content

Commit 7d7259f

Browse files
Merge pull request #95 from telerik/R2.2019Lite
Merge R2.2019Lite into master
2 parents 505e145 + 5685dd6 commit 7d7259f

28 files changed

+2741
-1850
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
JustMock Lite
3+
Copyright © 2010-2015,2019 Progress Software Corporation
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
using System;
19+
using System.Reflection;
20+
21+
namespace Telerik.JustMock.Core.Context
22+
{
23+
/// <summary>
24+
/// Class for automatically resolving the context of async method calls.
25+
/// </summary>
26+
public class AsyncContextResolver
27+
{
28+
/// <summary>
29+
/// Returns the actual test method from where the async call starts.
30+
/// </summary>
31+
/// <returns>The test method</returns>
32+
public static MethodBase GetContext()
33+
{
34+
return null;
35+
}
36+
}
37+
}
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
JustMock Lite
3-
Copyright © 2010-2015 Telerik EAD
3+
Copyright © 2010-2015,2019 Progress Software Corporation
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -18,31 +18,35 @@ limitations under the License.
1818
using System;
1919
using System.Collections.Generic;
2020
using System.Linq;
21+
using System.Reflection;
2122
using System.Text;
2223

2324
namespace Telerik.JustMock.Core.Context
2425
{
25-
internal abstract class MockingContextResolverBase : IMockingContextResolver
26-
{
27-
private readonly string assertFailedExceptionTypeName;
26+
internal abstract class MockingContextResolverBase : IMockingContextResolver
27+
{
28+
private readonly string assertFailedExceptionTypeName;
29+
protected readonly object repositorySync = new object();
2830

29-
public MockingContextResolverBase(string assertFailedExceptionTypeName, params string[] frameworkAssemblyNames)
30-
{
31-
this.assertFailedExceptionTypeName = assertFailedExceptionTypeName;
32-
}
31+
public MockingContextResolverBase(string assertFailedExceptionTypeName, params string[] frameworkAssemblyNames)
32+
{
33+
this.assertFailedExceptionTypeName = assertFailedExceptionTypeName;
34+
}
3335

34-
public abstract MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior);
36+
public abstract MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior);
3537

36-
public abstract bool RetireRepository();
38+
public abstract bool RetireRepository();
3739

38-
public Action<string, Exception> GetFailMethod()
39-
{
40-
return LocalMockingContextResolver.GetFailMethod(Type.GetType(this.assertFailedExceptionTypeName));
41-
}
40+
public abstract MethodBase GetTestMethod();
4241

43-
protected static Type FindType(string assemblyAndTypeName, bool throwOnNotFound)
44-
{
45-
return Type.GetType(assemblyAndTypeName, throwOnNotFound);
46-
}
47-
}
42+
public Action<string, Exception> GetFailMethod()
43+
{
44+
return LocalMockingContextResolver.GetFailMethod(Type.GetType(this.assertFailedExceptionTypeName));
45+
}
46+
47+
protected static Type FindType(string assemblyAndTypeName, bool throwOnNotFound)
48+
{
49+
return Type.GetType(assemblyAndTypeName, throwOnNotFound);
50+
}
51+
}
4852
}

Telerik.JustMock.Portable/Telerik.JustMock.Portable.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
<Compile Include="..\Telerik.JustMock\Core\ArrangeMethodAttribute.cs">
7474
<Link>Core\ArrangeMethodAttribute.cs</Link>
7575
</Compile>
76+
<Compile Include="..\Telerik.JustMock\Core\Behaviors\AfterAllBehavior.cs" >
77+
<Link>Core\Behaviors\AfterAllBehavior.cs</Link>
78+
</Compile>
7679
<Compile Include="..\Telerik.JustMock\Core\Behaviors\CallOriginalBehavior.cs">
7780
<Link>Core\Behaviors\CallOriginalBehavior.cs</Link>
7881
</Compile>
@@ -142,6 +145,9 @@
142145
<Compile Include="..\Telerik.JustMock\Core\CallPattern.cs">
143146
<Link>Core\CallPattern.cs</Link>
144147
</Compile>
148+
<Compile Include="..\Telerik.JustMock\Core\CallPatternCreator.cs">
149+
<Link>Core\CallPatternCreator.cs</Link>
150+
</Compile>
145151
<Compile Include="..\Telerik.JustMock\Core\ClrProfilerSetupHelper.cs">
146152
<Link>Core\ClrProfilerSetupHelper.cs</Link>
147153
</Compile>
@@ -376,6 +382,9 @@
376382
<Compile Include="..\Telerik.JustMock\Expectations\Abstraction\IOrder.cs">
377383
<Link>Expectations\Abstraction\IOrder.cs</Link>
378384
</Compile>
385+
<Compile Include="..\Telerik.JustMock\Expectations\Abstraction\IPrerequisite.cs">
386+
<Link>Expectations\Abstraction\IPrerequisite.cs</Link>
387+
</Compile>
379388
<Compile Include="..\Telerik.JustMock\Expectations\Abstraction\IReturnCollection.cs">
380389
<Link>Expectations\Abstraction\IReturnCollection.cs</Link>
381390
</Compile>
@@ -475,6 +484,7 @@
475484
<Compile Include="..\Telerik.JustMock\Core\JMDebug.cs">
476485
<Link>Core\JMDebug.cs</Link>
477486
</Compile>
487+
<Compile Include="Core\Context\AsyncContextResolver.cs" />
478488
<Compile Include="Core\Context\LocalMockingContextResolver.cs" />
479489
<Compile Include="Core\Context\MockingContextResolverBase.cs" />
480490
<Compile Include="Core\Context\VisualStudioPortableContextResolver.cs" />
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*
2+
JustMock Lite
3+
Copyright © 2019 Progress Software Corporation
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
using System;
19+
using System.Collections.Generic;
20+
using System.Linq;
21+
22+
#region JustMock Test Attributes
23+
#if NUNIT
24+
using NUnit.Framework;
25+
using TestCategory = NUnit.Framework.CategoryAttribute;
26+
using TestClass = NUnit.Framework.TestFixtureAttribute;
27+
using TestMethod = NUnit.Framework.TestAttribute;
28+
using TestInitialize = NUnit.Framework.SetUpAttribute;
29+
using TestCleanup = NUnit.Framework.TearDownAttribute;
30+
using AssertionException = NUnit.Framework.AssertionException;
31+
#elif XUNIT
32+
using Xunit;
33+
using Telerik.JustMock.XUnit.Test.Attributes;
34+
using TestCategory = Telerik.JustMock.XUnit.Test.Attributes.XUnitCategoryAttribute;
35+
using TestClass = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestClassAttribute;
36+
using TestMethod = Xunit.FactAttribute;
37+
using TestInitialize = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestInitializeAttribute;
38+
using TestCleanup = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestCleanupAttribute;
39+
#if XUNIT2
40+
using AssertionException = Xunit.Sdk.XunitException;
41+
#else
42+
using AssertionException = Xunit.Sdk.AssertException;
43+
#endif
44+
#elif VSTEST_PORTABLE
45+
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
46+
using AssertionException = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertFailedException;
47+
#else
48+
using Microsoft.VisualStudio.TestTools.UnitTesting;
49+
using AssertionException = Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException;
50+
#endif
51+
#endregion
52+
53+
namespace Telerik.JustMock.Tests
54+
{
55+
[TestClass]
56+
public class AfterAllFixture
57+
{
58+
[TestMethod, TestCategory("Lite"), TestCategory("AfterAll")]
59+
public void ShouldAssertAfterAllWithPrerequisites()
60+
{
61+
var foo = Mock.Create<IFoo>();
62+
63+
var init = Mock.Arrange(() => foo.Init());
64+
Mock.ArrangeSet(() => foo.Value = Arg.AnyInt).AfterAll(init);
65+
Mock.Arrange(() => foo.Save()).AfterAll(init);
66+
67+
foo.Init();
68+
foo.Value = 5;
69+
foo.Save();
70+
71+
Mock.AssertAll(foo);
72+
}
73+
74+
[TestMethod, TestCategory("Lite"), TestCategory("AfterAll")]
75+
public void ShouldThrowAfterAllWithoutPrerequisites()
76+
{
77+
var foo = Mock.Create<IFoo>();
78+
79+
var init = Mock.Arrange(() => foo.Init());
80+
Mock.ArrangeSet(() => foo.Value = Arg.AnyInt).AfterAll(init);
81+
Mock.Arrange(() => foo.Save()).AfterAll(init);
82+
83+
foo.Value = 5;
84+
foo.Save();
85+
86+
Assert.Throws<AssertionException>(() => Mock.AssertAll(foo));
87+
}
88+
89+
[TestMethod, TestCategory("Lite"), TestCategory("AfterAll")]
90+
public void ShouldAssertAfterAllWithPrerequisitesOrdered()
91+
{
92+
var foo = Mock.Create<IFoo>();
93+
94+
var init = Mock.Arrange(() => foo.Init());
95+
Mock.ArrangeSet(() => foo.Value = Arg.AnyInt).AfterAll(init).InOrder();
96+
Mock.Arrange(() => foo.Save()).AfterAll(init).InOrder();
97+
98+
foo.Init();
99+
foo.Value = 5;
100+
foo.Save();
101+
102+
Mock.AssertAll(foo);
103+
}
104+
105+
[TestMethod, TestCategory("Lite"), TestCategory("AfterAll")]
106+
public void ShouldThrowAfterAllWithoutPrerequisitesOrdered()
107+
{
108+
var foo = Mock.Create<IFoo>();
109+
110+
var init = Mock.Arrange(() => foo.Init());
111+
Mock.ArrangeSet(() => foo.Value = Arg.AnyInt).AfterAll(init).InOrder();
112+
Mock.Arrange(() => foo.Save()).AfterAll(init).InOrder();
113+
114+
foo.Value = 5;
115+
foo.Save();
116+
117+
Assert.Throws<AssertionException>(() => Mock.AssertAll(foo));
118+
}
119+
120+
121+
122+
[TestMethod, TestCategory("Lite"), TestCategory("AfterAll")]
123+
public void ShouldAssertAfterAllInderectChainedWithPrerequisites()
124+
{
125+
var foo = Mock.Create<IFoo>();
126+
var bar = Mock.Create<IBar>();
127+
128+
var fooInit = Mock.Arrange(() => foo.Init());
129+
var barInit = Mock.Arrange(() => bar.Init());
130+
Mock.ArrangeSet(() => bar.Foo = Arg.IsAny<IFoo>()).AfterAll(fooInit).AfterAll(barInit);
131+
132+
foo.Init();
133+
bar.Init();
134+
bar.Foo = foo;
135+
136+
Mock.AssertAll(bar);
137+
}
138+
139+
[TestMethod, TestCategory("Lite"), TestCategory("AfterAll")]
140+
public void ShouldThrowAfterAllInderectChainedWithPartialPrerequisites()
141+
{
142+
var foo = Mock.Create<IFoo>();
143+
var bar = Mock.Create<IBar>();
144+
145+
var fooInit = Mock.Arrange(() => foo.Init());
146+
var barInit = Mock.Arrange(() => bar.Init());
147+
Mock.ArrangeSet(() => bar.Foo = Arg.IsAny<IFoo>()).AfterAll(fooInit).AfterAll(barInit);
148+
149+
bar.Init();
150+
bar.Foo = foo;
151+
152+
Assert.Throws<AssertionException>(() => Mock.AssertAll(bar));
153+
}
154+
155+
public interface IFoo
156+
{
157+
void Init();
158+
int Value { get; set; }
159+
void Save();
160+
}
161+
162+
public interface IBar
163+
{
164+
void Init();
165+
IFoo Foo { get; set; }
166+
}
167+
168+
public interface IFooContainer
169+
{
170+
IEnumerable<IFoo> Values { get; }
171+
}
172+
}
173+
}

Telerik.JustMock.Tests/Telerik.JustMock.Tests.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<Compile Include="$(MSBuildThisFileDirectory)EventsFixtureDependencies.cs" />
4141
<Compile Include="$(MSBuildThisFileDirectory)FluentFixture.cs" />
4242
<Compile Include="$(MSBuildThisFileDirectory)FuncSpecFixture.cs" />
43+
<Compile Include="$(MSBuildThisFileDirectory)AfterAllFixture.cs" />
4344
<Compile Include="$(MSBuildThisFileDirectory)InOrderFixture.cs" />
4445
<Compile Include="$(MSBuildThisFileDirectory)InteropFixture.cs" />
4546
<Compile Include="$(MSBuildThisFileDirectory)MarshalByRefFixture.cs" />
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
JustMock Lite
3+
Copyright © 2019 Progress Software Corporation
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
using System;
19+
using Telerik.JustMock.Core.Context;
20+
using Telerik.JustMock.Expectations.Abstraction;
21+
22+
namespace Telerik.JustMock.Core.Behaviors
23+
{
24+
internal class AfterAllBehavior : IAssertableBehavior
25+
{
26+
private readonly IPrerequisite[] prerequisites;
27+
private string processedStackTrace;
28+
29+
public string DebugView
30+
{
31+
get { return String.Format("{0}: after all prerequisite expectations.", this.IsMet ? "Met" : "Unmet"); }
32+
}
33+
34+
public AfterAllBehavior(IPrerequisite[] prerequisites)
35+
{
36+
this.prerequisites = prerequisites;
37+
}
38+
39+
private string ExecutionMessage
40+
{
41+
get
42+
{
43+
var message = this.processedStackTrace;
44+
return !String.IsNullOrEmpty(message) ? message : "--no calls--\n";
45+
}
46+
}
47+
48+
private bool IsMet
49+
{
50+
get
51+
{
52+
foreach (var prerequisite in this.prerequisites)
53+
{
54+
if (!prerequisite.IsMet)
55+
{
56+
return false;
57+
}
58+
}
59+
return true;
60+
}
61+
}
62+
63+
public void Process(Invocation invocation)
64+
{
65+
this.processedStackTrace = invocation.InputToString() + " called at:\n" + MockingContext.GetStackTrace(" ");
66+
}
67+
68+
public void Assert()
69+
{
70+
if (!this.IsMet)
71+
{
72+
MockingContext.Fail("Not all prerequisites are met. Actual call processed:\n{0}", this.ExecutionMessage);
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)