Example #1
0
        public void DefaultOption()
        {
            // Arrange
            var source =
                "StringList : String Value 1\n" +
                "StringList : String Value 2\n" +
                "Default : Default Value 1\n" +
                "Default : Default Value 2\n" +
                "Default : Default Value 3";

            var options = new Options();
            var parser  = new OptionsReader <Options>(1, "String option test");

            // Act
            parser.Parse(options, "Unit test", source);

            // Assert
            for (var i = 0; i < options.StringListOption.Count; i++)
            {
                Assert.AreEqual($"String Value {i + 1}", options.StringListOption[i]);
            }

            for (var i = 0; i < options.DefaultOption.Count; i++)
            {
                Assert.AreEqual($"Default Value {i + 1}", options.DefaultOption[i]);
            }

            Assert.AreEqual(1, options.IntOption);
            Assert.Null(options.StringOption);

            Assert.Null(options.IntListOption);
            Assert.NotNull(options.StringEnumerableOption);
        }
Example #2
0
        public void MultiOptionListWithDefault()
        {
            // Arrange
            var source =
                "Default1 : 78\n" +
                "Default2 : 89\n" +
                "MultiOptionList1 : 23\n" +
                "MultiOptionList1 : 45\n" +
                "MultiOptionList2 : 56\n" +
                "MultiOptionList2 : 67";

            var options = new MultiListOptionsWithDefault();
            var parser  = new OptionsReader <MultiListOptionsWithDefault>(1, "String option test");

            // Act
            parser.Parse(options, "Unit Test", source);

            // Assert
            Assert.AreEqual(23, options.MultiOptionListWithDefault[0]);
            Assert.AreEqual(45, options.MultiOptionListWithDefault[1]);
            Assert.AreEqual(56, options.MultiOptionListWithDefault[2]);
            Assert.AreEqual(67, options.MultiOptionListWithDefault[3]);
            Assert.AreEqual(78, options.MultiOptionListWithDefault[4]);
            Assert.AreEqual(89, options.MultiOptionListWithDefault[5]);
        }
Example #3
0
        private static void GetOptions(string path, Options options)
        {
            var parserExceptions  = new ListenerAndAggregater <OptionReaderException>();
            var projectFileReader = new OptionsReader <Options>(5, "Project file")
            {
                ErrorListener = parserExceptions
            };
            var projFileSource = OtherUtils.ReadAllText(path);

            PathUtils.RunInDirectory(
                Path.GetDirectoryName(path),
                () => projectFileReader.Parse(options, path, projFileSource));
            var ws = parserExceptions.Warnings;
            var es = parserExceptions.Errors;

            // Report any errors to the user.
            Action <ConsolePublisher, OptionReaderException> action
                = (p, e) => p.DescriptionReasonLocation(ReportGenre.ProjectFile, e.Message, StringUtils.LocationString(e.StartLine, e.EndLine, Path.GetFullPath(path)));

            if (ws.Any())
            {
                Report.Warnings(action, ws);
            }

            if (es.Any())
            {
                Report.Errors(action, es);
            }
        }
Example #4
0
        public void TestBool()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/Help", options);

            Assert.IsTrue(options.Help);
            Assert.IsTrue(String.IsNullOrEmpty(s));
        }
Example #5
0
        public void TestInvalidChar()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/charp aa ", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.AreEqual('\0', options.CharP);
        }
Example #6
0
        public void TestCharInChinese()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/charp 中 ", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.AreEqual('中', options.CharP);
        }
Example #7
0
        public void TestInvalidSByte()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/sbytep -200 ", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.AreEqual(0, options.SbyteP);
        }
Example #8
0
        public void TestInvalidEnum()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/oe kkkk ", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.AreEqual(MyEnum.None, options.OkEnum);
        }
Example #9
0
        public void TestEnumFlagsInQuotedString()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/OkEnum \"hello, World\" ", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.AreEqual(MyEnum.Hello | MyEnum.World, options.OkEnum);
        }
Example #10
0
        public void TestInvalidInt()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/Intp 12U34 ", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.AreEqual(0, options.IntP);
        }
Example #11
0
        public void TestBoolWithExtraValues()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/Help Redundant", options);

            Assert.IsFalse(String.IsNullOrEmpty(s));

            Assert.IsTrue(options.Help);
        }
Example #12
0
        public void TestArgsWithSpace()
        {
            string[] args    = new string[] { "/F", "Do Something" };
            var      options = new Options();
            var      s       = OptionsReader.ReadOptions(args, options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.IsTrue(String.IsNullOrEmpty(s));
            Assert.AreEqual("Do Something", options.Function);
        }
Example #13
0
        public void TestNumericTypesArrayWithInvalidValues()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/Decimalsp 38.1234  1234 /floatsp:3.402e38 1234  /intp=-2147483648 /floatsp:2234 /uintsp=-4294967295 3233", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.AreEqual(2, options.DecimalsP.Length);
            Assert.AreEqual(3, options.FloatsP.Length);
            Assert.IsNull(options.UintsP);
        }
 public OptionsReader ReaderCheck(OptionsReader reader)
 {
     if (reader.CanRead)
     {
         return reader;
     }
     else
     {
         return new OptionsNullReader();
     }
 }
 public OptionsReader ReaderCheck(OptionsReader reader)
 {
     if (reader.CanRead)
     {
         return(reader);
     }
     else
     {
         return(new OptionsNullReader());
     }
 }
Example #16
0
        public unsafe EmitResult Emit(
            Stream rebuildPeStream,
            Compilation rebuildCompilation,
            ImmutableArray <EmbeddedText> embeddedTexts,
            CancellationToken cancellationToken)
        {
            var peHeader       = OptionsReader.PeReader.PEHeaders.PEHeader !;
            var win32Resources = OptionsReader.PeReader.GetSectionData(peHeader.ResourceTableDirectory.RelativeVirtualAddress);

            using var win32ResourceStream = win32Resources.Pointer != null
                ? new UnmanagedMemoryStream(win32Resources.Pointer, win32Resources.Length)
                : null;

            var sourceLink = OptionsReader.GetSourceLinkUTF8();

            var debugEntryPoint = getDebugEntryPoint();

            var emitResult = rebuildCompilation.Emit(
                peStream: rebuildPeStream,
                pdbStream: null,
                xmlDocumentationStream: null,
                win32Resources: win32ResourceStream,
                useRawWin32Resources: true,
                manifestResources: OptionsReader.GetManifestResources(),
                options: new EmitOptions(
                    debugInformationFormat: DebugInformationFormat.Embedded,
                    highEntropyVirtualAddressSpace: (peHeader.DllCharacteristics & DllCharacteristics.HighEntropyVirtualAddressSpace) != 0,
                    subsystemVersion: SubsystemVersion.Create(peHeader.MajorSubsystemVersion, peHeader.MinorSubsystemVersion)),
                debugEntryPoint: debugEntryPoint,
                metadataPEStream: null,
                pdbOptionsBlobReader: OptionsReader.GetMetadataCompilationOptionsBlobReader(),
                sourceLinkStream: sourceLink != null ? new MemoryStream(sourceLink) : null,
                embeddedTexts: embeddedTexts,
                cancellationToken: cancellationToken);

            return(emitResult);

            IMethodSymbol?getDebugEntryPoint()
            {
                if (OptionsReader.GetMainMethodInfo() is (string mainTypeName, string mainMethodName))
                {
                    var typeSymbol = rebuildCompilation.GetTypeByMetadataName(mainTypeName);
                    if (typeSymbol is object)
                    {
                        var methodSymbols = typeSymbol
                                            .GetMembers(mainMethodName)
                                            .OfType <IMethodSymbol>();
                        return(methodSymbols.FirstOrDefault());
                    }
                }

                return(null);
            }
        }
Example #17
0
        public void TwiceSpecifiedOptionsList()
        {
            // Arrange
            var source =
                "OptionList : 23";

            var options = new TwiceSpecifiedOptionsList();
            var parser  = new OptionsReader <TwiceSpecifiedOptionsList>(1, "TwiceSpecifiedOptionsList");

            parser.Parse(options, "Unit Test", source);
        }
Example #18
0
        public void InvalidMutlipleAttributesOptionsList()
        {
            // Arrange
            var source =
                "IntOption : 23\n" +
                "StringOption : 23";

            var options = new InvalidMultipleAttributesOptionsList();
            var parser  = new OptionsReader <InvalidMultipleAttributesOptionsList>(1, "InvalidMutlipleAttributesOptionsList");

            parser.Parse(options, "Unit Test", source);
        }
Example #19
0
        public void NoIListOptions()
        {
            // Arrange
            var source =
                "NoIList : String Value 2";

            var options = new NoIListOptions();
            var parser  = new OptionsReader <NoIListOptions>(1, "Abstract assign option test");

            // Act
            parser.Parse(options, "Unit Test", source);
        }
Example #20
0
        public void TestParseOptions()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/F DoSomething /Intp 1234 /filters f1 \"Akk kB\" f3 \"kke fff\" /byteP 123", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.IsTrue(String.IsNullOrEmpty(s));
            Assert.AreEqual("DoSomething", options.Function);
            Assert.AreEqual(1234, options.IntP);
            //   Assert.AreEqual(123, options.ByteP);
            Assert.AreEqual(4, options.Filters.Length);
            Assert.AreEqual(123, options.ByteP);
            Assert.IsFalse(options.Help);
        }
Example #21
0
        public void InvalidAttributeOptions()
        {
            // Arrange
            var source =
                "AbstractAssign : String Value 1\n" +
                "AbstractAssign : String Value 2\n" +
                "NoIList : String Value 2";

            var options = new InvalidAttributeOptions();
            var parser  = new OptionsReader <InvalidAttributeOptions>(1, "Abstract assign option test");

            // Act
            parser.Parse(options, "Unit Test", source);
        }
Example #22
0
        public void InvalidDefaultOption()
        {
            // Arrange
            var source =
                "Default : Default Value 1\n" +
                "Default : Default Value 2\n" +
                "Default : Default Value 3";

            var options = new InvalidDefaultOptions();
            var parser  = new OptionsReader <InvalidDefaultOptions>(1, "String option test");

            // Act
            parser.Parse(options, "Unit Test", source);
        }
Example #23
0
        public void ScalarStringOption()
        {
            // Arrange
            var source =
                "ScalarString : String Value";

            var options = new Options();
            var parser  = new OptionsReader <Options>(1, "String option test");

            // Act
            parser.Parse(options, "Unit Test", source);

            // Assert
            Assert.AreEqual("String Value", options.StringOption);
        }
Example #24
0
        public void TestNumericTypes()
        {
            var options = new Options();
            var s       = OptionsReader.ReadOptions("/Decimalp 38.1234 /duration -1.79769313E+308 /floatp:3.402e38 /intp=-2147483648 /uintp=4294967295 /ushortp 65535 /longp -9223372036854775808 /ulongp 18446744073709551615", options);

            System.Diagnostics.Trace.TraceWarning(s);
            Assert.AreEqual(38.1234m, options.DecimalP);
            Assert.AreEqual(-1.79769313E+308, options.DoubleP);
            Assert.AreEqual(3.402e38f, options.FloatP);
            Assert.AreEqual(-2147483648, options.IntP);
            Assert.AreEqual(4294967295, options.UintP);
            Assert.AreEqual(65535, options.UShortP);
            Assert.AreEqual(long.MinValue, options.LongP);
            Assert.AreEqual(ulong.MaxValue, options.UlongP);
        }
Example #25
0
        public void ScalarIntOption()
        {
            // Arrange
            var source =
                "ScalarInt : 23";

            var options = new Options();
            var parser  = new OptionsReader <Options>(1, "String option test");

            // Act
            parser.Parse(options, "Unit Test", source);

            // Assert
            Assert.AreEqual(23, options.IntOption);
        }
Example #26
0
            IMethodSymbol?getDebugEntryPoint()
            {
                if (OptionsReader.GetMainMethodInfo() is (string mainTypeName, string mainMethodName))
                {
                    var typeSymbol = rebuildCompilation.GetTypeByMetadataName(mainTypeName);
                    if (typeSymbol is object)
                    {
                        var methodSymbols = typeSymbol
                                            .GetMembers(mainMethodName)
                                            .OfType <IMethodSymbol>();
                        return(methodSymbols.FirstOrDefault());
                    }
                }

                return(null);
            }
Example #27
0
 public Options(string path, string name, OptionsReaderType type = OptionsReaderType.Default)
 {
     if (type != OptionsReaderType.Database)
     {
         if (string.IsNullOrEmpty(path))
         {
             path = Directory.GetCurrentDirectory();
         }
         path = path.Replace('\\', '/');
         if (path.Last() != '/')
         {
             path += '/';
         }
     }
     optionsReader = new OptionsReaderFactory().CreateOptionsReader(type, path, name);
     ReadOptions();
 }
Example #28
0
        Assembly OnAssembly(XPathNavigator nav)
        {
            var name     = OptionsReader.GetAttribute(nav, "name") ?? throw OptionsReader.ThrowError("<assembly> requires `name` attribute.");
            var sizeAttr = OptionsReader.GetAttribute(nav, "size");

            if (sizeAttr == null || !int.TryParse(sizeAttr, out var size))
            {
                throw OptionsReader.ThrowError("<assembly> requires `size` attribute.");
            }
            var tolerance = OptionsReader.GetAttribute(nav, "tolerance");

            var assembly = new Assembly(name, size, tolerance);

            nav.ProcessChildren("namespace", assembly.Namespaces, OnNamespaceEntry);

            return(assembly);
        }
Example #29
0
        public void IntListOption()
        {
            // Arrange
            var source =
                "IntList : 23\n" +
                "IntList : 45";

            var options = new Options();
            var parser  = new OptionsReader <Options>(1, "String option test");

            // Act
            parser.Parse(options, "Unit Test", source);

            // Assert
            Assert.AreEqual(23, options.IntListOption[0]);
            Assert.AreEqual(45, options.IntListOption[1]);
        }
Example #30
0
        public void StringEnumerableOption()
        {
            // Arrange
            var source =
                "StringEnumerable : String Value 1\n" +
                "StringEnumerable : String Value 2";

            var options = new Options();
            var parser  = new OptionsReader <Options>(1, "String option test");

            // Act
            parser.Parse(options, "Unit Test", source);

            // Assert
            Assert.AreEqual($"String Value 1", options.StringEnumerableOption.First());
            Assert.AreEqual($"String Value 2", options.StringEnumerableOption.Last());
        }
Example #31
0
 public Options(string path, string name, OptionsReaderType type = OptionsReaderType.Default)
 {
     if (type != OptionsReaderType.Database)
     {
         if (string.IsNullOrEmpty(path))
         {
             path = Directory.GetCurrentDirectory();
         }
         path = path.Replace('\\', '/');
         if (path.Last() != '/')
         {
             path += '/';
         }
     }
     optionsReader = new OptionsReaderFactory().CreateOptionsReader(type, path, name);
     ReadOptions();
 }
Example #32
0
        public void InvalidScalarIntOption()
        {
            // Arrange
            var source =
                "ScalarInt : Not a number";

            var options = new Options();
            var parser  = new OptionsReader <Options>(1, "String option test");

            var eCount = 0;

            parser.ErrorListener = new ErrorListener <OptionReaderException>(
                e => { eCount++; },
                e => { eCount++; });
            parser.Parse(options, "Unit Test", source);

            Assert.IsTrue(eCount > 0);
        }
Example #33
0
 /// <summary>
 /// Empty options without reader
 /// </summary>
 public Options()
 {
     optionsReader = new OptionsNullReader();
 }