Skip to content

Commit d9f0c07

Browse files
committed
Naming unnamed variables and parameters in translations - fixed issue #21
1 parent 6bd7898 commit d9f0c07

File tree

10 files changed

+435
-82
lines changed

10 files changed

+435
-82
lines changed

ReadableExpressions.UnitTests.Net35/ReadableExpressions.UnitTests.Net35.csproj

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -64,83 +64,8 @@
6464
</Compile>
6565
</ItemGroup>
6666
<ItemGroup>
67-
<Compile Include="..\ReadableExpressions.UnitTests\Extensions\WhenGettingAParentExpressionNode.cs">
68-
<Link>Extensions\WhenGettingAParentExpressionNode.cs</Link>
69-
</Compile>
70-
<Compile Include="..\ReadableExpressions.UnitTests\Extensions\WhenGettingFriendlyNames.cs">
71-
<Link>Extensions\WhenGettingFriendlyNames.cs</Link>
72-
</Compile>
73-
<Compile Include="..\ReadableExpressions.UnitTests\Properties\AssemblyInfo.cs">
74-
<Link>Properties\AssemblyInfo.cs</Link>
75-
</Compile>
76-
<Compile Include="..\ReadableExpressions.UnitTests\ShouldExtensions.cs">
77-
<Link>ShouldExtensions.cs</Link>
78-
</Compile>
79-
<Compile Include="..\ReadableExpressions.UnitTests\TestClassBase.cs">
80-
<Link>TestClassBase.cs</Link>
81-
</Compile>
82-
<Compile Include="..\ReadableExpressions.UnitTests\WhenFormattingCode.cs">
83-
<Link>WhenFormattingCode.cs</Link>
84-
</Compile>
85-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingAssignments.cs">
86-
<Link>WhenTranslatingAssignments.cs</Link>
87-
</Compile>
88-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingBlocks.cs">
89-
<Link>WhenTranslatingBlocks.cs</Link>
90-
</Compile>
91-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingComments.cs">
92-
<Link>WhenTranslatingComments.cs</Link>
93-
</Compile>
94-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingComparisons.cs">
95-
<Link>WhenTranslatingComparisons.cs</Link>
96-
</Compile>
97-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingConditionals.cs">
98-
<Link>WhenTranslatingConditionals.cs</Link>
99-
</Compile>
100-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingConstants.cs">
101-
<Link>WhenTranslatingConstants.cs</Link>
102-
</Compile>
103-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingConversions.cs">
104-
<Link>WhenTranslatingConversions.cs</Link>
105-
</Compile>
106-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingDebugInfo.cs">
107-
<Link>WhenTranslatingDebugInfo.cs</Link>
108-
</Compile>
109-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingDynamicOperations.cs">
110-
<Link>WhenTranslatingDynamicOperations.cs</Link>
111-
</Compile>
112-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingGotos.cs">
113-
<Link>WhenTranslatingGotos.cs</Link>
114-
</Compile>
115-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingLambdas.cs">
116-
<Link>WhenTranslatingLambdas.cs</Link>
117-
</Compile>
118-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingLanguageKeywords.cs">
119-
<Link>WhenTranslatingLanguageKeywords.cs</Link>
120-
</Compile>
121-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingLogicalOperations.cs">
122-
<Link>WhenTranslatingLogicalOperations.cs</Link>
123-
</Compile>
124-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingLoops.cs">
125-
<Link>WhenTranslatingLoops.cs</Link>
126-
</Compile>
127-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingMathsOperations.cs">
128-
<Link>WhenTranslatingMathsOperations.cs</Link>
129-
</Compile>
130-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingMemberAccesses.cs">
131-
<Link>WhenTranslatingMemberAccesses.cs</Link>
132-
</Compile>
133-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingObjectCreations.cs">
134-
<Link>WhenTranslatingObjectCreations.cs</Link>
135-
</Compile>
136-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingStringConcatenation.cs">
137-
<Link>WhenTranslatingStringConcatenation.cs</Link>
138-
</Compile>
139-
<Compile Include="..\ReadableExpressions.UnitTests\WhenTranslatingTryCatchFinallys.cs">
140-
<Link>WhenTranslatingTryCatchFinallys.cs</Link>
141-
</Compile>
142-
<Compile Include="..\ReadableExpressions.UnitTests\WhenUsingPartialTrust.cs">
143-
<Link>WhenUsingPartialTrust.cs</Link>
67+
<Compile Include="..\ReadableExpressions.UnitTests\**\*.cs" Exclude="..\ReadableExpressions.UnitTests\obj\**\*.cs;..\ReadableExpressions.UnitTests\WhenUsingPartialTrust.cs;">
68+
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
14469
</Compile>
14570
</ItemGroup>
14671
<ItemGroup>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
namespace AgileObjects.ReadableExpressions.UnitTests.Extensions
2+
{
3+
using System.Collections.Generic;
4+
using ReadableExpressions.Extensions;
5+
#if !NET35
6+
using Xunit;
7+
#else
8+
using Fact = NUnit.Framework.TestAttribute;
9+
10+
[NUnit.Framework.TestFixture]
11+
#endif
12+
public class WhenGeneratingVariableNames
13+
{
14+
[Fact]
15+
public void ShouldNameAVariableForAnArrayType()
16+
{
17+
typeof(Box[]).GetVariableNameInCamelCase().ShouldBe("boxArray");
18+
}
19+
20+
[Fact]
21+
public void ShouldNameAVariableForACollectionTypeEndingInX()
22+
{
23+
typeof(ICollection<Box>).GetVariableNameInCamelCase().ShouldBe("boxes");
24+
}
25+
26+
[Fact]
27+
public void ShouldNameAVariableForAnEnumerableTypeEndingInZ()
28+
{
29+
typeof(IEnumerable<Fuzz>).GetVariableNameInPascalCase().ShouldBe("Fuzzes");
30+
}
31+
32+
[Fact]
33+
public void ShouldNameAVariableForAnEnumerableTypeEndingInDoubleS()
34+
{
35+
typeof(IEnumerable<Glass>).GetVariableNameInPascalCase().ShouldBe("Glasses");
36+
}
37+
38+
[Fact]
39+
public void ShouldNameAVariableForAListTypeEndingInCh()
40+
{
41+
typeof(List<Church>).GetVariableNameInCamelCase().ShouldBe("churches");
42+
}
43+
44+
[Fact]
45+
public void ShouldNameAVariableForAListTypeEndingInSh()
46+
{
47+
typeof(List<Hush>).GetVariableNameInCamelCase().ShouldBe("hushes");
48+
}
49+
50+
[Fact]
51+
public void ShouldNameAVariableForAListTypeEndingInVowelY()
52+
{
53+
typeof(List<Journey>).GetVariableNameInCamelCase().ShouldBe("journeys");
54+
}
55+
56+
[Fact]
57+
public void ShouldNameAVariableForAnIListTypeEndingInConsonantY()
58+
{
59+
typeof(IList<Body>).GetVariableNameInPascalCase().ShouldBe("Bodies");
60+
}
61+
62+
[Fact]
63+
public void ShouldNameANullableLongVariable()
64+
{
65+
typeof(long?).GetVariableNameInCamelCase().ShouldBe("nullableLong");
66+
}
67+
68+
[Fact]
69+
public void ShouldNameAnArrayOfArraysVariable()
70+
{
71+
typeof(int?[][]).GetVariableNameInCamelCase().ShouldBe("nullableIntArrayArray");
72+
}
73+
74+
// ReSharper disable ClassNeverInstantiated.Local
75+
private class Box { }
76+
77+
private class Fuzz { }
78+
79+
private class Glass { }
80+
81+
private class Church { }
82+
83+
private class Hush { }
84+
85+
private class Journey { }
86+
87+
private class Body { }
88+
// ReSharper restore ClassNeverInstantiated.Local
89+
}
90+
}

ReadableExpressions.UnitTests/ReadableExpressions.UnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@
112112
<Compile Include="..\VersionInfo.cs">
113113
<Link>VersionInfo.cs</Link>
114114
</Compile>
115+
<Compile Include="Extensions\WhenGeneratingVariableNames.cs" />
115116
<Compile Include="Extensions\WhenGettingAParentExpressionNode.cs" />
116117
<Compile Include="Extensions\WhenGettingFriendlyNames.cs" />
117118
<Compile Include="ShouldExtensions.cs" />
118119
<Compile Include="TestClassBase.cs" />
120+
<Compile Include="WhenTranslatingVariableNames.cs" />
119121
<Compile Include="WhenUsingPartialTrust.cs" />
120122
<Compile Include="WhenTranslatingStringConcatenation.cs" />
121123
<Compile Include="WhenTranslatingComments.cs" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace AgileObjects.ReadableExpressions.UnitTests
2+
{
3+
#if !NET35
4+
using System.Linq.Expressions;
5+
using Xunit;
6+
#else
7+
using Expression = Microsoft.Scripting.Ast.Expression;
8+
using Fact = NUnit.Framework.TestAttribute;
9+
using MethodCallExpression = Microsoft.Scripting.Ast.MethodCallExpression;
10+
using ParameterExpression = Microsoft.Scripting.Ast.ParameterExpression;
11+
12+
[NUnit.Framework.TestFixture]
13+
#endif
14+
public class WhenTranslatingVariableNames : TestClassBase
15+
{
16+
// See https://github.com/agileobjects/ReadableExpressions/issues/21
17+
[Fact]
18+
public void ShouldNameAnUnnamedVariable()
19+
{
20+
var intVariable = Expression.Variable(typeof(int));
21+
var assignDefaultToInt = Expression.Assign(intVariable, Expression.Default(typeof(int)));
22+
23+
var translated = ToReadableString(assignDefaultToInt);
24+
25+
translated.ShouldBe("@int = default(int)");
26+
}
27+
28+
[Fact]
29+
public void ShouldNameAnUnnamedParameter()
30+
{
31+
var stringParameter = Expression.Parameter(typeof(string), string.Empty);
32+
var stringVariable = Expression.Variable(typeof(string), " ");
33+
var assignVariableToParameter = Expression.Assign(stringVariable, stringParameter);
34+
35+
var translated = ToReadableString(assignVariableToParameter);
36+
37+
translated.ShouldBe("string1 = string2");
38+
}
39+
}
40+
}

ReadableExpressions/Extensions/InternalEnumerableExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
#if NET35
67
using System.Linq;
78
#endif
89

910
internal static class InternalEnumerableExtensions
1011
{
12+
[DebuggerStepThrough]
1113
public static IEnumerable<TResult> Project<TItem, TResult>(this IEnumerable<TItem> items, Func<TItem, TResult> projector)
1214
{
1315
foreach (var item in items)
@@ -16,6 +18,7 @@ public static IEnumerable<TResult> Project<TItem, TResult>(this IEnumerable<TIte
1618
}
1719
}
1820

21+
[DebuggerStepThrough]
1922
public static IEnumerable<TResult> Project<TItem, TResult>(this IEnumerable<TItem> items, Func<TItem, int, TResult> projector)
2023
{
2124
var i = 0;
@@ -26,6 +29,7 @@ public static IEnumerable<TResult> Project<TItem, TResult>(this IEnumerable<TIte
2629
}
2730
}
2831

32+
[DebuggerStepThrough]
2933
public static IEnumerable<T> Combine<T>(this IEnumerable<T> first, IEnumerable<T> second)
3034
{
3135
foreach (var item in first)
@@ -39,6 +43,7 @@ public static IEnumerable<T> Combine<T>(this IEnumerable<T> first, IEnumerable<T
3943
}
4044
}
4145

46+
[DebuggerStepThrough]
4247
public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool> predicate)
4348
{
4449
foreach (var item in items)
@@ -50,7 +55,29 @@ public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool>
5055
}
5156
}
5257

58+
[DebuggerStepThrough]
59+
public static T First<T>(this IList<T> items) => items[0];
60+
61+
[DebuggerStepThrough]
62+
public static T Last<T>(this IList<T> items) => items[items.Count - 1];
63+
64+
[DebuggerStepThrough]
65+
public static T FirstOrDefault<T>(this IList<T> items, Func<T, bool> predicate)
66+
{
67+
for (var i = 0; i < items.Count; i++)
68+
{
69+
var item = items[i];
70+
71+
if (predicate.Invoke(item))
72+
{
73+
return item;
74+
}
75+
}
76+
77+
return default(T);
78+
}
5379
#if NET35
80+
[DebuggerStepThrough]
5481
public static bool Contains<TContained, TItem>(this ICollection<TContained> items, TItem item)
5582
where TContained : TItem
5683
{

0 commit comments

Comments
 (0)