Exemplo n.º 1
0
        private void LoadEnums()
        {
            var types = Config.TypeScript.Entity.Assemblys.SelectMany(c => c.GetTypes()).Where(c => c.GetTypeInfo().IsEnum).ToList();

            types.AddRange(Config.TypeScript.Entity.TypesExtra.Where(c => c.GetTypeInfo().IsEnum));
            types.Add(typeof(ExecuteMessageType));
            types.Add(typeof(EntityAction));
            types = types.Distinct().ToList();

            foreach (var type in types)
            {
                var enumProperty = new TypeScriptEnum
                {
                    Name   = type.Name,
                    Type   = type,
                    Values = new Dictionary <string, int>()
                };

                var enumValues = System.Enum.GetValues(type);

                for (var i = 0; i < enumValues.Length; i++)
                {
                    enumProperty.Values.Add(enumValues.GetValue(i).ToString(), (int)enumValues.GetValue(i));
                }

                TSEnums.Add(enumProperty);
            }
        }
Exemplo n.º 2
0
        public TypeScriptEnum Build(
            CodeEnum codeEnum,
            TypeContext typeContext)
        {
            string moduleName = this.settings.GetModuleNameFromNamespace(codeEnum.Namespace);

            bool           enumCreated;
            TypeScriptEnum result = typeContext.GetOrCreateEnum(
                moduleName,
                TypeName.ParseDte(codeEnum.FullName),
                codeEnum.Name,
                out enumCreated);

            foreach (CodeVariable member in codeEnum.Members)
            {
                result.Values.Add(new TypeScriptEnumValue()
                {
                    Name  = member.Name,
                    Value = (member.InitExpression != null)
                            ? member.InitExpression.ToString()
                            : null
                });
            }

            return(result);
        }
Exemplo n.º 3
0
        public void TestEnumGeneration()
        {
            // arrange
            var draftEnumMember = new TypeScriptEnumMember {
                Name = "Draft"
            };
            var finalEnumMember = new TypeScriptEnumMember {
                Name = "Final"
            };
            var typeScriptEnum = new TypeScriptEnum
            {
                Name    = "BillStatus",
                Members = new List <TypeScriptEnumMember> {
                    draftEnumMember, finalEnumMember
                }
            };
            var output             = new StringWriter();
            var expectOutputWriter = new StringWriter();

            expectOutputWriter.WriteLine("enum BillStatus {");
            draftEnumMember.Generate(expectOutputWriter, false);
            finalEnumMember.Generate(expectOutputWriter, true);
            expectOutputWriter.WriteLine("}");

            // act
            typeScriptEnum.Generate(output);

            // assert
            Assert.Equal(expectOutputWriter.GetStringBuilder().ToString(), output.GetStringBuilder().ToString());
        }
Exemplo n.º 4
0
        /*  

        Syntax:

            enum Direction {
                Up = 1,
                Down,
                Left,
                Right,
            }

        */

        private bool GenerateEnum(TypeScriptEnum tsEnum, TextWriter output) {
            output.WriteLine($"export enum {tsEnum.Name} {{");
            foreach (TypeScriptField tsField in tsEnum.Fields) {
                output.Write($"  {tsField.Name} ");
                if (tsField.Value != null) {
                    output.Write($"= {tsField.Value.ToString()}");
                }
                output.WriteLine(",");
            }
            output.WriteLine("}");
            return true;
        }
        public EnumOutputAppenderTests()
        {
            bool created;

            explicitEnumType = this.typeContext.GetOrCreateEnum(
                "Foo",
                TypeName.ParseDte("Foo.ExplicitEnum"),
                "ExplicitEnum",
                out created);
            explicitEnumType.Values = new List <TypeScriptEnumValue>()
            {
                new TypeScriptEnumValue()
                {
                    Name  = "First",
                    Value = "1"
                },
                new TypeScriptEnumValue()
                {
                    Name  = "Second",
                    Value = "2"
                },
                new TypeScriptEnumValue()
                {
                    Name  = "Fifth",
                    Value = "5"
                }
            };

            implicitEnumType = this.typeContext.GetOrCreateEnum(
                "Foo",
                TypeName.ParseDte("Foo.ImplicitEnum"),
                "ImplicitEnum",
                out created);
            implicitEnumType.Values = new List <TypeScriptEnumValue>()
            {
                new TypeScriptEnumValue()
                {
                    Name = "First"
                },
                new TypeScriptEnumValue()
                {
                    Name = "Second"
                },
                new TypeScriptEnumValue()
                {
                    Name = "Third"
                }
            };
        }