PageRenderTime 33ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Boo.Lang.Runtime.Tests/RuntimeCoercionTestCase.cs

https://github.com/boo/boo-lang
C# | 103 lines | 88 code | 15 blank | 0 comment | 0 complexity | 9eba40b51642ead03e4e4ef8a3bc5078 MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using NUnit.Framework;
  3. namespace Boo.Lang.Runtime.Tests
  4. {
  5. class Coercible : ICoercible
  6. {
  7. public List Invocations = new List();
  8. public object Coerce(Type to)
  9. {
  10. Invocations.Add(to);
  11. return this;
  12. }
  13. }
  14. internal class Integer
  15. {
  16. protected int _value;
  17. public Integer(int value)
  18. {
  19. _value = value;
  20. }
  21. public int Value
  22. {
  23. get { return _value; }
  24. }
  25. }
  26. class IntegerExtensions
  27. {
  28. [Boo.Lang.Extension]
  29. public static int op_Implicit(Integer i)
  30. {
  31. return i.Value;
  32. }
  33. }
  34. class ImplicitInt : Integer
  35. {
  36. public ImplicitInt(int value) : base(value)
  37. {
  38. }
  39. public static implicit operator int(ImplicitInt i)
  40. {
  41. return i._value;
  42. }
  43. }
  44. [TestFixture]
  45. public class RuntimeCoercionTestCase
  46. {
  47. [Test]
  48. public void TestICoercible()
  49. {
  50. Coercible c = new Coercible();
  51. Assert.AreSame(c, Coerce(c, typeof (string)));
  52. Assert.AreSame(c, Coerce(c, typeof (int)));
  53. Assert.AreEqual(new List(new object[] { typeof(string), typeof(int) }), c.Invocations);
  54. }
  55. [Test]
  56. public void TestNumericPromotion()
  57. {
  58. Assert.AreEqual(42, Coerce(41.51, typeof(int)));
  59. }
  60. [Test]
  61. public void TestImplicitCast()
  62. {
  63. ImplicitInt i = new ImplicitInt(42);
  64. Assert.AreSame(i, Coerce(i, i.GetType()));
  65. Assert.AreEqual(42, Coerce(i, typeof(int)));
  66. }
  67. [Test]
  68. public void TestImplicitCastExtension()
  69. {
  70. Integer i = new Integer(42);
  71. RuntimeServices.WithExtensions(typeof (IntegerExtensions), delegate
  72. {
  73. Assert.AreEqual(42, Coerce(i, typeof (int)));
  74. });
  75. }
  76. [Test]
  77. public void TestIdentity()
  78. {
  79. string s = "foo";
  80. Assert.AreSame(s, Coerce(s, typeof(string)));
  81. Assert.AreSame(s, Coerce(s, typeof(object)));
  82. }
  83. static object Coerce(object value, Type to)
  84. {
  85. return RuntimeServices.Coerce(value, to);
  86. }
  87. }
  88. }