Esempio n. 1
0
        public void CallScope_PushBack_AddsItemsToLastPosition()
        {
            CallScope callScope = new CallScope(new MockScope());

            callScope.PushBack(Value.Get(11));
            callScope.PushBack(Value.Get(22));
            callScope.PushBack(Value.Get(33));

            Assert.Equal(11, callScope.Args.AsSequence[0].AsLong);
            Assert.Equal(22, callScope.Args.AsSequence[1].AsLong);
            Assert.Equal(33, callScope.Args.AsSequence[2].AsLong);
        }
Esempio n. 2
0
        public void CallScope_Size_ReflectsNumberOfArguments()
        {
            CallScope callScope = new CallScope(new MockScope());

            Assert.Equal(0, callScope.Size);
            callScope.PushBack(Value.Get(11));
            Assert.Equal(1, callScope.Size);
            callScope.PushBack(Value.Get(22));
            Assert.Equal(2, callScope.Size);
            callScope.PushBack(Value.Get(33));
            Assert.Equal(3, callScope.Size);
            callScope.PopBack();
            Assert.Equal(2, callScope.Size);
        }
Esempio n. 3
0
        public void CallScope_PopBack_RemovesLastItem()
        {
            CallScope callScope = new CallScope(new MockScope());

            callScope.PushBack(Value.Get(11));
            callScope.PushBack(Value.Get(22));
            callScope.PushBack(Value.Get(33));

            callScope.PopBack();

            Assert.Equal(11, callScope.Args.AsSequence[0].AsLong);
            Assert.Equal(22, callScope.Args.AsSequence[1].AsLong);
            Assert.Equal(2, callScope.Args.AsSequence.Count);
        }
Esempio n. 4
0
        // query_command
        public static Value QueryCommand(CallScope args)
        {
            Report        report = args.FindScope <Report>();
            StringBuilder sb     = new StringBuilder();

            sb.AppendLine("--- Input arguments ---");
            sb.AppendLine(args.Value().Dump());
            sb.AppendLine();

            Query query = new Query(args.Value(), report.WhatToKeep(), report.CollapseHandler.Handled);

            if (query.HasQuery(QueryKindEnum.QUERY_LIMIT))
            {
                CallScope subArgs = new CallScope(args);
                subArgs.PushBack(Value.StringValue(query.GetQuery(QueryKindEnum.QUERY_LIMIT)));

                report.OutputStream.Write(sb.ToString());
                ParseCommand(subArgs);
            }

            if (query.HasQuery(QueryKindEnum.QUERY_SHOW))
            {
                sb.AppendLine();
                sb.AppendLine("====== Display predicate ======");
                sb.AppendLine();

                CallScope dispSubArgs = new CallScope(args);
                dispSubArgs.PushBack(Value.StringValue(query.GetQuery(QueryKindEnum.QUERY_SHOW)));

                report.OutputStream.Write(sb.ToString());
                ParseCommand(dispSubArgs);
            }

            return(Value.Empty);
        }
Esempio n. 5
0
        public void Session_FnLotDate_ReturnsDateFromFirstArgAmount()
        {
            Session session = new Session();

            Amount    amount1 = new Amount(0); // No date
            CallScope scope1  = new CallScope(new EmptyScope());

            //scope1.PushBack(Value.Get(false));  // first argument
            scope1.PushBack(Value.Get(amount1));
            Assert.AreEqual(Value.Empty, session.FnLotDate(scope1));

            Commodity  commodity  = new Commodity(CommodityPool.Current, new CommodityBase("base"));
            Date       date       = (Date)DateTime.Now.Date;
            Annotation annotation = new Annotation()
            {
                Date = date
            };
            AnnotatedCommodity annotatedCommodity = new AnnotatedCommodity(commodity, annotation);
            Amount             amount2            = new Amount(BigInt.FromInt(10), annotatedCommodity); // With date
            CallScope          scope2             = new CallScope(new EmptyScope());

            //scope2.PushBack(Value.Get(false));  // first argument
            scope2.PushBack(Value.Get(amount2));
            Assert.AreEqual(date, session.FnLotDate(scope2).AsDate);
        }
Esempio n. 6
0
        public void Session_FnLotTag_ReturnsDateFromFirstArgAmouunt()
        {
            Session session = new Session();

            Amount    amount1 = new Amount(0); // No tag
            CallScope scope1  = new CallScope(new EmptyScope());

            //scope1.PushBack(Value.Get(false));  // first argument
            scope1.PushBack(Value.Get(amount1));
            Assert.AreEqual(Value.Empty, session.FnLotTag(scope1));

            Commodity  commodity  = new Commodity(CommodityPool.Current, new CommodityBase("base"));
            string     tag        = "my-tag";
            Annotation annotation = new Annotation()
            {
                Tag = tag
            };
            AnnotatedCommodity annotatedCommodity = new AnnotatedCommodity(commodity, annotation);
            Amount             amount2            = new Amount(BigInt.FromInt(10), annotatedCommodity); // With date
            CallScope          scope2             = new CallScope(new EmptyScope());

            //scope2.PushBack(Value.Get(false));  // first argument
            scope2.PushBack(Value.Get(amount2));
            Assert.AreEqual(tag, session.FnLotTag(scope2).AsString);
        }
Esempio n. 7
0
        public void Option_Handler_ChecksThatTheFirstArgumentIsStringWhenWantsArgIsYes()
        {
            Option    option1 = new Option("myname_");
            CallScope scope1  = new CallScope(new EmptyScope());

            scope1.PushFront(Value.Get("string"));
            scope1.PushBack(Value.Get(true));
            option1.Handler(scope1);
            Assert.AreEqual("true", option1.Value); // Indicates that the call was successfull

            Option    option2 = new Option("myname_");
            CallScope scope2  = new CallScope(new EmptyScope());

            scope2.PushBack(Value.Get(true));
            scope2.PushBack(Value.Get(true));
            option2.Handler(scope2);
        }
Esempio n. 8
0
        public void Session_Lookup_LooksForFunctors()
        {
            Session session = new Session();
            ExprOp  minFunc = session.Lookup(SymbolKindEnum.FUNCTION, "min");

            Assert.IsNotNull(minFunc);
            Assert.IsTrue(minFunc.IsFunction);
            Assert.AreEqual(OpKindEnum.FUNCTION, minFunc.Kind);
            Assert.IsNotNull(minFunc.AsFunction);
            // Check that the function is callable
            CallScope callScope = new CallScope(new EmptyScope());

            callScope.PushBack(Value.Get("1"));
            callScope.PushBack(Value.Get("2"));
            string result = minFunc.AsFunction(callScope).AsString;

            Assert.AreEqual("1", result);
        }
Esempio n. 9
0
        public void Session_FnMax_ReturnsMaxArgument()
        {
            Session session = new Session();

            Value minVal = Value.Get(-1);
            Value maxVal = Value.Get(1);

            CallScope scope1 = new CallScope(new EmptyScope());

            scope1.PushBack(minVal);
            scope1.PushBack(maxVal);
            Assert.AreEqual(maxVal, session.FnMax(scope1));

            CallScope scope2 = new CallScope(new EmptyScope());

            scope2.PushBack(maxVal);
            scope2.PushBack(minVal);
            Assert.AreEqual(maxVal, session.FnMax(scope2));
        }
Esempio n. 10
0
        public void CallScope_IsEmpty_IndicatesWhetherArgsAreEmpty()
        {
            CallScope callScope = new CallScope(new MockScope());

            Assert.True(callScope.IsEmpty);
            callScope.PushBack(Value.Get(11));
            Assert.False(callScope.IsEmpty);
            callScope.PopBack();
            Assert.True(callScope.IsEmpty);
        }
Esempio n. 11
0
        public void Session_FnStr_ReturnsFirstArgumentAsString()
        {
            Session session = new Session();
            Value   val     = Value.Get("USD23");

            CallScope scope1 = new CallScope(new EmptyScope());

            scope1.PushBack(val);
            Assert.AreEqual(23, session.FnStr(scope1).AsAmount.Quantity.ToLong());  // TODO - validate this case
        }
Esempio n. 12
0
        public void Session_FnInt_ReturnsFirstArgumentAsLong()
        {
            Session session = new Session();
            Value   val     = Value.Get(234);

            CallScope scope1 = new CallScope(new EmptyScope());

            scope1.PushBack(val);
            Assert.AreEqual(234, session.FnInt(scope1).AsLong);
        }
Esempio n. 13
0
        public void Option_Handler_CallsOnWithTwoArgumentsIfWantsArgIsYes()
        {
            Option    option1 = new Option("myname_");
            CallScope scope1  = new CallScope(new EmptyScope());

            scope1.PushFront(Value.Get("whence"));
            scope1.PushBack(Value.Get("str"));
            option1.Handler(scope1);
            Assert.AreEqual("str", option1.Value);
            Assert.IsTrue(option1.Handled);
        }
Esempio n. 14
0
        public void Session_FnAccount_FindsAccountByMaskIfFirstArgIsMask()
        {
            Session session = new Session();
            Account account = session.Journal.Master.FindAccount("my-account");
            Value   val     = Value.Get(new Mask("my-account"));

            CallScope scope1 = new CallScope(new EmptyScope());

            scope1.PushBack(val);

            var result = session.FnAccount(scope1).AsScope;

            Assert.AreEqual(result, account);
        }
Esempio n. 15
0
        public void Option_Handler_ChecksThatTheFirstArgumentIsStringWhenWantsArgIsNo()
        {
            Option    option1 = new Option("myname");
            CallScope scope1  = new CallScope(new EmptyScope());

            scope1.PushFront(Value.Get("string"));
            option1.Handler(scope1);
            Assert.True(option1.Handled); // Indicates that the call was successfull

            Option    option2 = new Option("myname");
            CallScope scope2  = new CallScope(new EmptyScope());

            scope2.PushBack(Value.Get(true));
            Assert.Throws <InvalidOperationException>(() => option2.Handler(scope2));
        }
Esempio n. 16
0
        public void PreCmd_ParseCommand_OutputsExpressionContent()
        {
            string command = "2+2";

            MemoryStream memoryStream = new MemoryStream();
            Report       report       = new Report(new Session())
            {
                OutputStream = new StreamWriter(memoryStream)
            };
            CallScope callScope = new CallScope(report);

            callScope.PushBack(Value.StringValue(command));

            PreCmd.ParseCommand(callScope);

            report.OutputStream.Flush();
            memoryStream.Position = 0;
            string result = new StreamReader(memoryStream).ReadToEnd();

            string expected =
                @"--- Context is first posting of the following transaction ---
2004/05/27 Book Store
    ; This note applies to all postings. :SecondTag:
    Expenses:Books                 20 BOOK @ $10
    ; Metadata: Some Value
    ; Typed:: $100 + $200
    ; :ExampleTag:
    ; Here follows a note describing the posting.
    Liabilities:MasterCard        $-200.00

--- Input expression ---
2+2
--- Text as parsed ---
(2 + 2)
--- Expression tree ---
O_ADD (0)
 VALUE: 2 (0)
 VALUE: 2 (0)
--- Compiled tree ---
O_ADD (0)
 VALUE: 2 (0)
 VALUE: 2 (0)
--- Calculated value ---
{4}
";

            Assert.AreEqual(0, CompareInfo.GetCompareInfo(CultureInfo.CurrentCulture.LCID).Compare(expected, result, CompareOptions.IgnoreSymbols));
        }
Esempio n. 17
0
        public void Session_Lookup_LooksForHandlers()
        {
            Session session = new Session();

            ExprOp checkPayeesOp = session.Lookup(SymbolKindEnum.OPTION, "check-payees");

            Assert.IsNotNull(checkPayeesOp);
            Assert.IsTrue(checkPayeesOp.IsFunction);
            Assert.AreEqual(OpKindEnum.FUNCTION, checkPayeesOp.Kind);
            Assert.IsNotNull(checkPayeesOp.AsFunction);
            // Check that the function is callable
            CallScope callScope = new CallScope(new EmptyScope());

            callScope.PushBack(Value.Get("str"));
            checkPayeesOp.AsFunction(callScope);
            Assert.IsTrue(session.CheckPayeesHandler.Handled);
        }
Esempio n. 18
0
        public void Select_SelectCommand_GetEmptyAccountsListTest()
        {
            Report       report = new Report(new Session());
            StringWriter output = new StringWriter();

            report.OutputStream = output;

            CallScope scope = new CallScope(report);

            scope.PushBack(Value.StringValue("account from accounts"));

            Value  result    = Select.SelectCommand(scope);
            string outString = output.ToString();

            Assert.IsTrue(String.IsNullOrEmpty(outString)); // No accounts
            Assert.IsTrue(result.AsBoolean);
        }
Esempio n. 19
0
        public void Select_SelectCommand_GetEmptyAccountsListTest()
        {
            MainApplicationContext.Current.SetApplicationServiceProvider(new ApplicationServiceProvider
                                                                             (virtualConsoleProviderFactory: () => new TestConsoleProvider()));

            Report       report = new Report(new Session());
            StringWriter output = new StringWriter();

            report.OutputStream = output;

            CallScope scope = new CallScope(report);

            scope.PushBack(Value.StringValue("account from accounts"));

            Value  result    = Select.SelectCommand(scope);
            string outString = output.ToString();

            Assert.True(String.IsNullOrEmpty(outString)); // No accounts
            Assert.True(result.AsBoolean);
        }
Esempio n. 20
0
        public void Session_FnLotPrice_ReturnsPriceFromFirstArgAmouunt()
        {
            Session session = new Session();

            Amount    amount1 = new Amount(0); // No price
            CallScope scope1  = new CallScope(new EmptyScope());

            //scope1.PushBack(Value.Get(false));  // first argument
            scope1.PushBack(Value.Get(amount1));
            Assert.AreEqual(Value.Empty, session.FnLotPrice(scope1));

            Commodity          commodity          = new Commodity(CommodityPool.Current, new CommodityBase("base"));
            Amount             price              = new Amount(5);
            Annotation         annotation         = new Annotation(price);
            AnnotatedCommodity annotatedCommodity = new AnnotatedCommodity(commodity, annotation);
            Amount             amount2            = new Amount(BigInt.FromInt(10), annotatedCommodity); // With price
            CallScope          scope2             = new CallScope(new EmptyScope());

            //scope2.PushBack(Value.Get(false));  // first argument
            scope2.PushBack(Value.Get(amount2));
            Assert.AreEqual(price, session.FnLotPrice(scope2).AsAmount);
        }
Esempio n. 21
0
        protected override string RealCalc(Scope scope)
        {
            StringBuilder outStr = new StringBuilder();

            for (FormatElement elem = Elements; elem != null; elem = elem.Next)
            {
                string s;

                switch (elem.Type)
                {
                case FormatElementEnum.STRING:
                    string stringFormat = StringExtensions.GetWidthAlignFormatString(elem.MinWidth, !elem.IsElementAlignLeft);
                    s = String.Format(stringFormat, elem.Data.GetValue <string>());
                    break;

                case FormatElementEnum.EXPR:
                    Expr expr = new Expr(elem.Data.GetValue <Expr>());
                    try
                    {
                        expr.Compile(scope);

                        Value value;
                        if (expr.IsFunction)
                        {
                            CallScope args = new CallScope(scope);
                            args.PushBack(Value.Get(elem.MaxWidth));
                            value = expr.GetFunction()(args);
                        }
                        else
                        {
                            value = expr.Calc(scope);
                        }
                        Logger.Current.Debug("format.expr", () => String.Format("value = ({0})", value));

                        if (elem.MinWidth > 0)
                        {
                            s = value.Print(elem.MinWidth, -1, elem.IsElementAlignLeft ? AmountPrintEnum.AMOUNT_PRINT_NO_FLAGS : AmountPrintEnum.AMOUNT_PRINT_RIGHT_JUSTIFY);
                        }
                        else
                        {
                            s = value.ToString();
                        }
                    }
                    catch
                    {
                        string currentContext = ErrorContext.Current.GetContext();

                        ErrorContext.Current.AddErrorContext("While calculating format expression:");
                        ErrorContext.Current.AddErrorContext(expr.ContextToStr());

                        if (!String.IsNullOrEmpty(currentContext))
                        {
                            ErrorContext.Current.AddErrorContext(currentContext);
                        }

                        throw;
                    }
                    break;

                default:
                    throw new InvalidOperationException("Unknown enum item");
                }

                if (elem.MaxWidth > 0 || elem.MinWidth > 0)
                {
                    string result;

                    if (elem.MaxWidth > 0 && elem.MaxWidth < s.Length)
                    {
                        result = Truncate(s, elem.MaxWidth);
                    }
                    else
                    {
                        result = s;
                        if (elem.MinWidth > s.Length)
                        {
                            result = result + new String(' ', s.Length - elem.MinWidth);
                        }
                    }

                    outStr.Append(result);
                }
                else
                {
                    outStr.Append(s);
                }
            }

            return(outStr.ToString());
        }