Merge pull request #496 from nicolas-raoul/unit-test-for-issue2907
[mono.git] / mcs / class / System.ServiceModel / Test / MetadataTests / TestLabel.cs
1 //
2 // TestLabel.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 using System;
27 using System.Text;
28 using System.Collections.Generic;
29
30 namespace MonoTests.System.ServiceModel.MetadataTests {
31
32         public class TestLabel {
33
34                 List<Scope> scopes;
35                 string delimiter;
36                 Style defaultStyle;
37
38                 public enum Style {
39                         Letter,
40                         Number,
41                         HexNumer
42                 }
43
44                 public TestLabel (string prefix)
45                         : this (prefix, ".", Style.Letter)
46                 {
47                 }
48
49                 public TestLabel (string prefix, string delimiter, Style style)
50                 {
51                         if ((prefix == null) || (prefix.Equals (string.Empty)))
52                                 throw new ArgumentException ("Cannot be null or empty.", "prefix");
53                         if (delimiter == null)
54                                 throw new ArgumentNullException ("delimiter");
55
56                         scopes = new List<Scope> ();
57                         scopes.Add (new Scope (prefix, style));
58
59                         this.delimiter = delimiter;
60                         this.defaultStyle = style;
61                 }
62
63                 class Scope {
64                         public readonly string Text;
65                         public readonly Style Style;
66                         int id;
67
68                         public Scope (string text, Style style)
69                         {
70                                 this.Text = text;
71                                 this.Style = style;
72                                 this.id = 0;
73                         }
74
75                         public int GetID ()
76                         {
77                                 return ++id;
78                         }
79                 }
80
81                 public void EnterScope (string scope)
82                 {
83                         scopes.Add (new Scope (scope, defaultStyle));
84                 }
85
86                 public void LeaveScope ()
87                 {
88                         if (scopes.Count <= 1)
89                                 throw new InvalidOperationException ();
90                         scopes.RemoveAt (scopes.Count - 1);
91                 }
92
93                 public string Get ()
94                 {
95                         var sb = new StringBuilder ();
96                         for (int i = 0; i < scopes.Count; i++) {
97                                 sb.Append (scopes [i].Text);
98                                 sb.Append (delimiter);
99                         }
100
101                         var scope = scopes [scopes.Count - 1];
102                         var id = scope.GetID ();
103
104                         switch (scope.Style) {
105                         case Style.Letter:
106                                 if (id <= 26)
107                                         sb.Append ((char)('a' + id - 1));
108                                 else
109                                         goto case Style.Number;
110                                 break;
111
112                         case Style.Number:
113                                 sb.Append (id);
114                                 break;
115
116                         case Style.HexNumer:
117                                 sb.AppendFormat ("{0:x2}", id);
118                                 break;
119                         }
120
121                         return sb.ToString ();
122                 }
123
124                 public override string ToString ()
125                 {
126                         var sb = new StringBuilder ();
127                         sb.Append ("[");
128                         for (int i = 0; i < scopes.Count; i++) {
129                                 if (i > 0)
130                                         sb.Append (delimiter);
131                                 sb.Append (scopes [i].Text);
132                         }
133                         sb.Append ("]");
134                         return sb.ToString ();
135                 }
136         }
137 }
138