Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 939319febe
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 182 lines (151 sloc) 6.227 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#region Usings

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.IronRuby.Controllers;
using System.Web.Mvc.IronRuby.Core;
using System.Web.Mvc.IronRuby.Tests.Core;
using System.Web.Mvc.IronRuby.Tests.Core;
using System.Web.Routing;
using System.Web.Mvc.IronRuby.Controllers;
using System.Web.Mvc.IronRuby.Core;
using System.Web.Mvc.IronRuby.Tests.Core;
using IronRuby;
using IronRuby.Runtime;
using Microsoft.Scripting.Hosting;
using Moq.Mvc;
using Rhino.Mocks;
using Xunit;

#endregion

namespace System.Web.Mvc.IronRuby.Tests.Controllers
{
    public abstract class with_ironruby_initialized<SystemUnderTest> : InstanceContextSpecification<SystemUnderTest>
        where SystemUnderTest : class
    {
        protected static ScriptRuntime _scriptRuntime;
        protected RubyContext _context;
        protected ScriptEngine _engine;

        protected override void EstablishContext()
        {
            if (_scriptRuntime == null)
            {
                var rubySetup = Ruby.CreateRubySetup();
                rubySetup.Options["InterpretedMode"] = true;

                var runtimeSetup = new ScriptRuntimeSetup();
                runtimeSetup.LanguageSetups.Add(rubySetup);
                runtimeSetup.DebugMode = true;

                _scriptRuntime = Ruby.CreateRuntime(runtimeSetup);
            }
            _engine = _scriptRuntime.GetRubyEngine();
            _context = Ruby.GetExecutionContext(_engine);
        }
    }

    public abstract class with_ironruby_and_an_engine_initialized<SystemUnderTest> :
        with_ironruby_initialized<SystemUnderTest> where SystemUnderTest : class
    {
        protected IPathProvider _pathProvider;
        protected IRubyEngine _rubyEngine;

        protected override void EstablishContext()
        {
            base.EstablishContext();


            _pathProvider = Dependency<IPathProvider>();
            _pathProvider.WhenToldTo(pp => pp.ApplicationPhysicalPath).Return(Environment.CurrentDirectory);

            _rubyEngine = new RubyEngine(_scriptRuntime, _pathProvider);
        }
    }

[Concern(typeof (RubyActionDescriptor))]
    public class when_descriptor_executes : with_ironruby_and_an_engine_initialized<RubyActionDescriptor>
    {
        private ControllerContext _controllerContext;
        private RubyControllerDescriptor _controllerDescriptor;
        private string _result;

        protected override void EstablishContext()
        {
            base.EstablishContext();
            var script = new StringBuilder();
            script.AppendLine("class SamuraisController < Controller");
            script.AppendLine(" def my_action");
            script.AppendLine(" \"Can't see ninjas\".to_clr_string");
            script.AppendLine(" end");
            script.AppendLine("end");
            _rubyEngine.ExecuteScript(script.ToString());

            var rubyClass = _rubyEngine.GetRubyClass("SamuraisController");
            var httpContext = new HttpContextMock().Object;
            var requestContext = new RequestContext(httpContext, new RouteData());

            var controller = _rubyEngine.CreateInstance<RubyController>(rubyClass);
            controller.InternalInitialize(new ControllerConfiguration { Context = requestContext, Engine = _rubyEngine, RubyClass = rubyClass });

            _controllerContext = new ControllerContext(requestContext, controller);

            _controllerDescriptor = new RubyControllerDescriptor(rubyClass, _rubyEngine);
        }

        protected override RubyActionDescriptor CreateSut()
        {
            return new RubyActionDescriptor("my_action", _controllerDescriptor, _rubyEngine);
        }

        protected override void Because()
        {
            _result = Sut.Execute(_controllerContext, new Dictionary<string, object>()).ToString();
        }

[Observation]
        public void should_execute_the_action()
        {
            _result.ShouldBeEqualTo("Can't see ninjas");
        }
    }

[Concern(typeof (RubyActionDescriptor))]
    public class when_descriptor_gets_initialized : with_ironruby_and_an_engine_initialized<RubyActionDescriptor>
    {
        private RubyControllerDescriptor _controllerDescriptor;

        protected override void EstablishContext()
        {
            base.EstablishContext();
            var script = new StringBuilder();
            script.AppendLine("class SamuraisController < Controller");
            script.AppendLine(" def my_action");
            script.AppendLine(" $counter = $counter + 5");
            script.AppendLine(" \"Can't see ninjas\".to_clr_string");
            script.AppendLine(" end");
            script.AppendLine("end");
            _rubyEngine.ExecuteScript(script.ToString());

            _controllerDescriptor =
                MockRepository.GenerateStub<RubyControllerDescriptor>(_rubyEngine.GetRubyClass("SamuraisController"), _rubyEngine);
        }

        protected override RubyActionDescriptor CreateSut()
        {
            return new RubyActionDescriptor("my_action", _controllerDescriptor, _rubyEngine);
        }

        protected override void Because()
        {
        }

[Observation]
        public void should_have_an_action_name()
        {
            Sut.ActionName.ShouldNotBeEmpty();
        }

[Observation]
        public void should_have_the_correct_action_name()
        {
            Sut.ActionName.ShouldBeEqualTo("my_action");
        }

[Observation]
        public void should_have_a_controller_descriptor()
        {
            Sut.ControllerDescriptor.ShouldNotBeNull();
        }

[Observation]
        public void should_have_a_ruby_controller_descriptor()
        {
            Sut.ControllerDescriptor.ShouldBeAnInstanceOf<RubyControllerDescriptor>();
        }

[Observation]
        public void should_have_the_correct_controller_descriptor()
        {
            Assert.Same(Sut.ControllerDescriptor, _controllerDescriptor);
        }

[Observation]
        public void should_return_an_empty_array_for_parameters()
        {
            Sut.GetParameters().ShouldBeEmpty();
        }
    }
}
Something went wrong with that request. Please try again.