public void ErlFormatTest() { var tests = new Dictionary<string, ErlList> { {"abc 10.5", new ErlList("abc ~w.~w", 10, 5)}, {"xx 8", new ErlList("xx ~i~w", 12, 8)}, {"~z", new ErlList("~~z", 16)}, {"a 16", new ErlList("~c ~w", (byte)'a', 16)}, {"xyz 12\n", new ErlList("xyz ~10.6.B~n", 12)}, {"x~y21", new ErlList("x~~y~w1", 2)}, {"{ok, A}", new ErlList("{ok, ~v}", "A")}, {"{ok, A}.", new ErlList("{ok, ~v}.", new ErlAtom("A"))}, {"{ok, A} ", new ErlList("{ok, ~v} ", new ErlString("A"))}, {"{ok, A} ", new ErlList("{ok, ~v} ", new ErlVar("A"))}, { "{ok, A::a()}", new ErlList("{ok, ~v::a()}", new ErlVar("A", ErlTypeOrder.ErlLong)) }, {"{ok, A::int()}", new ErlList("{ok, ~v}", new ErlVar("A", ErlTypeOrder.ErlLong))}, }; foreach (var t in tests) Assert.AreEqual(t.Key, ErlObject.Format(t.Value), "Error in test: {0} <- format({1})".Args(t.Key, t.Value.ToString())); var failTests = new List<ErlList> { new ErlList("abc ~w.~w"), new ErlList("xx ~i~w", 12), new ErlList("~y", 12), }; foreach (var t in failTests) Assert.Throws<ErlException>( () => ErlObject.Format(t), "Errorneously formatted term: {0}".Args(t)); var V = new ErlVar("V", ErlTypeOrder.ErlLong); var expected = new ErlTuple(new ErlAtom("ok"), V).ToString(); Assert.AreEqual(expected, "{ok, V::int()}".ToErlObject().ToString()); Assert.AreEqual(expected, "{ok, ~w}".ToErlObject(V).ToString()); }
public IErlObject this[ErlVar name] { get { return(this[name.Name]); } }
/// <summary> /// Add a variable binding converting an object to variable by name /// </summary> /// <param name="var">Name of the variable</param> /// <param name="o">Value to associate with name</param> public void Add(ErlVar var, object o) { var eo = o.ToErlObject(var.ValueType); Add(var.Name, eo); }
public void ErlVarTest() { var t = ErlVar.Any; Assert.IsFalse(t.Equals(new ErlVar(ConstAtoms.ANY))); Assert.AreEqual(ConstAtoms.ANY, t.Name); Assert.AreEqual(ErlTypeOrder.ErlObject, t.ValueType); t = new ErlVar(N, ErlTypeOrder.ErlLong); Assert.AreEqual("N", t.Name.Value); Assert.AreEqual(ErlTypeOrder.ErlLong, t.ValueType); { var bind = new ErlVarBind(); Assert.IsTrue(t.Match(new ErlByte(10), bind)); Assert.AreEqual(10, bind["N"].ValueAsLong); bind.Clear(); var q = new ErlVar("N", ErlTypeOrder.ErlByte); Assert.IsTrue(q.Match(new ErlLong(111), bind)); Assert.AreEqual(111, bind["N"].ValueAsLong); } Assert.IsFalse(t.Matches(new ErlVar())); Assert.IsFalse(new ErlVar(A).Matches(new ErlVar(A))); Assert.IsFalse(new ErlVar(A).Matches(new ErlVar(B))); Assert.AreEqual(new ErlVarBind { { N, (ErlLong)10 } }, t.Match((ErlLong)10)); Assert.AreEqual(new ErlVarBind { { A, (ErlLong)10 } }, new ErlVar(A).Match((ErlLong)10)); Assert.AreEqual(-1, new ErlAtom("ok").CompareTo(t)); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsObject; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsInt; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsLong; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsDecimal; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsDateTime; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsTimeSpan; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsDouble; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsString; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsBool; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsChar; }); Assert.Throws<ErlIncompatibleTypesException>(() => { var x = t.ValueAsByteArray; }); Assert.AreEqual("N::int()", t.ToString()); Assert.IsTrue(t.IsScalar); Assert.AreEqual(ErlTypeOrder.ErlVar, t.TypeOrder); IErlObject temp = null; Assert.IsFalse(t.Subst(ref temp, new ErlVarBind { { M, new ErlLong(100) } })); Assert.IsTrue(t.Subst(ref temp, new ErlVarBind { { N, new ErlLong(100) } })); Assert.AreEqual(new ErlLong(100), temp); temp = new ErlVar(M, ErlTypeOrder.ErlLong); Assert.IsTrue(temp.Subst(ref temp, new ErlVarBind { { M, new ErlLong(100) } })); Assert.AreEqual(ErlTypeOrder.ErlVar, t.Visit(ErlTypeOrder.ErlByte, (acc, o) => ((ErlVar)o).TypeOrder)); Assert.AreEqual(new ErlLong(100), temp); temp = new ErlVar(N, ErlTypeOrder.ErlObject); Assert.IsTrue(temp.Subst(ref temp, new ErlVarBind { { N, new ErlLong(100) } })); // Invalid variable type temp = new ErlVar(N, ErlTypeOrder.ErlAtom); Assert.Throws<ErlException>(() => temp.Subst(ref temp, new ErlVarBind { { N, new ErlLong(100) } })); }