Permalink
Browse files

BaseClass with tests, FuncMethod with test

  • Loading branch information...
1 parent 103218f commit 1b3549124d2b1ffac7315123373da689dff253d6 @ajlopez committed Apr 22, 2012
@@ -61,7 +61,9 @@
<Compile Include="Expressions\DotExpressionTests.cs" />
<Compile Include="Expressions\VariableExpressionTests.cs" />
<Compile Include="Functions\PrintTests.cs" />
+ <Compile Include="Language\BaseClassTests.cs" />
<Compile Include="Language\BaseObjectTests.cs" />
+ <Compile Include="Language\FuncMethodTests.cs" />
<Compile Include="Language\PredicateTests.cs" />
<Compile Include="ObjectUtilitiesTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -0,0 +1,70 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using DartSharp.Language;
+
+namespace DartSharp.Tests.Language
+{
+ [TestClass]
+ public class BaseClassTests
+ {
+ [TestMethod]
+ public void ClassWithoutSuperclass()
+ {
+ IClass klass = new BaseClass("Object", null);
+ Assert.AreEqual("Object", klass.Name);
+ Assert.IsNull(klass.Super);
+ }
+
+ [TestMethod]
+ public void ClassWithSuperclass()
+ {
+ IClass super = new BaseClass("Object", null);
+ IClass klass = new BaseClass("Rectangle", super);
+ Assert.AreEqual("Rectangle", klass.Name);
+ Assert.AreEqual(super, klass.Super);
+ }
+
+ [TestMethod]
+ public void UnknowVariableAsNullType()
+ {
+ IClass klass = new BaseClass("Object", null);
+ Assert.IsNull(klass.GetVariableType("a"));
+ }
+
+ [TestMethod]
+ public void DefineVariable()
+ {
+ IClass type = new BaseClass("int", null);
+ IClass klass = new BaseClass("MyClass", null);
+ klass.DefineVariable("age", type);
+ var result = klass.GetVariableType("age");
+ Assert.IsNotNull(result);
+ Assert.AreEqual(type, result);
+ }
+
+ [TestMethod]
+ public void DefineVariableAndGetVariableFromSuper()
+ {
+ IClass type = new BaseClass("int", null);
+ IClass super = new BaseClass("MySuperclass", null);
+ IClass klass = new BaseClass("MyClass", super);
+ super.DefineVariable("age", type);
+ var result = klass.GetVariableType("age");
+ Assert.IsNotNull(result);
+ Assert.AreEqual(type, result);
+ }
+
+ [TestMethod]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void RaiseIfVariableIsRedefined()
+ {
+ IClass type = new BaseClass("int", null);
+ IClass klass = new BaseClass("MyClass", null);
+ klass.DefineVariable("age", type);
+ klass.DefineVariable("age", type);
+ }
+ }
+}
@@ -10,19 +10,45 @@ namespace DartSharp.Tests.Language
[TestClass]
public class BaseObjectTests
{
+ private IClass klass;
+
+ [TestInitialize]
+ public void Setup()
+ {
+ IClass type = new BaseClass("String", null);
+ this.klass = new BaseClass("MyClass", null);
+ this.klass.DefineVariable("name", type);
+ }
+
[TestMethod]
- public void GetValueAsNull()
+ public void GetInstanceVariableAsNull()
{
- IObject obj = new BaseObject(null);
+ IObject obj = new BaseObject(klass);
Assert.IsNull(obj.GetValue("name"));
}
[TestMethod]
- public void SetAndGetValue()
+ public void SetAndGetInstanceVariable()
{
- IObject obj = new BaseObject(null);
+ IObject obj = new BaseObject(klass);
obj.SetValue("name", "Adam");
Assert.AreEqual("Adam", obj.GetValue("name"));
}
+
+ [TestMethod]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void RaiseWhenGetUndefinedVariable()
+ {
+ IObject obj = new BaseObject(klass);
+ obj.GetValue("length");
+ }
+
+ [TestMethod]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void RaiseWhenSetUndefinedVariable()
+ {
+ IObject obj = new BaseObject(klass);
+ obj.SetValue("length", 100);
+ }
}
}
@@ -0,0 +1,21 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using DartSharp.Language;
+
+namespace DartSharp.Tests.Language
+{
+ [TestClass]
+ public class FuncMethodTests
+ {
+ [TestMethod]
+ public void DefineAndCallFuncMethod()
+ {
+ IMethod method = new FuncMethod(null, (obj, context, args) => ((string)obj).Length);
+ Assert.IsNull(method.Type);
+ Assert.AreEqual(3, method.Call("foo", null, null));
+ }
+ }
+}
@@ -77,8 +77,11 @@
<Compile Include="Expressions\IExpression.cs" />
<Compile Include="Expressions\VariableExpression.cs" />
<Compile Include="Functions\DefinedFunction.cs" />
+ <Compile Include="Language\BaseClass.cs" />
<Compile Include="Language\BaseObject.cs" />
<Compile Include="Language\ComparisonOperator.cs" />
+ <Compile Include="Language\IType.cs" />
+ <Compile Include="Language\FuncMethod.cs" />
<Compile Include="Language\IMethod.cs" />
<Compile Include="Language\ICallable.cs" />
<Compile Include="Language\IObject.cs" />
@@ -0,0 +1,56 @@
+namespace DartSharp.Language
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+
+ public class BaseClass : IClass
+ {
+ private IClass super;
+ private string name;
+ private Dictionary<string, IMethod> methods;
+ private Dictionary<string, IType> variables;
+
+ public BaseClass(string name, IClass super)
+ {
+ this.name = name;
+ this.super = super;
+ }
+
+ public string Name { get { return this.name; } }
+
+ public IType Super { get { return this.super; } }
+
+ public void DefineVariable(string name, IType type)
+ {
+ if (this.variables == null)
+ this.variables = new Dictionary<string, IType>();
+ else if (this.variables.ContainsKey(name))
+ throw new InvalidOperationException("Variable already defined");
+
+ this.variables[name] = type;
+ }
+
+ public void DefineMethod(string name, IMethod method)
+ {
+ throw new NotImplementedException();
+ }
+
+ public IType GetVariableType(string name)
+ {
+ if (this.variables == null || !this.variables.ContainsKey(name))
+ if (this.super != null)
+ return this.super.GetVariableType(name);
+ else
+ return null;
+
+ return this.variables[name];
+ }
+
+ public IMethod GetMethod(string name)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
@@ -12,6 +12,9 @@ public class BaseObject : IObject
public BaseObject(IClass @class)
{
+ if (@class == null)
+ throw new ArgumentNullException("class");
+
this.@class = @class;
}
@@ -22,6 +25,9 @@ public IClass Class
public object GetValue(string name)
{
+ if (this.@class.GetVariableType(name) == null)
+ throw new InvalidOperationException(string.Format("Undefined Variable '{0}'", name));
+
if (this.values == null || !this.values.ContainsKey(name))
return null;
@@ -30,16 +36,14 @@ public object GetValue(string name)
public void SetValue(string name, object value)
{
+ if (this.@class.GetVariableType(name) == null)
+ throw new InvalidOperationException(string.Format("Undefined Variable '{0}'", name));
+
if (this.values == null)
this.values = new Dictionary<string, object>();
this.values[name] = value;
}
- public IEnumerable<string> GetNames()
- {
- throw new NotImplementedException();
- }
-
public object Invoke(string name, object[] parameters)
{
throw new NotImplementedException();
@@ -0,0 +1,26 @@
+namespace DartSharp.Language
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+
+ public class FuncMethod : IMethod
+ {
+ private IType type;
+ private Func<object, Context, object[], object> func;
+
+ public FuncMethod(IType type, Func<object, Context, object[], object> func)
+ {
+ this.type = type;
+ this.func = func;
+ }
+
+ public IType Type { get { return this.type; } }
+
+ public object Call(object self, Context context, object[] arguments)
+ {
+ return this.func(self, context, arguments);
+ }
+ }
+}
@@ -5,16 +5,10 @@
using System.Linq;
using System.Text;
- public interface IClass
+ public interface IClass : IType
{
- IClass Superclass { get; }
-
- void DefineVariable(string name, IClass type);
+ void DefineVariable(string name, IType type);
void DefineMethod(string name, IMethod method);
-
- IClass GetVariableType(string name);
-
- IMethod GetMethod(string name);
}
}
@@ -7,8 +7,8 @@
public interface IMethod
{
- IClass Type { get; }
+ IType Type { get; }
- object Call(object self, Context context, IList<object> arguments);
+ object Call(object self, Context context, object[] arguments);
}
}
@@ -13,8 +13,6 @@ public interface IObject
void SetValue(string name, object value);
- IEnumerable<string> GetNames();
-
object Invoke(string name, object[] parameters);
}
}
@@ -0,0 +1,18 @@
+namespace DartSharp.Language
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+
+ public interface IType
+ {
+ string Name { get; }
+
+ IType Super { get; }
+
+ IType GetVariableType(string name);
+
+ IMethod GetMethod(string name);
+ }
+}

0 comments on commit 1b35491

Please sign in to comment.