public void Can_use_computed_columns_with_nullable_enum() { var serviceProvider = new ServiceCollection() .AddEntityFramework() .AddSqlServer() .ServiceCollection() .BuildServiceProvider(); using (var context = new NullableContext(serviceProvider, "NullableEnumComputedColumns")) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); var entity = context.EnumItems.Add(new EnumItem { FlagEnum = FlagEnum.AValue, OptionalFlagEnum = FlagEnum.BValue }).Entity; context.SaveChanges(); Assert.Equal(FlagEnum.AValue | FlagEnum.BValue, entity.CalculatedFlagEnum); } }
public NonexclusiveInterfaceDescription(LazinatorCompilation fileSet, INamedTypeSymbol t, NullableContext nullableContextSetting, ObjectDescription container) { string typeName = LazinatorCompilation.TypeSymbolToString(t); NullableContextSetting = nullableContextSetting; if (!fileSet.NonexclusiveInterfaces.Contains(typeName)) { throw new LazinatorCodeGenException("NonexclusiveLazinator must be applied to a nonexclusive interface."); } Container = container; CloneNonexclusiveLazinatorAttribute nonexclusiveLazinatorAttribute = fileSet.GetFirstAttributeOfType <CloneNonexclusiveLazinatorAttribute>(t); if (nonexclusiveLazinatorAttribute == null) { throw new LazinatorCodeGenException("Expected nonexclusive self-serialize attribute."); } if (fileSet.PropertiesForType.ContainsKey(typeName)) { Properties = fileSet.PropertiesForType[typeName].Select(x => new PropertyDescription(x.Property, container, NullableContextSetting, null, null, false)).ToList(); } }
public ExclusiveInterfaceDescription(Compilation compilation, INamedTypeSymbol t, NullableContext nullableContextSetting, ObjectDescription container, bool isUnofficialInterface = false) { Compilation = compilation; NamedTypeSymbol = t; NullableContextSetting = nullableContextSetting; IsUnofficialInterface = isUnofficialInterface; Container = container; var lazinatorAttribute = Container.Compilation.GetFirstAttributeOfType <CloneLazinatorAttribute>(t); if (lazinatorAttribute == null) { throw new LazinatorCodeGenException("Lazinator attribute is required for each interface implementing ILazinator, including inherited attributes."); } UniqueID = lazinatorAttribute.UniqueID; Version = lazinatorAttribute.Version; var genericArguments = t.TypeArguments.ToList(); GenericArgumentNames = genericArguments.Select(x => x.Name).ToList(); SetPropertiesIncludingInherited(t); TotalNumProperties = PropertiesIncludingInherited.Count(); }
public void Can_use_computed_columns_with_nullable_enum() { var serviceProvider = new ServiceCollection() .AddEntityFramework() .AddSqlServer() .ServiceCollection() .BuildServiceProvider(); using (var context = new NullableContext(serviceProvider, "NullableEnumComputedColumns")) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); var entity = context.EnumItems.Add(new EnumItem { FlagEnum = FlagEnum.AValue, OptionalFlagEnum = FlagEnum.BValue }).Entity; context.SaveChanges(); Assert.Equal(FlagEnum.AValue | FlagEnum.BValue, entity.CalculatedFlagEnum); } }
private static bool IsFlagSet(NullableContext context, NullableContext flag) => (context & flag) == flag;
/// <summary> /// Returns whether the nullable annotation state was inherited from the project default for this file type. /// </summary> public static bool AnnotationsInherited(this NullableContext context) => IsFlagSet(context, NullableContext.AnnotationsContextInherited);
/// <summary> /// Returns whether the nullable warning state was inherited from the project default for this file type. /// </summary> public static bool WarningsInherited(this NullableContext context) => IsFlagSet(context, NullableContext.WarningsContextInherited);
/// <summary> /// Returns whether nullable annotations are enabled for this context. /// </summary> public static bool AnnotationsEnabled(this NullableContext context) => IsFlagSet(context, NullableContext.AnnotationsEnabled);
/// <summary> /// Returns whether nullable warnings are enabled for this context. /// </summary> public static bool WarningsEnabled(this NullableContext context) => IsFlagSet(context, NullableContext.WarningsEnabled);
public void NullableContextExplicitlySpecifiedAndRestoredInFile(string pragma, NullableContextOptions globalContext, NullableContext expectedContext) { var source = $@" {pragma} class C {{ #nullable restore void M() {{}} }}"; var comp = CreateCompilation(source, options: WithNonNullTypes(globalContext)); var syntaxTree = comp.SyntaxTrees[0]; var model = comp.GetSemanticModel(syntaxTree); var classDeclPosition = syntaxTree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>().Single().SpanStart; var methodDeclPosition = syntaxTree.GetRoot().DescendantNodes().OfType <MethodDeclarationSyntax>().Single().SpanStart; Assert.Equal(expectedContext, model.GetNullableContext(classDeclPosition)); // The context at the start of the file should always be inherited and match the global context var restoredContext = ((NullableContext)globalContext) | NullableContext.ContextInherited; Assert.Equal(restoredContext, model.GetNullableContext(0)); Assert.Equal(restoredContext, model.GetNullableContext(methodDeclPosition)); }
public static bool ParentAndChildShareNullabilityContext(NullableContext parentContext, NullableContext childContext) => parentContext.AnnotationsEnabled() == childContext.AnnotationsEnabled();