Skip to content

Commit 238f938

Browse files
committed
Handling empty blocks, re: issue #78 / Merging refactoring from source code generation branch
1 parent 31d760f commit 238f938

30 files changed

+198
-155
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
</Compile>
3636
</ItemGroup>
3737

38+
<ItemGroup>
39+
<Compile Remove="..\ReadableExpressions.UnitTests\ShouldExtensions.cs" />
40+
</ItemGroup>
41+
3842
<ItemGroup>
3943
<Reference Include="System" />
4044
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />

ReadableExpressions.UnitTests.NetCore3.1/ReadableExpressions.UnitTests.NetCore3.1.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
</Compile>
3232
</ItemGroup>
3333

34+
<ItemGroup>
35+
<Compile Remove="..\ReadableExpressions.UnitTests\ShouldExtensions.cs" />
36+
</ItemGroup>
37+
3438
<ItemGroup>
3539
<PackageReference Include="AgileObjects.NetStandardPolyfills" Version="1.4.1" />
3640
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />

ReadableExpressions.UnitTests/ShouldExtensions.cs renamed to ReadableExpressions.UnitTests/FluentAssertionExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using NetStandardPolyfills;
88
using ReadableExpressions.Extensions;
99

10-
public static class ShouldExtensions
10+
public static class FluentAssertionExtensions
1111
{
1212
public static void ShouldBeDefault<T>(this T value) => value.ShouldBe(default(T));
1313

@@ -54,7 +54,7 @@ public static void ShouldBe<TActual, TExpected>(this TActual value, TExpected ex
5454
}
5555
}
5656

57-
private static readonly MethodInfo _areEqualMethod = typeof(ShouldExtensions)
57+
private static readonly MethodInfo _areEqualMethod = typeof(FluentAssertionExtensions)
5858
.GetNonPublicStaticMethod("AreEqual");
5959

6060
private static bool AreEqual<TExpected, TActual>(TExpected expected, TActual actual)
@@ -203,11 +203,11 @@ public static void ShouldBeTrue(this bool? value)
203203
value.GetValueOrDefault().ShouldBeTrue();
204204
}
205205

206-
public static void ShouldBeTrue(this bool boolValue)
206+
public static void ShouldBeTrue(this bool boolValue, string errorMessage = null)
207207
{
208208
if (boolValue != true)
209209
{
210-
Asplode("true", "false");
210+
Asplode("true", "false", errorMessage);
211211
}
212212
}
213213

ReadableExpressions.UnitTests/WhenTranslatingBlocks.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,15 @@ public void ShouldIncludeAReturnKeywordForANewArrayInitStatement()
600600
translated.ShouldBe(EXPECTED.TrimStart());
601601
}
602602

603+
// See https://github.com/agileobjects/ReadableExpressions/issues/78
604+
[Fact]
605+
public void ShouldHandleAnEmptyBlock()
606+
{
607+
var emptyBlock = Block(Default(typeof(void)));
608+
609+
var translated = ToReadableString(emptyBlock);
610+
}
611+
603612
#region Helper Classes
604613

605614
private class Company

ReadableExpressions.UnitTests/WhenTranslatingConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ internal class GenericTwo<T> { }
485485
internal class Generic<T1, T2, T3> { }
486486
// ReSharper restore UnusedTypeParameter
487487

488-
internal enum OddNumber
488+
public enum OddNumber
489489
{
490490
One = 1
491491
}

ReadableExpressions.UnitTests/WhenTranslatingObjectCreations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public void ShouldTranslateAnAnonymousTypeCreationWithACustomFactory()
448448
// ReSharper disable once AssignNullToNotNullAttribute
449449
var creation = New(constructor, Constant(10));
450450

451-
var translated = ToReadableString(creation, cfg => cfg.NameAnonymousTypesUsing(t => "MyMagicObject"));
451+
var translated = ToReadableString(creation, s => s.NameAnonymousTypesUsing(t => "MyMagicObject"));
452452

453453
translated.ShouldBe("new MyMagicObject { ValueInt = 10 }");
454454
}

ReadableExpressions.Visualizers.ObjectSource/ExpressionVisualizerObjectSource.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Linq.Expressions;
66
using System.Reflection;
77
using Core.Configuration;
8-
using Extensions;
98
using Formatting;
109
using Translations.Formatting;
1110

@@ -52,7 +51,7 @@ internal static string GetTranslationFor(object target)
5251
}
5352
}
5453

55-
private static string Translate(Type type)
54+
private static string Translate(Type type)
5655
=> type.ToReadableString(ApplyDialogSettings);
5756

5857
private static TranslationFormattingSettings ApplyDialogSettings(
@@ -63,7 +62,7 @@ private static TranslationFormattingSettings ApplyDialogSettings(
6362
.IndentUsing(GetDialogSettings().Indent);
6463
}
6564

66-
private static TranslationSettings ApplyDialogSettings(TranslationSettings settings)
65+
private static TranslationSettings ApplyDialogSettings(TranslationSettings settings)
6766
=> GetDialogSettings().Update(settings.FormatUsing(_htmlFormatter));
6867

6968
private static VisualizerDialogSettings GetDialogSettings()

ReadableExpressions/ExpressionAnalysis.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public ExpressionAnalysis Finalise()
6767

6868
public ICollection<ParameterExpression> JoinedAssignmentVariables => _joinedAssignmentVariables;
6969

70-
public bool IsNotJoinedAssignment(Expression expression)
70+
public bool IsJoinedAssignment(Expression expression)
7171
{
72-
return (expression.NodeType != ExpressionType.Assign) ||
73-
_joinedAssignments?.Contains((BinaryExpression)expression) != true;
72+
return (expression.NodeType == ExpressionType.Assign) &&
73+
_joinedAssignments?.Contains((BinaryExpression)expression) == true;
7474
}
7575

7676
public bool IsCatchBlockVariable(Expression variable)

ReadableExpressions/ExpressionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static string ToReadableString(
4646
}
4747

4848
var settings = configuration.GetTranslationSettings();
49-
var translation = new ExpressionTreeTranslation(expression, settings);
49+
var translation = new ExpressionTranslation(expression, settings);
5050

5151
return translation.GetTranslation();
5252
}

ReadableExpressions/Extensions/InternalEnumerableExtensions.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,6 @@ public static IEnumerable<TResult> Project<TItem, TResult>(this IEnumerable<TIte
5858
}
5959
}
6060

61-
[DebuggerStepThrough]
62-
public static IList<T> Combine<T>(this ICollection<T> first, IList<T> second)
63-
{
64-
var secondCount = second.Count;
65-
var combined = new T[first.Count + secondCount];
66-
var index = 0;
67-
68-
foreach (var item in first)
69-
{
70-
combined[index] = item;
71-
72-
++index;
73-
}
74-
75-
for (var i = 0; i < secondCount;)
76-
{
77-
combined[index] = second[i];
78-
79-
++index;
80-
++i;
81-
}
82-
83-
return combined;
84-
}
85-
8661
[DebuggerStepThrough]
8762
public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool> predicate)
8863
{
@@ -104,14 +79,16 @@ public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool>
10479
[DebuggerStepThrough]
10580
public static T FirstOrDefault<T>(this IList<T> items, Func<T, bool> predicate)
10681
{
107-
for (var i = 0; i < items.Count; i++)
82+
for (var i = 0; i < items.Count;)
10883
{
10984
var item = items[i];
11085

11186
if (predicate.Invoke(item))
11287
{
11388
return item;
11489
}
90+
91+
++i;
11592
}
11693

11794
return default(T);

0 commit comments

Comments
 (0)