forked from sebastienros/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscFiltersTests.cs
937 lines (756 loc) · 37.4 KB
/
MiscFiltersTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
using System;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Fluid.Filters;
using Fluid.Values;
using Newtonsoft.Json.Linq;
using TimeZoneConverter;
using Xunit;
namespace Fluid.Tests
{
public class MiscFiltersTests
{
private static readonly TimeZoneInfo Pacific = TZConvert.GetTimeZoneInfo("America/Los_Angeles");
private static readonly TimeZoneInfo Eastern = TZConvert.GetTimeZoneInfo("America/New_York");
private static readonly string RoundTripDateTimePattern = "%Y-%m-%dT%H:%M:%S.%L%Z"; // Equivalent to "o" format
[Fact]
public async Task DefaultReturnsValueIfDefined()
{
var input = new StringValue("foo");
var arguments = new FilterArguments().Add(new StringValue("bar"));
var context = new TemplateContext();
var result = await MiscFilters.Default(input, arguments, context);
Assert.Equal("foo", result.ToStringValue());
}
[Theory]
[InlineData("foo", "foo", "bar", false)]
[InlineData("bar", null, "bar", false)]
[InlineData("bar", false, "bar", false)]
[InlineData("bar", new int[0], "bar", false)]
[InlineData("bar", "", "bar", false)]
[InlineData("bar", "empty", "bar", false)]
[InlineData("foo", "foo", "bar", true)]
[InlineData("bar", null, "bar", true)]
[InlineData("bar", "", "bar", true)]
[InlineData(false, false, "bar", true)]
[InlineData("bar", new int[0], "bar", true)]
[InlineData("bar", "empty", "bar", true)]
public async Task DefaultReturnsDefaultIfNotDefinedOrEmptyOrFalse(object expected, object input, object @default, bool allowFalse)
{
var arguments = new FilterArguments()
.Add("default", FluidValue.Create(@default, TemplateOptions.Default))
.Add("allow_false", FluidValue.Create(allowFalse, TemplateOptions.Default));
var context = new TemplateContext();
var result = await MiscFilters.Default("empty" == input as string ? EmptyValue.Instance : FluidValue.Create(input, TemplateOptions.Default), arguments, context);
Assert.Equal(expected, result.ToObjectValue());
}
[Fact]
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(context).Count());
}
[Fact]
public async Task EncodeUrl()
{
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.UrlEncode(input, arguments, context);
Assert.Equal("john%40liquid.com", result.ToStringValue());
}
[Fact]
public async Task DecodeUrl()
{
var input = new StringValue("john%40liquid.com");
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.UrlDecode(input, arguments, context);
}
[Theory]
[InlineData("a<>:a?", "YTw+OmE/")]
public async Task Base64Encode(string value, string expected)
{
var input = new StringValue(value);
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.Base64Encode(input, arguments, context);
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("YTw+OmE/", "a<>:a?")]
public async Task Base64Decode(string value, string expected)
{
var input = new StringValue(value);
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.Base64Decode(input, arguments, context);
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("a<>:a?", "YTw-OmE_")]
public async Task Base64UrlSafeEncode(string value, string expected)
{
// Arrange
var input = new StringValue(value);
var arguments = new FilterArguments();
var context = new TemplateContext();
// Act
var result = await MiscFilters.Base64UrlSafeEncode(input, arguments, context);
// Assert
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("YTw-OmE_", "a<>:a?")]
public async Task Base64UrlSafeDecode(string value, string expected)
{
// Arrange
var input = new StringValue(value);
var arguments = new FilterArguments();
var context = new TemplateContext();
// Act
var result = await MiscFilters.Base64UrlSafeDecode(input, arguments, context);
// Assert
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("Have <em>you</em> read <strong>Ulysses</strong>?", "Have you read Ulysses?")]
[InlineData("Have you read Ulysses?", "Have you read Ulysses?")]
[InlineData("", "")]
[InlineData(null, "")]
public async Task StripHtml(string value, string expected)
{
var input = new StringValue(value);
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.StripHtml(input, arguments, context);
Assert.Equal(expected, result.ToStringValue());
}
[Fact]
public async Task Escape()
{
var input = new StringValue("Have you read 'James & the Giant Peach'?");
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.Escape(input, arguments, context);
Assert.Equal("Have you read 'James & the Giant Peach'?", result.ToStringValue());
}
[Fact]
public async Task EscapeOnce()
{
var input = new StringValue("1 < 2 & 3");
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.EscapeOnce(input, arguments, context);
Assert.Equal("1 < 2 & 3", result.ToStringValue());
}
[Theory]
[InlineData("%a", "Tue")]
[InlineData("%a", "Sun", "2022-06-26 00:00:00 -0500")]
[InlineData("%^a", "TUE")]
[InlineData("%A", "Tuesday")]
[InlineData("%^A", "TUESDAY")]
[InlineData("%b", "Aug")]
[InlineData("%^b", "AUG")]
[InlineData("%B", "August")]
[InlineData("%^B", "AUGUST")]
[InlineData("%c", "Tuesday, August 1, 2017 5:04:36 PM")]
[InlineData("%^c", "TUESDAY, AUGUST 1, 2017 5:04:36 PM")]
[InlineData("%C", "20")]
[InlineData("%C", "02", "0217-01-01")]
[InlineData("%d", "01")]
[InlineData("%_d", " 1")]
[InlineData("%-d", "1")]
[InlineData("%D", "08/01/17")]
[InlineData("%e", " 1")]
[InlineData("%F", "2017-08-01")]
[InlineData("%G", "2022", "2023-01-01 12:00:00")]
[InlineData("%G", "2024", "2024-01-01 12:00:00")]
[InlineData("%g", "22", "2023-01-01 12:00:00")]
[InlineData("%g", "24", "2024-01-01 12:00:00")]
[InlineData("%H", "17")]
[InlineData("%I", "05")]
[InlineData("%j", "213")]
[InlineData("%k", "17")]
[InlineData("%l", " 5")]
[InlineData("%L", "123")]
[InlineData("%m", "08")]
[InlineData("%_m", " 8")]
[InlineData("%-m", "8")]
[InlineData("%M", "04")]
[InlineData("%n", "\n")]
[InlineData("%N", "123456800")] // nanoseconds are parsed to the 7th digit
[InlineData("%3N", "123")]
[InlineData("%1N", "1")]
[InlineData("%p", "PM")]
[InlineData("%P", "pm")]
[InlineData("%r", "05:04:36 PM")]
[InlineData("%R", "17:04")]
[InlineData("%s", "1501578276")]
[InlineData("%20s", "00000000001501578276")]
[InlineData("%_20s", " 1501578276")]
[InlineData("%S", "36")]
[InlineData("%t", "\t")]
[InlineData("%T", "17:04:36")]
[InlineData("%u", "2")]
[InlineData("%u", "7", "2022-06-26 00:00:00")]
[InlineData("%U", "52", "2016-12-31T12:00:00")] // Saturday 12/31
[InlineData("%U", "01", "2017-01-01T12:00:00")] // Sunday 01/01 - week begins on a Sunday (for %U)
[InlineData("%U", "01", "2017-01-02T12:00:00")] // Monday 01/02 - week begins on a Sunday (for %U)
[InlineData("%U", "26", "2022-06-26T00:00:00")]
[InlineData("%v", " 1-Aug-2017")]
[InlineData("%^v", " 1-AUG-2017")]
[InlineData("%V", "31")]
[InlineData("%W", "52", "2016-12-31T12:00:00")] // Saturday 12/31
[InlineData("%W", "00", "2017-01-01T12:00:00")] // Sunday 01/01 - still not first week of the year
[InlineData("%W", "01", "2017-01-02T12:00:00")] // Monday 01/02 - week begins on a Monday (for %W)
[InlineData("%W", "25", "2022-06-26T00:00:00")]
[InlineData("%y", "17")]
[InlineData("%Y", "2017")]
[InlineData("%Y", "0217", "0217-01-01")]
[InlineData("%y", "17", "0217-01-01")]
[InlineData("%y", "07", "0207-01-01")]
[InlineData("%z", "+0800")]
[InlineData("%Z", "+08:00")]
[InlineData("%:z", "+08:00")]
[InlineData("%+", "Tue Aug 1 17:04:36 +08:00 2017")]
[InlineData("%%", "%")]
[InlineData("It is %r", "It is 05:04:36 PM")]
[InlineData("Chained %z%:z%a%a%^a", "Chained +0800+08:00TueTueTUE")]
[InlineData("%Y-%m-%dT%H:%M:%S.%L", "2017-08-01T17:04:36.123")]
public async Task Date(string format, string expected, string dateTime = "2017-08-01T17:04:36.123456789+08:00")
{
var arguments = new FilterArguments(new StringValue(format));
var options = new TemplateOptions() { CultureInfo = new CultureInfo("en-US", useUserOverride: false), TimeZone = TimeZoneInfo.Utc };
var context = new TemplateContext(options);
new StringValue(dateTime).TryGetDateTimeInput(new TemplateContext(), out var customDateTime);
var input = new DateTimeValue(customDateTime);
var result = await MiscFilters.Date(input, arguments, context);
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("2020-05-18T12:00:00+01:00", "%l:%M%P", "12:00pm")]
[InlineData("2020-05-18T08:00:00+01:00", "%l:%M%P", " 8:00am")]
[InlineData("2020-05-18T20:00:00+01:00", "%l:%M%P", " 8:00pm")]
[InlineData("2020-05-18T20:00:00+01:00", "%-l:%M%P", "8:00pm")]
[InlineData("2020-05-18T20:00:00+01:00", "%I:%M%P", "08:00pm")]
[InlineData("2020-05-18T23:59:00+01:00", "%l:%M%P", "11:59pm")]
[InlineData("2020-05-18T00:00:00+01:00", "%l:%M%P", "12:00am")]
[InlineData("2020-05-18T11:59:00+01:00", "%l:%M%P", "11:59am")]
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());
}
[Theory]
[InlineData("2020-05-18T02:13:09+00:00", "America/New_York", "2020-05-17T22:13:09-04:00")]
[InlineData("2020-05-18T02:13:09+00:00", "Europe/London", "2020-05-18T03:13:09+01:00")]
[InlineData("2020-05-18T02:13:09+00:00", "Europe/wrongTZ", "2020-05-18T02:13:09+00:00")]
public async Task ChangeTimeZone(string initialDateTime, string timeZone, string expected)
{
var input = new DateTimeValue(DateTimeOffset.Parse(initialDateTime));
var arguments = new FilterArguments(new StringValue(timeZone));
var options = new TemplateOptions() { CultureInfo = CultureInfo.InvariantCulture };
var context = new TemplateContext(options);
var result = await MiscFilters.ChangeTimeZone(input, arguments, context);
Assert.Equal(expected, ((DateTimeOffset)result.ToObjectValue()).ToString("yyyy-MM-ddTHH:mm:ssK"));
}
[Theory]
[InlineData("2022-12-13T21:02:18.399+00:00", "utc", "2022-12-13T21:02:18.399+00:00")]
[InlineData("2022-12-13T21:02:18.399+00:00", "America/New_York", "2022-12-13T21:02:18.399+00:00")]
[InlineData("2022-12-13T21:02:18.399+00:00", "Australia/Adelaide", "2022-12-13T21:02:18.399+00:00")]
[InlineData("2022-12-13T21:02:18.399", "utc", "2022-12-13T21:02:18.399+00:00")]
[InlineData("2022-12-13T21:02:18.399", "America/New_York", "2022-12-13T21:02:18.399-05:00")]
[InlineData("2022-12-13T21:02:18.399", "Australia/Adelaide", "2022-12-13T21:02:18.399+10:30")]
[InlineData("2022-12-13T21:02:18.399+01:00", "utc", "2022-12-13T21:02:18.399+01:00")] // Parsed as UTC+1, converted to UTC
public async Task DateFilterUsesContextTimezone(string initialDateTime, string timeZone, string expected)
{
// - When a TZ is provided in the source string, the resulting DateTimeOffset uses it
// - When no TZ is provided, we assume the local offset (context.TimeZone)
var input = new StringValue(initialDateTime);
var context = new TemplateContext { TimeZone = TZConvert.GetTimeZoneInfo(timeZone) };
var date = await MiscFilters.Date(input, new FilterArguments(new StringValue(RoundTripDateTimePattern)), context);
Assert.Equal(expected, date.ToStringValue());
}
[Theory]
[InlineData("2020-05-18T02:13:09+00:00", "America/New_York", "%l:%M%P", "10:13pm")]
[InlineData("2020-05-18T02:13:09+00:00", "Europe/London", "%l:%M%P", "3:13am")]
[InlineData("2020-05-18T02:13:09+00:00", "Europe/wrongTZ", "%l:%M%P", "2:13am")]
[InlineData("2020-05-18T02:13:09+00:00", "Australia/Adelaide", "%l:%M%P", "11:43am")]
public async Task 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 options = new TemplateOptions() { CultureInfo = CultureInfo.InvariantCulture };
var context = new TemplateContext(options);
var result = await MiscFilters.ChangeTimeZone(input, timeZoneArgument, context);
result = await MiscFilters.Date(result, formatArgument, context);
Assert.Equal(expected, result.ToStringValue().Trim());
}
[Fact]
public async Task ShouldChangeToLocalTimeZone()
{
// - When a TZ is provided in the source string, the resulting DateTimeOffset uses it
// - When no TZ is provided, we assume the local offset (context.TimeZone)
var input = new StringValue("2022-12-13T21:02:18.399+01:00");
var context = new TemplateContext { TimeZone = Pacific };
var date = await MiscFilters.ChangeTimeZone(input, new FilterArguments(new StringValue("local")), context);
var formatted = await MiscFilters.Date(date, new FilterArguments(new StringValue(RoundTripDateTimePattern)), context);
Assert.Equal("2022-12-13T12:02:18.399-08:00", formatted.ToStringValue());
}
[Fact]
public async Task DateResolvesNow()
{
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.Date(input, arguments, context);
Assert.Equal("08/01/17", result.ToStringValue());
}
[Fact]
public async Task DateResolvesToday()
{
var input = new StringValue("today");
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.Date(input, arguments, context);
Assert.Equal("08/01/17", result.ToStringValue());
}
[Fact]
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());
}
[Fact]
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());
}
[Fact]
public async Task DateWithoutFormatShouldReturnInput()
{
var input = new StringValue("08/01/2017");
var options = new TemplateOptions() { CultureInfo = CultureInfo.InvariantCulture, TimeZone = TimeZoneInfo.Utc };
var context = new TemplateContext(options);
var result = await MiscFilters.Date(input, FilterArguments.Empty, context);
Assert.IsType<DateTimeValue>(result);
Assert.Equal("2017-08-01 00:00:00Z", result.ToStringValue());
}
[Theory]
[InlineData(0, "1969-12-31T19:00:00.000-05:00")]
[InlineData(10, "1969-12-31T19:00:10.000-05:00")]
[InlineData(-10, "1969-12-31T18:59:50.000-05:00")]
public async Task DateNumberIsParsedAsSeconds(long number, string expected)
{
// The resulting DateTimeValue should use the default TimeZone
var options = new TemplateOptions { TimeZone = Eastern };
var input = NumberValue.Create(number);
var format = new FilterArguments(new StringValue(RoundTripDateTimePattern));
var context = new TemplateContext(options);
var result = await MiscFilters.Date(input, format, context);
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("0", "1969-12-31T19:00:00.000-05:00")]
[InlineData("1:2", "1969-12-31T20:02:00.000-05:00")]
[InlineData("1:2:3.1", "1969-12-31T20:02:03.100-05:00")]
public async Task DateTimeSpanIsParsedWithLocalTimeZone(string timespan, string expected)
{
var options = new TemplateOptions { TimeZone = Eastern };
var input = FluidValue.Create(TimeSpan.Parse(timespan), options);
var format = new FilterArguments(new StringValue(RoundTripDateTimePattern));
var context = new TemplateContext(options);
var result = await MiscFilters.Date(input, format, context);
Assert.Equal(expected, result.ToStringValue());
}
[Fact]
public async Task NoTimeZoneIsParsedAsLocal()
{
var input = StringValue.Create("1970-01-01 00:00:00");
var format = new FilterArguments(new StringValue(RoundTripDateTimePattern));
var context = new TemplateContext { TimeZone = Pacific };
var result = await MiscFilters.Date(input, format, context);
Assert.Equal("1970-01-01T00:00:00.000-08:00", result.ToStringValue());
}
[Fact]
public async Task TimeZoneIsParsed()
{
// 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 -05:00");
var format = new FilterArguments(new StringValue("%s"));
var context = new TemplateContext { TimeZone = TimeZoneInfo.Utc };
var result = await MiscFilters.Date(input, format, context);
Assert.Equal("18000", result.ToStringValue());
}
[Fact]
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());
}
[Fact]
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());
}
[Fact]
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());
}
[Fact]
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", useUserOverride: false), TimeZone = TimeZoneInfo.Utc });
var resultFR = await MiscFilters.Date(input, arguments, context);
context = new TemplateContext(new TemplateOptions { CultureInfo = new CultureInfo("en-US", useUserOverride: false), 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());
}
[Fact]
public async Task DateIsRenderedWithCulture()
{
var input = new StringValue("08/01/2017");
var format = "%c";
var arguments = new FilterArguments(new StringValue(format));
var context = new TemplateContext { CultureInfo = new CultureInfo("fr-FR", useUserOverride: false), TimeZone = TimeZoneInfo.Utc };
var resultFR = await MiscFilters.Date(input, arguments, context);
context = new TemplateContext { CultureInfo = new CultureInfo("en-US", useUserOverride: false), TimeZone = TimeZoneInfo.Utc };
var resultUS = await 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());
}
[Theory]
[InlineData("SomeThing", "some-thing")]
[InlineData("capsONInside", "caps-on-inside")]
[InlineData("CAPSOnOUTSIDE", "caps-on-outside")]
[InlineData("ALLCAPS", "allcaps")]
[InlineData("One1Two2Three3", "one1-two2-three3")]
[InlineData("ONE1TWO2THREE3", "one1two2three3")]
[InlineData("First_Second_ThirdHi", "first-second-third-hi")]
[InlineData("100% M & Ms!!!", "100-m-ms")]
[InlineData("!!!100% M & Ms", "100-m-ms")]
public async Task Handleize(string text, string expected)
{
var input = new StringValue(text);
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.Handleize(input, arguments, context);
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("Hello World!", "\"Hello World!\"")]
[InlineData("\"", "\"\\u0022\"")]
[InlineData("'", "\"\\u0027\"")]
[InlineData(123, "123")]
[InlineData(123.12, "123.12")]
[InlineData(-123.12, "-123.12")]
[InlineData(null, "null")]
[InlineData("", "\"\"")]
[InlineData(new int[] { 1, 2, 3 }, "[1,2,3]")]
[InlineData(new string[] { "a", "b", "c" }, "[\"a\",\"b\",\"c\"]")]
[InlineData(new object[0], "[]")]
[InlineData(new object[] { 1, "a", true }, "[1,\"a\",true]")]
public async Task Json(object value, string expected)
{
var input = FluidValue.Create(value, TemplateOptions.Default);
var arguments = new FilterArguments();
var context = new TemplateContext();
var result = await MiscFilters.Json(input, arguments, context);
Assert.Equal(expected, result.ToStringValue());
}
[Fact]
public async Task JsonShouldHideMembers()
{
var inputObject = new JsonAccessStrategy();
var templateOptions = new TemplateOptions();
templateOptions.MemberAccessStrategy.Register<JsonAccessStrategy, FluidValue>((obj, name, context) =>
{
return name switch
{
nameof(JsonAccessStrategy.Visible) => new StringValue(obj.Visible),
nameof(JsonAccessStrategy.Null) => new StringValue(obj.Null),
_ => NilValue.Instance
};
});
var input = FluidValue.Create(inputObject, templateOptions);
var expected = "{\"Visible\":\"Visible\",\"Null\":\"\"}";
var arguments = new FilterArguments();
var context = new TemplateContext(templateOptions);
var result = await MiscFilters.Json(input, arguments, context);
Assert.Equal(expected, result.ToStringValue());
}
[Fact]
public async Task JsonShouldHandleCircularReferences()
{
var model = TestObjects.RecursiveReferenceObject;
var input = FluidValue.Create(model, TemplateOptions.Default);
var to = new TemplateOptions();
to.MemberAccessStrategy.Register<TestObjects.Node>();
var result = await MiscFilters.Json(input, new FilterArguments(), new TemplateContext(to));
Assert.Equal("{\"Name\":\"Object1\",\"NodeRef\":{\"Name\":\"Child1\",\"NodeRef\":\"Circular reference has been detected.\"}}", result.ToStringValue());
}
[Fact]
public async Task JsonShouldHandleCircularReferencesOnSiblingPropertiesSeparately()
{
var model = TestObjects.SiblingPropertiesHaveSameReferenceObject;
var input = FluidValue.Create(model, TemplateOptions.Default);
var to = new TemplateOptions();
to.MemberAccessStrategy.Register<TestObjects.Node>();
to.MemberAccessStrategy.Register<TestObjects.MultipleNode>();
var result = await MiscFilters.Json(input, new FilterArguments(), new TemplateContext(to));
Assert.Equal("{\"Name\":\"MultipleNode1\",\"Node1\":{\"Name\":\"Object1\",\"NodeRef\":{\"Name\":\"Child1\",\"NodeRef\":\"Circular reference has been detected.\"}},\"Node2\":{\"Name\":\"Object1\",\"NodeRef\":{\"Name\":\"Child1\",\"NodeRef\":\"Circular reference has been detected.\"}}}", result.ToStringValue());
}
[Fact]
public async Task JsonShouldIgnoreStaticMembers()
{
var model = new JsonWithStaticMember { Id = 100 };
var input = FluidValue.Create(model, TemplateOptions.Default);
var options = new TemplateOptions();
options.MemberAccessStrategy.Register<JsonWithStaticMember>();
var result = await MiscFilters.Json(input, new FilterArguments(), new TemplateContext(options));
Assert.Equal("{\"Id\":100}", result.ToStringValue());
}
[Fact]
public async Task JsonShouldWriteNullIfDictionaryNotReturnFluidIndexable()
{
var model = new
{
Id = 1,
WithoutIndexable = new DictionaryWithoutIndexableTestObjects(new { }),
Bool = true
};
var options = new TemplateOptions();
options.MemberAccessStrategy.Register(model.GetType());
var input = FluidValue.Create(model, options);
var result = await MiscFilters.Json(input, new FilterArguments(), new TemplateContext(options));
Assert.Equal("{\"Id\":1,\"WithoutIndexable\":null,\"Bool\":true}", result.ToStringValue());
}
[Fact]
public async Task JsonShouldWriteValuesWithCorrectDataTypeForJObjectInput()
{
var model = new JObject
{
["a"] = true,
["b"] = 1,
["c"] = new DateTimeOffset(2017, 6, 8, 12, 53, 10, new TimeSpan(-7, 0, 0)),
["d"] = "string",
["e"] = null,
["f"] = new JObject
{
["f_a"] = 1.2,
["f_b"] = false,
["f_c"] = ""
},
["g"] = new JArray
{
"val1", "val2"
}
};
var input = FluidValue.Create(model, TemplateOptions.Default);
var result = await MiscFilters.Json(input, new FilterArguments(), new TemplateContext(TemplateOptions.Default));
var expected = "{\"a\":true,\"b\":1,\"c\":\"06/08/2017 12:53:10 -07:00\",\"d\":\"string\",\"e\":null,\"f\":{\"f_a\":1.2,\"f_b\":false,\"f_c\":\"\"},\"g\":[\"val1\",\"val2\"]}";
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("", "", "", "0")]
[InlineData(123456, "", "", "123456")]
[InlineData(123456.00, "", "en-US", "123456")]
[InlineData(123456.00, "N2", "en-US", "123,456.00")]
[InlineData(123456.00, "C2", "en-US", "$123,456.00")]
// Skip tests with spaces as Linux and Windows implementation don't use the same space
//[InlineData(123456.00, "C2", "fr-FR", "123 456,00 €")]
//[InlineData("123456.00", "C2", "fr-FR", "123 456,00 €")]
public async Task FormatNumber(object input, string format, string culture, string expected)
{
var cultureInfo = String.IsNullOrEmpty(culture)
? CultureInfo.InvariantCulture
: CultureInfo.CreateSpecificCulture(culture)
;
var arguments = new FilterArguments(new StringValue(format));
var context = new TemplateContext(new TemplateOptions { CultureInfo = cultureInfo });
var result = await MiscFilters.FormatNumber(FluidValue.Create(input, context.Options), arguments, context);
Assert.Equal(expected, result.ToStringValue());
}
[Theory]
[InlineData("", new object[] { 123 }, "", "")]
[InlineData("{0}", new object[] { 123 }, "", "123")]
[InlineData("hello {0}", new object[] { "world", 123 }, "", "hello world")]
[InlineData("{0:C2} {1:N2}", new object[] { 123, 456 }, "fr-FR", "123,00 € 456,00")]
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());
}
[Fact]
public async Task MD5()
{
// Arrange
var input = new StringValue("Fluid");
var arguments = new FilterArguments();
var context = new TemplateContext();
// Act
var result = await MiscFilters.MD5(input, arguments, context);
// Assert
Assert.Equal("c2e7db5ac7ab74ea4bb9a7a89d251f3a", result.ToStringValue());
}
[Fact]
public async Task Sha1()
{
// Arrange
var input = new StringValue("Fluid");
var arguments = new FilterArguments();
var context = new TemplateContext();
// Act
var result = await MiscFilters.Sha1(input, arguments, context);
// Assert
Assert.Equal("8bc9b7abbb676300656203a17863a0f0b8a2c2bf", result.ToStringValue());
}
[Fact]
public async Task Sha256()
{
// Arrange
var input = new StringValue("Fluid");
var arguments = new FilterArguments();
var context = new TemplateContext();
// Act
var result = await MiscFilters.Sha256(input, arguments, context);
// Assert
Assert.Equal("c7ac4687585ab5d3d5030db5a5cfc959fdf4e608cc396f1f615db345e35adb9e", result.ToStringValue());
}
public static class TestObjects
{
public class Node
{
public string Name { get; set; }
public Node NodeRef { get; set; }
}
public class MultipleNode
{
public string Name { get; set; }
public Node Node1 { get; set; }
public Node Node2 { get; set; }
}
public static Node RecursiveReferenceObject
{
get
{
var parent = new Node
{
Name = "Object1",
};
var child = new Node
{
Name = "Child1",
NodeRef = parent
};
parent.NodeRef = child;
return parent;
}
}
public static object SiblingPropertiesHaveSameReferenceObject
{
get
{
var n = RecursiveReferenceObject;
var m = new MultipleNode
{
Name = "MultipleNode1",
Node1 = n,
Node2 = n
};
return m;
}
}
}
private class JsonAccessStrategy
{
public string Visible { get; set; } = "Visible";
public string Null { get; set; }
public string Hidden { get; set; } = "Hidden";
}
private class JsonWithStaticMember
{
public static int StaticMember { get; set; } = 1;
public int Id { get; set; }
}
private class DictionaryWithoutIndexableTestObjects : ObjectValueBase
{
public override FluidValues Type => FluidValues.Dictionary;
public DictionaryWithoutIndexableTestObjects(object value) : base(value)
{
}
}
}
}