PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/RuntimeTests/StepExecutionTestsWithConversions.cs

http://github.com/techtalk/SpecFlow
C# | 180 lines | 130 code | 46 blank | 4 comment | 0 complexity | 92783e700e989d052fb3ba2d71938bce MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Globalization;
  3. using NUnit.Framework;
  4. using Rhino.Mocks;
  5. using TechTalk.SpecFlow.Bindings;
  6. using TechTalk.SpecFlow.Bindings.Reflection;
  7. using TechTalk.SpecFlow.Infrastructure;
  8. using TestStatus = TechTalk.SpecFlow.Infrastructure.TestStatus;
  9. namespace TechTalk.SpecFlow.RuntimeTests
  10. {
  11. internal static class LegacyStepArgumentTypeConverterExtensions
  12. {
  13. public static object Convert(this IStepArgumentTypeConverter converter, object value, Type typeToConvertTo, CultureInfo cultureInfo)
  14. {
  15. return converter.Convert(value, new RuntimeBindingType(typeToConvertTo), cultureInfo);
  16. }
  17. public static Function<IStepArgumentTypeConverter, bool> GetCanConvertMethodFilter(object argument, Type type)
  18. {
  19. return c => c.CanConvert(
  20. Arg<string>.Is.Equal(argument),
  21. Arg<IBindingType>.Matches(bt => bt.TypeEquals(type)),
  22. Arg<CultureInfo>.Is.Anything);
  23. }
  24. public static Function<IStepArgumentTypeConverter, object> GetConvertMethodFilter(object argument, Type type)
  25. {
  26. return c => c.Convert(
  27. Arg<string>.Is.Equal(argument),
  28. Arg<IBindingType>.Matches(bt => bt.TypeEquals(type)),
  29. Arg<CultureInfo>.Is.Anything);
  30. }
  31. }
  32. [Binding]
  33. public class StepExecutionTestsBindingsForArgumentConvert
  34. {
  35. [Given("sample step for argument convert: (.*)")]
  36. public virtual void IntArg(int param)
  37. {
  38. }
  39. [Given("sample step for argument convert: (.*)")]
  40. public virtual void DoubleArg(double param)
  41. {
  42. }
  43. [Given("sample step for argument convert with table: (.*)")]
  44. public virtual void IntArgWithTable(int param, Table table)
  45. {
  46. }
  47. [Given("sample step for argument convert with table: (.*)")]
  48. public virtual void DoubleArgWithTable(double param, Table table)
  49. {
  50. }
  51. }
  52. [TestFixture]
  53. public class StepExecutionTestsWithConversions : StepExecutionTestsBase
  54. {
  55. [Test]
  56. public void ShouldCallBindingWithSimpleConvertParam()
  57. {
  58. StepExecutionTestsBindings bindingInstance;
  59. TestRunner testRunner = GetTestRunnerFor(out bindingInstance);
  60. bindingInstance.Expect(b => b.BindingWithSimpleConvertParam(1.23));
  61. MockRepository.ReplayAll();
  62. testRunner.Given("sample step with simple convert param: 1.23");
  63. Assert.AreEqual(TestStatus.OK, GetLastTestStatus());
  64. MockRepository.VerifyAll();
  65. }
  66. [Test]
  67. public void ShouldRaiseErrorIfSimpleConvertParamFails()
  68. {
  69. StepExecutionTestsBindings bindingInstance;
  70. TestRunner testRunner = GetTestRunnerFor(out bindingInstance);
  71. MockRepository.ReplayAll();
  72. testRunner.Given("sample step with simple convert param: not-a-double");
  73. Assert.AreEqual(TestStatus.TestError, GetLastTestStatus());
  74. MockRepository.VerifyAll();
  75. }
  76. [Test]
  77. public void ShouldCallTheOnlyThatCanConvert()
  78. {
  79. StepExecutionTestsBindingsForArgumentConvert bindingInstance;
  80. TestRunner testRunner = GetTestRunnerWithConverterStub(out bindingInstance);
  81. // return false unless its a Double
  82. StepArgumentTypeConverterStub.Stub(LegacyStepArgumentTypeConverterExtensions.GetCanConvertMethodFilter("argument", typeof(double))).Return(true);
  83. StepArgumentTypeConverterStub.Stub(c => c.CanConvert(null, null, null)).IgnoreArguments().Return(false);
  84. StepArgumentTypeConverterStub.Expect(LegacyStepArgumentTypeConverterExtensions.GetConvertMethodFilter("argument", typeof(double))).Return(1.23);
  85. bindingInstance.Expect(b => b.DoubleArg(1.23));
  86. MockRepository.ReplayAll();
  87. testRunner.Given("sample step for argument convert: argument");
  88. Assert.AreEqual(TestStatus.OK, GetLastTestStatus());
  89. MockRepository.VerifyAll();
  90. }
  91. [Test]
  92. public void ShouldRaiseAmbiguousIfMultipleCanConvert()
  93. {
  94. StepExecutionTestsBindingsForArgumentConvert bindingInstance;
  95. TestRunner testRunner = GetTestRunnerWithConverterStub(out bindingInstance);
  96. // return false unless its a Double or an Int
  97. StepArgumentTypeConverterStub.Stub(LegacyStepArgumentTypeConverterExtensions.GetCanConvertMethodFilter("argument", typeof(double))).Return(true);
  98. StepArgumentTypeConverterStub.Stub(LegacyStepArgumentTypeConverterExtensions.GetCanConvertMethodFilter("argument", typeof(int))).Return(true);
  99. StepArgumentTypeConverterStub.Stub(c => c.CanConvert(null, null, null)).IgnoreArguments().Return(false);
  100. MockRepository.ReplayAll();
  101. testRunner.Given("sample step for argument convert: argument");
  102. Assert.AreEqual(TestStatus.BindingError, GetLastTestStatus());
  103. MockRepository.VerifyAll();
  104. }
  105. [Test]
  106. public void ShouldCallTheOnlyThatCanConvertWithTable()
  107. {
  108. StepExecutionTestsBindingsForArgumentConvert bindingInstance;
  109. TestRunner testRunner = GetTestRunnerWithConverterStub(out bindingInstance);
  110. Table table = new Table("h1");
  111. // return false unless its a Double or table->table
  112. StepArgumentTypeConverterStub.Stub(LegacyStepArgumentTypeConverterExtensions.GetCanConvertMethodFilter("argument", typeof(double))).Return(true);
  113. StepArgumentTypeConverterStub.Stub(c => c.CanConvert(null, null, null)).IgnoreArguments().Return(false);
  114. StepArgumentTypeConverterStub.Expect(LegacyStepArgumentTypeConverterExtensions.GetConvertMethodFilter("argument", typeof(double))).Return(1.23);
  115. bindingInstance.Expect(b => b.DoubleArgWithTable(1.23, table));
  116. MockRepository.ReplayAll();
  117. testRunner.Given("sample step for argument convert with table: argument", null, table);
  118. Assert.AreEqual(TestStatus.OK, GetLastTestStatus());
  119. MockRepository.VerifyAll();
  120. }
  121. [Test]
  122. public void ShouldRaiseParamErrorIfNoneCanConvert()
  123. {
  124. StepExecutionTestsBindingsForArgumentConvert bindingInstance;
  125. TestRunner testRunner = GetTestRunnerWithConverterStub(out bindingInstance);
  126. // none can convert
  127. StepArgumentTypeConverterStub.Stub(c => c.CanConvert(null, null, null)).IgnoreArguments().Return(false);
  128. MockRepository.ReplayAll();
  129. testRunner.Given("sample step for argument convert: argument");
  130. Assert.AreEqual(TestStatus.BindingError, GetLastTestStatus());
  131. MockRepository.VerifyAll();
  132. }
  133. }
  134. }