5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
28 using System.Collections.Generic;
30 namespace MonoTests.System.ServiceModel.MetadataTests {
32 public class TestLabel {
44 public TestLabel (string prefix)
45 : this (prefix, ".", Style.Letter)
49 public TestLabel (string prefix, string delimiter, Style style)
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");
56 scopes = new List<Scope> ();
57 scopes.Add (new Scope (prefix, style));
59 this.delimiter = delimiter;
60 this.defaultStyle = style;
64 public readonly string Text;
65 public readonly Style Style;
68 public Scope (string text, Style style)
81 public void EnterScope (string scope)
83 scopes.Add (new Scope (scope, defaultStyle));
86 public void LeaveScope ()
88 if (scopes.Count <= 1)
89 throw new InvalidOperationException ();
90 scopes.RemoveAt (scopes.Count - 1);
95 var sb = new StringBuilder ();
96 for (int i = 0; i < scopes.Count; i++) {
97 sb.Append (scopes [i].Text);
98 sb.Append (delimiter);
101 var scope = scopes [scopes.Count - 1];
102 var id = scope.GetID ();
104 switch (scope.Style) {
107 sb.Append ((char)('a' + id - 1));
109 goto case Style.Number;
117 sb.AppendFormat ("{0:x2}", id);
121 return sb.ToString ();
124 public override string ToString ()
126 var sb = new StringBuilder ();
128 for (int i = 0; i < scopes.Count; i++) {
130 sb.Append (delimiter);
131 sb.Append (scopes [i].Text);
134 return sb.ToString ();