Skip to content

Commit ffb49da

Browse files
committed
Write some tests
1 parent c4dc451 commit ffb49da

File tree

12 files changed

+194
-13
lines changed

12 files changed

+194
-13
lines changed

src/NeinLinq/InjectLambdaMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ static Func<Expression, LambdaExpression> DynamicLambdaFactory(string method, In
9393
var targetObject = Expression.Lambda<Func<object>>(Expression.Convert(value, typeof(object))).Compile()();
9494

9595
// retrieve actual target type at runtime, whatever it may be
96-
var target = targetObject != null ? targetObject.GetType() : typeof(object);
96+
var target = targetObject.GetType();
9797

9898
// actual method may provide different information
9999
var concreteMethod = signature.FindMatch(target, method);
100100

101101
// configuration over convention, if any
102-
var metadata = concreteMethod?.GetCustomAttribute<InjectLambdaAttribute>() ?? InjectLambdaAttribute.None;
102+
var metadata = concreteMethod.GetCustomAttribute<InjectLambdaAttribute>() ?? InjectLambdaAttribute.None;
103103

104104
// retrieve validated factory method
105105
var factory = signature.FindFactory(target, metadata.Method ?? method);

test/NeinLinq.Fakes/InjectableQuery/ConcreteFunctions.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq.Expressions;
34

45
namespace NeinLinq.Fakes.InjectableQuery
@@ -33,11 +34,21 @@ public Expression<Func<Dummy, double>> Narf()
3334
return v => Math.Round(v.Distance / v.Time, digits);
3435
}
3536

37+
public IEnumerable<Func<Dummy, double>> VelocityWithStupidSiblingResult()
38+
{
39+
yield return v => Math.Round(v.Distance / v.Time, digits);
40+
}
41+
3642
public Func<Dummy, double> VelocityWithInvalidSiblingResult()
3743
{
3844
return v => Math.Round(v.Distance / v.Time, digits);
3945
}
4046

47+
public Expression<Func<Dummy, float>> VelocityWithStupidSiblingSignature()
48+
{
49+
return v => (float)Math.Round(v.Distance / v.Time, digits);
50+
}
51+
4152
public Expression<Func<double, double, double>> VelocityWithInvalidSiblingSignature()
4253
{
4354
return (d, t) => Math.Round(d / t, digits);
@@ -46,18 +57,18 @@ public Expression<Func<double, double, double>> VelocityWithInvalidSiblingSignat
4657
public Expression<Func<TDummy, TOther, double>> VelocityWithGenericArguments<TDummy, TOther>()
4758
where TDummy : IDummy
4859
{
49-
return (v, _) => v.Distance / v.Time;
60+
return (v, _) => Math.Round(v.Distance / v.Time, digits);
5061
}
5162

5263
public Expression<Func<TDummy, double>> VelocityWithGenericArguments<TDummy>()
5364
where TDummy : IDummy
5465
{
55-
return v => v.Distance / v.Time;
66+
return v => Math.Round(v.Distance / v.Time, digits);
5667
}
5768

5869
public Expression<Func<Dummy, double>> VelocityWithInvalidGenericArguments()
5970
{
60-
return v => v.Distance / v.Time;
71+
return v => Math.Round(v.Distance / v.Time, digits);
6172
}
6273
}
6374
}

test/NeinLinq.Fakes/InjectableQuery/Dummy.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,18 @@ public double VelocityExternalWithGetter
5454

5555
public double VelocityWithoutSibling { get; }
5656

57+
public double VelocityWithStupidSiblingResult { get; }
58+
59+
public static Lazy<Func<Dummy, double>> VelocityWithStupidSiblingResultExpr => new Lazy<Func<Dummy, double>>(() => v => v.Distance / v.Time);
60+
5761
public double VelocityWithInvalidSiblingResult { get; }
5862

5963
public static Func<Dummy, double> VelocityWithInvalidSiblingResultExpr => v => v.Distance / v.Time;
6064

65+
public double VelocityWithStupidSiblingSignature { get; }
66+
67+
public static Expression<Func<Dummy, float>> VelocityWithStupidSiblingSignatureExpr => v => (float)(v.Distance / v.Time);
68+
6169
public double VelocityWithInvalidSiblingSignature { get; }
6270

6371
public static Expression<Func<double, double, double>> VelocityWithInvalidSiblingSignatureExpr => (d, t) => d / t;

test/NeinLinq.Fakes/InjectableQuery/Functions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq.Expressions;
34

45
namespace NeinLinq.Fakes.InjectableQuery
@@ -54,6 +55,16 @@ public static Expression<Func<Dummy, double>> Narf()
5455
return v => v.Distance / v.Time;
5556
}
5657

58+
public static double VelocityWithStupidSiblingResult(this Dummy value)
59+
{
60+
throw new NotSupportedException();
61+
}
62+
63+
public static IEnumerable<Func<Dummy, double>> VelocityWithStupidSiblingResult()
64+
{
65+
yield return v => v.Distance / v.Time;
66+
}
67+
5768
public static double VelocityWithInvalidSiblingResult(this Dummy value)
5869
{
5970
throw new NotSupportedException();
@@ -64,6 +75,16 @@ public static Func<Dummy, double> VelocityWithInvalidSiblingResult()
6475
return v => v.Distance / v.Time;
6576
}
6677

78+
public static double VelocityWithStupidSiblingSignature(this Dummy value)
79+
{
80+
throw new NotSupportedException();
81+
}
82+
83+
public static Expression<Func<Dummy, float>> VelocityWithStupidSiblingSignature()
84+
{
85+
return v => (float)(v.Distance / v.Time);
86+
}
87+
6788
public static double VelocityWithInvalidSiblingSignature(this Dummy value)
6889
{
6990
throw new NotSupportedException();

test/NeinLinq.Fakes/InjectableQuery/FunctionsBase.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ public double VelocityWithMethodMetadata(Dummy value)
2626
throw new NotSupportedException();
2727
}
2828

29+
public double VelocityWithStupidSiblingResult(Dummy value)
30+
{
31+
throw new NotSupportedException();
32+
}
33+
2934
public double VelocityWithInvalidSiblingResult(Dummy value)
3035
{
3136
throw new NotSupportedException();
3237
}
3338

39+
public double VelocityWithStupidSiblingSignature(Dummy value)
40+
{
41+
throw new NotSupportedException();
42+
}
43+
3444
public double VelocityWithInvalidSiblingSignature(Dummy value)
3545
{
3646
throw new NotSupportedException();
@@ -40,33 +50,33 @@ public double VelocityWithInvalidSiblingSignature(Dummy value)
4050
public double VelocityWithGenericArguments<TDummy, TOther>(TDummy value, TOther other)
4151
where TDummy : IDummy
4252
{
43-
throw new NotImplementedException();
53+
throw new NotSupportedException();
4454
}
4555

4656
[InjectLambda("fail-me")]
4757
public double VelocityWithGenericArguments<TDummy>(TDummy value, object other)
4858
where TDummy : IDummy
4959
{
50-
throw new NotImplementedException();
60+
throw new NotSupportedException();
5161
}
5262

5363
[InjectLambda("fail-me")]
5464
public double VelocityWithGenericArguments<TDummy>(object other)
5565
where TDummy : IDummy
5666
{
57-
throw new NotImplementedException();
67+
throw new NotSupportedException();
5868
}
5969

6070
public double VelocityWithGenericArguments<TDummy>(TDummy value)
6171
where TDummy : IDummy
6272
{
63-
throw new NotImplementedException();
73+
throw new NotSupportedException();
6474
}
6575

6676
public double VelocityWithInvalidGenericArguments<TDummy>(TDummy value)
6777
where TDummy : IDummy
6878
{
69-
throw new NotImplementedException();
79+
throw new NotSupportedException();
7080
}
7181
}
7282
}

test/NeinLinq.Fakes/InjectableQuery/IFunctions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ public interface IFunctions
1212
[InjectLambda("ignore-me")]
1313
double VelocityWithMethodMetadata(Dummy value);
1414

15+
double VelocityWithStupidSiblingResult(Dummy value);
16+
1517
double VelocityWithInvalidSiblingResult(Dummy value);
1618

19+
double VelocityWithStupidSiblingSignature(Dummy value);
20+
1721
double VelocityWithInvalidSiblingSignature(Dummy value);
1822

1923
double VelocityWithGenericArguments<TDummy>(TDummy value)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace NeinLinq.Tests.InjectableQuery
5+
{
6+
public class AttributeTest
7+
{
8+
[Fact]
9+
public void ShouldHandleInvalidArguments()
10+
{
11+
Assert.Throws<ArgumentNullException>(() => new InjectLambdaAttribute(default(string)));
12+
Assert.Throws<ArgumentNullException>(() => new InjectLambdaAttribute(default(Type)));
13+
Assert.Throws<ArgumentNullException>(() => new InjectLambdaAttribute(default(Type), "Narf"));
14+
Assert.Throws<ArgumentNullException>(() => new InjectLambdaAttribute(typeof(object), default(string)));
15+
}
16+
}
17+
}

test/NeinLinq.Tests/InjectableQuery/QueryBaseTest.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public void ShouldSucceedWithMethodMetadata()
5656
Assert.Equal(new[] { 200.0, .0, .1 }, result);
5757
}
5858

59+
[Fact]
60+
public void ShouldFailWithStupidSiblingResult()
61+
{
62+
var query = from d in data.ToInjectable(typeof(FunctionsBase))
63+
select functions.VelocityWithStupidSiblingResult(d);
64+
65+
var error = Assert.Throws<InvalidOperationException>(() => query.ToList());
66+
67+
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithStupidSiblingResult: returns no lambda expression.", error.Message);
68+
}
69+
5970
[Fact]
6071
public void ShouldFailWithInvalidSiblingResult()
6172
{
@@ -67,6 +78,17 @@ public void ShouldFailWithInvalidSiblingResult()
6778
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithInvalidSiblingResult: returns no lambda expression.", error.Message);
6879
}
6980

81+
[Fact]
82+
public void ShouldFailWithStupidSiblingSignature()
83+
{
84+
var query = from d in data.ToInjectable(typeof(FunctionsBase))
85+
select functions.VelocityWithStupidSiblingSignature(d);
86+
87+
var error = Assert.Throws<InvalidOperationException>(() => query.ToList());
88+
89+
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithStupidSiblingSignature: returns non-matching expression.", error.Message);
90+
}
91+
7092
[Fact]
7193
public void ShouldFailWithInvalidSiblingSignature()
7294
{
@@ -86,7 +108,7 @@ public void ShouldSucceedWithGenericArguments()
86108

87109
var result = query.ToList();
88110

89-
Assert.Equal(new[] { 200.0, .0, .125 }, result);
111+
Assert.Equal(new[] { 200.0, .0, .1 }, result);
90112
}
91113

92114
[Fact]

test/NeinLinq.Tests/InjectableQuery/QueryConcreteTest.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public void ShouldSucceedWithMethodMetadata()
5656
Assert.Equal(new[] { 200.0, .0, .1 }, result);
5757
}
5858

59+
[Fact]
60+
public void ShouldFailWithStupidSiblingResult()
61+
{
62+
var query = from d in data.ToInjectable(typeof(ConcreteFunctions))
63+
select functions.VelocityWithStupidSiblingResult(d);
64+
65+
var error = Assert.Throws<InvalidOperationException>(() => query.ToList());
66+
67+
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithStupidSiblingResult: returns no lambda expression.", error.Message);
68+
}
69+
5970
[Fact]
6071
public void ShouldFailWithInvalidSiblingResult()
6172
{
@@ -67,6 +78,17 @@ public void ShouldFailWithInvalidSiblingResult()
6778
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithInvalidSiblingResult: returns no lambda expression.", error.Message);
6879
}
6980

81+
[Fact]
82+
public void ShouldFailWithStupidSiblingSignature()
83+
{
84+
var query = from d in data.ToInjectable(typeof(ConcreteFunctions))
85+
select functions.VelocityWithStupidSiblingSignature(d);
86+
87+
var error = Assert.Throws<InvalidOperationException>(() => query.ToList());
88+
89+
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithStupidSiblingSignature: returns non-matching expression.", error.Message);
90+
}
91+
7092
[Fact]
7193
public void ShouldFailWithInvalidSiblingSignature()
7294
{
@@ -86,7 +108,7 @@ public void ShouldSucceedWithGenericArguments()
86108

87109
var result = query.ToList();
88110

89-
Assert.Equal(new[] { 200.0, .0, .125 }, result);
111+
Assert.Equal(new[] { 200.0, .0, .1 }, result);
90112
}
91113

92114
[Fact]

test/NeinLinq.Tests/InjectableQuery/QueryInterfaceTest.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public void ShouldSucceedWithMethodMetadata()
5656
Assert.Equal(new[] { 200.0, .0, .1 }, result);
5757
}
5858

59+
[Fact]
60+
public void ShouldFailWithStupidSiblingResult()
61+
{
62+
var query = from d in data.ToInjectable(typeof(IFunctions))
63+
select functions.VelocityWithStupidSiblingResult(d);
64+
65+
var error = Assert.Throws<InvalidOperationException>(() => query.ToList());
66+
67+
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithStupidSiblingResult: returns no lambda expression.", error.Message);
68+
}
69+
5970
[Fact]
6071
public void ShouldFailWithInvalidSiblingResult()
6172
{
@@ -67,6 +78,17 @@ public void ShouldFailWithInvalidSiblingResult()
6778
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithInvalidSiblingResult: returns no lambda expression.", error.Message);
6879
}
6980

81+
[Fact]
82+
public void ShouldFailWithStupidSiblingSignature()
83+
{
84+
var query = from d in data.ToInjectable(typeof(IFunctions))
85+
select functions.VelocityWithStupidSiblingSignature(d);
86+
87+
var error = Assert.Throws<InvalidOperationException>(() => query.ToList());
88+
89+
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.ConcreteFunctions.VelocityWithStupidSiblingSignature: returns non-matching expression.", error.Message);
90+
}
91+
7092
[Fact]
7193
public void ShouldFailWithInvalidSiblingSignature()
7294
{
@@ -86,7 +108,7 @@ public void ShouldSucceedWithGenericArguments()
86108

87109
var result = query.ToList();
88110

89-
Assert.Equal(new[] { 200.0, .0, .125 }, result);
111+
Assert.Equal(new[] { 200.0, .0, .1 }, result);
90112
}
91113

92114
[Fact]

test/NeinLinq.Tests/InjectableQuery/QueryPropertyTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ public void ShouldSucceedWithExternalProperty()
131131
Assert.Equal(new[] { 200.0, .0, .125 }, result);
132132
}
133133

134+
[Fact]
135+
public void ShouldFailWithStupidSiblingResult()
136+
{
137+
var query = from d in data.ToInjectable(typeof(Dummy))
138+
select d.VelocityWithStupidSiblingResult;
139+
140+
var error = Assert.Throws<InvalidOperationException>(() => query.ToList());
141+
142+
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.Dummy.VelocityWithStupidSiblingResultExpr: returns no lambda expression.", error.Message);
143+
}
144+
134145
[Fact]
135146
public void ShouldFailWithInvalidSiblingResult()
136147
{
@@ -142,6 +153,17 @@ public void ShouldFailWithInvalidSiblingResult()
142153
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.Dummy.VelocityWithInvalidSiblingResultExpr: returns no lambda expression.", error.Message);
143154
}
144155

156+
[Fact]
157+
public void ShouldFailWithStupidSiblingSignature()
158+
{
159+
var query = from d in data.ToInjectable(typeof(Dummy))
160+
select d.VelocityWithStupidSiblingSignature;
161+
162+
var error = Assert.Throws<InvalidOperationException>(() => query.ToList());
163+
164+
Assert.Equal("Unable to retrieve lambda expression from NeinLinq.Fakes.InjectableQuery.Dummy.VelocityWithStupidSiblingSignatureExpr: returns non-matching expression.", error.Message);
165+
}
166+
145167
[Fact]
146168
public void ShouldFailWithInvalidSiblingSignature()
147169
{

0 commit comments

Comments
 (0)