internal void Option_Value_Should_Be_Default_Value_Multiple() { // Arrange var mapper = new ArgsMapper <ThreeIntOptionsArgs>(); mapper.AddOption(x => x.Option1, optionSettings => { optionSettings.DefaultValue = 1; }); mapper.AddOption(x => x.Option2, optionSettings => { optionSettings.DefaultValue = 2; }); mapper.AddOption(x => x.Option3, optionSettings => { optionSettings.DefaultValue = 3; }); // Act var result = mapper.Map(); // Assert Assert.Equal(1, result.Model.Option1); Assert.Equal(2, result.Model.Option2); Assert.Equal(3, result.Model.Option3); }
internal void AddOption_Should_Throw_OptionShortNameAlreadyExistsException() { // Arrange var mapper = new ArgsMapper <OneBoolOptionArgs>(); mapper.AddOption(x => x.Option, 'o', "option1"); // Assert Assert.Throws <OptionShortNameAlreadyExistsException>(() => mapper.AddOption(x => x.Option, 'o', "option2") ); }
internal void AddOption_Should_Throw_UnsupportedOptionPropertyTypeException() { // Arrange var mapper = new ArgsMapper <UnsupportedTypeOptionArgs>(); // Assert Assert.Throws <UnsupportedOptionPropertyTypeException>(() => mapper.AddOption(x => x.QueueOption) ); Assert.Throws <UnsupportedOptionPropertyTypeException>(() => mapper.AddOption(x => x.StackOption) ); }
internal void Option_Values_Should_Be_Matched_With_One_Level_Nested_Class() { // Arrange var mapper = new ArgsMapper <OneLevelNestedClassWithTwoBoolOptionArgs>(); mapper.AddOption(x => x.Nested.Option1); mapper.AddOption(x => x.Nested.Option2); // Act var result = mapper.Map("--nested.option1", "--nested.option2"); // Assert Assert.True(result.Model.Nested.Option1); Assert.True(result.Model.Nested.Option2); }
internal void Option_Values_Should_Be_Merged_Same_Property_Different_LongNames() { // Arrange var mapper = new ArgsMapper <OneListIntOptionArgs>(); mapper.AddOption(x => x.Option, "option1"); mapper.AddOption(x => x.Option, "option2"); // Act var result = mapper.Map("--option1", "1", "--option2", "2"); // Assert Assert.Contains(1, result.Model.Option); Assert.Contains(2, result.Model.Option); }
internal void Mapper_AddOption_Should_Be_Initialized_With_Property_ShortName_LongName() { // Arrange var mapper = new ArgsMapper <OneBoolOptionArgs>(); mapper.AddOption(x => x.Option, 'o', "option"); }
internal void MapperResult_Should_Have_Error_When_Stacked_Options_Invalid(string arg, string invalidOption) { // Arrange var mapper = new ArgsMapper <ThreeBoolOptionsArgs>(); mapper.AddOption(x => x.Option1, 'x', "option-1"); mapper.AddOption(x => x.Option2, 'y', "option-2"); mapper.AddOption(x => x.Option3, 'z', "option-3"); // Act var result = mapper.Map(arg); // Assert Assert.True(result.HasError); Assert.Equal($"Unknown option '-{invalidOption}'.", result.ErrorMessage); }
internal void Mapper_AddOption_Should_Be_Initialized_With_Property() { // Arrange var mapper = new ArgsMapper <OneBoolOptionArgs>(); mapper.AddOption(x => x.Option); }
internal void MapperResult_Should_Have_Error_When_Stacked_Options_Have_Invalid_Value() { // Arrange var mapper = new ArgsMapper <ThreeIntOptionsArgs>(); mapper.AddOption(x => x.Option1, 'x', "option-1"); mapper.AddOption(x => x.Option2, 'y', "option-2"); mapper.AddOption(x => x.Option3, 'z', "option-3"); // Act var result = mapper.Map("-xyz", "invalid-integer-value"); // Assert Assert.True(result.HasError); Assert.Equal("'invalid-integer-value' not a valid value for '-x|--option-1' option.", result.ErrorMessage); }
internal void Should_Be_Valid_Adding_Multiple_Option() { // Arrange var mapper = new ArgsMapper <ThreeIntOptionsArgs>(); mapper.AddOption(x => x.Option1); mapper.AddOption(x => x.Option2); mapper.AddOption(x => x.Option3); // Act var result = mapper.Map("--option1", "1", "--option2", "2", "--option3", "3"); // Assert Assert.Equal(1, result.Model.Option1); Assert.Equal(2, result.Model.Option2); Assert.Equal(3, result.Model.Option3); }
internal void Mapper_AddOption_Should_Be_Initialized_With_Property_LongName_Settings() { // Arrange var mapper = new ArgsMapper <OneBoolOptionArgs>(); mapper.AddOption(x => x.Option, "option", optionSettings => { }); }
internal void Mapper_AddPositionalOption_List_AddOption_Should_Be_Initialized() { // Arrange var mapper = new ArgsMapper <OneListStringOptionWithOneBoolOptionArgs>(); mapper.AddPositionalOption(x => x.Options); mapper.AddOption(x => x.Option); }
internal void Stacked_Options_Different_Typed_Values_Should_Be_Given_Values() { // Arrange var mapper = new ArgsMapper <BoolIntStringOptionsArgs>(); mapper.AddOption(x => x.Option1, 'x', "option-1"); mapper.AddOption(x => x.Option2, 'y', "option-2"); mapper.AddOption(x => x.Option3, 'z', "option-3"); // Act var result = mapper.Map("-xyz"); // Assert Assert.True(result.Model.Option1); Assert.Equal(0, result.Model.Option2); Assert.Null(result.Model.Option3); }
internal void Stacked_Options_Value_Should_Be_Given_Value() { // Arrange var mapper = new ArgsMapper <ThreeIntOptionsArgs>(); mapper.AddOption(x => x.Option1, 'x', "option-1"); mapper.AddOption(x => x.Option2, 'y', "option-2"); mapper.AddOption(x => x.Option3, 'z', "option-3"); // Act var result = mapper.Map("-xyz", "999"); // Assert Assert.Equal(999, result.Model.Option1); Assert.Equal(999, result.Model.Option2); Assert.Equal(999, result.Model.Option3); }
internal void MapperResult_Should_Have_Required_Error_Even_When_Same_Property_Mapped_By_Another_Option() { // Arrange var mapper = new ArgsMapper <OneIntOptionArgs>(); mapper.AddOption(x => x.Option, "option-1"); mapper.AddOption(x => x.Option, "option-2", optionSettings => { optionSettings.IsRequired = true; }); // Act var result = mapper.Map("--option-1", "1"); // Assert Assert.True(result.HasError); Assert.Equal("Required option '--option-2' is missing.", result.ErrorMessage); }
internal void AddOption_Should_Throw_ReservedOptionLongNameException(string optionLongName) { // Arrange var mapper = new ArgsMapper <OneBoolOptionArgs>(); // Assert Assert.Throws <ReservedOptionLongNameException>(() => mapper.AddOption(x => x.Option, optionLongName) ); }
internal void AddOption_Should_Throw_InvalidOptionShortNameException(char optionShortName) { // Arrange var mapper = new ArgsMapper <OneBoolOptionArgs>(); // Assert Assert.Throws <InvalidOptionShortNameException>(() => mapper.AddOption(x => x.Option, optionShortName, "option") ); }
internal void AddOption_Should_Throw_OptionLongNameRequiredException() { // Arrange var mapper = new ArgsMapper <OneBoolOptionArgs>(); // Assert Assert.Throws <OptionLongNameRequiredException>(() => mapper.AddOption(x => x.Option, string.Empty) ); }
internal void AddOption_Should_Throw_ArgumentException_When_Property_Method() { // Arrange var mapper = new ArgsMapper <OneBoolMethodOptionArgs>(); // Assert Assert.Throws <ArgumentException>(() => mapper.AddOption(x => x.Option()) ); }
internal void AddOption_Should_Throw_ArgumentException_When_Property_Has_No_Setter() { // Arrange var mapper = new ArgsMapper <OneBoolWithoutSetterOptionArgs>(); // Assert Assert.Throws <ArgumentException>(() => mapper.AddOption(x => x.Option) ); }
internal void PositionalOption_Should_Be_Used_With_Named_Options() { // Arrange var mapper = new ArgsMapper <FourBoolOptionArgs>(); mapper.AddPositionalOption(x => x.Option1); mapper.AddOption(x => x.Option2); mapper.AddPositionalOption(x => x.Option3); mapper.AddOption(x => x.Option4, 'o', "option4"); // Act var result = mapper.Map("1", "off", "-o", "--option2", "on"); // Assert Assert.True(result.Model.Option1); Assert.True(result.Model.Option2); Assert.False(result.Model.Option3); Assert.True(result.Model.Option4); }
internal void Mapper_Output_Should_Be_Unknown_Option_Error_When_Version_Alias_In_Stacked_Option() { // Arrange var mapper = new ArgsMapper <ThreeBoolOptionsArgs>(); var output = new StringBuilder(); mapper.Settings.DefaultWriter = new StringWriter(output); mapper.AddOption(x => x.Option1, 'x', "option-1"); mapper.AddOption(x => x.Option2, 'y', "option-2"); mapper.AddOption(x => x.Option3, 'z', "option-3"); mapper.Usage.AddText("sample usage text."); // Act mapper.Execute(new[] { "-vxyz" }, null); // Assert Assert.Equal("Unknown option '-v'.", output.ToString().TrimEnd()); }
internal void Usage_AddSection_AddOption_Should_Throw_OptionCouldNotBeFoundException() { var mapper = new ArgsMapper <OneBoolOptionArgs>(); mapper.Usage.AddSection("header", section => { Assert.Throws <OptionCouldNotBeFoundException>(() => section.AddOption(x => x.Option) ); }); mapper.AddOption(x => x.Option); }
internal void Option_Should_Be_Matched_With_Default_LongName_HyphenCase() { // Arrange var mapper = new ArgsMapper <OneLongNamedBoolOptionArgs>(); mapper.AddOption(x => x.SomeOptionProperty); // Act var result = mapper.Map("--some-option-property"); // Assert Assert.True(result.Model.SomeOptionProperty); }
internal void Stacked_Option_Values_Should_Be_Equal_As_Much_As_Occurrences_If_It_Is_A_Collection_Type() { // Arrange var mapper = new ArgsMapper <OneListIntOptionArgs>(); mapper.AddOption(x => x.Option, 'o', "option-1"); // Act var result = mapper.Map("-ooo", "555"); // Assert Assert.Equal(new[] { 555, 555, 555 }, result.Model.Option); }
internal void Option_Bool_Value_Should_Be_True(params string[] args) { // Arrange var mapper = new ArgsMapper <OneBoolOptionArgs>(); mapper.AddOption(x => x.Option, 'a', "option"); // Act var result = mapper.Map(args); // Assert Assert.True(result.Model.Option); }
internal void Option_Values_Should_Be_Matched_With_Two_Level_Nested_Class() { // Arrange var mapper = new ArgsMapper <TwoLevelNestedClass>(); mapper.AddOption(x => x.NestedA.NestedB.Option); // Act var result = mapper.Map("--nested-a.nested-b.option"); // Assert Assert.True(result.Model.NestedA.NestedB.Option); }
internal void Mapper_Should_Throw_MissingMethodException_When_There_Is_No_Default_Constructor() { // Arrange var mapper = new ArgsMapper <ThereIsNoDefaultConstructor>(); mapper.AddOption(x => x.Option); // Act // Assert Assert.Throws <MissingMethodException>(() => { mapper.Map(); }); }
internal void Option_Should_Be_Matched_With_Custom_LongName() { // Arrange var mapper = new ArgsMapper <OneIntOptionArgs>(); mapper.AddOption(x => x.Option, "custom-named-option-1"); // Act var result = mapper.Map("--custom-named-option-1", "1"); // Assert Assert.Equal(1, result.Model.Option); }
internal void Option_Should_Be_Matched_With_ShortName() { // Arrange var mapper = new ArgsMapper <OneIntOptionArgs>(); mapper.AddOption(x => x.Option, 'a', "option-1"); // Act var result = mapper.Map("-a", "1"); // Assert Assert.Equal(1, result.Model.Option); }