TestLabel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // TestLabel.cs
  3. //
  4. // Author:
  5. // Martin Baulig <[email protected]>
  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. namespace MonoTests.System.ServiceModel.MetadataTests {
  30. public class TestLabel {
  31. List<Scope> scopes;
  32. string delimiter;
  33. Style defaultStyle;
  34. public enum Style {
  35. Letter,
  36. Number,
  37. HexNumer
  38. }
  39. public TestLabel (string prefix)
  40. : this (prefix, ".", Style.Letter)
  41. {
  42. }
  43. public TestLabel (string prefix, string delimiter, Style style)
  44. {
  45. if ((prefix == null) || (prefix.Equals (string.Empty)))
  46. throw new ArgumentException ("Cannot be null or empty.", "prefix");
  47. if (delimiter == null)
  48. throw new ArgumentNullException ("delimiter");
  49. scopes = new List<Scope> ();
  50. scopes.Add (new Scope (prefix, style));
  51. this.delimiter = delimiter;
  52. this.defaultStyle = style;
  53. }
  54. class Scope {
  55. public readonly string Text;
  56. public readonly Style Style;
  57. int id;
  58. public Scope (string text, Style style)
  59. {
  60. this.Text = text;
  61. this.Style = style;
  62. this.id = 0;
  63. }
  64. public int GetID ()
  65. {
  66. return ++id;
  67. }
  68. }
  69. public void EnterScope (string scope)
  70. {
  71. scopes.Add (new Scope (scope, defaultStyle));
  72. }
  73. public void LeaveScope ()
  74. {
  75. if (scopes.Count <= 1)
  76. throw new InvalidOperationException ();
  77. scopes.RemoveAt (scopes.Count - 1);
  78. }
  79. public string Get ()
  80. {
  81. var sb = new StringBuilder ();
  82. for (int i = 0; i < scopes.Count; i++) {
  83. sb.Append (scopes [i].Text);
  84. sb.Append (delimiter);
  85. }
  86. var scope = scopes [scopes.Count - 1];
  87. var id = scope.GetID ();
  88. switch (scope.Style) {
  89. case Style.Letter:
  90. if (id <= 26)
  91. sb.Append ((char)('a' + id - 1));
  92. else
  93. goto case Style.Number;
  94. break;
  95. case Style.Number:
  96. sb.Append (id);
  97. break;
  98. case Style.HexNumer:
  99. sb.AppendFormat ("{0:x2}", id);
  100. break;
  101. }
  102. return sb.ToString ();
  103. }
  104. public override string ToString ()
  105. {
  106. var sb = new StringBuilder ();
  107. sb.Append ("[");
  108. for (int i = 0; i < scopes.Count; i++) {
  109. if (i > 0)
  110. sb.Append (delimiter);
  111. sb.Append (scopes [i].Text);
  112. }
  113. sb.Append ("]");
  114. return sb.ToString ();
  115. }
  116. }
  117. }