11namespace AgileObjects . ReadableExpressions . UnitTests
22{
33 using System ;
4+ using System . Collections . Generic ;
45 using NetStandardPolyfills ;
56#if ! NET35
67 using System . Linq . Expressions ;
78 using Xunit ;
9+ using static Issue22 ;
10+ using static Issue22 . InheritanceTests ;
811#else
912 using Expression = Microsoft . Scripting . Ast . Expression ;
1013 using Fact = NUnit . Framework . TestAttribute ;
@@ -499,6 +502,102 @@ public void ShouldTranslateEnumComparisonTests()
499502
500503 translated . ShouldBe ( "flag => (flag ? Test.One : Test.Two) == Test.Two" ) ;
501504 }
505+
506+ #if ! NET35
507+ [ Fact ]
508+ public void ShouldTranslateConditionalWithConditionalTest ( )
509+ {
510+ var dataContext = Expression . Parameter ( typeof ( IDataContext ) , "dctx" ) ;
511+ var dataReader = Expression . Parameter ( typeof ( IDataReader ) , "rd" ) ;
512+
513+ var ldr = Expression . Variable ( typeof ( SqLiteDataReader ) , "ldr" ) ;
514+
515+ var onEntityCreatedMethod = typeof ( TableContext )
516+ . GetPublicStaticMethod ( nameof ( TableContext . OnEntityCreated ) ) ;
517+
518+ var mapperBody = Expression . Block (
519+ new [ ] { ldr } ,
520+ Expression . Assign ( ldr , Expression . Convert ( dataReader , typeof ( SqLiteDataReader ) ) ) ,
521+ Expression . Condition (
522+ Expression . Equal (
523+ Expression . Condition (
524+ Expression . Call ( ldr , nameof ( SqLiteDataReader . IsDbNull ) , null , Expression . Constant ( 0 ) ) ,
525+ Expression . Constant ( TypeCodeEnum . Base ) ,
526+ Expression . Convert (
527+ Expression . Call ( ldr , nameof ( SqLiteDataReader . GetInt32 ) , null , Expression . Constant ( 0 ) ) ,
528+ typeof ( TypeCodeEnum ) ) ) ,
529+ Expression . Constant ( TypeCodeEnum . A1 ) ) ,
530+ Expression . Convert (
531+ Expression . Convert (
532+ Expression . Call (
533+ onEntityCreatedMethod ,
534+ dataContext ,
535+ Expression . MemberInit (
536+ Expression . New ( typeof ( InheritanceA1 ) ) ,
537+ Expression . Bind (
538+ typeof ( InheritanceA1 ) . GetPublicInstanceProperty ( nameof ( InheritanceBase . GuidValue ) ) ,
539+ Expression . Condition (
540+ Expression . Call ( ldr , nameof ( SqLiteDataReader . IsDbNull ) , null , Expression . Constant ( 1 ) ) ,
541+ Expression . Constant ( Guid . Empty ) ,
542+ Expression . Call ( ldr , nameof ( SqLiteDataReader . GetGuid ) , null , Expression . Constant ( 1 ) ) ) )
543+ )
544+ ) ,
545+ typeof ( InheritanceA1 ) ) ,
546+ typeof ( InheritanceA ) ) ,
547+ Expression . Convert (
548+ Expression . Convert (
549+ Expression . Call (
550+ onEntityCreatedMethod ,
551+ dataContext ,
552+ Expression . MemberInit (
553+ Expression . New ( typeof ( InheritanceA2 ) ) ,
554+ Expression . Bind (
555+ typeof ( InheritanceA2 ) . GetPublicInstanceProperty ( nameof ( InheritanceBase . GuidValue ) ) ,
556+ Expression . Condition (
557+ Expression . Call ( ldr , nameof ( SqLiteDataReader . IsDbNull ) , null , Expression . Constant ( 1 ) ) ,
558+ Expression . Constant ( Guid . Empty ) ,
559+ Expression . Call ( ldr , nameof ( SqLiteDataReader . GetGuid ) , null , Expression . Constant ( 1 ) ) ) )
560+ )
561+ ) ,
562+ typeof ( InheritanceA2 ) ) ,
563+ typeof ( InheritanceA ) ) ) ) ;
564+
565+ var mapper = Expression . Lambda < Func < IDataContext , IDataReader , InheritanceA > > (
566+ mapperBody ,
567+ dataContext ,
568+ dataReader ) ;
569+
570+ var body = Expression . Invoke ( mapper , dataContext , dataReader ) ;
571+
572+ var lambda = Expression . Lambda < Func < IDataContext , IDataReader , InheritanceA > > ( body , dataContext , dataReader ) ;
573+
574+ const string EXPECTED = @"
575+ (dctx, rd) => ((dctx, rd) =>
576+ {
577+ var ldr = (Issue22.SqLiteDataReader)rd;
578+
579+ return ((ldr.IsDbNull(0)
580+ ? Issue22.InheritanceTests.TypeCodeEnum.Base
581+ : (Issue22.InheritanceTests.TypeCodeEnum)ldr.GetInt32(0)) == Issue22.InheritanceTests.TypeCodeEnum.A1)
582+ ? (Issue22.InheritanceTests.InheritanceA)((Issue22.InheritanceTests.InheritanceA1)Issue22.TableContext.OnEntityCreated(
583+ dctx,
584+ new Issue22.InheritanceTests.InheritanceA1
585+ {
586+ GuidValue = ldr.IsDbNull(1) ? default(Guid) : ldr.GetGuid(1)
587+ }))
588+ : (Issue22.InheritanceTests.InheritanceA)((Issue22.InheritanceTests.InheritanceA2)Issue22.TableContext.OnEntityCreated(
589+ dctx,
590+ new Issue22.InheritanceTests.InheritanceA2
591+ {
592+ GuidValue = ldr.IsDbNull(1) ? default(Guid) : ldr.GetGuid(1)
593+ }));
594+ }).Invoke(dctx, rd)" ;
595+
596+ var translated = ToReadableString ( lambda ) ;
597+
598+ translated . ShouldBe ( EXPECTED . TrimStart ( ) ) ;
599+ }
600+ #endif
502601 }
503602
504603 #region Helpers
@@ -513,5 +612,71 @@ public bool MultipleParameterMethod(string stringValue, int intValue)
513612
514613 internal enum Test { One , Two } ;
515614
615+ #if ! NET35
616+ internal static class Issue22
617+ {
618+ public interface IDataContext
619+ {
620+ }
621+
622+ public interface IDataReader
623+ {
624+ }
625+
626+ public class SqLiteDataReader : IDataReader
627+ {
628+ public bool IsDbNull ( int idx ) => default ( bool ) ;
629+
630+ public int GetInt32 ( int idx ) => default ( int ) ;
631+
632+ public Guid GetGuid ( int idx ) => default ( Guid ) ;
633+ }
634+
635+ public static class InheritanceTests
636+ {
637+ public enum TypeCodeEnum
638+ {
639+ Base ,
640+ A ,
641+ A1 ,
642+ A2 ,
643+ }
644+
645+ public abstract class InheritanceBase
646+ {
647+ public Guid GuidValue { get ; set ; }
648+ }
649+
650+ public abstract class InheritanceA : InheritanceBase
651+ {
652+ public List < InheritanceB > Bs { get ; set ; }
653+ }
654+
655+ public class InheritanceB : InheritanceBase
656+ {
657+ }
658+
659+ public class InheritanceA2 : InheritanceA
660+ {
661+ }
662+
663+ public class InheritanceA1 : InheritanceA
664+ {
665+ }
666+ }
667+
668+ public class TableContext
669+ {
670+ public static object OnEntityCreated ( IDataContext context , object entity ) => entity ;
671+ }
672+
673+ public enum Test
674+ {
675+ One ,
676+ Two
677+ }
678+ }
679+ #endif
680+
516681 #endregion
517682}
0 commit comments