PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/IronRuby.Tests/Runtime/RubyUtilsTests.cs

http://github.com/IronLanguages/main
C# | 187 lines | 137 code | 25 blank | 25 comment | 110 complexity | 8360679ce27f89ff995029303b8de644 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using IronRuby.Runtime;
  17. using IronRuby.Builtins;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. namespace IronRuby.Tests {
  21. public partial class Tests {
  22. public void NameMangling1() {
  23. Assert(RubyUtils.TryUnmangleName("stack") == "Stack");
  24. Assert(RubyUtils.TryUnmangleName("this_is_my_long_name") == "ThisIsMyLongName");
  25. Assert(RubyUtils.TryUnmangleName("f") == "F");
  26. Assert(RubyUtils.TryUnmangleName("initialize") == "Initialize");
  27. // non-alpha characters are treated as lower-case letters:
  28. Assert(RubyUtils.TryUnmangleName("foo_bar=") == "FooBar=");
  29. Assert(RubyUtils.TryUnmangleName("foo?") == "Foo?");
  30. Assert(RubyUtils.TryUnmangleName("???") == "???");
  31. Assert(RubyUtils.TryUnmangleName("1_2_3") == "123");
  32. // special cases:
  33. Assert(RubyUtils.TryUnmangleName("on") == "On");
  34. Assert(RubyUtils.TryUnmangleName("or") == "Or");
  35. Assert(RubyUtils.TryUnmangleName("up") == "Up");
  36. Assert(RubyUtils.TryUnmangleName("in") == "In");
  37. Assert(RubyUtils.TryUnmangleName("to") == "To");
  38. Assert(RubyUtils.TryUnmangleName("of") == "Of");
  39. Assert(RubyUtils.TryUnmangleName("it") == "It");
  40. Assert(RubyUtils.TryUnmangleName("is") == "Is");
  41. Assert(RubyUtils.TryUnmangleName("if") == "If");
  42. Assert(RubyUtils.TryUnmangleName("go") == "Go");
  43. Assert(RubyUtils.TryUnmangleName("do") == "Do");
  44. Assert(RubyUtils.TryUnmangleName("by") == "By");
  45. Assert(RubyUtils.TryUnmangleName("at") == "At");
  46. Assert(RubyUtils.TryUnmangleName("as") == "As");
  47. Assert(RubyUtils.TryUnmangleName("my") == "My");
  48. Assert(RubyUtils.TryUnmangleName("me") == "Me");
  49. Assert(RubyUtils.TryUnmangleName("no") == "No");
  50. Assert(RubyUtils.TryUnmangleName("ip") == "IP");
  51. Assert(RubyUtils.TryUnmangleName("rx") == "RX");
  52. Assert(RubyUtils.TryUnmangleName("pi") == "PI");
  53. Assert(RubyUtils.TryUnmangleName("na_n") == null);
  54. Assert(RubyUtils.TryUnmangleName("nan") == "Nan");
  55. Assert(RubyUtils.TryUnmangleName("ip_address") == "IPAddress");
  56. Assert(RubyUtils.TryUnmangleName("i_id_id") == "IIdId");
  57. Assert(RubyUtils.TryUnmangleName("i_ip_ip") == null);
  58. Assert(RubyUtils.TryUnmangleName("ip_foo_ip") == "IPFooIP");
  59. Assert(RubyUtils.TryUnmangleName("active_x") == "ActiveX");
  60. Assert(RubyUtils.TryUnmangleName("get_u_int16") == "GetUInt16");
  61. Assert(RubyUtils.TryUnmangleName("get_ui_parent_core") == "GetUIParentCore");
  62. // TODO: ???
  63. Assert(RubyUtils.TryUnmangleName("i_pv6") == "IPv6");
  64. // names that cannot be mangled:
  65. Assert(RubyUtils.TryUnmangleName("") == null);
  66. Assert(RubyUtils.TryUnmangleName("IPX") == null);
  67. Assert(RubyUtils.TryUnmangleName("FO") == null);
  68. Assert(RubyUtils.TryUnmangleName("FOOBar") == null);
  69. Assert(RubyUtils.TryUnmangleName("FooBAR") == null);
  70. Assert(RubyUtils.TryUnmangleName("foo__bar") == null);
  71. Assert(RubyUtils.TryUnmangleName("_foo") == null);
  72. Assert(RubyUtils.TryUnmangleName("foo_") == null);
  73. // special method names:
  74. Assert(RubyUtils.TryUnmangleMethodName("initialize") == null);
  75. Assert(RubyUtils.TryUnmangleMethodName("class") == null);
  76. Assert(RubyUtils.TryUnmangleMethodName("message") == "Message"); // we don't special case Exception.Message
  77. }
  78. public void NameMangling2() {
  79. Assert(RubyUtils.TryMangleName("Stack") == "stack");
  80. Assert(RubyUtils.TryMangleName("ThisIsMyLongName") == "this_is_my_long_name");
  81. Assert(RubyUtils.TryMangleName("F") == "f");
  82. Assert(RubyUtils.TryMangleName("Initialize") == "initialize");
  83. Assert(RubyUtils.TryMangleName("fooBar") == "foo_bar");
  84. // characters that are not upper case letters are treated as lower-case:
  85. Assert(RubyUtils.TryMangleName("Foo123bar") == "foo123bar");
  86. Assert(RubyUtils.TryMangleName("123Bar") == "123_bar");
  87. Assert(RubyUtils.TryMangleName("?Bar") == "?_bar");
  88. // special cases:
  89. Assert(RubyUtils.TryUnmangleName("ON") == null);
  90. Assert(RubyUtils.TryUnmangleName("OR") == null);
  91. Assert(RubyUtils.TryUnmangleName("UP") == null);
  92. Assert(RubyUtils.TryUnmangleName("IN") == null);
  93. Assert(RubyUtils.TryUnmangleName("TO") == null);
  94. Assert(RubyUtils.TryUnmangleName("OF") == null);
  95. Assert(RubyUtils.TryUnmangleName("IT") == null);
  96. Assert(RubyUtils.TryUnmangleName("IF") == null);
  97. Assert(RubyUtils.TryUnmangleName("IS") == null);
  98. Assert(RubyUtils.TryUnmangleName("GO") == null);
  99. Assert(RubyUtils.TryUnmangleName("DO") == null);
  100. Assert(RubyUtils.TryUnmangleName("BY") == null);
  101. Assert(RubyUtils.TryUnmangleName("AT") == null);
  102. Assert(RubyUtils.TryUnmangleName("AS") == null);
  103. Assert(RubyUtils.TryUnmangleName("MY") == null);
  104. Assert(RubyUtils.TryUnmangleName("ME") == null);
  105. Assert(RubyUtils.TryUnmangleName("ID") == null);
  106. Assert(RubyUtils.TryUnmangleName("OK") == null);
  107. Assert(RubyUtils.TryUnmangleName("NO") == null);
  108. Assert(RubyUtils.TryMangleName("NaN") == null);
  109. Assert(RubyUtils.TryMangleName("NaNValue") == null);
  110. Assert(RubyUtils.TryMangleName("At") == "at");
  111. Assert(RubyUtils.TryMangleName("IP") == "ip");
  112. Assert(RubyUtils.TryMangleName("FO") == "fo");
  113. Assert(RubyUtils.TryMangleName("PI") == "pi");
  114. Assert(RubyUtils.TryMangleName("IPAddress") == "ip_address");
  115. Assert(RubyUtils.TryMangleName("MyDB") == "my_db");
  116. Assert(RubyUtils.TryMangleName("PyPy") == "py_py");
  117. Assert(RubyUtils.TryMangleName("IPFooIP") == "ip_foo_ip");
  118. Assert(RubyUtils.TryMangleName("ActiveX") == "active_x");
  119. Assert(RubyUtils.TryMangleName("GetUInt16") == "get_u_int16");
  120. Assert(RubyUtils.TryMangleName("GetUIParentCore") == "get_ui_parent_core");
  121. // TODO: ???
  122. Assert(RubyUtils.TryMangleName("IPv6") == "i_pv6");
  123. // names that cannot be mangled:
  124. Assert(RubyUtils.TryMangleName("") == null);
  125. Assert(RubyUtils.TryMangleName("IPX") == null);
  126. Assert(RubyUtils.TryMangleName("FOO") == null);
  127. Assert(RubyUtils.TryMangleName("FOOBar") == null);
  128. Assert(RubyUtils.TryMangleName("FooBAR") == null);
  129. Assert(RubyUtils.TryMangleName("foo") == null);
  130. Assert(RubyUtils.TryMangleName("initialize") == null);
  131. // name containing underscore(s) cannot be mangled:
  132. Assert(RubyUtils.TryMangleName("a_b") == null);
  133. Assert(RubyUtils.TryMangleName("add_Foo") == null);
  134. Assert(RubyUtils.TryMangleName("B__") == null);
  135. Assert(RubyUtils.TryMangleName("foo_bar=") == null);
  136. Assert(RubyUtils.TryMangleName("foo__bar") == null);
  137. Assert(RubyUtils.TryMangleName("_foo") == null);
  138. Assert(RubyUtils.TryMangleName("foo_") == null);
  139. // special method names:
  140. Assert(RubyUtils.TryMangleMethodName("Initialize") == null);
  141. Assert(RubyUtils.TryMangleMethodName("Class") == null);
  142. Assert(RubyUtils.TryMangleMethodName("Message") == "message"); // we don't special case Exception.Message
  143. }
  144. [Options(NoRuntime = true)]
  145. public void DelegateChainClone1() {
  146. StringBuilder sb = new StringBuilder();
  147. Action<RubyModule> f = (_) => { sb.Append('1'); };
  148. f += (_) => { sb.Append('2'); };
  149. f += (_) => { sb.Append('3'); };
  150. f(null);
  151. Assert(sb.ToString() == "123");
  152. sb.Length = 0;
  153. Action<RubyModule> g = Utils.CloneInvocationChain(f);
  154. g += (_) => { sb.Append('4'); };
  155. g(null);
  156. Assert(sb.ToString() == "1234");
  157. sb.Length = 0;
  158. f(null);
  159. Assert(sb.ToString() == "123");
  160. }
  161. }
  162. }