RoadieRich
RoadieRich

Reputation: 6586

Why does this throw exception

The test:

[TestMethod]
public void TestStringWithValidAndInvalid()
{
    MockRepository mocks = new MockRepository();
    ICentipedeCore mockCore = mocks.DynamicMock<ICentipedeCore>();
    IPythonEngine pythonEngine = mocks.StrictMock<IPythonEngine>();
    IPythonByteCode mockPythonByteCode = mocks.Stub<IPythonByteCode>();

    mockCore.Stub(c => c.PythonEngine)
            .Return(pythonEngine);

    pythonEngine.Expect(e => e.Compile(Arg<String>.Is.Equal("\"String\""),
                                       Arg<PythonByteCode.SourceCodeType>.Is.Anything))
                .Return(mockPythonByteCode);

    pythonEngine.Expect(e => e.Evaluate(Arg<IPythonByteCode>.Is.Equal(mockPythonByteCode),
                                        Arg<PythonScope>.Is.Anything))
                .Return(3);

    pythonEngine.Expect(e => e.Compile(Arg<String>.Is.Equal("this is invalid python"),
                                       Arg<PythonByteCode.SourceCodeType>.Is.Anything))
                .Throw(new PythonParseException(mocks.Stub<Exception>()));


    ActionWrapper testAction = new ActionWrapper(mockCore);

    var original = @"{1+2} with {invalid python}";
    var expected = "3 with {invalid python}";
    var result = testAction.ParseStringForVariable(original); // ActionTest.cs: line 267

    mocks.VerifyAll();
    Assert.AreEqual(expected, result);
}

The method under test (exposed by a wrapper):

protected String ParseStringForVariable([NotNull] String str)
{
    IPythonEngine pythonEngine = GetCurrentCore().PythonEngine;

    for (int i = 0; i < str.Length; i++)
    {
        if (str[i] != '{')
        {
            continue;
        }

        int opening = i;
        foreach (var expression in from closing in str.IndexesWhere('}'.Equals)
                                   where closing > opening
                                   select new
                                          {
                                              Template = str.Substring(opening, closing - opening + 1),
                                              Code = str.Substring(opening + 1, closing - opening - 1)
                                          })
        {
            IPythonByteCode compiled;
            try
            {
                compiled = pythonEngine.Compile(expression.Code, PythonByteCode.SourceCodeType.Expression);
            }
            catch (PythonParseException)
            {
                // not valid python, try next expression
                continue;
            }
            dynamic r = pythonEngine.Evaluate(compiled);
            String result = r.ToString(); //  Action.cs: line 217, wrapped at  ActionTest.cs: line 96
            str = str.Replace(expression.Template, result);
            break;
        }
    }

    return str;
}

The Exception:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

The exception is thrown on the line after dynamic r = pythonEngine.Evaluate(compiled); with r being null. but I don't know why - compiled has the same value as mockPythonByteCode, pythonEngine is the mock created in the test, and the identical call works in a different method. The only difference here, is that pythonEngine.Compile has two Expectations, on different inputs, with different results.

The problem is, I've got TestStringWithValidCode() and TestStringWithInvalidCode(), both working fine, which I merged to form TestStringWithValidAndInvalid(), so each half should work.

Upvotes: 1

Views: 400

Answers (1)

ΩmegaMan
ΩmegaMan

Reputation: 31721

A a check for null would be more prudent, because the due to the structure of the code, when a catch happens and compiled is null and it then runs code which happens to expect that compiled will not be null.

So

Change:

       catch (PythonParseException)
        {
            // not valid python, try next expression
            continue;
        }
        dynamic r = pythonEngine.Evaluate(compiled);

to

       catch (PythonParseException)
        {
            // not valid python, try next expression
            continue;
        }
       if (compiled != null)
       {
        dynamic r = pythonEngine.Evaluate(compiled);
        String result = r.ToString(); //  Action.cs: line 217, wrapped at  ActionTest.cs: line 96
        str = str.Replace(expression.Template, result);
       }
       else
       {
           str="Exception Caught";
       }
       break;

Upvotes: 1

Related Questions