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); }
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]); }
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); } }
public void TestBool() { var options = new Options(); var s = OptionsReader.ReadOptions("/Help", options); Assert.IsTrue(options.Help); Assert.IsTrue(String.IsNullOrEmpty(s)); }
public void TestInvalidChar() { var options = new Options(); var s = OptionsReader.ReadOptions("/charp aa ", options); System.Diagnostics.Trace.TraceWarning(s); Assert.AreEqual('\0', options.CharP); }
public void TestCharInChinese() { var options = new Options(); var s = OptionsReader.ReadOptions("/charp 中 ", options); System.Diagnostics.Trace.TraceWarning(s); Assert.AreEqual('中', options.CharP); }
public void TestInvalidSByte() { var options = new Options(); var s = OptionsReader.ReadOptions("/sbytep -200 ", options); System.Diagnostics.Trace.TraceWarning(s); Assert.AreEqual(0, options.SbyteP); }
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); }
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); }
public void TestInvalidInt() { var options = new Options(); var s = OptionsReader.ReadOptions("/Intp 12U34 ", options); System.Diagnostics.Trace.TraceWarning(s); Assert.AreEqual(0, options.IntP); }
public void TestBoolWithExtraValues() { var options = new Options(); var s = OptionsReader.ReadOptions("/Help Redundant", options); Assert.IsFalse(String.IsNullOrEmpty(s)); Assert.IsTrue(options.Help); }
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); }
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()); } }
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); } }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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(); }
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); }
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]); }
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()); }
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); }
/// <summary> /// Empty options without reader /// </summary> public Options() { optionsReader = new OptionsNullReader(); }