Exemple #1
0
        public void TestReadingStrings()
        {
            string[]      expected = new string[] { "reading", "\"this should\"", "be", "ok" };
            string        compact  = "reading \"this should\" be ok";
            string        padded   = "    reading    \"this should\"  be ok  \n";
            ISourceReader reader   = new SourceReader();

            reader.Initialize(compact);
            TestReader(reader, expected);
            reader.Initialize(padded);
            TestReader(reader, expected);
        }
        /// <summary>
        ///     <see cref="ISourceReaderFactory.MakeReader(string)" />
        /// </summary>
        public ISourceReader MakeReader(string source)
        {
            var reader = new SourceReader();

            reader.Initialize(source);
            return(reader);
        }
        void TestInput <TInput>(string code, bool expectError, IDictionary <string, Type> expectedParameters, string unexpectedName = null)
            where TInput : ISyntaxTreeItem, new()
        {
            ISourceReader reader = new SourceReader();

            reader.Initialize(code);
            TInput tested = new TInput();

            try
            {
                var exec = tested.Execute(reader, m_Scope, false);
                var pmap = exec.ExecuteNext() as IDictionary <string, Type>;

                Assert.IsFalse(expectError);
                foreach (var kvp in expectedParameters)
                {
                    Assert.IsTrue(pmap.ContainsKey(kvp.Key));
                    Assert.AreEqual(kvp.Value, pmap[kvp.Key]);
                }
                if (!string.IsNullOrEmpty(unexpectedName))
                {
                    Assert.IsFalse(pmap.ContainsKey(unexpectedName));
                }
            }
            catch (SyntaxException se)
            {
                se.RemoveUnusedWarning();
                Assert.IsTrue(expectError, se.Message);
            }
        }
Exemple #4
0
        public void TestScript()
        {
            ISourceReader   reader = new SourceReader();
            IExecutionScope scope  = new ScopeFactory().MakeScope();

            reader.Initialize("input ( \n $x is Int \n ) \n if inf $x 2 ( \n testcall \n ) \n else ( \n testargs $x \n )");
            IScript script = new Script(reader, scope);

            TestScript(script, new Dictionary <string, object>()
            {
                { "$x", 1 }
            }, false, "OK");
            TestScript(script, new Dictionary <string, object>()
            {
                { "$x", 2 }
            }, false, 2);
            TestScript(script, new Dictionary <string, object>()
            {
                { "$y", 1 }
            }, true, null);
            TestScript(script, new Dictionary <string, object>()
            {
                { "$x", true }
            }, true, null);
            TestScript(script, new Dictionary <string, object>()
            {
                { "$x", 1 }, { "$y", true }
            }, false, "OK");
        }
Exemple #5
0
        void TestOperation <TOperation>(string source, bool expectError, object expectedValue, Type expectedErrorType)
            where TOperation : ISyntaxTreeItem, new()
        {
            ISourceReader reader = new SourceReader();

            reader.Initialize(source);
            TOperation op = new TOperation();

            try
            {
                var    exec      = op.Execute(reader, m_Scope, false);
                object lastValue = null;
                foreach (var o in exec)
                {
                    lastValue = o;
                }
                Assert.IsFalse(expectError);
                Assert.AreEqual(expectedValue, lastValue);
            }
            catch (HCEngineException he)
            {
                Assert.IsTrue(expectError && expectedErrorType.IsAssignableFrom(he.GetType()));
                return;
            }
        }
        void TestSection <TSection>(string source, bool expectError, Type expectedError, params object[] execTrace)
            where TSection : ISyntaxTreeItem, new()
        {
            ISourceReader reader = new SourceReader();

            reader.Initialize(source);
            TSection section = new TSection();

            try
            {
                var exec = section.Execute(reader, m_Scope, false);
                int i    = 0;
                foreach (object o in exec)
                {
                    if (expectError)
                    {
                        continue;
                    }
                    Assert.IsTrue(i < execTrace.Length);
                    object expected = execTrace[i++];
                    if (expected is IList)
                    {
                        Assert.IsInstanceOfType(o, typeof(IList));
                        IList collection = expected as IList;
                        IList actual     = o as IList;
                        for (int j = 0; j < collection.Count; ++j)
                        {
                            Assert.IsTrue(j < actual.Count);
                            Assert.AreEqual(collection[j], actual[j]);
                        }
                    }
                    else
                    {
                        Assert.AreEqual(expected, o);
                    }
                }
                Assert.IsFalse(expectError);
                Assert.AreEqual(execTrace.Length, i);
            }
            catch (HCEngineException he)
            {
                Assert.IsTrue(expectError && expectedError.IsAssignableFrom(he.GetType()));
            }
        }
Exemple #7
0
        public void TestReadingLines()
        {
            string        source = "reading\nthis should\nbe ok";
            ISourceReader reader = new SourceReader();

            reader.Initialize(source);
            Assert.AreEqual(1, reader.Line);
            Assert.AreEqual(1, reader.Column);
            reader.ReadNext();
            Assert.AreEqual(2, reader.Line);
            Assert.AreEqual(1, reader.Column);
            reader.ReadNext();
            Assert.AreEqual(2, reader.Line);
            Assert.AreEqual(6, reader.Column);
            reader.ReadNext();
            Assert.AreEqual(3, reader.Line);
            Assert.AreEqual(1, reader.Column);
            reader.ReadNext();
            Assert.AreEqual(3, reader.Line);
            Assert.AreEqual(4, reader.Column);
        }
Exemple #8
0
        void AssignSubTest(string source, IExecutionScope scope, string expectedId, object expectedValue, string unexpectedId)
        {
            ISourceReader reader = new SourceReader();

            reader.Initialize(source);
            AssignationSyntax assign = new AssignationSyntax();

            var exec = assign.Execute(reader, scope, false);

            object[] expected = new object[] { expectedValue, null };
            int      i        = 0;

            foreach (object o in exec)
            {
                Assert.AreEqual(expected[i++], o);
            }

            Assert.IsTrue(scope.Contains(expectedId));
            Assert.AreEqual(expectedValue, scope[expectedId]);
            Assert.IsFalse(scope.Contains(unexpectedId));
        }