Exemple #1
0
        public void BasicUsage()
        {
            DemoClass dc = new DemoClass();

            FunctionDefinition[] defs = FunctionDefinitionCreator.CreateDefinitions(dc, typeof(DemoClass));
            foreach (FunctionDefinition fd in defs)
            {
                if (fd.functionName == "NumberToString")
                {
                    ReturnValue rv = fd.callback(new ReturnValue[] { new ReturnValue(ReturnValueType.NUMBER, 1.5f) });
                    Assert.AreEqual((1.5f).ToString(), rv.StringValue);
                }
                if (fd.functionName == "GetValues")
                {
                    Console.WriteLine("retval " + fd.functionDocumentation.GetFunctionDescription() + ", " +
                                      fd.functionDocumentation.GetArgumentDescription(0) +
                                      ", " + fd.functionDocumentation.GetArgumentDescription(1));
                    ReturnValue rv = fd.callback(new ReturnValue[] { new ReturnValue(3.0f), new ReturnValue(4.0f) });
                    Assert.AreEqual(12f, rv.NumberValue, 0.001f);
                }
                if (fd.functionName == "UseBool")
                {
                    ReturnValue rv = fd.callback(new ReturnValue[] { new ReturnValue(true) });
                    Assert.AreEqual(true, rv.BoolValue);
                }
            }
        }
Exemple #2
0
        public void BasicUsage()
        {
            DemoClass dc = new DemoClass();

            FunctionDefinition[] defs = FunctionDefinitionCreator.CreateDefinitions(dc, typeof(DemoClass));
            foreach (FunctionDefinition fd in defs)
            {
                if (fd.functionName == "NumberToString")
                {
                    object rv = fd.callback(new object[] { 1.5f });
                    Assert.AreEqual((1.5f).ToString(), (string)rv);
                }
                if (fd.functionName == "GetValues")
                {
                    Console.WriteLine("retval " + fd.functionDocumentation.GetFunctionDescription() + ", " +
                                      fd.functionDocumentation.GetArgumentDescription(0) +
                                      ", " + fd.functionDocumentation.GetArgumentDescription(1));
                    object rv = fd.callback(new object[] { 3.0f, 4.0f });
                    Console.WriteLine("rv type: " + rv.GetType());
                    Assert.AreEqual(12f, (float)rv, 0.001f);
                }
                if (fd.functionName == "UseBool")
                {
                    object rv = fd.callback(new object[] { true });
                    Assert.AreEqual(true, (bool)rv);
                }
            }
        }
Exemple #3
0
        public void ArrayAsArgumentListTest()
        {
            DemoClassThree dc3 = new DemoClassThree();

            FunctionDefinition[] defs = FunctionDefinitionCreator.CreateDefinitions(dc3, typeof(DemoClassThree));
            Assert.AreEqual(1, defs.Length);

            List <FunctionDefinition> moreFunctionDefinitions = new List <FunctionDefinition> {
                GetPrintFunction()
            };

            moreFunctionDefinitions.AddRange(defs);

            TextReader  programString = File.OpenText("code75.txt");
            SprakRunner program       = new SprakRunner(programString, moreFunctionDefinitions.ToArray());

            program.run();

            foreach (var e in program.getRuntimeErrorHandler().getErrors())
            {
                Console.WriteLine(e);
            }

            foreach (var e in program.getCompileTimeErrorHandler().getErrors())
            {
                Console.WriteLine(e);
            }

            Assert.AreEqual(0, program.getRuntimeErrorHandler().getErrors().Count);
            Assert.AreEqual(0, program.getCompileTimeErrorHandler().getErrors().Count);
        }
Exemple #4
0
        public void CallingFunctionWithWrongArgumentType_USING_FUNCTION_DEFINITION_CREATOR()
        {
            TextReader programString = File.OpenText("code73.txt");

            ClassWithFunction c = new ClassWithFunction();

            FunctionDefinition[] funcDefs = FunctionDefinitionCreator.CreateDefinitions(c, typeof(ClassWithFunction));

            List <FunctionDefinition> moreFunctionDefinitions = new List <FunctionDefinition> {
                GetPrintFunction()
            };

            moreFunctionDefinitions.AddRange(funcDefs);

            SprakRunner program = new SprakRunner(programString, moreFunctionDefinitions.ToArray());

            program.run();

            Assert.AreEqual(0, program.getCompileTimeErrorHandler().getErrors().Count);
        }
Exemple #5
0
        public void CallFunctionThatThrowsException()
        {
            DemoClassFour dc3 = new DemoClassFour();

            FunctionDefinition[] defs = FunctionDefinitionCreator.CreateDefinitions(dc3, typeof(DemoClassFour));
            Assert.AreEqual(1, defs.Length);

            List <FunctionDefinition> moreFunctionDefinitions = new List <FunctionDefinition> {
                GetPrintFunction()
            };

            moreFunctionDefinitions.AddRange(defs);

            TextReader  programString = File.OpenText("code77.txt");
            SprakRunner program       = new SprakRunner(programString, moreFunctionDefinitions.ToArray());

            program.run();

            Assert.AreEqual(0, program.getCompileTimeErrorHandler().getErrors().Count);
            Assert.AreEqual(1, program.getRuntimeErrorHandler().getErrors().Count);

            program.getRuntimeErrorHandler().printErrorsToConsole();
        }
Exemple #6
0
        public void ThrowsException()
        {
            DemoClassTwo dc = new DemoClassTwo();

            Assert.Throws <Exception>(() => FunctionDefinitionCreator.CreateDefinitions(dc, typeof(DemoClassTwo)));
        }