1+ #region License
2+ // CommandLineOptionGrouperTests.cs
3+ // Copyright (c) 2013, Simon Williams
4+ // All rights reserved.
5+ //
6+ // Redistribution and use in source and binary forms, with or without modification, are permitted provide
7+ // d that the following conditions are met:
8+ //
9+ // Redistributions of source code must retain the above copyright notice, this list of conditions and the
10+ // following disclaimer.
11+ //
12+ // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13+ // the following disclaimer in the documentation and/or other materials provided with the distribution.
14+ //
15+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16+ // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+ // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18+ // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19+ // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20+ // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22+ // POSSIBILITY OF SUCH DAMAGE.
23+ #endregion
24+
25+ using Fclp . Internals ;
26+ using Fclp . Internals . Parsing ;
27+ using Machine . Specifications ;
28+
29+ namespace Fclp . Tests . Internals
30+ {
31+ public class CommandLineOptionGrouperTests
32+ {
33+ [ Subject ( typeof ( CommandLineOptionGrouper ) ) ]
34+ abstract class CommandLineOptionGrouperTestContext : TestContextBase < CommandLineOptionGrouper >
35+ {
36+ Establish context = ( ) =>
37+ CreateSut ( ) ;
38+ }
39+
40+ abstract class GroupByOptionTestContext : CommandLineOptionGrouperTestContext
41+ {
42+ protected static string [ ] [ ] actualResult ;
43+ protected static string [ ] args ;
44+
45+ Because of = ( ) =>
46+ error = Catch . Exception ( ( ) =>
47+ actualResult = sut . GroupByOption ( args ) ) ;
48+ }
49+
50+ class when_double_dashes_are_used_to_terminate_option_parsing : GroupByOptionTestContext
51+ {
52+ Establish context = ( ) =>
53+ {
54+ args = new [ ]
55+ {
56+ "-A" , "1" , "2" , "3" ,
57+ "-B" , "a" , "b" , "c" ,
58+ "-C" , "--" , "-a" , "-1" , "-b"
59+ } ;
60+ } ;
61+
62+ It should_return_three_sets = ( ) =>
63+ actualResult . Length . ShouldEqual ( 3 ) ;
64+
65+ It should_group_the_A_elements = ( ) =>
66+ actualResult [ 0 ] . ShouldContainOnly ( "-A" , "1" , "2" , "3" ) ;
67+
68+ It should_group_the_B_elements = ( ) =>
69+ actualResult [ 1 ] . ShouldContainOnly ( "-B" , "a" , "b" , "c" ) ;
70+
71+ It should_group_the_C_elements = ( ) =>
72+ actualResult [ 2 ] . ShouldContainOnly ( "-C" , "--" , "-a" , "-1" , "-b" ) ;
73+ }
74+
75+ class when_double_dashes_are_used_immediately_to_terminate_option_parsing : GroupByOptionTestContext
76+ {
77+ Establish context = ( ) =>
78+ args = new [ ]
79+ {
80+ "--" , "-A" , "1" , "2" , "3" ,
81+ "-B" , "a" , "b" , "c" ,
82+ "-C" , "-a" , "-1" , "-b"
83+ } ;
84+
85+ It should_return_a_single_set = ( ) =>
86+ actualResult . Length . ShouldEqual ( 1 ) ;
87+
88+ It should_group_the_A_elements = ( ) =>
89+ actualResult [ 0 ] . ShouldContainOnly ( "--" , "-A" , "1" , "2" , "3" , "-B" , "a" , "b" , "c" , "-C" , "-a" , "-1" , "-b" ) ;
90+ }
91+
92+ class when_there_are_only_arguments_and_no_options : GroupByOptionTestContext
93+ {
94+ Establish context = ( ) =>
95+ args = new [ ] { "0" , "1" , "2" , "3" } ;
96+
97+ It should_return_a_single_set = ( ) =>
98+ actualResult . Length . ShouldEqual ( 1 ) ;
99+
100+ It should_group_all_the_provided_elements = ( ) =>
101+ actualResult [ 0 ] . ShouldContainOnly ( "0" , "1" , "2" , "3" ) ;
102+ }
103+
104+ class when_there_is_a_double_dash_but_no_options_before_it : GroupByOptionTestContext
105+ {
106+ Establish context = ( ) =>
107+ args = new [ ] { "--" , "-A" , "1" , "2" , "3" } ;
108+
109+ It should_return_a_single_set = ( ) =>
110+ actualResult . Length . ShouldEqual ( 1 ) ;
111+
112+ It should_group_all_the_provided_elements = ( ) =>
113+ actualResult [ 0 ] . ShouldContainOnly ( "--" , "-A" , "1" , "2" , "3" ) ;
114+ }
115+
116+ class when_there_is_a_double_dash_at_the_end : GroupByOptionTestContext
117+ {
118+ Establish context = ( ) =>
119+ args = new [ ]
120+ {
121+ "-A" , "1" , "2" , "3" ,
122+ "-B" , "a" , "b" , "c" ,
123+ "-C" , "a1" , "b1" , "c1" , "--"
124+ } ;
125+
126+ It should_return_three_sets = ( ) =>
127+ actualResult . Length . ShouldEqual ( 3 ) ;
128+
129+ It should_group_the_A_elements = ( ) =>
130+ actualResult [ 0 ] . ShouldContainOnly ( "-A" , "1" , "2" , "3" ) ;
131+
132+ It should_group_the_B_elements = ( ) =>
133+ actualResult [ 1 ] . ShouldContainOnly ( "-B" , "a" , "b" , "c" ) ;
134+
135+ It should_group_the_C_elements = ( ) =>
136+ actualResult [ 2 ] . ShouldContainOnly ( "-C" , "a1" , "b1" , "c1" , "--" ) ;
137+ }
138+
139+ class when_options_are_repeated : GroupByOptionTestContext
140+ {
141+ Establish context = ( ) =>
142+ args = new [ ] { "-A" , "1" , "2" , "3" , "-A" , "4" , "5" , "6" } ;
143+
144+ It should_return_two_sets = ( ) =>
145+ actualResult . Length . ShouldEqual ( 2 ) ;
146+
147+ It should_group_the_first_elements = ( ) =>
148+ actualResult [ 0 ] . ShouldContainOnly ( "-A" , "1" , "2" , "3" ) ;
149+
150+ It should_group_the_second_repeated_elements = ( ) =>
151+ actualResult [ 1 ] . ShouldContainOnly ( "-A" , "4" , "5" , "6" ) ;
152+ }
153+
154+ class when_the_args_is_empty : GroupByOptionTestContext
155+ {
156+ Establish context = ( ) =>
157+ args = new string [ 0 ] ;
158+
159+ It should_not_throw_an_error = ( ) =>
160+ error . ShouldBeNull ( ) ;
161+
162+ It should_return_empty_args = ( ) =>
163+ actualResult . ShouldBeEmpty ( ) ;
164+ }
165+ }
166+ }
0 commit comments