Skip to content

Commit a83b98c

Browse files
committed
Tidying tests / Fixing object-typed null value binary translations, re: #147
1 parent f7b441f commit a83b98c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4714
-4942
lines changed

src/ReadableExpressions/Translations/BinaryTranslation.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,19 @@ private static Expression GetEnumValue(
219219
}
220220

221221
var value = ((ConstantExpression)expression).Value;
222+
223+
if (value == null)
224+
{
225+
return expression;
226+
}
227+
222228
var enumType = enumValueType.GetNonNullableType();
229+
230+
if (expression.Type.GetNonNullableType() == enumType)
231+
{
232+
return expression;
233+
}
234+
223235
var enumValue = Enum.Parse(enumType, value.ToString());
224236
return Expression.Constant(enumValue, enumValueType);
225237
}
Lines changed: 111 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,114 @@
1-
namespace AgileObjects.ReadableExpressions.UnitTests.Extensions
2-
{
3-
using System.Collections;
4-
using System.Collections.Generic;
5-
using Common;
6-
using ReadableExpressions.Extensions;
7-
#if !NET35
8-
using Xunit;
9-
#else
10-
using Fact = NUnit.Framework.TestAttribute;
11-
12-
[NUnit.Framework.TestFixture]
1+
namespace AgileObjects.ReadableExpressions.UnitTests.Extensions;
2+
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using ReadableExpressions.Extensions;
6+
7+
#if NET35
8+
[NUnitTestFixture]
139
#endif
14-
public class WhenGeneratingVariableNames
10+
public class WhenGeneratingVariableNames
11+
{
12+
[Fact]
13+
public void ShouldNameAStringVariable()
14+
{
15+
typeof(string).GetVariableNameInCamelCase().ShouldBe("string");
16+
}
17+
18+
[Fact]
19+
public void ShouldNameAnArrayTypeVariable()
20+
{
21+
typeof(Box[]).GetVariableNameInCamelCase().ShouldBe("boxArray");
22+
}
23+
24+
[Fact]
25+
public void ShouldNameAnIEnumerableTypeVariable()
26+
{
27+
typeof(IEnumerable<Fuzz>).GetVariableNameInPascalCase().ShouldBe("FuzzIEnumerable");
28+
}
29+
30+
[Fact]
31+
public void ShouldNameAnICollectionTypeVariable()
32+
{
33+
typeof(ICollection<Box>).GetVariableNameInCamelCase().ShouldBe("boxICollection");
34+
}
35+
36+
[Fact]
37+
public void ShouldNameAnIListTypeVariable()
38+
{
39+
typeof(IList<Body>).GetVariableNameInPascalCase().ShouldBe("BodyIList");
40+
}
41+
42+
[Fact]
43+
public void ShouldNameAListTypeVariable()
44+
{
45+
typeof(List<Church>).GetVariableNameInCamelCase().ShouldBe("churchList");
46+
}
47+
48+
[Fact]
49+
public void ShouldNameAHashSetTypeVariable()
50+
{
51+
typeof(HashSet<int>).GetVariableNameInCamelCase().ShouldBe("intHashSet");
52+
}
53+
54+
[Fact]
55+
public void ShouldNameAnArrayListVariable()
1556
{
16-
[Fact]
17-
public void ShouldNameAStringVariable()
18-
{
19-
typeof(string).GetVariableNameInCamelCase().ShouldBe("string");
20-
}
21-
22-
[Fact]
23-
public void ShouldNameAnArrayTypeVariable()
24-
{
25-
typeof(Box[]).GetVariableNameInCamelCase().ShouldBe("boxArray");
26-
}
27-
28-
[Fact]
29-
public void ShouldNameAnIEnumerableTypeVariable()
30-
{
31-
typeof(IEnumerable<Fuzz>).GetVariableNameInPascalCase().ShouldBe("FuzzIEnumerable");
32-
}
33-
34-
[Fact]
35-
public void ShouldNameAnICollectionTypeVariable()
36-
{
37-
typeof(ICollection<Box>).GetVariableNameInCamelCase().ShouldBe("boxICollection");
38-
}
39-
40-
[Fact]
41-
public void ShouldNameAnIListTypeVariable()
42-
{
43-
typeof(IList<Body>).GetVariableNameInPascalCase().ShouldBe("BodyIList");
44-
}
45-
46-
[Fact]
47-
public void ShouldNameAListTypeVariable()
48-
{
49-
typeof(List<Church>).GetVariableNameInCamelCase().ShouldBe("churchList");
50-
}
51-
52-
[Fact]
53-
public void ShouldNameAHashSetTypeVariable()
54-
{
55-
typeof(HashSet<int>).GetVariableNameInCamelCase().ShouldBe("intHashSet");
56-
}
57-
58-
[Fact]
59-
public void ShouldNameAnArrayListVariable()
60-
{
61-
typeof(ArrayList).GetVariableNameInCamelCase().ShouldBe("arrayList");
62-
}
63-
64-
[Fact]
65-
public void ShouldNameADictionaryTypeVariable()
66-
{
67-
typeof(Dictionary<string, Church>).GetVariableNameInCamelCase().ShouldBe("stringChurchDictionary");
68-
}
69-
70-
[Fact]
71-
public void ShouldNameANullableLongVariable()
72-
{
73-
typeof(long?).GetVariableNameInCamelCase().ShouldBe("nullableLong");
74-
}
75-
76-
[Fact]
77-
public void ShouldNameAnArrayOfArraysVariable()
78-
{
79-
typeof(int?[][]).GetVariableNameInCamelCase().ShouldBe("nullableIntArrayArray");
80-
}
81-
82-
// See https://github.com/agileobjects/ReadableExpressions/issues/48
83-
[Fact]
84-
public void ShouldNameAGenericTypeInnerClassVariable()
85-
{
86-
typeof(Issue48<int>.Inner)
87-
.GetVariableNameInPascalCase()
88-
.ShouldBe($"{nameof(WhenGeneratingVariableNames)}_IntIssue48_Inner");
89-
}
90-
91-
[Fact]
92-
public void ShouldNameAGenericGenericTypeArgument()
93-
{
94-
typeof(Dictionary<int, Dictionary<string, List<byte>>>)
95-
.GetVariableNameInPascalCase()
96-
.ShouldBe("IntStringByteListDictionaryDictionary");
97-
}
98-
99-
#region Helper Members
100-
101-
// ReSharper disable ClassNeverInstantiated.Local
102-
private class Box { }
103-
104-
private class Fuzz { }
105-
106-
private class Church { }
107-
108-
private class Body { }
109-
110-
// ReSharper disable once UnusedTypeParameter
111-
private class Issue48<T>
112-
{
113-
public class Inner { }
114-
}
115-
// ReSharper restore ClassNeverInstantiated.Local
116-
117-
#endregion
57+
typeof(ArrayList).GetVariableNameInCamelCase().ShouldBe("arrayList");
11858
}
119-
}
59+
60+
[Fact]
61+
public void ShouldNameADictionaryTypeVariable()
62+
{
63+
typeof(Dictionary<string, Church>).GetVariableNameInCamelCase().ShouldBe("stringChurchDictionary");
64+
}
65+
66+
[Fact]
67+
public void ShouldNameANullableLongVariable()
68+
{
69+
typeof(long?).GetVariableNameInCamelCase().ShouldBe("nullableLong");
70+
}
71+
72+
[Fact]
73+
public void ShouldNameAnArrayOfArraysVariable()
74+
{
75+
typeof(int?[][]).GetVariableNameInCamelCase().ShouldBe("nullableIntArrayArray");
76+
}
77+
78+
// See https://github.com/agileobjects/ReadableExpressions/issues/48
79+
[Fact]
80+
public void ShouldNameAGenericTypeInnerClassVariable()
81+
{
82+
typeof(Issue48<int>.Inner)
83+
.GetVariableNameInPascalCase()
84+
.ShouldBe($"{nameof(WhenGeneratingVariableNames)}_IntIssue48_Inner");
85+
}
86+
87+
[Fact]
88+
public void ShouldNameAGenericGenericTypeArgument()
89+
{
90+
typeof(Dictionary<int, Dictionary<string, List<byte>>>)
91+
.GetVariableNameInPascalCase()
92+
.ShouldBe("IntStringByteListDictionaryDictionary");
93+
}
94+
95+
#region Helper Members
96+
97+
// ReSharper disable ClassNeverInstantiated.Local
98+
private class Box { }
99+
100+
private class Fuzz { }
101+
102+
private class Church { }
103+
104+
private class Body { }
105+
106+
// ReSharper disable once UnusedTypeParameter
107+
private class Issue48<T>
108+
{
109+
public class Inner { }
110+
}
111+
// ReSharper restore ClassNeverInstantiated.Local
112+
113+
#endregion
114+
}
Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,53 @@
1-
namespace AgileObjects.ReadableExpressions.UnitTests.Extensions
2-
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using Common;
7-
using NetStandardPolyfills;
8-
using ReadableExpressions.Extensions;
9-
#if !NET35
10-
using System.Linq.Expressions;
11-
using Xunit;
12-
#else
13-
using Microsoft.Scripting.Ast;
14-
using Fact = NUnit.Framework.TestAttribute;
15-
16-
[NUnit.Framework.TestFixture]
1+
namespace AgileObjects.ReadableExpressions.UnitTests.Extensions;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using ReadableExpressions.Extensions;
7+
8+
#if NET35
9+
[NUnitTestFixture]
1710
#endif
18-
public class WhenGettingAParentExpressionNode : TestClassBase
11+
public class WhenGettingAParentExpressionNode : TestClassBase
12+
{
13+
[Fact]
14+
public void ShouldReturnAMemberAccessParent()
1915
{
20-
[Fact]
21-
public void ShouldReturnAMemberAccessParent()
22-
{
23-
var personViewModelName = CreateLambda((Type t) => t.Name);
16+
var personViewModelName = CreateLambda((Type t) => t.Name);
2417

25-
var namePropertyParent = personViewModelName.Body.GetParentOrNull() as ParameterExpression;
18+
var namePropertyParent = personViewModelName.Body.GetParentOrNull() as ParameterExpression;
2619

27-
namePropertyParent.ShouldNotBeNull().Name.ShouldBe("t");
28-
}
20+
namePropertyParent.ShouldNotBeNull().Name.ShouldBe("t");
21+
}
2922

30-
[Fact]
31-
public void ShouldReturnANestedMemberAccessParent()
32-
{
33-
var typeNameLength = CreateLambda((Type t) => t.Name.Length);
23+
[Fact]
24+
public void ShouldReturnANestedMemberAccessParent()
25+
{
26+
var typeNameLength = CreateLambda((Type t) => t.Name.Length);
3427

35-
var typeNameLengthParent = typeNameLength.Body.GetParentOrNull() as MemberExpression;
28+
var typeNameLengthParent = typeNameLength.Body.GetParentOrNull() as MemberExpression;
3629

37-
typeNameLengthParent.ShouldNotBeNull().Member.Name.ShouldBe("Name");
38-
}
30+
typeNameLengthParent.ShouldNotBeNull().Member.Name.ShouldBe("Name");
31+
}
3932

40-
[Fact]
41-
public void ShouldReturnAMemberMethodCallParent()
42-
{
43-
var typeNameSubstring = CreateLambda((Type t) => t.Name.Substring(0, 3));
33+
[Fact]
34+
public void ShouldReturnAMemberMethodCallParent()
35+
{
36+
var typeNameSubstring = CreateLambda((Type t) => t.Name.Substring(0, 3));
4437

45-
var typeNameSubstringParent = typeNameSubstring.Body.GetParentOrNull() as MemberExpression;
38+
var typeNameSubstringParent = typeNameSubstring.Body.GetParentOrNull() as MemberExpression;
4639

47-
typeNameSubstringParent.ShouldNotBeNull().Member.Name.ShouldBe("Name");
48-
}
40+
typeNameSubstringParent.ShouldNotBeNull().Member.Name.ShouldBe("Name");
41+
}
4942

50-
[Fact]
51-
public void ShouldReturnAnExtensionMethodCallParent()
52-
{
53-
var typesToArray = CreateLambda((IEnumerable<Type> ts) => ts.ToArray());
43+
[Fact]
44+
public void ShouldReturnAnExtensionMethodCallParent()
45+
{
46+
var typesToArray = CreateLambda((IEnumerable<Type> ts) => ts.ToArray());
5447

55-
var typesToArrayPropertyParent = typesToArray.Body.GetParentOrNull() as ParameterExpression;
48+
var typesToArrayPropertyParent = typesToArray.Body.GetParentOrNull() as ParameterExpression;
5649

57-
typesToArrayPropertyParent.ShouldNotBeNull();
58-
typesToArrayPropertyParent.Name.ShouldBe("ts");
59-
}
50+
typesToArrayPropertyParent.ShouldNotBeNull();
51+
typesToArrayPropertyParent!.Name.ShouldBe("ts");
6052
}
61-
}
53+
}

0 commit comments

Comments
 (0)