Skip to content

Commit

Permalink
Refactor IFunction implementations, using self
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Dec 20, 2014
1 parent 8f12a97 commit 3196608
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Src/Mass.Core.Tests/Expressions/FunctionExpressionTests.cs
Expand Up @@ -26,7 +26,7 @@ public void EvaluateFunctionExpression()

var function = (IFunction)result;

Assert.AreEqual(3, function.Apply(new object[] { 1, 2 }));
Assert.AreEqual(3, function.Apply(null, new object[] { 1, 2 }));
}

[TestMethod]
Expand Down
4 changes: 2 additions & 2 deletions Src/Mass.Core.Tests/Functions/DefinedFunctionTests.cs
Expand Up @@ -23,7 +23,7 @@ public void DefineAndExecuteSimplePrintln()

DefinedFunction function = new DefinedFunction(new ExpressionCommand(new CallExpression(new NameExpression("puts"), new IExpression[] { new ConstantExpression(123) })), new string[] { }, machine.RootContext);

Assert.IsNull(function.Apply(new object[] { }));
Assert.IsNull(function.Apply(null, new object[] { }));
Assert.AreEqual("123\r\n", writer.ToString());
}

Expand All @@ -34,7 +34,7 @@ public void DefineAndExecuteFunctionWithParameters()

DefinedFunction function = new DefinedFunction(new ExpressionCommand(new BinaryArithmeticExpression(new NameExpression("a"), new NameExpression("b"), ArithmeticOperator.Add)), new string[] { "a", "b" }, context);

var result = function.Apply(new object[] { 1, 2 });
var result = function.Apply(null, new object[] { 1, 2 });

Assert.IsNotNull(result);
Assert.AreEqual(3, result);
Expand Down
4 changes: 2 additions & 2 deletions Src/Mass.Core.Tests/Functions/PrintFunctionTests.cs
Expand Up @@ -17,7 +17,7 @@ public void PrintInteger()
StringWriter writer = new StringWriter();
PrintFunction function = new PrintFunction(writer);

Assert.IsNull(function.Apply(new object[] { 123 }));
Assert.IsNull(function.Apply(null, new object[] { 123 }));

Assert.AreEqual("123", writer.ToString());
}
Expand All @@ -39,7 +39,7 @@ public void PrintTwoIntegers()
StringWriter writer = new StringWriter();
PrintFunction function = new PrintFunction(writer);

Assert.IsNull(function.Apply(new object[] { 123, 456 }));
Assert.IsNull(function.Apply(null, new object[] { 123, 456 }));

Assert.AreEqual("123456", writer.ToString());
}
Expand Down
4 changes: 2 additions & 2 deletions Src/Mass.Core.Tests/Functions/PrintlnFunctionTests.cs
Expand Up @@ -17,7 +17,7 @@ public void PrintlnInteger()
StringWriter writer = new StringWriter();
PrintlnFunction function = new PrintlnFunction(writer);

Assert.IsNull(function.Apply(new object[] { 123 }));
Assert.IsNull(function.Apply(null, new object[] { 123 }));

Assert.AreEqual("123\r\n", writer.ToString());
}
Expand All @@ -39,7 +39,7 @@ public void PrintlnTwoIntegers()
StringWriter writer = new StringWriter();
PrintlnFunction function = new PrintlnFunction(writer);

Assert.IsNull(function.Apply(new object[] { 123, 456 }));
Assert.IsNull(function.Apply(null, new object[] { 123, 456 }));

Assert.AreEqual("123\r\n456\r\n", writer.ToString());
}
Expand Down
16 changes: 8 additions & 8 deletions Src/Mass.Core.Tests/Functions/RequireFunctionTests.cs
Expand Up @@ -18,7 +18,7 @@ public void RequireLocalFile()
Machine machine = new Machine();
RequireFunction require = new RequireFunction(machine);

AssertModule(require.Apply(new object[] { "SimpleModule.ms" }));
AssertModule(require.Apply(null, new object[] { "SimpleModule.ms" }));
}

[TestMethod]
Expand All @@ -38,8 +38,8 @@ public void RequireLocalFileTwice()
Machine machine = new Machine();
RequireFunction require = new RequireFunction(machine);

var original = require.Apply(new object[] { "SimpleModule" });
var result = require.Apply(new object[] { "SimpleModule" });
var original = require.Apply(null, new object[] { "SimpleModule" });
var result = require.Apply(null, new object[] { "SimpleModule" });

AssertModule(result);
Assert.AreSame(original, result);
Expand All @@ -52,7 +52,7 @@ public void RequireLocalFileWithExplicitExtension()
Machine machine = new Machine();
RequireFunction require = new RequireFunction(machine);

AssertModule(require.Apply(new object[] { "SimpleModule.ms" }));
AssertModule(require.Apply(null, new object[] { "SimpleModule.ms" }));
}

[TestMethod]
Expand All @@ -62,7 +62,7 @@ public void RequireLocalFileWithLocalDirectory()
Machine machine = new Machine();
RequireFunction require = new RequireFunction(machine);

AssertModule(require.Apply(new object[] { "./SimpleModule" }));
AssertModule(require.Apply(null, new object[] { "./SimpleModule" }));
}

[TestMethod]
Expand All @@ -73,7 +73,7 @@ public void RequireUnknownModule()

try
{
require.Apply(new object[] { "unknown" });
require.Apply(null, new object[] { "unknown" });
Assert.Fail();
}
catch (Exception ex)
Expand All @@ -91,7 +91,7 @@ public void RequireUnknownLocalModule()

try
{
require.Apply(new object[] { "./unknown" });
require.Apply(null, new object[] { "./unknown" });
Assert.Fail();
}
catch (Exception ex)
Expand All @@ -109,7 +109,7 @@ public void RequireUnknownAbsoluteModule()

try
{
require.Apply(new object[] { "/unknown" });
require.Apply(null, new object[] { "/unknown" });
Assert.Fail();
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion Src/Mass.Core/Expressions/CallExpression.cs
Expand Up @@ -28,7 +28,7 @@ public object Evaluate(Context context)
foreach (var argument in this.arguments)
values.Add(argument.Evaluate(context));

return function.Apply(values);
return function.Apply(null, values);
}

public override bool Equals(object obj)
Expand Down
6 changes: 0 additions & 6 deletions Src/Mass.Core/Functions/DefinedFunction.cs
Expand Up @@ -20,12 +20,6 @@ public DefinedFunction(ICommand body, IList<string> parameters, Context context)
this.parameters = parameters;
}

public object Apply(IList<object> values)
{
Context newcontext = new Context(this.context, null);
return this.DoApply(newcontext, values);
}

public object Apply(object self, IList<object> values)
{
Context newcontext = new Context(this.context, null);
Expand Down
2 changes: 0 additions & 2 deletions Src/Mass.Core/Functions/IFunction.cs
Expand Up @@ -8,8 +8,6 @@

public interface IFunction
{
object Apply(IList<object> values);

object Apply(object self, IList<object> values);
}
}
7 changes: 1 addition & 6 deletions Src/Mass.Core/Functions/PrintFunction.cs
Expand Up @@ -16,17 +16,12 @@ public PrintFunction(TextWriter writer)
this.writer = writer;
}

public object Apply(IList<object> values)
public object Apply(object self, IList<object> values)
{
foreach (var value in values)
this.writer.Write(value);

return null;
}

public object Apply(object self, IList<object> values)
{
return this.Apply(values);
}
}
}
7 changes: 1 addition & 6 deletions Src/Mass.Core/Functions/PrintlnFunction.cs
Expand Up @@ -16,17 +16,12 @@ public PrintlnFunction(TextWriter writer)
this.writer = writer;
}

public object Apply(IList<object> values)
public object Apply(object self, IList<object> values)
{
foreach (var value in values)
this.writer.WriteLine(value);

return null;
}

public object Apply(object self, IList<object> values)
{
return this.Apply(values);
}
}
}
7 changes: 1 addition & 6 deletions Src/Mass.Core/Functions/RequireFunction.cs
Expand Up @@ -23,7 +23,7 @@ public RequireFunction(Machine machine, string path)
this.path = path;
}

public object Apply(IList<object> values)
public object Apply(object self, IList<object> values)
{
string name = (string)values[0];

Expand All @@ -38,11 +38,6 @@ public object Apply(IList<object> values)
return this.machine.ExecuteFile(filename, true);
}

public object Apply(object self, IList<object> values)
{
return this.Apply(values);
}

private string GetFilename(string path, string name)
{
string filename = name;
Expand Down

0 comments on commit 3196608

Please sign in to comment.