Beispiel #1
0
        public async Task ExecuteInMemoryTest()
        {
            var fileName = ".\\test-project.mdj";
            var project  = await DVDpro.StarUML.FileFormat.Project.LoadAsync(fileName);

            var generator = new EnumGenerator();

            using (var ms = new System.IO.MemoryStream())
            {
                using (var outStream = new CSharpWriter(ms))
                {
                    foreach (var model in project.GetChildrenByType <DVDpro.StarUML.FileFormat.Nodes.UmlModelNode>())
                    {
                        var enumNode = model.GetChildrenByType <DVDpro.StarUML.FileFormat.Nodes.UmlEnumerationNode>().First(r => r.Name == "MasterEnum");
                        generator.Generate(outStream, enumNode);
                    }

                    outStream.Flush();
                    ms.Seek(0, System.IO.SeekOrigin.Begin);
                    using (var reader = new System.IO.StreamReader(ms))
                    {
                        var output = reader.ReadToEnd();
                        Assert.Equal("/// <summary>\r\n/// Test enum comment\r\n/// multiline\r\n/// </summary>\r\npublic enum MasterEnum\r\n{\r\n    /// <summary>test literal comment</summary>\r\n    Literal1 = 0x1,\r\n\r\n    Literal2\r\n}\r\n", output);
                    }
                }
            }
        }
Beispiel #2
0
        public void EnumGenerator()
        {
            IEnumGenerator enumgenerator = new EnumGenerator();
            Days           daything      = (Days)enumgenerator.Generate(random, typeof(Days));

            Assert.IsNotNull(daything);
        }
Beispiel #3
0
        public async Task ExecuteToFileTest()
        {
            var fileName = ".\\test-project.mdj";
            var project  = await DVDpro.StarUML.FileFormat.Project.LoadAsync(fileName);

            var tmpFile = System.IO.Path.GetTempFileName();

            try
            {
                var generator = new EnumGenerator();
                using (var outStream = new CSharpWriter(tmpFile))
                {
                    foreach (var model in project.GetChildrenByType <DVDpro.StarUML.FileFormat.Nodes.UmlModelNode>())
                    {
                        var enumNode = model.GetChildrenByType <DVDpro.StarUML.FileFormat.Nodes.UmlEnumerationNode>().First(r => r.Name == "MasterEnum");
                        generator.Generate(outStream, enumNode);
                    }
                }
                var output = await System.IO.File.ReadAllTextAsync(tmpFile);

                Assert.Equal("/// <summary>\r\n/// Test enum comment\r\n/// multiline\r\n/// </summary>\r\npublic enum MasterEnum\r\n{\r\n    /// <summary>test literal comment</summary>\r\n    Literal1 = 0x1,\r\n\r\n    Literal2\r\n}\r\n", output);
            }
            finally
            {
                System.IO.File.Delete(tmpFile);
            }
        }
        public void EnumGeneratorTest()
        {
            var enumgenerator = new EnumGenerator(Enum.GetValues(typeof(CustomerType)));
            var enumvalue     = enumgenerator.Generate();

            Assert.IsNotNull(enumvalue);
            Assert.IsInstanceOf <CustomerType>(enumvalue);
        }
Beispiel #5
0
        public void Generate_Enum_One_Element()
        {
            // arrange
            var descriptor = new EnumDescriptor(
                "Abc",
                "Def",
                new List <EnumValueDescriptor>
            {
                new EnumValueDescriptor("Ghi", "GHI")
            });

            // act
            _generator.Generate(_codeWriter, descriptor, out _);

            // assert
            _stringBuilder.ToString().MatchSnapshot();
        }
Beispiel #6
0
        public override string Generate(int indent)
        {
            if (Data != null)
            {
                var output = string.Empty;
                NamespaceGenerator @namespace = null;
                EnumGenerator      @enum      = null;

                if (string.IsNullOrEmpty(Data.title))
                {
                    return(output);
                }

                if (!string.IsNullOrEmpty(Data.category))
                {
                    @namespace = NamespaceGenerator.Namespace(Data.category);
                }

                @enum = EnumGenerator.Enum(Data.title.LegalMemberName());

                indices.Clear();

                for (int i = 0; i < Data.items.Count; i++)
                {
                    if (!string.IsNullOrEmpty(Data.items[i].name))
                    {
                        while (indices.Contains(Data.items[i].index))
                        {
                            Data.items[i].index++;
                        }

                        indices.Add(Data.items[i].index);

                        @enum.AddItem(Data.items[i].name, Data.items[i].index);
                    }
                }

                @enum.indexing = Data.useIndex;

#if VISUAL_SCRIPTING_1_7
                if (Data.lastCompiledName != Data.GetFullTypeName())
                {
                    @enum.AddAttribute(AttributeGenerator.Attribute <RenamedFromAttribute>().AddParameter(Data.lastCompiledName));
                }
#endif

                if (@namespace != null)
                {
                    @namespace.AddEnum(@enum);
                    return(@namespace.Generate(indent));
                }

                return(@enum.Generate(indent));
            }

            return(string.Empty);
        }
Beispiel #7
0
        public void GeneratesEnum()
        {
            var generator = new EnumGenerator();
            var random    = new FuzzRandom();

            var generatedValue = generator.Generate(Mock.Of <IFuzzProfile>(), typeof(TestEnum), random);

            Assert.IsTrue(generatedValue is TestEnum);
        }
Beispiel #8
0
        public static void PrintEnum(Symbols.Enum en, string root)
        {
            string packageDirectory = Path.Combine(root, JavaConventionHelper.packageConvention(en.Namespace.Name));
            string enumDirectory    = Path.Combine(packageDirectory, "enums");

            Directory.CreateDirectory(enumDirectory);
            using (StreamWriter writer = new StreamWriter(Path.Combine(enumDirectory, en.Name + ".java")))
            {
                EnumGenerator javaGen = new EnumGenerator();
                writer.WriteLine(javaGen.Generate(en));
            }
        }
        // ReSharper disable once ExcessiveIndentation
        public CodeGeneratorResult Generate()
        {
            var driverSettingsDefinitions = _readDirectory
                                            .GetFiles("NvApiDriverSettings.h", SearchOption.TopDirectoryOnly)
                                            .FirstOrDefault();
            var filesDeleted = new List <FileInfo>();
            var filesAdded   = new List <FileInfo>();

            if (driverSettingsDefinitions?.Exists != true)
            {
                throw new FileNotFoundException("NvApiDriverSettings.h file not found.");
            }

            var parser = new CHeaderFileParser(driverSettingsDefinitions);

            parser.Parse();

            var settings = parser.Root.SubTrees
                           .FirstOrDefault(tree =>
                                           tree is CEnum e &&
                                           e.Name.Equals("ESetting", StringComparison.InvariantCultureIgnoreCase)
                                           )
                           ?.SubTrees
                           .Where(value => value.Name.EndsWith("_ID", StringComparison.InvariantCultureIgnoreCase))
                           .OfType <CEnumValue>()
                           .Select(
                value =>
            {
                var stringId       = value.Name.Substring(0, value.Name.Length - "_ID".Length);
                var description    = GetDescription(stringId, parser.Root.SubTrees);
                var possibleValues = GetPossibleValues(
                    stringId,
                    parser.Root.SubTrees,
                    out var isCustomPossibleValues
                    );
                var integerId = value.ResolveValue() as long?;

                return(new
                {
                    StringId = stringId,
                    Description = description,
                    IntegerId = integerId.HasValue ? (uint)integerId.Value : (uint?)null,
                    PossibleValues = possibleValues,
                    IsCustomPossibleValues = isCustomPossibleValues
                });
            }
                )
                           .ToArray();

            if (settings == null || settings.Length == 0)
            {
                throw new InvalidOperationException(
                          "ESetting enum is not supported with the NvApiDriverSettings.h file.");
            }

            var _valuesWriteDirectory = new DirectoryInfo(Path.Combine(_writeDirectory.FullName, "SettingValues"));

            if (!_valuesWriteDirectory.Exists)
            {
                _valuesWriteDirectory.Create();
            }
            else
            {
                foreach (var file in _valuesWriteDirectory.EnumerateFiles())
                {
                    file.Delete();
                    filesDeleted.Add(file);
                }
            }

            var settingsEnumValues       = new Dictionary <string, uint>();
            var settingsEnumDescriptions = new Dictionary <string, string>();

            foreach (var setting in settings)
            {
                var friendlyName = _nameRuleSet.Apply(setting.StringId);

                var possibleValues = setting.PossibleValues
                                     .ToDictionary(pair =>
                {
                    var name = pair.Key;

                    if (name.StartsWith(setting.StringId + "_"))
                    {
                        name = name.Substring(setting.StringId.Length + 1);
                    }

                    return(name.Trim().Trim('_'));
                }, pair => pair.Value).ToArray();

                if (setting.IntegerId != null)
                {
                    settingsEnumValues.Add(setting.StringId, setting.IntegerId.Value);

                    if (!string.IsNullOrWhiteSpace(setting.Description))
                    {
                        settingsEnumDescriptions.Add(setting.StringId, setting.Description);
                    }
                }

                if (possibleValues.Any())
                {
                    CodeGeneratorBase generator;

                    if (setting.IsCustomPossibleValues)
                    {
                        generator = new ConstEnumGenerator(
                            _valuesWriteDirectory,
                            friendlyName,
                            possibleValues.ToDictionary(pair => pair.Key, pair => pair.Value),
                            _nameRuleSet,
                            _nameSpace + ".SettingValues"
                            );
                    }
                    else
                    {
                        generator = new EnumGenerator <uint>(
                            _valuesWriteDirectory,
                            friendlyName,
                            possibleValues
                            .Where(pair => pair.Value is long)
                            .ToDictionary(pair => pair.Key, pair => (uint)(long)pair.Value),
                            _nameRuleSet,
                            _nameSpace + ".SettingValues"
                            );
                    }

                    var possibleValuesEnumGeneratorResult = generator.Generate();

                    foreach (var fileInfo in possibleValuesEnumGeneratorResult.FilesRemoved)
                    {
                        filesDeleted.Add(fileInfo);
                    }

                    foreach (var fileInfo in possibleValuesEnumGeneratorResult.FilesAdded)
                    {
                        filesAdded.Add(fileInfo);
                    }
                }
            }


            var enumGenerator = new EnumGenerator <uint>(
                _writeDirectory,
                "KnownSettingId",
                settingsEnumValues,
                _nameRuleSet,
                _nameSpace,
                settingsEnumDescriptions
                );
            var enumGeneratorResult = enumGenerator.Generate();

            foreach (var fileInfo in enumGeneratorResult.FilesRemoved)
            {
                filesDeleted.Add(fileInfo);
            }

            foreach (var fileInfo in enumGeneratorResult.FilesAdded)
            {
                filesAdded.Add(fileInfo);
            }

            return(new CodeGeneratorResult(filesAdded.ToArray(), filesDeleted.ToArray(), _writeDirectory));
        }
Beispiel #10
0
        private void AfterGeneration()
        {
            @namespace?.AddEnum(@enum);
            var usings = CodeBuilder.Using(new string[] { "Ludiq" });
            var output = (string.IsNullOrEmpty(decorated.@namespace) || string.IsNullOrWhiteSpace(decorated.@namespace)) ? usings + "\n\n" + @enum.Generate(0) : usings + "\n\n" + @namespace.Generate(0);

            EnumExtensions.Save(guid, decorated, output);
        }
 public void EnumGenerator()
 {
     IEnumGenerator enumgenerator = new EnumGenerator();
     Days daything = (Days)enumgenerator.Generate(random, typeof(Days));
     Assert.IsNotNull(daything);
 }