Exemplo n.º 1
0
        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) } }));
        }