@@ -593,4 +593,127 @@ let ``check on-demand production of members``() =
593593 Assert.Equal( 5 , containersType.GetMethods( bindAll) .Length) // 5 properties, 5 getters for properties
594594 Assert.Equal( 5 , containersType.GetProperties( bindAll) .Length) // 5 properties, 5 getters for properties
595595 Assert.Equal( 0 , containersType.GetFields( bindAll) .Length) // 5 properties, 5 getters for properties
596- Assert.Equal( 0 , containersType.GetEvents( bindAll) .Length) // 5 properties, 5 getters for properties
596+ Assert.Equal( 0 , containersType.GetEvents( bindAll) .Length) // 5 properties, 5 getters for properties
597+
598+ // ---------------------------------------------------------------------------
599+ // Tests for type definition properties: nonNullable, hideObjectMethods
600+ // Addresses https://github.com/fsprojects/FSharp.TypeProviders.SDK/issues/170
601+ // ---------------------------------------------------------------------------
602+
603+ [<Fact>]
604+ let ``nonNullable = true sets NonNullable property and adds AllowNullLiteralAttribute`` () =
605+ let refs = Targets.DotNetStandard20FSharpRefs()
606+ let config = Testing.MakeSimulatedTypeProviderConfig( resolutionFolder=__ SOURCE_ DIRECTORY__, runtimeAssembly= " whatever.dll" , runtimeAssemblyRefs= refs)
607+ use _tp = new TypeProviderForNamespaces( config)
608+ let asm = Assembly.GetExecutingAssembly()
609+
610+ let t = ProvidedTypeDefinition( asm, " Test.Namespace" , " NonNullableType" , Some typeof< obj>, nonNullable = true )
611+ Assert.True( t.NonNullable, " NonNullable property should be true" )
612+ let attrs = t.GetCustomAttributesData()
613+ Assert.True(
614+ attrs |> Seq.exists ( fun a -> a.Constructor.DeclaringType.Name = typeof< AllowNullLiteralAttribute>. Name),
615+ " Expected AllowNullLiteralAttribute to be present when nonNullable=true" )
616+
617+ [<Fact>]
618+ let ``nonNullable = false ( default) does not add AllowNullLiteralAttribute``() =
619+ let refs = Targets.DotNetStandard20FSharpRefs()
620+ let config = Testing.MakeSimulatedTypeProviderConfig( resolutionFolder=__ SOURCE_ DIRECTORY__, runtimeAssembly= " whatever.dll" , runtimeAssemblyRefs= refs)
621+ use _tp = new TypeProviderForNamespaces( config)
622+ let asm = Assembly.GetExecutingAssembly()
623+
624+ let t = ProvidedTypeDefinition( asm, " Test.Namespace" , " NullableType" , Some typeof< obj>)
625+ Assert.False( t.NonNullable, " NonNullable property should default to false" )
626+ let attrs = t.GetCustomAttributesData()
627+ Assert.False(
628+ attrs |> Seq.exists ( fun a -> a.Constructor.DeclaringType.Name = typeof< AllowNullLiteralAttribute>. Name),
629+ " AllowNullLiteralAttribute should not be present when nonNullable is not set" )
630+
631+ [<Fact>]
632+ let ``hideObjectMethods = true sets HideObjectMethods property``() =
633+ let refs = Targets.DotNetStandard20FSharpRefs()
634+ let config = Testing.MakeSimulatedTypeProviderConfig( resolutionFolder=__ SOURCE_ DIRECTORY__, runtimeAssembly= " whatever.dll" , runtimeAssemblyRefs= refs)
635+ use _tp = new TypeProviderForNamespaces( config)
636+ let asm = Assembly.GetExecutingAssembly()
637+
638+ let t = ProvidedTypeDefinition( asm, " Test.Namespace" , " HiddenObjectType" , Some typeof< obj>, hideObjectMethods = true )
639+ Assert.True( t.HideObjectMethods, " HideObjectMethods should be true when hideObjectMethods=true" )
640+
641+ [<Fact>]
642+ let ``hideObjectMethods = false ( default) leaves HideObjectMethods false ``() =
643+ let refs = Targets.DotNetStandard20FSharpRefs()
644+ let config = Testing.MakeSimulatedTypeProviderConfig( resolutionFolder=__ SOURCE_ DIRECTORY__, runtimeAssembly= " whatever.dll" , runtimeAssemblyRefs= refs)
645+ use _tp = new TypeProviderForNamespaces( config)
646+ let asm = Assembly.GetExecutingAssembly()
647+
648+ let t = ProvidedTypeDefinition( asm, " Test.Namespace" , " NormalType" , Some typeof< obj>)
649+ Assert.False( t.HideObjectMethods, " HideObjectMethods should default to false" )
650+
651+ [<Fact>]
652+ let ``AddCustomAttribute on method is visible`` () =
653+ let asm = Assembly.GetExecutingAssembly()
654+ let t = ProvidedTypeDefinition( asm, " Test.Namespace" , " TypeWithAttrMethod" , Some typeof< obj>)
655+ let m = ProvidedMethod( " OldMethod" , [], typeof< unit>, invokeCode = fun _ -> <@@ () @@>)
656+ m.AddCustomAttribute {
657+ new CustomAttributeData() with
658+ member _.Constructor = typeof< ObsoleteAttribute>. GetConstructor([| typeof< string> |])
659+ member _.ConstructorArguments = [| CustomAttributeTypedArgument( typeof< string>, " use NewMethod" :> obj ) |] :> _
660+ member _.NamedArguments = [||] :> _
661+ }
662+ t.AddMember m
663+ let mi = t.GetMethod( " OldMethod" )
664+ Assert.NotNull( mi)
665+ let methodAttrs = mi.GetCustomAttributesData()
666+ Assert.True(
667+ methodAttrs |> Seq.exists ( fun a -> a.Constructor.DeclaringType.Name = typeof< ObsoleteAttribute>. Name),
668+ " Expected ObsoleteAttribute on method" )
669+
670+ // ---------------------------------------------------------------------------
671+ // Tests for ProvidedMeasureBuilder: SI units, compound units, annotation
672+ // Addresses https://github.com/fsprojects/FSharp.TypeProviders.SDK/issues/67
673+ // ---------------------------------------------------------------------------
674+
675+ [<Fact>]
676+ let ``ProvidedMeasureBuilder SI symbol creates a FSharpTypeAbbreviation`` () =
677+ let kg = ProvidedMeasureBuilder.SI " kg"
678+ match kg with
679+ | :? ProvidedTypeSymbol as sym ->
680+ Assert.True( sym.IsFSharpTypeAbbreviation, " SI 'kg' should be a FSharpTypeAbbreviation" )
681+ | _ -> failwith " Expected ProvidedTypeSymbol for 'kg'"
682+
683+ [<Fact>]
684+ let ``ProvidedMeasureBuilder AnnotateType produces IsFSharpUnitAnnotated`` () =
685+ let kg = ProvidedMeasureBuilder.SI " kg"
686+ let floatKg = ProvidedMeasureBuilder.AnnotateType( typeof< float>, [ kg ])
687+ match floatKg with
688+ | :? ProvidedTypeSymbol as sym ->
689+ Assert.True( sym.IsFSharpUnitAnnotated, " float<kg> should be IsFSharpUnitAnnotated" )
690+ | _ -> failwith " Expected ProvidedTypeSymbol for annotated type"
691+
692+ [<Fact>]
693+ let ``ProvidedMeasureBuilder compound units Product , Ratio , Square , Inverse , One are non - null`` () =
694+ let kg = ProvidedMeasureBuilder.SI " kg"
695+ let m = ProvidedMeasureBuilder.SI " m"
696+ let s = ProvidedMeasureBuilder.SI " s"
697+
698+ Assert.NotNull( ProvidedMeasureBuilder.Product( kg, m))
699+ Assert.NotNull( ProvidedMeasureBuilder.Ratio( kg, m))
700+ Assert.NotNull( ProvidedMeasureBuilder.Square( m))
701+ Assert.NotNull( ProvidedMeasureBuilder.Inverse( s))
702+ Assert.NotNull( ProvidedMeasureBuilder.One)
703+
704+ // float<m/s²> annotated type should also be IsFSharpUnitAnnotated
705+ let accel = ProvidedMeasureBuilder.Ratio( m, ProvidedMeasureBuilder.Square( s))
706+ let floatAccel = ProvidedMeasureBuilder.AnnotateType( typeof< float>, [ accel ])
707+ match floatAccel with
708+ | :? ProvidedTypeSymbol as sym ->
709+ Assert.True( sym.IsFSharpUnitAnnotated, " float<m/s²> should be IsFSharpUnitAnnotated" )
710+ | _ -> failwith " Expected ProvidedTypeSymbol for float<m/s²>"
711+
712+ [<Fact>]
713+ let ``ProvidedMeasureBuilder SI name ( lowercase ) creates a FSharpTypeAbbreviation`` () =
714+ let kelvin = ProvidedMeasureBuilder.SI " kelvin"
715+ Assert.NotNull( kelvin)
716+ match kelvin with
717+ | :? ProvidedTypeSymbol as sym ->
718+ Assert.True( sym.IsFSharpTypeAbbreviation, " SI 'kelvin' should be a FSharpTypeAbbreviation" )
719+ | _ -> failwith " Expected ProvidedTypeSymbol for 'kelvin'"
0 commit comments