示例#1
0
        public void GetTsTypeName_MemberGiven_TsTypeNameReturned(MemberInfo memberInfo, TypeNameConverterCollection converters, bool strictNullChecks,
                                                                 StrictNullFlags csNullableTranslation, string expectedResult)
        {
            string actualResult = _typeService.GetTsTypeName(memberInfo, converters, strictNullChecks, csNullableTranslation);

            Assert.Equal(expectedResult, actualResult);
        }
示例#2
0
        public static string ToFlagString(this StrictNullFlags strictNullFlags)
        {
            var result = "";

            if (strictNullFlags.HasFlag(StrictNullFlags.Null))
            {
                result += "null ";
            }
            if (strictNullFlags.HasFlag(StrictNullFlags.Undefined))
            {
                result += "undefined ";
            }

            return(result.TrimEnd().Replace(" ", "|"));
        }
示例#3
0
        public void GetTsTypeName_MemberGiven_TsTypeNameReturned(MemberInfo memberInfo, TypeNameConverterCollection converters,
                                                                 StrictNullFlags csNullableTranslation, string expectedResult)
        {
            //arrange
            var generatorOptionsProvider = new GeneratorOptionsProvider {
                GeneratorOptions = new GeneratorOptions
                {
                    TypeNameConverters    = converters,
                    CsNullableTranslation = csNullableTranslation
                }
            };

            _typeService = new TypeService(_metadataReaderFactory, generatorOptionsProvider);

            //act
            string actualResult = _typeService.GetTsTypeName(memberInfo);

            //assert
            Assert.Equal(expectedResult, actualResult);
        }
示例#4
0
        private string GetStrictNullChecksTypeSuffix(MemberInfo memberInfo, StrictNullFlags csNullableTranslation)
        {
            Type memberType = memberInfo is PropertyInfo info
                ? info.PropertyType
                : ((FieldInfo)memberInfo).FieldType;

            StrictNullFlags flags = Nullable.GetUnderlyingType(memberType) != null ? csNullableTranslation : StrictNullFlags.Regular;

            if (_metadataReader.GetAttribute <TsNullAttribute>(memberInfo) != null)
            {
                flags |= StrictNullFlags.Null;
            }
            if (_metadataReader.GetAttribute <TsUndefinedAttribute>(memberInfo) != null)
            {
                flags |= StrictNullFlags.Undefined;
            }

            if (_metadataReader.GetAttribute <TsNotNullAttribute>(memberInfo) != null)
            {
                flags &= ~StrictNullFlags.Null;
            }
            if (_metadataReader.GetAttribute <TsNotUndefinedAttribute>(memberInfo) != null)
            {
                flags &= ~StrictNullFlags.Undefined;
            }

            var result = "";

            if (flags.HasFlag(StrictNullFlags.Null))
            {
                result += " | null";
            }
            if (flags.HasFlag(StrictNullFlags.Undefined))
            {
                result += " | undefined";
            }

            return(result);
        }
示例#5
0
        /// <inheritdoc />
        public string GetTsTypeName(MemberInfo memberInfo, TypeNameConverterCollection typeNameConverters, bool strictNullChecks, StrictNullFlags csNullableTranslation)
        {
            Requires.NotNull(memberInfo, nameof(memberInfo));
            Requires.NotNull(typeNameConverters, nameof(typeNameConverters));

            string typeUnionSuffix = strictNullChecks ? GetStrictNullChecksTypeSuffix(memberInfo, csNullableTranslation) : "";

            var typeAttribute = _metadataReader.GetAttribute <TsTypeAttribute>(memberInfo);

            if (typeAttribute != null)
            {
                if (string.IsNullOrWhiteSpace(typeAttribute.TypeName))
                {
                    throw new CoreException($"No type specified in TsType attribute for member '{memberInfo.Name}' declared in '{memberInfo.DeclaringType?.FullName}'");
                }
                return(typeAttribute.TypeName + typeUnionSuffix);
            }

            return(GetTsTypeNameForMember(memberInfo, typeNameConverters) + typeUnionSuffix);
        }
示例#6
0
        /// <summary>
        /// Gets the TypeScript type name to generate for a member
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <param name="typeNameConverters"></param>
        /// <param name="strictNullChecks"></param>
        /// <param name="csNullableTranslation"></param>
        /// <returns></returns>
        public string GetTsTypeNameForMember(MemberInfo memberInfo, TypeNameConverterCollection typeNameConverters, bool strictNullChecks, StrictNullFlags csNullableTranslation)
        {
            string typeUnionSuffix = strictNullChecks ? GetStrictNullChecksTypeSuffix(memberInfo, csNullableTranslation) : "";

            var typeAttribute = memberInfo.GetCustomAttribute <TsTypeAttribute>();

            if (typeAttribute != null)
            {
                if (typeAttribute.TypeName.IsNullOrWhitespace())
                {
                    throw new CoreException($"No type specified in TsType attribute for member '{memberInfo.Name}' declared in '{memberInfo.DeclaringType?.FullName}'");
                }
                return(typeAttribute.TypeName + typeUnionSuffix);
            }

            return(GetTsTypeName(memberInfo, typeNameConverters, isMember: true) + typeUnionSuffix);
        }
示例#7
0
        public void ToStrictNullFlags_StringTranslationGiven_FlagsReturned(string input, StrictNullFlags expectedResult)
        {
            StrictNullFlags actualResult = input.ToStrictNullFlags();

            Assert.Equal(expectedResult, actualResult);
        }
示例#8
0
        public void ToFlagString_FlagsGiven_StringTranslationReturned(StrictNullFlags flags, string expectedResult)
        {
            string actualResult = flags.ToFlagString();

            Assert.Equal(expectedResult, actualResult);
        }