File: standard_filter_test.rb

package info (click to toggle)
ruby-liquid 5.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,372 kB
  • sloc: ruby: 14,164; makefile: 6
file content (1335 lines) | stat: -rw-r--r-- 45,254 bytes parent folder | download
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
# encoding: utf-8
# frozen_string_literal: true

require 'test_helper'

class TestThing
  attr_reader :foo

  def initialize
    @foo = 0
  end

  def to_s
    "woot: #{@foo}"
  end

  def [](_whatever)
    to_s
  end

  def to_liquid
    @foo += 1
    self
  end
end

class TestDrop < Liquid::Drop
  def initialize(value:)
    @value = value
  end

  attr_reader :value

  def registers
    "{#{@value.inspect}=>#{@context.registers[@value].inspect}}"
  end
end

class TestModel
  def initialize(value:)
    @value = value
  end

  def to_liquid
    TestDrop.new(value: @value)
  end
end

class TestEnumerable < Liquid::Drop
  include Enumerable

  def each(&block)
    [{ "foo" => 1, "bar" => 2 }, { "foo" => 2, "bar" => 1 }, { "foo" => 3, "bar" => 3 }].each(&block)
  end
end

class NumberLikeThing < Liquid::Drop
  def initialize(amount)
    @amount = amount
  end

  def to_number
    @amount
  end
end

class StandardFiltersTest < Minitest::Test
  Filters = Class.new(Liquid::StrainerTemplate)
  Filters.add_filter(Liquid::StandardFilters)

  include Liquid

  def setup
    @filters = Filters.new(Context.new)
  end

  def test_size
    assert_equal(3, @filters.size([1, 2, 3]))
    assert_equal(0, @filters.size([]))
    assert_equal(0, @filters.size(nil))
  end

  def test_downcase
    assert_equal('testing', @filters.downcase("Testing"))
    assert_equal('', @filters.downcase(nil))
  end

  def test_upcase
    assert_equal('TESTING', @filters.upcase("Testing"))
    assert_equal('', @filters.upcase(nil))
  end

  def test_slice
    assert_equal('oob', @filters.slice('foobar', 1, 3))
    assert_equal('oobar', @filters.slice('foobar', 1, 1000))
    assert_equal('', @filters.slice('foobar', 1, 0))
    assert_equal('o', @filters.slice('foobar', 1, 1))
    assert_equal('bar', @filters.slice('foobar', 3, 3))
    assert_equal('ar', @filters.slice('foobar', -2, 2))
    assert_equal('ar', @filters.slice('foobar', -2, 1000))
    assert_equal('r', @filters.slice('foobar', -1))
    assert_equal('', @filters.slice(nil, 0))
    assert_equal('', @filters.slice('foobar', 100, 10))
    assert_equal('', @filters.slice('foobar', -100, 10))
    assert_equal('oob', @filters.slice('foobar', '1', '3'))
    assert_raises(Liquid::ArgumentError) do
      @filters.slice('foobar', nil)
    end
    assert_raises(Liquid::ArgumentError) do
      @filters.slice('foobar', 0, "")
    end
    assert_equal("", @filters.slice("foobar", 0, -(1 << 64)))
    assert_equal("foobar", @filters.slice("foobar", 0, 1 << 63))
    assert_equal("", @filters.slice("foobar", 1 << 63, 6))
    assert_equal("", @filters.slice("foobar", -(1 << 63), 6))
  end

  def test_slice_on_arrays
    input = 'foobar'.split(//)
    assert_equal(%w(o o b), @filters.slice(input, 1, 3))
    assert_equal(%w(o o b a r), @filters.slice(input, 1, 1000))
    assert_equal(%w(), @filters.slice(input, 1, 0))
    assert_equal(%w(o), @filters.slice(input, 1, 1))
    assert_equal(%w(b a r), @filters.slice(input, 3, 3))
    assert_equal(%w(a r), @filters.slice(input, -2, 2))
    assert_equal(%w(a r), @filters.slice(input, -2, 1000))
    assert_equal(%w(r), @filters.slice(input, -1))
    assert_equal(%w(), @filters.slice(input, 100, 10))
    assert_equal(%w(), @filters.slice(input, -100, 10))
    assert_equal([], @filters.slice(input, 0, -(1 << 64)))
    assert_equal(input, @filters.slice(input, 0, 1 << 63))
    assert_equal([], @filters.slice(input, 1 << 63, 6))
    assert_equal([], @filters.slice(input, -(1 << 63), 6))
  end

  def test_find_on_empty_array
    assert_nil(@filters.find([], 'foo', 'bar'))
  end

  def test_find_index_on_empty_array
    assert_nil(@filters.find_index([], 'foo', 'bar'))
  end

  def test_has_on_empty_array
    refute(@filters.has([], 'foo', 'bar'))
  end

  def test_truncate
    assert_equal('1234...', @filters.truncate('1234567890', 7))
    assert_equal('1234567890', @filters.truncate('1234567890', 20))
    assert_equal('...', @filters.truncate('1234567890', 0))
    assert_equal('1234567890', @filters.truncate('1234567890'))
    assert_equal("测试...", @filters.truncate("测试测试测试测试", 5))
    assert_equal('12341', @filters.truncate("1234567890", 5, 1))
    assert_equal("foobar", @filters.truncate("foobar", 1 << 63))
    assert_equal("...", @filters.truncate("foobar", -(1 << 63)))
  end

  def test_split
    assert_equal(['12', '34'], @filters.split('12~34', '~'))
    assert_equal(['A? ', ' ,Z'], @filters.split('A? ~ ~ ~ ,Z', '~ ~ ~'))
    assert_equal(['A?Z'], @filters.split('A?Z', '~'))
    assert_equal([], @filters.split(nil, ' '))
    assert_equal(['A', 'Z'], @filters.split('A1Z', 1))
  end

  def test_escape
    assert_equal('&lt;strong&gt;', @filters.escape('<strong>'))
    assert_equal('1', @filters.escape(1))
    assert_equal('2001-02-03', @filters.escape(Date.new(2001, 2, 3)))
    assert_nil(@filters.escape(nil))
  end

  def test_h
    assert_equal('&lt;strong&gt;', @filters.h('<strong>'))
    assert_equal('1', @filters.h(1))
    assert_equal('2001-02-03', @filters.h(Date.new(2001, 2, 3)))
    assert_nil(@filters.h(nil))
  end

  def test_escape_once
    assert_equal('&lt;strong&gt;Hulk&lt;/strong&gt;', @filters.escape_once('&lt;strong&gt;Hulk</strong>'))
  end

  def test_base64_encode
    assert_equal('b25lIHR3byB0aHJlZQ==', @filters.base64_encode('one two three'))
    assert_equal('', @filters.base64_encode(nil))
  end

  def test_base64_decode
    decoded = @filters.base64_decode('b25lIHR3byB0aHJlZQ==')
    assert_equal('one two three', decoded)
    assert_equal(Encoding::UTF_8, decoded.encoding)

    decoded = @filters.base64_decode('4pyF')
    assert_equal('✅', decoded)
    assert_equal(Encoding::UTF_8, decoded.encoding)

    decoded = @filters.base64_decode("/w==")
    assert_equal(Encoding::ASCII_8BIT, decoded.encoding)
    assert_equal((+"\xFF").force_encoding(Encoding::ASCII_8BIT), decoded)

    exception = assert_raises(Liquid::ArgumentError) do
      @filters.base64_decode("invalidbase64")
    end

    assert_equal('Liquid error: invalid base64 provided to base64_decode', exception.message)
  end

  def test_base64_url_safe_encode
    assert_equal(
      'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXogQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVogMTIzNDU2Nzg5MCAhQCMkJV4mKigpLT1fKy8_Ljo7W117fVx8',
      @filters.base64_url_safe_encode('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 !@#$%^&*()-=_+/?.:;[]{}\|'),
    )
    assert_equal('', @filters.base64_url_safe_encode(nil))
  end

  def test_base64_url_safe_decode
    decoded = @filters.base64_url_safe_decode('YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXogQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVogMTIzNDU2Nzg5MCAhQCMkJV4mKigpLT1fKy8_Ljo7W117fVx8')
    assert_equal(
      'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 !@#$%^&*()-=_+/?.:;[]{}\|',
      decoded,
    )
    assert_equal(Encoding::UTF_8, decoded.encoding)

    decoded = @filters.base64_url_safe_decode('4pyF')
    assert_equal('✅', decoded)
    assert_equal(Encoding::UTF_8, decoded.encoding)

    decoded = @filters.base64_url_safe_decode("_w==")
    assert_equal(Encoding::ASCII_8BIT, decoded.encoding)
    assert_equal((+"\xFF").force_encoding(Encoding::ASCII_8BIT), decoded)

    exception = assert_raises(Liquid::ArgumentError) do
      @filters.base64_url_safe_decode("invalidbase64")
    end
    assert_equal('Liquid error: invalid base64 provided to base64_url_safe_decode', exception.message)
  end

  def test_url_encode
    assert_equal('foo%2B1%40example.com', @filters.url_encode('foo+1@example.com'))
    assert_equal('1', @filters.url_encode(1))
    assert_equal('2001-02-03', @filters.url_encode(Date.new(2001, 2, 3)))
    assert_nil(@filters.url_encode(nil))
  end

  def test_url_decode
    assert_equal('foo bar', @filters.url_decode('foo+bar'))
    assert_equal('foo bar', @filters.url_decode('foo%20bar'))
    assert_equal('foo+1@example.com', @filters.url_decode('foo%2B1%40example.com'))
    assert_equal('1', @filters.url_decode(1))
    assert_equal('2001-02-03', @filters.url_decode(Date.new(2001, 2, 3)))
    assert_nil(@filters.url_decode(nil))
    exception = assert_raises(Liquid::ArgumentError) do
      @filters.url_decode('%ff')
    end
    assert_equal('Liquid error: invalid byte sequence in UTF-8', exception.message)
  end

  def test_truncatewords
    assert_equal('one two three', @filters.truncatewords('one two three', 4))
    assert_equal('one two...', @filters.truncatewords('one two three', 2))
    assert_equal('one two three', @filters.truncatewords('one two three'))
    assert_equal(
      'Two small (13&#8221; x 5.5&#8221; x 10&#8221; high) baskets fit inside one large basket (13&#8221;...',
      @filters.truncatewords('Two small (13&#8221; x 5.5&#8221; x 10&#8221; high) baskets fit inside one large basket (13&#8221; x 16&#8221; x 10.5&#8221; high) with cover.', 15),
    )
    assert_equal("测试测试测试测试", @filters.truncatewords('测试测试测试测试', 5))
    assert_equal('one two1', @filters.truncatewords("one two three", 2, 1))
    assert_equal('one two three...', @filters.truncatewords("one  two\tthree\nfour", 3))
    assert_equal('one two...', @filters.truncatewords("one two three four", 2))
    assert_equal('one...', @filters.truncatewords("one two three four", 0))
    assert_equal('one two three four', @filters.truncatewords("one two three four", 1 << 31))
    assert_equal('one...', @filters.truncatewords("one two three four", -(1 << 32)))
  end

  def test_strip_html
    assert_equal('test', @filters.strip_html("<div>test</div>"))
    assert_equal('test', @filters.strip_html("<div id='test'>test</div>"))
    assert_equal('', @filters.strip_html("<script type='text/javascript'>document.write('some stuff');</script>"))
    assert_equal('', @filters.strip_html("<style type='text/css'>foo bar</style>"))
    assert_equal('test', @filters.strip_html("<div\nclass='multiline'>test</div>"))
    assert_equal('test', @filters.strip_html("<!-- foo bar \n test -->test"))
    assert_equal('', @filters.strip_html(nil))

    # Quirk of the existing implementation
    assert_equal('foo;', @filters.strip_html("<<<script </script>script>foo;</script>"))
  end

  def test_join
    assert_equal('1 2 3 4', @filters.join([1, 2, 3, 4]))
    assert_equal('1 - 2 - 3 - 4', @filters.join([1, 2, 3, 4], ' - '))
    assert_equal('1121314', @filters.join([1, 2, 3, 4], 1))
  end

  def test_join_calls_to_liquid_on_each_element
    drop = Class.new(Liquid::Drop) do
      def to_liquid
        'i did it'
      end
    end

    assert_equal('i did it, i did it', @filters.join([drop.new, drop.new], ", "))
  end

  def test_sort
    assert_equal([1, 2, 3, 4], @filters.sort([4, 3, 2, 1]))
    assert_equal([{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a"))
  end

  def test_sort_with_nils
    assert_equal([1, 2, 3, 4, nil], @filters.sort([nil, 4, 3, 2, 1]))
    assert_equal([{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }, {}], @filters.sort([{ "a" => 4 }, { "a" => 3 }, {}, { "a" => 1 }, { "a" => 2 }], "a"))
  end

  def test_sort_when_property_is_sometimes_missing_puts_nils_last
    input       = [
      { "price" => 4, "handle" => "alpha" },
      { "handle" => "beta" },
      { "price" => 1, "handle" => "gamma" },
      { "handle" => "delta" },
      { "price" => 2, "handle" => "epsilon" },
    ]
    expectation = [
      { "price" => 1, "handle" => "gamma" },
      { "price" => 2, "handle" => "epsilon" },
      { "price" => 4, "handle" => "alpha" },
      { "handle" => "beta" },
      { "handle" => "delta" },
    ]
    assert_equal(expectation, @filters.sort(input, "price"))
  end

  def test_sort_natural
    assert_equal(["a", "B", "c", "D"], @filters.sort_natural(["c", "D", "a", "B"]))
    assert_equal([{ "a" => "a" }, { "a" => "B" }, { "a" => "c" }, { "a" => "D" }], @filters.sort_natural([{ "a" => "D" }, { "a" => "c" }, { "a" => "a" }, { "a" => "B" }], "a"))
  end

  def test_sort_natural_with_nils
    assert_equal(["a", "B", "c", "D", nil], @filters.sort_natural([nil, "c", "D", "a", "B"]))
    assert_equal([{ "a" => "a" }, { "a" => "B" }, { "a" => "c" }, { "a" => "D" }, {}], @filters.sort_natural([{ "a" => "D" }, { "a" => "c" }, {}, { "a" => "a" }, { "a" => "B" }], "a"))
  end

  def test_sort_natural_when_property_is_sometimes_missing_puts_nils_last
    input       = [
      { "price" => "4", "handle" => "alpha" },
      { "handle" => "beta" },
      { "price" => "1", "handle" => "gamma" },
      { "handle" => "delta" },
      { "price" => 2, "handle" => "epsilon" },
    ]
    expectation = [
      { "price" => "1", "handle" => "gamma" },
      { "price" => 2, "handle" => "epsilon" },
      { "price" => "4", "handle" => "alpha" },
      { "handle" => "beta" },
      { "handle" => "delta" },
    ]
    assert_equal(expectation, @filters.sort_natural(input, "price"))
  end

  def test_sort_natural_case_check
    input = [
      { "key" => "X" },
      { "key" => "Y" },
      { "key" => "Z" },
      { "fake" => "t" },
      { "key" => "a" },
      { "key" => "b" },
      { "key" => "c" },
    ]
    expectation = [
      { "key" => "a" },
      { "key" => "b" },
      { "key" => "c" },
      { "key" => "X" },
      { "key" => "Y" },
      { "key" => "Z" },
      { "fake" => "t" },
    ]
    assert_equal(expectation, @filters.sort_natural(input, "key"))
    assert_equal(["a", "b", "c", "X", "Y", "Z"], @filters.sort_natural(["X", "Y", "Z", "a", "b", "c"]))
  end

  def test_sort_empty_array
    assert_equal([], @filters.sort([], "a"))
  end

  def test_sort_invalid_property
    foo = [
      [1],
      [2],
      [3],
    ]

    assert_raises(Liquid::ArgumentError) do
      @filters.sort(foo, "bar")
    end
  end

  def test_sort_natural_empty_array
    assert_equal([], @filters.sort_natural([], "a"))
  end

  def test_sort_natural_invalid_property
    foo = [
      [1],
      [2],
      [3],
    ]

    assert_raises(Liquid::ArgumentError) do
      @filters.sort_natural(foo, "bar")
    end
  end

  def test_legacy_sort_hash
    assert_equal([{ a: 1, b: 2 }], @filters.sort(a: 1, b: 2))
  end

  def test_numerical_vs_lexicographical_sort
    assert_equal([2, 10], @filters.sort([10, 2]))
    assert_equal([{ "a" => 2 }, { "a" => 10 }], @filters.sort([{ "a" => 10 }, { "a" => 2 }], "a"))
    assert_equal(["10", "2"], @filters.sort(["10", "2"]))
    assert_equal([{ "a" => "10" }, { "a" => "2" }], @filters.sort([{ "a" => "10" }, { "a" => "2" }], "a"))
  end

  def test_uniq
    assert_equal(["foo"], @filters.uniq("foo"))
    assert_equal([1, 3, 2, 4], @filters.uniq([1, 1, 3, 2, 3, 1, 4, 3, 2, 1]))
    assert_equal([{ "a" => 1 }, { "a" => 3 }, { "a" => 2 }], @filters.uniq([{ "a" => 1 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a"))
    test_drop = TestDrop.new(value: "test")
    test_drop_alternate = TestDrop.new(value: "test")
    assert_equal([test_drop], @filters.uniq([test_drop, test_drop_alternate], 'value'))
  end

  def test_uniq_empty_array
    assert_equal([], @filters.uniq([], "a"))
  end

  def test_uniq_invalid_property
    foo = [
      [1],
      [2],
      [3],
    ]

    assert_raises(Liquid::ArgumentError) do
      @filters.uniq(foo, "bar")
    end
  end

  def test_compact_empty_array
    assert_equal([], @filters.compact([], "a"))
  end

  def test_compact_invalid_property
    foo = [
      [1],
      [2],
      [3],
    ]

    assert_raises(Liquid::ArgumentError) do
      @filters.compact(foo, "bar")
    end
  end

  def test_reverse
    assert_equal([4, 3, 2, 1], @filters.reverse([1, 2, 3, 4]))
  end

  def test_legacy_reverse_hash
    assert_equal([{ a: 1, b: 2 }], @filters.reverse(a: 1, b: 2))
  end

  def test_map
    assert_equal([1, 2, 3, 4], @filters.map([{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], 'a'))
    assert_template_result(
      'abc',
      "{{ ary | map:'foo' | map:'bar' }}",
      { 'ary' => [{ 'foo' => { 'bar' => 'a' } }, { 'foo' => { 'bar' => 'b' } }, { 'foo' => { 'bar' => 'c' } }] },
    )
  end

  def test_map_doesnt_call_arbitrary_stuff
    assert_template_result("", '{{ "foo" | map: "__id__" }}')
    assert_template_result("", '{{ "foo" | map: "inspect" }}')
  end

  def test_map_calls_to_liquid
    t = TestThing.new
    assert_template_result("woot: 1", '{{ foo | map: "whatever" }}', { "foo" => [t] })
  end

  def test_map_calls_context=
    model = TestModel.new(value: :test)

    template = Template.parse('{{ foo | map: "registers" }}')
    template.registers[:test] = 1234
    template.assigns['foo'] = [model]

    assert_template_result("{:test=>1234}", template.render!)
  end

  def test_map_on_hashes
    assert_template_result(
      "4217",
      '{{ thing | map: "foo" | map: "bar" }}',
      { "thing" => { "foo" => [{ "bar" => 42 }, { "bar" => 17 }] } },
    )
  end

  def test_legacy_map_on_hashes_with_dynamic_key
    template = "{% assign key = 'foo' %}{{ thing | map: key | map: 'bar' }}"
    hash     = { "foo" => { "bar" => 42 } }
    assert_template_result("42", template, { "thing" => hash })
  end

  def test_sort_calls_to_liquid
    t = TestThing.new
    Liquid::Template.parse('{{ foo | sort: "whatever" }}').render("foo" => [t])
    assert(t.foo > 0)
  end

  def test_map_over_proc
    drop  = TestDrop.new(value: "testfoo")
    p     = proc { drop }
    output = Liquid::Template.parse('{{ procs | map: "value" }}').render!({ "procs" => [p] })
    assert_equal("testfoo", output)
  end

  def test_map_over_drops_returning_procs
    drops = [
      {
        "proc" => -> { "foo" },
      },
      {
        "proc" => -> { "bar" },
      },
    ]
    output = Liquid::Template.parse('{{ drops | map: "proc" }}').render!({ "drops" => drops })
    assert_equal("foobar", output)
  end

  def test_map_works_on_enumerables
    output = Liquid::Template.parse('{{ foo | map: "foo" }}').render!({ "foo" => TestEnumerable.new })
    assert_equal("123", output)
  end

  def test_map_returns_empty_on_2d_input_array
    foo = [
      [1],
      [2],
      [3],
    ]

    assert_raises(Liquid::ArgumentError) do
      @filters.map(foo, "bar")
    end
  end

  def test_map_with_value_property
    array = [
      { "handle" => "alpha", "value" => "A" },
      { "handle" => "beta", "value" => "B" },
      { "handle" => "gamma", "value" => "C" }
    ]

    assert_template_result("A B C", "{{ array | map: 'value' | join: ' ' }}", { "array" => array })
  end

  def test_map_returns_input_with_no_property
    foo = [
      [1],
      [2],
      [3],
    ]

    assert_raises(Liquid::ArgumentError) do
      @filters.map(foo, nil)
    end
  end

  def test_sort_works_on_enumerables
    assert_template_result("213", '{{ foo | sort: "bar" | map: "foo" }}', { "foo" => TestEnumerable.new })
  end

  def test_first_and_last_call_to_liquid
    assert_template_result('foobar', '{{ foo | first }}', { 'foo' => [ThingWithToLiquid.new] })
    assert_template_result('foobar', '{{ foo | last }}', { 'foo' => [ThingWithToLiquid.new] })
  end

  def test_truncate_calls_to_liquid
    assert_template_result("wo...", '{{ foo | truncate: 5 }}', { "foo" => TestThing.new })
  end

  def test_date
    assert_equal('May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B"))
    assert_equal('June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B"))
    assert_equal('July', @filters.date(Time.parse("2006-07-05 10:00:00"), "%B"))

    assert_equal('May', @filters.date("2006-05-05 10:00:00", "%B"))
    assert_equal('June', @filters.date("2006-06-05 10:00:00", "%B"))
    assert_equal('July', @filters.date("2006-07-05 10:00:00", "%B"))

    assert_equal('2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", ""))
    assert_equal('2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", ""))
    assert_equal('2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", ""))
    assert_equal('2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", nil))

    assert_equal('07/05/2006', @filters.date("2006-07-05 10:00:00", "%m/%d/%Y"))

    assert_equal("07/16/2004", @filters.date("Fri Jul 16 01:00:00 2004", "%m/%d/%Y"))
    assert_equal(Date.today.year.to_s, @filters.date('now', '%Y'))
    assert_equal(Date.today.year.to_s, @filters.date('today', '%Y'))
    assert_equal(Date.today.year.to_s, @filters.date('Today', '%Y'))

    assert_nil(@filters.date(nil, "%B"))

    assert_equal('', @filters.date('', "%B"))

    with_timezone("UTC") do
      assert_equal("07/05/2006", @filters.date(1152098955, "%m/%d/%Y"))
      assert_equal("07/05/2006", @filters.date("1152098955", "%m/%d/%Y"))
    end
  end

  def test_first_last
    assert_equal(1, @filters.first([1, 2, 3]))
    assert_equal(3, @filters.last([1, 2, 3]))
    assert_nil(@filters.first([]))
    assert_nil(@filters.last([]))
  end

  def test_replace
    assert_equal('b b b b', @filters.replace('a a a a', 'a', 'b'))
    assert_equal('2 2 2 2', @filters.replace('1 1 1 1', 1, 2))
    assert_equal('1 1 1 1', @filters.replace('1 1 1 1', 2, 3))
    assert_template_result('2 2 2 2', "{{ '1 1 1 1' | replace: '1', 2 }}")

    assert_equal('b a a a', @filters.replace_first('a a a a', 'a', 'b'))
    assert_equal('2 1 1 1', @filters.replace_first('1 1 1 1', 1, 2))
    assert_equal('1 1 1 1', @filters.replace_first('1 1 1 1', 2, 3))
    assert_template_result('2 1 1 1', "{{ '1 1 1 1' | replace_first: '1', 2 }}")

    assert_equal('a a a b', @filters.replace_last('a a a a', 'a', 'b'))
    assert_equal('1 1 1 2', @filters.replace_last('1 1 1 1', 1, 2))
    assert_equal('1 1 1 1', @filters.replace_last('1 1 1 1', 2, 3))
    assert_template_result('1 1 1 2', "{{ '1 1 1 1' | replace_last: '1', 2 }}")
  end

  def test_remove
    assert_equal('   ', @filters.remove("a a a a", 'a'))
    assert_template_result('   ', "{{ '1 1 1 1' | remove: 1 }}")

    assert_equal('b a a', @filters.remove_first("a b a a", 'a '))
    assert_template_result(' 1 1 1', "{{ '1 1 1 1' | remove_first: 1 }}")

    assert_equal('a a b', @filters.remove_last("a a b a", ' a'))
    assert_template_result('1 1 1 ', "{{ '1 1 1 1' | remove_last: 1 }}")
  end

  def test_pipes_in_string_arguments
    assert_template_result('foobar', "{{ 'foo|bar' | remove: '|' }}")
  end

  def test_strip
    assert_template_result('ab c', "{{ source | strip }}", { 'source' => " ab c  " })
    assert_template_result('ab c', "{{ source | strip }}", { 'source' => " \tab c  \n \t" })
  end

  def test_lstrip
    assert_template_result('ab c  ', "{{ source | lstrip }}", { 'source' => " ab c  " })
    assert_template_result("ab c  \n \t", "{{ source | lstrip }}", { 'source' => " \tab c  \n \t" })
  end

  def test_rstrip
    assert_template_result(" ab c", "{{ source | rstrip }}", { 'source' => " ab c  " })
    assert_template_result(" \tab c", "{{ source | rstrip }}", { 'source' => " \tab c  \n \t" })
  end

  def test_strip_newlines
    assert_template_result('abc', "{{ source | strip_newlines }}", { 'source' => "a\nb\nc" })
    assert_template_result('abc', "{{ source | strip_newlines }}", { 'source' => "a\r\nb\nc" })
  end

  def test_newlines_to_br
    assert_template_result("a<br />\nb<br />\nc", "{{ source | newline_to_br }}", { 'source' => "a\nb\nc" })
    assert_template_result("a<br />\nb<br />\nc", "{{ source | newline_to_br }}", { 'source' => "a\r\nb\nc" })
  end

  def test_plus
    assert_template_result("2", "{{ 1 | plus:1 }}")
    assert_template_result("2.0", "{{ '1' | plus:'1.0' }}")

    assert_template_result("5", "{{ price | plus:'2' }}", { 'price' => NumberLikeThing.new(3) })
  end

  def test_minus
    assert_template_result("4", "{{ input | minus:operand }}", { 'input' => 5, 'operand' => 1 })
    assert_template_result("2.3", "{{ '4.3' | minus:'2' }}")

    assert_template_result("5", "{{ price | minus:'2' }}", { 'price' => NumberLikeThing.new(7) })
  end

  def test_abs
    assert_template_result("17", "{{ 17 | abs }}")
    assert_template_result("17", "{{ -17 | abs }}")
    assert_template_result("17", "{{ '17' | abs }}")
    assert_template_result("17", "{{ '-17' | abs }}")
    assert_template_result("0", "{{ 0 | abs }}")
    assert_template_result("0", "{{ '0' | abs }}")
    assert_template_result("17.42", "{{ 17.42 | abs }}")
    assert_template_result("17.42", "{{ -17.42 | abs }}")
    assert_template_result("17.42", "{{ '17.42' | abs }}")
    assert_template_result("17.42", "{{ '-17.42' | abs }}")
  end

  def test_times
    assert_template_result("12", "{{ 3 | times:4 }}")
    assert_template_result("0", "{{ 'foo' | times:4 }}")
    assert_template_result("6", "{{ '2.1' | times:3 | replace: '.','-' | plus:0}}")
    assert_template_result("7.25", "{{ 0.0725 | times:100 }}")
    assert_template_result("-7.25", '{{ "-0.0725" | times:100 }}')
    assert_template_result("7.25", '{{ "-0.0725" | times: -100 }}')
    assert_template_result("4", "{{ price | times:2 }}", { 'price' => NumberLikeThing.new(2) })
  end

  def test_divided_by
    assert_template_result("4", "{{ 12 | divided_by:3 }}")
    assert_template_result("4", "{{ 14 | divided_by:3 }}")

    assert_template_result("5", "{{ 15 | divided_by:3 }}")
    assert_equal("Liquid error: divided by 0", Template.parse("{{ 5 | divided_by:0 }}").render)

    assert_template_result("0.5", "{{ 2.0 | divided_by:4 }}")
    assert_raises(Liquid::ZeroDivisionError) do
      assert_template_result("4", "{{ 1 | modulo: 0 }}")
    end

    assert_template_result("5", "{{ price | divided_by:2 }}", { 'price' => NumberLikeThing.new(10) })
  end

  def test_modulo
    assert_template_result("1", "{{ 3 | modulo:2 }}")
    assert_raises(Liquid::ZeroDivisionError) do
      assert_template_result("4", "{{ 1 | modulo: 0 }}")
    end

    assert_template_result("1", "{{ price | modulo:2 }}", { 'price' => NumberLikeThing.new(3) })
  end

  def test_round
    assert_template_result("5", "{{ input | round }}", { 'input' => 4.6 })
    assert_template_result("4", "{{ '4.3' | round }}")
    assert_template_result("4.56", "{{ input | round: 2 }}", { 'input' => 4.5612 })
    assert_raises(Liquid::FloatDomainError) do
      assert_template_result("4", "{{ 1.0 | divided_by: 0.0 | round }}")
    end

    assert_template_result("5", "{{ price | round }}", { 'price' => NumberLikeThing.new(4.6) })
    assert_template_result("4", "{{ price | round }}", { 'price' => NumberLikeThing.new(4.3) })
  end

  def test_ceil
    assert_template_result("5", "{{ input | ceil }}", { 'input' => 4.6 })
    assert_template_result("5", "{{ '4.3' | ceil }}")
    assert_raises(Liquid::FloatDomainError) do
      assert_template_result("4", "{{ 1.0 | divided_by: 0.0 | ceil }}")
    end

    assert_template_result("5", "{{ price | ceil }}", { 'price' => NumberLikeThing.new(4.6) })
  end

  def test_floor
    assert_template_result("4", "{{ input | floor }}", { 'input' => 4.6 })
    assert_template_result("4", "{{ '4.3' | floor }}")
    assert_raises(Liquid::FloatDomainError) do
      assert_template_result("4", "{{ 1.0 | divided_by: 0.0 | floor }}")
    end

    assert_template_result("5", "{{ price | floor }}", { 'price' => NumberLikeThing.new(5.4) })
  end

  def test_at_most
    assert_template_result("4", "{{ 5 | at_most:4 }}")
    assert_template_result("5", "{{ 5 | at_most:5 }}")
    assert_template_result("5", "{{ 5 | at_most:6 }}")

    assert_template_result("4.5", "{{ 4.5 | at_most:5 }}")
    assert_template_result("5", "{{ width | at_most:5 }}", { 'width' => NumberLikeThing.new(6) })
    assert_template_result("4", "{{ width | at_most:5 }}", { 'width' => NumberLikeThing.new(4) })
    assert_template_result("4", "{{ 5 | at_most: width }}", { 'width' => NumberLikeThing.new(4) })
  end

  def test_at_least
    assert_template_result("5", "{{ 5 | at_least:4 }}")
    assert_template_result("5", "{{ 5 | at_least:5 }}")
    assert_template_result("6", "{{ 5 | at_least:6 }}")

    assert_template_result("5", "{{ 4.5 | at_least:5 }}")
    assert_template_result("6", "{{ width | at_least:5 }}", { 'width' => NumberLikeThing.new(6) })
    assert_template_result("5", "{{ width | at_least:5 }}", { 'width' => NumberLikeThing.new(4) })
    assert_template_result("6", "{{ 5 | at_least: width }}", { 'width' => NumberLikeThing.new(6) })
  end

  def test_append
    assigns = { 'a' => 'bc', 'b' => 'd' }
    assert_template_result('bcd', "{{ a | append: 'd'}}", assigns)
    assert_template_result('bcd', "{{ a | append: b}}", assigns)
  end

  def test_concat
    assert_equal([1, 2, 3, 4], @filters.concat([1, 2], [3, 4]))
    assert_equal([1, 2, 'a'],  @filters.concat([1, 2], ['a']))
    assert_equal([1, 2, 10],   @filters.concat([1, 2], [10]))

    assert_raises(Liquid::ArgumentError, "concat filter requires an array argument") do
      @filters.concat([1, 2], 10)
    end
  end

  def test_prepend
    assigns = { 'a' => 'bc', 'b' => 'a' }
    assert_template_result('abc', "{{ a | prepend: 'a'}}", assigns)
    assert_template_result('abc', "{{ a | prepend: b}}", assigns)
  end

  def test_default
    assert_equal("foo", @filters.default("foo", "bar"))
    assert_equal("bar", @filters.default(nil, "bar"))
    assert_equal("bar", @filters.default("", "bar"))
    assert_equal("bar", @filters.default(false, "bar"))
    assert_equal("bar", @filters.default([], "bar"))
    assert_equal("bar", @filters.default({}, "bar"))
    assert_template_result('bar', "{{ false | default: 'bar' }}")
    assert_template_result('bar', "{{ drop | default: 'bar' }}", { 'drop' => BooleanDrop.new(false) })
    assert_template_result('Yay', "{{ drop | default: 'bar' }}", { 'drop' => BooleanDrop.new(true) })
  end

  def test_default_handle_false
    assert_equal("foo", @filters.default("foo", "bar", "allow_false" => true))
    assert_equal("bar", @filters.default(nil, "bar", "allow_false" => true))
    assert_equal("bar", @filters.default("", "bar", "allow_false" => true))
    assert_equal(false, @filters.default(false, "bar", "allow_false" => true))
    assert_equal("bar", @filters.default([], "bar", "allow_false" => true))
    assert_equal("bar", @filters.default({}, "bar", "allow_false" => true))
    assert_template_result('false', "{{ false | default: 'bar', allow_false: true }}")
    assert_template_result('Nay', "{{ drop | default: 'bar', allow_false: true }}", { 'drop' => BooleanDrop.new(false) })
    assert_template_result('Yay', "{{ drop | default: 'bar', allow_false: true }}", { 'drop' => BooleanDrop.new(true) })
  end

  def test_cannot_access_private_methods
    assert_template_result('a', "{{ 'a' | to_number }}")
  end

  def test_date_raises_nothing
    assert_template_result('', "{{ '' | date: '%D' }}")
    assert_template_result('abc', "{{ 'abc' | date: '%D' }}")
  end

  def test_reject
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => true },
    ]

    template = "{{ array | reject: 'ok' | map: 'handle' | join: ' ' }}"
    expected_output = "beta gamma"

    assert_template_result(expected_output, template, { "array" => array })
  end

  def test_reject_with_value
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => true },
    ]

    template = "{{ array | reject: 'ok', true | map: 'handle' | join: ' ' }}"
    expected_output = "beta gamma"

    assert_template_result(expected_output, template, { "array" => array })
  end

  def test_reject_with_false_value
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => true },
    ]

    template = "{{ array | reject: 'ok', false | map: 'handle' | join: ' ' }}"
    expected_output = "alpha delta"

    assert_template_result(expected_output, template, { "array" => array })
  end

  def test_has
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => false },
    ]

    expected_output = "true"

    assert_template_result(expected_output, "{{ array | has: 'ok' }}", { "array" => array })
    assert_template_result(expected_output, "{{ array | has: 'ok', true }}", { "array" => array })
  end

  def test_has_when_does_not_have_it
    array = [
      { "handle" => "alpha", "ok" => false },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => false },
    ]

    expected_output = "false"

    assert_template_result(expected_output, "{{ array | has: 'ok' }}", { "array" => array })
    assert_template_result(expected_output, "{{ array | has: 'ok', true }}", { "array" => array })
  end

  def test_has_with_empty_arrays
    template = <<~LIQUID
      {%- assign has_product = products | has: 'title.content', 'Not found' -%}
      {%- unless has_product -%}
        Product not found.
      {%- endunless -%}
    LIQUID
    expected_output = "Product not found."

    assert_template_result(expected_output, template, { "products" => [] })
  end

  def test_has_with_false_value
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => true },
    ]

    template = "{{ array | has: 'ok', false }}"
    expected_output = "true"

    assert_template_result(expected_output, template, { "array" => array })
  end

  def test_has_with_false_value_when_does_not_have_it
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => true },
      { "handle" => "gamma", "ok" => true },
      { "handle" => "delta", "ok" => true },
    ]

    template = "{{ array | has: 'ok', false }}"
    expected_output = "false"

    assert_template_result(expected_output, template, { "array" => array })
  end

  def test_find_with_value
    products = [
      { "title" => "Pro goggles",    "price" => 1299 },
      { "title" => "Thermal gloves", "price" => 1499 },
      { "title" => "Alpine jacket",  "price" => 3999 },
      { "title" => "Mountain boots", "price" => 3899 },
      { "title" => "Safety helmet",  "price" => 1999 }
    ]

    template = <<~LIQUID
      {%- assign product = products | find: 'price', 3999 -%}
      {{- product.title -}}
    LIQUID
    expected_output = "Alpine jacket"

    assert_template_result(expected_output, template, { "products" => products })
  end

  def test_find_with_empty_arrays
    template = <<~LIQUID
      {%- assign product = products | find: 'title.content', 'Not found' -%}
      {%- unless product -%}
        Product not found.
      {%- endunless -%}
    LIQUID
    expected_output = "Product not found."

    assert_template_result(expected_output, template, { "products" => [] })
  end

  def test_find_index_with_value
    products = [
      { "title" => "Pro goggles",    "price" => 1299 },
      { "title" => "Thermal gloves", "price" => 1499 },
      { "title" => "Alpine jacket",  "price" => 3999 },
      { "title" => "Mountain boots", "price" => 3899 },
      { "title" => "Safety helmet",  "price" => 1999 }
    ]

    template = <<~LIQUID
      {%- assign index = products | find_index: 'price', 3999 -%}
      {{- index -}}
    LIQUID
    expected_output = "2"

    assert_template_result(expected_output, template, { "products" => products })
  end

  def test_find_index_with_empty_arrays
    template = <<~LIQUID
      {%- assign index = products | find_index: 'title.content', 'Not found' -%}
      {%- unless index -%}
        Index not found.
      {%- endunless -%}
    LIQUID
    expected_output = "Index not found."

    assert_template_result(expected_output, template, { "products" => [] })
  end

  def test_where
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => true },
    ]

    template = "{{ array | where: 'ok' | map: 'handle' | join: ' ' }}"
    expected_output = "alpha delta"

    assert_template_result(expected_output, template, { "array" => array })
  end

  def test_where_with_empty_string_is_a_no_op
    environment = { "array" => ["alpha", "beta", "gamma"] }
    expected_output = "alpha beta gamma"
    template = "{{ array | where: '' | join: ' ' }}"

    assert_template_result(expected_output, template, environment)
  end

  def test_where_with_nil_is_a_no_op
    environment = { "array" => ["alpha", "beta", "gamma"] }
    template = "{{ array | where: nil | join: ' ' }}"

    assert_raises(Liquid::ArgumentError) do
      assert_template_result("alpha beta gamma", template, environment)
    end
  end

  def test_where_with_value
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => true },
    ]

    template = "{{ array | where: 'ok', true | map: 'handle' | join: ' ' }}"
    expected_output = "alpha delta"

    assert_template_result(expected_output, template, { "array" => array })
  end

  def test_where_with_false_value
    array = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta", "ok" => false },
      { "handle" => "gamma", "ok" => false },
      { "handle" => "delta", "ok" => true },
    ]

    template = "{{ array | where: 'ok', false | map: 'handle' | join: ' ' }}"
    expected_output = "beta gamma"

    assert_template_result(expected_output, template, { "array" => array })
  end

  def test_where_string_keys
    input = [
      "alpha", "beta", "gamma", "delta"
    ]

    expectation = [
      "beta",
    ]

    assert_equal(expectation, @filters.where(input, "be"))
  end

  def test_where_no_key_set
    input = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "beta" },
      { "handle" => "gamma" },
      { "handle" => "delta", "ok" => true },
    ]

    expectation = [
      { "handle" => "alpha", "ok" => true },
      { "handle" => "delta", "ok" => true },
    ]

    assert_equal(expectation, @filters.where(input, "ok", true))
    assert_equal(expectation, @filters.where(input, "ok"))
  end

  def test_where_non_array_map_input
    assert_equal([{ "a" => "ok" }], @filters.where({ "a" => "ok" }, "a", "ok"))
    assert_equal([], @filters.where({ "a" => "not ok" }, "a", "ok"))
  end

  def test_where_indexable_but_non_map_value
    assert_raises(Liquid::ArgumentError) { @filters.where(1, "ok", true) }
    assert_raises(Liquid::ArgumentError) { @filters.where(1, "ok") }
  end

  def test_where_non_boolean_value
    input = [
      { "message" => "Bonjour!", "language" => "French" },
      { "message" => "Hello!", "language" => "English" },
      { "message" => "Hallo!", "language" => "German" },
    ]

    assert_equal([{ "message" => "Bonjour!", "language" => "French" }], @filters.where(input, "language", "French"))
    assert_equal([{ "message" => "Hallo!", "language" => "German" }], @filters.where(input, "language", "German"))
    assert_equal([{ "message" => "Hello!", "language" => "English" }], @filters.where(input, "language", "English"))
  end

  def test_where_array_of_only_unindexable_values
    assert_nil(@filters.where([nil], "ok", true))
    assert_nil(@filters.where([nil], "ok"))
  end

  def test_all_filters_never_raise_non_liquid_exception
    test_drop = TestDrop.new(value: "test")
    test_drop.context = Context.new
    test_enum = TestEnumerable.new
    test_enum.context = Context.new
    test_types = [
      "foo",
      123,
      0,
      0.0,
      -1234.003030303,
      -99999999,
      1234.38383000383830003838300,
      nil,
      true,
      false,
      TestThing.new,
      test_drop,
      test_enum,
      ["foo", "bar"],
      { "foo" => "bar" },
      { foo: "bar" },
      [{ "foo" => "bar" }, { "foo" => 123 }, { "foo" => nil }, { "foo" => true }, { "foo" => ["foo", "bar"] }],
      { 1 => "bar" },
      ["foo", 123, nil, true, false, Drop, ["foo"], { foo: "bar" }],
    ]
    StandardFilters.public_instance_methods(false).each do |method|
      arg_count = @filters.method(method).arity
      arg_count *= -1 if arg_count < 0

      test_types.repeated_permutation(arg_count) do |args|
        @filters.send(method, *args)
      rescue Liquid::Error
        nil
      end
    end
  end

  def test_where_no_target_value
    input = [
      { "foo" => false },
      { "foo" => true },
      { "foo" => "for sure" },
      { "bar" => true },
    ]

    assert_equal([{ "foo" => true }, { "foo" => "for sure" }], @filters.where(input, "foo"))
  end

  def test_sum_with_all_numbers
    input = [1, 2]

    assert_equal(3, @filters.sum(input))
    assert_raises(Liquid::ArgumentError, "cannot select the property 'quantity'") do
      @filters.sum(input, "quantity")
    end
  end

  def test_sum_with_numeric_strings
    input = [1, 2, "3", "4"]

    assert_equal(10, @filters.sum(input))
    assert_raises(Liquid::ArgumentError, "cannot select the property 'quantity'") do
      @filters.sum(input, "quantity")
    end
  end

  def test_sum_with_nested_arrays
    input = [1, [2, [3, 4]]]

    assert_equal(10, @filters.sum(input))
    assert_raises(Liquid::ArgumentError, "cannot select the property 'quantity'") do
      @filters.sum(input, "quantity")
    end
  end

  def test_sum_with_indexable_map_values
    input = [{ "quantity" => 1 }, { "quantity" => 2, "weight" => 3 }, { "weight" => 4 }]

    assert_equal(0, @filters.sum(input))
    assert_equal(3, @filters.sum(input, "quantity"))
    assert_equal(7, @filters.sum(input, "weight"))
    assert_equal(0, @filters.sum(input, "subtotal"))
  end

  def test_sum_with_indexable_non_map_values
    input = [1, [2], "foo", { "quantity" => 3 }]

    assert_equal(3, @filters.sum(input))
    assert_raises(Liquid::ArgumentError, "cannot select the property 'quantity'") do
      @filters.sum(input, "quantity")
    end
  end

  def test_sum_with_unindexable_values
    input = [1, true, nil, { "quantity" => 2 }]

    assert_equal(1, @filters.sum(input))
    assert_raises(Liquid::ArgumentError, "cannot select the property 'quantity'") do
      @filters.sum(input, "quantity")
    end
  end

  def test_sum_without_property_calls_to_liquid
    t = TestThing.new
    Liquid::Template.parse('{{ foo | sum }}').render("foo" => [t])
    assert(t.foo > 0)
  end

  def test_sum_with_property_calls_to_liquid_on_property_values
    t = TestThing.new
    Liquid::Template.parse('{{ foo | sum: "quantity" }}').render("foo" => [{ "quantity" => t }])
    assert(t.foo > 0)
  end

  def test_sum_of_floats
    input = [0.1, 0.2, 0.3]
    assert_equal(0.6, @filters.sum(input))
    assert_template_result("0.6", "{{ input | sum }}", { "input" => input })
  end

  def test_sum_of_negative_floats
    input = [0.1, 0.2, -0.3]
    assert_equal(0.0, @filters.sum(input))
    assert_template_result("0.0", "{{ input | sum }}", { "input" => input })
  end

  def test_sum_with_float_strings
    input = [0.1, "0.2", "0.3"]
    assert_equal(0.6, @filters.sum(input))
    assert_template_result("0.6", "{{ input | sum }}", { "input" => input })
  end

  def test_sum_resulting_in_negative_float
    input = [0.1, -0.2, -0.3]
    assert_equal(-0.4, @filters.sum(input))
    assert_template_result("-0.4", "{{ input | sum }}", { "input" => input })
  end

  def test_sum_with_floats_and_indexable_map_values
    input = [{ "quantity" => 1 }, { "quantity" => 0.2, "weight" => -0.3 }, { "weight" => 0.4 }]
    assert_equal(0.0, @filters.sum(input))
    assert_equal(1.2, @filters.sum(input, "quantity"))
    assert_equal(0.1, @filters.sum(input, "weight"))
    assert_equal(0.0, @filters.sum(input, "subtotal"))
    assert_template_result("0", "{{ input | sum }}", { "input" => input })
    assert_template_result("1.2", "{{ input | sum: 'quantity' }}", { "input" => input })
    assert_template_result("0.1", "{{ input | sum: 'weight' }}", { "input" => input })
    assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
  end

  def test_sum_with_non_string_property
    input = [{ true => 1 }, { 1.0 => 0.2, 1 => -0.3 }, { 1..5 => 0.4 }]

    assert_equal(1, @filters.sum(input, true))
    assert_equal(0.2, @filters.sum(input, 1.0))
    assert_equal(-0.3, @filters.sum(input, 1))
    assert_equal(0.4, @filters.sum(input, (1..5)))
    assert_equal(0, @filters.sum(input, nil))
    assert_equal(0, @filters.sum(input, ""))
  end

  def test_uniq_with_to_liquid_value
    input = [StringDrop.new("foo"), StringDrop.new("bar"), "foo"]
    expected = [StringDrop.new("foo"), StringDrop.new("bar")]
    result = @filters.uniq(input)

    assert_equal(expected, result)
  end

  def test_uniq_with_to_liquid_value_pick_correct_classes
    input = ["foo", StringDrop.new("foo"), StringDrop.new("bar")]
    expected = [String, StringDrop]
    result = @filters.uniq(input).map(&:class)

    assert_equal(expected, result)
  end

  private

  def with_timezone(tz)
    old_tz    = ENV['TZ']
    ENV['TZ'] = tz
    yield
  ensure
    ENV['TZ'] = old_tz
  end
end # StandardFiltersTest