Пример #1
0
        public void ChangeTimeZone(string initialDateTime, string timeZone, string expected)
        {
            var input = new DateTimeValue(DateTimeOffset.Parse(initialDateTime));

            var arguments = new FilterArguments(new StringValue(timeZone));
            var context   = new TemplateContext {
                CultureInfo = CultureInfo.InvariantCulture
            };

            var result = MiscFilters.ChangeTimeZone(input, arguments, context);

            Assert.Equal(expected, ((DateTimeOffset)result.ToObjectValue()).ToString("yyyy-MM-ddTHH:mm:ssK"));
        }
Пример #2
0
        public void Time12hFormatFormDateTimeOffset(string dateTimeOffset, string format, string expected)
        {
            var input = new DateTimeValue(DateTimeOffset.Parse(dateTimeOffset));

            var arguments = new FilterArguments(new StringValue(format));
            var context   = new TemplateContext();

            context.CultureInfo = CultureInfo.InvariantCulture;

            var result = MiscFilters.Date(input, arguments, context);

            Assert.Equal(expected, result.ToStringValue().Trim());
        }
Пример #3
0
        public void Date(string format, string expected)
        {
            var input = new DateTimeValue(new DateTimeOffset(new DateTime(2017, 8, 1, 17, 4, 36, 123), TimeSpan.FromHours(8)));

            var arguments = new FilterArguments(new StringValue(format));
            var context   = new TemplateContext();

            context.CultureInfo = new CultureInfo("en-US");

            var result = MiscFilters.Date(input, arguments, context);

            Assert.Equal(expected, result.ToStringValue());
        }
Пример #4
0
        public async Task DateNumberIsParsedAsSeconds(long number, string expected)
        {
            // Converting to Unix time should not vary by TimeSone

            var input   = NumberValue.Create(number);
            var format  = new FilterArguments(new StringValue("%s"));
            var context = new TemplateContext {
                TimeZone = Eastern
            };

            var result = await MiscFilters.Date(input, format, context);

            Assert.Equal(expected, result.ToStringValue());
        }
Пример #5
0
        public async Task DefaultTimeZoneIsSetWhenNotParsed()
        {
            // This test ensures that when a TZ is specified it uses it instead of the settings one
            var input = StringValue.Create("1970-01-01 00:00:00");

            var format  = new FilterArguments(new StringValue("%s"));
            var context = new TemplateContext {
                TimeZone = Eastern
            };

            var result = await MiscFilters.Date(input, format, context);

            Assert.Equal("18000", result.ToStringValue());
        }
Пример #6
0
        public void DateIsParsed()
        {
            var input  = new StringValue("08/01/2017");
            var format = "%D";

            var arguments = new FilterArguments(new StringValue(format));
            var context   = new TemplateContext();

            context.CultureInfo = CultureInfo.InvariantCulture;

            var result = MiscFilters.Date(input, arguments, context);

            Assert.Equal("08/01/2017", result.ToStringValue());
        }
Пример #7
0
        public async Task DateNumberIsParsedInLocalTimeZone()
        {
            // This test is issued from a template running in Ruby on a system with -5 TZ
            // {{ 0 | date: '%c' }}

            var input   = NumberValue.Create(0);
            var format  = new FilterArguments(new StringValue("%+"));
            var context = new TemplateContext {
                TimeZone = Eastern
            };

            var result = await MiscFilters.Date(input, format, context);

            Assert.Equal("Wed Dec 31 19:00:00 -05:00 1969", result.ToStringValue());
        }
Пример #8
0
        public void FormatDate()
        {
            var input  = new StringValue("now");
            var format = "d";

            var arguments = new FilterArguments(new StringValue(format));
            var context   = new TemplateContext();

            context.Now         = () => new DateTimeOffset(new DateTime(2017, 8, 1, 5, 4, 36, 123), new TimeSpan(0));
            context.CultureInfo = CultureInfo.InvariantCulture;

            var result = MiscFilters.FormatDate(input, arguments, context);

            Assert.Equal("08/01/2017", result.ToStringValue());
        }
Пример #9
0
        public void ChangeTimeZoneAndApply12hFormat(string initialDateTime, string timeZone, string format, string expected)
        {
            var input            = new DateTimeValue(DateTimeOffset.Parse(initialDateTime));
            var timeZoneArgument = new FilterArguments(new StringValue(timeZone));
            var formatArgument   = new FilterArguments(new StringValue(format));
            var context          = new TemplateContext {
                CultureInfo = CultureInfo.InvariantCulture
            };

            var result = MiscFilters.ChangeTimeZone(input, timeZoneArgument, context);

            result = MiscFilters.Date(result, formatArgument, context);

            Assert.Equal(expected, result.ToStringValue().Trim());
        }
Пример #10
0
        public async Task Time12hFormatFormDateTimeOffset(string dateTimeOffset, string format, string expected)
        {
            var input = new DateTimeValue(DateTimeOffset.Parse(dateTimeOffset));

            var arguments = new FilterArguments(new StringValue(format));
            var options   = new TemplateOptions()
            {
                CultureInfo = CultureInfo.InvariantCulture
            };
            var context = new TemplateContext(options);

            var result = await MiscFilters.Date(input, arguments, context);

            Assert.Equal(expected, result.ToStringValue().Trim());
        }
Пример #11
0
        public void FormatString(object input, object[] args, string culture, string expected)
        {
            var cultureInfo = String.IsNullOrEmpty(culture)
                ? CultureInfo.InvariantCulture
                : CultureInfo.CreateSpecificCulture(culture)
            ;

            var arguments = new FilterArguments(args.Select(FluidValue.Create).ToArray());
            var context   = new TemplateContext {
                CultureInfo = cultureInfo
            };

            var result = MiscFilters.FormatString(FluidValue.Create(input), arguments, context);

            Assert.Equal(expected, result.ToStringValue());
        }
Пример #12
0
        public async Task FormatString(object input, object[] args, string culture, string expected)
        {
            var cultureInfo = String.IsNullOrEmpty(culture)
                ? CultureInfo.InvariantCulture
                : CultureInfo.CreateSpecificCulture(culture)
            ;

            var context = new TemplateContext(new TemplateOptions {
                CultureInfo = cultureInfo
            });
            var arguments = new FilterArguments(args.Select(x => FluidValue.Create(x, context.Options)).ToArray());

            var result = await MiscFilters.FormatString(FluidValue.Create(input, context.Options), arguments, context);

            Assert.Equal(expected, result.ToStringValue());
        }
Пример #13
0
        public async Task DateIsParsed()
        {
            var input  = new StringValue("08/01/2017");
            var format = "%D";

            var arguments = new FilterArguments(new StringValue(format));
            var options   = new TemplateOptions()
            {
                CultureInfo = CultureInfo.InvariantCulture, TimeZone = TimeZoneInfo.Utc
            };
            var context = new TemplateContext(options);

            var result = await MiscFilters.Date(input, arguments, context);

            Assert.Equal("08/01/17", result.ToStringValue());
        }
Пример #14
0
        public async Task Date(string format, string expected)
        {
            var input = new DateTimeValue(new DateTimeOffset(
                                              new DateTime(2017, 8, 1, 17, 4, 36, 123), TimeSpan.FromHours(8)));

            var arguments = new FilterArguments(new StringValue(format));
            var options   = new TemplateOptions()
            {
                CultureInfo = new CultureInfo("en-US"), TimeZone = TimeZoneInfo.Utc
            };
            var context = new TemplateContext(options);

            var result = await MiscFilters.Date(input, arguments, context);

            Assert.Equal(expected, result.ToStringValue());
        }
Пример #15
0
        public async Task CompactRemovesNilValues()
        {
            var input = new ArrayValue(new FluidValue[] {
                new StringValue("a"),
                NumberValue.Zero,
                NilValue.Instance,
                new StringValue("b")
            });

            var arguments = new FilterArguments();
            var context   = new TemplateContext();

            var result = await MiscFilters.Compact(input, arguments, context);

            Assert.Equal(3, result.Enumerate().Count());
        }
Пример #16
0
        public void DateIsParsedWithCulture()
        {
            var input  = new StringValue("08/01/2017");
            var format = "%d/%m/%Y";

            var arguments = new FilterArguments(new StringValue(format));
            var context   = new TemplateContext();

            context.CultureInfo = new CultureInfo("fr-FR");
            var resultFR = MiscFilters.Date(input, arguments, context);

            context.CultureInfo = new CultureInfo("en-US");
            var resultUS = MiscFilters.Date(input, arguments, context);

            Assert.Equal("08/01/2017", resultFR.ToStringValue());
            Assert.Equal("01/08/2017", resultUS.ToStringValue());
        }
Пример #17
0
        public void DateIsRenderedWithCulture()
        {
            var input  = new StringValue("08/01/2017");
            var format = "%c";

            var arguments = new FilterArguments(new StringValue(format));
            var context   = new TemplateContext();

            context.CultureInfo = new CultureInfo("fr-FR");
            var resultFR = MiscFilters.Date(input, arguments, context);

            context.CultureInfo = new CultureInfo("en-US");
            var resultUS = MiscFilters.Date(input, arguments, context);

            Assert.Equal("dimanche 8 janvier 2017 00:00:00", resultFR.ToStringValue());
            Assert.Equal("Tuesday, August 1, 2017 12:00:00 AM", resultUS.ToStringValue());
        }
Пример #18
0
        public async Task FormatDate()
        {
            var input  = new StringValue("now");
            var format = "d";

            var arguments = new FilterArguments(new StringValue(format));
            var options   = new TemplateOptions()
            {
                CultureInfo = CultureInfo.InvariantCulture,
                Now         = () => new DateTimeOffset(new DateTime(2017, 8, 1, 5, 4, 36, 123), new TimeSpan(0))
            };
            var context = new TemplateContext(options);

            var result = await MiscFilters.FormatDate(input, arguments, context);

            Assert.Equal("08/01/2017", result.ToStringValue());
        }
Пример #19
0
        public async Task DateIsParsedWithCulture()
        {
            var input  = new StringValue("08/01/2017");
            var format = "%d/%m/%Y";

            var arguments = new FilterArguments(new StringValue(format));

            var context = new TemplateContext(new TemplateOptions {
                CultureInfo = new CultureInfo("fr-FR"), TimeZone = TimeZoneInfo.Utc
            });
            var resultFR = await MiscFilters.Date(input, arguments, context);

            context = new TemplateContext(new TemplateOptions {
                CultureInfo = new CultureInfo("en-US"), TimeZone = TimeZoneInfo.Utc
            });
            var resultUS = await MiscFilters.Date(input, arguments, context);

            Assert.Equal("08/01/2017", resultFR.ToStringValue());
            Assert.Equal("01/08/2017", resultUS.ToStringValue());
        }
Пример #20
0
        public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext context)
        {
            var content = input.ToObjectValue();

            if (content == null || _hostEnvironment.IsProduction())
            {
                return(NilValue.Instance);
            }

            using var sb = ZString.CreateStringBuilder();
            sb.Append("<script>console.log(");

            if (content is string stringContent)
            {
                sb.Append("\"");
                sb.Append(stringContent);
                sb.Append("\"");
            }
            else if (content is JToken jTokenContent)
            {
                sb.Append(jTokenContent.ToString());
            }
            else if (content is ContentItem contentItem)
            {
                sb.Append(OrchardRazorHelperExtensions.ConvertContentItem(contentItem).ToString());
            }
            else if (content is IShape shape)
            {
                sb.Append(shape.ShapeToJson().ToString());
            }
            else
            {
                sb.Append((await MiscFilters.Json(input, arguments, context)).ToStringValue());
            }

            sb.Append(")</script>");

            var result = new StringValue(sb.ToString(), false);

            return(result);
        }
Пример #21
0
        public async Task DefaultTimeZoneAppliesDaylightSaving()
        {
            // This test ensures that the DST is respected when TZ is rendered

            var input = StringValue.Create("2021-01-01 00:00:00");

            var format  = new FilterArguments(new StringValue("%z"));
            var context = new TemplateContext {
                TimeZone = Pacific
            };

            var result = await MiscFilters.Date(input, format, context);

            Assert.Equal("-0800", result.ToStringValue());

            input = StringValue.Create("2021-06-01 00:00:00");

            result = await MiscFilters.Date(input, format, context);

            Assert.Equal("-0700", result.ToStringValue());
        }