<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
using System.IO;
using System.Reflection;
using NUnit;

namespace NUnitTestExtractor
{
    class Program
    {
        private static Options _options = new Options();

        private enum Level { Namespace, Class, Function, Null }

        static void Main(string[] args)
        {
            //Setting a default value
            Level level = Level.Null;

            using (var parser = new CommandLine.Parser(with => with.HelpWriter = Console.Error))
            {
                if (parser.ParseArgumentsStrict(args, _options, () => Environment.Exit(-1)))
                {
                    if (_options.Help)
                    {
                        Console.WriteLine(CommandLine.Text.HelpText.AutoBuild(_options));
                        return;
                    }
                   
                    if(_options.Output != null)
                    {
                        if (!Directory.Exists(_options.Output))
                        {
                            Directory.CreateDirectory(_options.Output);
                        }
                    }

                    if (_options.Level != null)
                    {
                       

                        try
                        {
                            level = (Level)Enum.Parse(typeof(Level), _options.Level, true);

                            GetTests(_options.DLLs, level);
                            //WriteToTxtFile("file", _options.Output, _options.DLLs, TestList(_options.DLLs, level));
                        }
                        catch (ArgumentException e)
                        {
                            Console.Error.WriteLine(e.GetType() + ": Please enter either Namespace, Class, or Function for level");
                            Environment.Exit(-1);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// While reflection is not implemented method writes each dll verified to be existing to either a txt file or stdout along with the specified level
        /// </summary>
        /// <param name="name"> the name of the txt file that data is being appended to </param>
        /// <param name="outputDirectory"> the directory in that the file is being saved to </param>
        /// <param name="dllDirectories"> list of all dll directories which the user wishes to append to a file or stdout </param>
        /// <param name="level"> Specifies the level of granuality to use for the output tests entered by user. </param>
        private static void WriteTestDllAndName(string name, string outputDirectory, string dll, string data)
        {
            StreamWriter writer;
            if(outputDirectory == null)
            {
                writer = new StreamWriter(Console.OpenStandardOutput());
            }
            else
            {
                writer = new StreamWriter(outputDirectory + name + ".txt", true);
            }
            writer.Write(dll + "|" +data);
            writer.Close();
        }

        private static void GetTests(IList<string> dlls, Level level)
        {
            foreach(string dll in dlls)
            {
                byte[] dllBytes = File.ReadAllBytes(dll);
                var assembly = Assembly.Load(dllBytes);
                foreach (Type type in assembly.GetTypes())
                {
                    Console.WriteLine(type);
                    foreach (MethodInfo methodInfo in type.GetMethods())
                    {
                        var attributes = methodInfo.GetCustomAttributes(true);
                        foreach (var attr in attributes)
                        {
                            if (attr is NUnit.Framework.TestAttribute)
                            {
                                
                                string value = "";
                                Console.WriteLine(type.Namespace);
                                switch (level)
                                {
                                    case Level.Namespace:
                                        value = type.Namespace;
                                        break;
                                    case Level.Class:
                                        value = methodInfo.DeclaringType.ToString();
                                        break;
                                    case Level.Function:
                                        value = methodInfo.DeclaringType + "." + methodInfo;
                                        break;
                                }
                                WriteTestDllAndName("file", _options.Output, dll, value);
                            }
                        }
                    }
                }      
            }
            
        }
    }
}<commit_msg>GetTests() function now works as intended, WriteTestDllAndName() was made cleaner<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
using System.IO;
using System.Reflection;
using NUnit;

namespace NUnitTestExtractor
{
    class Program
    {
        private static Options _options = new Options();

        private enum Level { Namespace, Class, Function, Null }

        static void Main(string[] args)
        {
            //Setting a default value
            Level level = Level.Null;

            using (var parser = new CommandLine.Parser(with => with.HelpWriter = Console.Error))
            {
                if (parser.ParseArgumentsStrict(args, _options, () => Environment.Exit(-1)))
                {
                    if (_options.Help)
                    {
                        Console.WriteLine(CommandLine.Text.HelpText.AutoBuild(_options));
                        return;
                    }
                   
                    if(_options.Output != null)
                    {
                        if (!Directory.Exists(_options.Output))
                        {
                            Directory.CreateDirectory(_options.Output);
                        }
                    }

                    if (_options.Level != null)
                    {
                        try
                        {
                            level = (Level)Enum.Parse(typeof(Level), _options.Level, true);

                            GetTests(_options.DLLs, level);
                        }
                        catch (ArgumentException e)
                        {
                            Console.Error.WriteLine(e.GetType() + ": Please enter either Namespace, Class, or Function for level");
                            Environment.Exit(-1);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Writes each DLL file path along with either a namespace, class or function which contains tests(dependent on level specified by user)
        /// </summary>
        /// <param name="name"> the name of the txt file that data is being appended to </param>
        /// <param name="outputDirectory"> the directory in that the file is being saved to </param>
        /// <param name="dllDirectories"> list of all dll directories which the user wishes to append to a file or stdout </param>
        /// <param name="level"> Specifies the level of granuality to use for the output tests entered by user. </param>
        private static void WriteTestDllAndName(StreamWriter writer, string dll, string data)
        {
            using (writer)
            {
                writer.Write(dll + "|" + data + Environment.NewLine);
            }           
        }
        /// <summary>
        /// Uses reflection in order to search user submitted dlls to find either namespaces, classes, or functions which contain tests, then uses WriteTestDllAndName() to
        /// write data to either stdout or a txt file
        /// </summary>
        /// <param name="dlls">The dlls which are submitted by user</param>
        /// <param name="level">Specifies the level of granuality to use for the output: either namepspace, class or function</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")]
        private static void GetTests(IList<string> dlls, Level level)
        {
            foreach(string dll in dlls)
            {
                Assembly assembly = null;
                try
                {
                    assembly = Assembly.LoadFrom(dll);
                }catch(FileNotFoundException e)
                {
                    Console.Error.WriteLine("{0}: file {1} was not found on system", e.GetType(), dll);
                    continue;
                }
               
                foreach (Type type in assembly.GetTypes())
                {
                    foreach (MethodInfo methodInfo in type.GetMethods())
                    {
                        var attributes = methodInfo.GetCustomAttributes(true);
                        foreach (var attr in attributes)
                        {
                            if (attr is NUnit.Framework.TestAttribute)
                            {                               
                                string value = "";
                                
                                switch (level)
                                {
                                    case Level.Namespace:
                                        value = type.Namespace;
                                        break;
                                    case Level.Class:
                                        value = methodInfo.DeclaringType.ToString();
                                        break;
                                    case Level.Function:
                                        string method = methodInfo.ToString().Substring(methodInfo.ToString().IndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1);
                                        value = methodInfo.DeclaringType + "." + method;
                                        break;
                                }
                                StreamWriter writer;
                                if (_options.Output== null)
                                {
                                    writer = new StreamWriter(Console.OpenStandardOutput());
                                }
                                else
                                {
                                    writer = new StreamWriter(_options.Output+ "file.txt", true);
                                }
                                WriteTestDllAndName(writer, dll, value);
                            }
                        }
                    }
                }      
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07733309242027284, 'fraction_numerical': 0.0009937663745595807, 'mean_word_length': 1.0278439274592417, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6535949', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2216, 'n_words': 635}