public void SimpleReplacement()
        {
            var rs = new TemplateReplacer(new { just = "JUST", test = "TEST" });
            var r  = rs.ReplacePlaceholders("This is {{just}} a {{test}}.");

            Assert.Equal("This is JUST a TEST.", r);
        }
Exemplo n.º 2
0
        [Test] public void SubstitutionWithIncludes()
        {
            const string include     = "over the [adjective2]";
            const string template    = "The [speed] [colour] [noun1] [adjective1] [include] [noun2]";
            const string substituted = "The quick brown fox jumped over the lazy dog";

            var template_replacer = new TemplateReplacer(new StringSrc(template), @"\[(\w+)\]", (tr, match) =>
            {
                switch (match.Value)
                {
                default: throw new Exception("Unknown field");

                case "[speed]": return("quick");

                case "[colour]": return("brown");

                case "[noun1]": return("fox");

                case "[adjective1]": return("jumped");

                case "[adjective2]": return("lazy");

                case "[noun2]": return("dog");

                case "[include]":  tr.PushSource(new StringSrc(include)); return(string.Empty);
                }
            });

            using (template_replacer)
            {
                var result = template_replacer.ReadToEnd();
                Assert.Equal(substituted, result);
            }
        }
        public void FormattedReplacement()
        {
            var rs = new TemplateReplacer(new { date = DateTime.MinValue });
            var r  = rs.ReplacePlaceholders("{{date:R}}");

            Assert.Equal("Mon, 01 Jan 0001 00:00:00 GMT", r);
        }
        public void NotFormattableValueKey()
        {
            var ex = Assert.Throws <FormatException>(() => {
                var rs = new TemplateReplacer(new { key = new object() });
                var r  = rs.ReplacePlaceholders("Testing {{key:N0}} with no formatting.");
            });

            Assert.Equal("Value for key 'key' is not IFormattable, but custom format string was provided.", ex.Message);
        }
        public void NonExistingKey()
        {
            var ex = Assert.Throws <FormatException>(() => {
                var rs = new TemplateReplacer(new { just = "JUST", test = "TEST" });
                var r  = rs.ReplacePlaceholders("Requesting {{nonexisting}} key.");
            });

            Assert.Equal("Requested key 'nonexisting' was not found in supplied values.", ex.Message);
        }
Exemplo n.º 6
0
        /// <summary>Show the TOTD for the current index</summary>
        private void ShowTip()
        {
            var tip = m_tips[m_order[TipIndex]];

            using (var tr = new TemplateReplacer(new StringSrc(Resources.totd), @"\[(\w+)\]", (x, match) =>
            {
                switch (match.Value)
                {
                default: throw new Exception("Unknown template field");

                case "[Title]":   return(tip.Title);

                case "[Content]": return(tip.Body);
                }
            }))
                Html = tr.ReadToEnd();
        }
Exemplo n.º 7
0
        public static KeyValuePair <string, string> WriteNotification <T>(NotificationObjectType objectType,
                                                                          ActionType actionType, UserAccountModel user, T additionalData)
        {
            var templateBody    = string.Empty;
            var templateSubject = string.Empty;

            switch (objectType)
            {
            case NotificationObjectType.PAT:
                templateBody    = NotificationTemplates.PAT.Value;
                templateSubject = NotificationTemplates.PAT.Key;
                break;

            case NotificationObjectType.AsBuilt:
                break;

            case NotificationObjectType.BeginLeaseWorkflow:
                templateSubject = NotificationTemplates.BeginLeasePaymentProcess.Key;
                templateBody    = NotificationTemplates.BeginLeasePaymentProcess.Value;
                break;

            case NotificationObjectType.LeaseContractWorkflowAction:
                templateSubject = NotificationTemplates.LeasePaymentWorkflowAction.Key;
                templateBody    = NotificationTemplates.LeasePaymentWorkflowAction.Value;
                break;

            case NotificationObjectType.BeginInvoiceWorkflow:
                templateSubject = NotificationTemplates.BeginInvoiceApprovalProcess.Key;
                templateBody    = NotificationTemplates.BeginInvoiceApprovalProcess.Value;
                break;

            case NotificationObjectType.InvoiceWorkflowAction:
                templateSubject = NotificationTemplates.InvoiceWorkflowAction.Key;
                templateBody    = NotificationTemplates.InvoiceWorkflowAction.Value;
                break;

            //    break;
            //case NotificationObjectType.ProjectTask:
            //    break;
            //case NotificationObjectType.Project:
            //    break;
            //case NotificationObjectType.MileStone:
            //    break;
            //case NotificationObjectType.Ticket:
            //    break;
            //case NotificationObjectType.ChangeRequest:
            //    break;
            default:
                break;
            }

            if (string.IsNullOrEmpty(templateBody))
            {
                return(new KeyValuePair <string, string>("Simple Email from Pmcs v1.2 email subject",
                                                         "<p>Sample Email</p>"));
            }

            var replacer     = new TemplateReplacer <T>(objectType, user, additionalData);
            var filteredBody = replacer.FilterEmailPlaceHolders(templateBody);

            return(new KeyValuePair <string, string>(templateSubject, filteredBody));
        }