-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecursive_selective_match_test.exs
1121 lines (930 loc) · 35.6 KB
/
recursive_selective_match_test.exs
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
defmodule RecursiveSelectiveMatchTest do
use ExUnit.Case
import ExUnit.CaptureLog
import ExUnit.CaptureIO
doctest RecursiveSelectiveMatch
alias RecursiveSelectiveMatch, as: RSM
defp celtics_actual() do
%{
players: [
%Person{id: 1187, fname: "Robert", lname: "Parrish", position: :center, jersey_num: "00"},
%Person{id: 979, fname: "Kevin", lname: "McHale", position: :forward, jersey_num: "32"},
%Person{id: 1033, fname: "Larry", lname: "Bird", position: :forward, jersey_num: "33"}
],
team: %{
name: "Celtics",
nba_id: 13,
greatest_player: %Person{
id: 4,
fname: "Bill",
lname: "Russell",
position: :center,
jersey_num: "6",
born: ~D[1934-02-12]
},
plays_at: %{
arena: %{name: "Boston Garden", location: %{"city" => "Boston", "state" => "MA"}}
}
},
formatted_data_fetched_at: ~N[2018-04-17 11:14:53],
data_fetched_at: "2018-04-17 11:14:53"
}
end
defp celtics_expected() do
%{
players: :any_list,
team: %{
name: :any_binary,
nba_id: :any_integer,
greatest_player: :any_struct,
plays_at: %{
arena: %{name: :any_binary, location: %{"city" => :any_binary, "state" => :any_binary}}
}
},
formatted_data_fetched_at: :any_naive_datetime,
data_fetched_at: :any_binary
}
end
defp invalid_team() do
%{
team: %{name: "Lakers"}
}
end
test "Lakers are the wrong team" do
refute RSM.matches?(invalid_team(), celtics_actual(), %{suppress_warnings: true})
end
test "errors are logged" do
assert capture_log(fn ->
RSM.matches?(invalid_team(), celtics_actual())
end) =~ " does not match "
end
test "tuples that match print no warning" do
expected = {:a, :b, :c}
actual = {:a, :b, :c}
assert capture_log(fn ->
RSM.matches?(expected, actual)
end) == ""
end
test "tuples with elements that don't match print warnings by default" do
expected = {:a, :b, :c}
actual = {:a, :b, :d}
assert capture_log(fn -> RSM.matches?(expected, actual) end) =~
"[error] :d does not match :c"
assert capture_log(fn -> RSM.matches?(expected, actual) end) =~
"[error] {:a, :b, :d} does not match {:a, :b, :c}"
end
test "tuples with more actual elements than expected don't match" do
expected = {:a, :b, :c}
actual = {:a, :b, :c, :d}
assert capture_log(fn -> RSM.matches?(expected, actual) end) =~
"[error] Actual tuple is larger than expected tuple:\n{:a, :b, :c, :d} does not match {:a, :b, :c}"
end
test "tuples with fewer actual elements than expected don't match" do
expected = {:a, :b, :c, :f}
actual = {:a, :b, :c}
assert capture_log(fn -> RSM.matches?(expected, actual) end) =~
"[error] Expected tuple is larger than actual tuple:\n{:a, :b, :c} does not match {:a, :b, :c, :f}"
end
test "tuples that don't match print warnings via IO.inspect when io_errors: true" do
expected = {:a, :b, :c}
actual = {:a, :b, :d}
# TODO: The next line produces IO output I'd like to suppress without voiding the test
assert capture_log(fn -> RSM.matches?(expected, actual, %{io_errors: true}) end) == ""
assert capture_io(fn -> RSM.matches?(expected, actual, %{io_errors: true}) end) =~
":d does not match :c"
assert capture_io(fn -> RSM.matches?(expected, actual, %{io_errors: true}) end) =~
"{:a, :b, :d} does not match {:a, :b, :c}"
end
defp efgh_list() do
[["e", "f"], ["g", "h"]]
end
test "even with %{full_lists: true}, exact matches of lists of lists match" do
assert RSM.matches?([["e", "f"], ["g", "h"]], efgh_list(), %{full_lists: true})
end
test "by default, unexpected actual list elements are ignored" do
assert RSM.matches?([["e", "f"]], efgh_list())
end
# TODO: Make this work
@tag :skip
test "regexs can be used to match list elements" do
assert RSM.matches?([[~r/e/, ~r/f/]], efgh_list())
end
test "when %{full_lists: true}, unexpected actual list elements cause match failure" do
assert capture_log(fn ->
RSM.matches?([["e", "f"]], efgh_list(), %{full_lists: true})
end) =~ "[error] [[\"e\", \"f\"], [\"g\", \"h\"]] does not match [[\"e\", \"f\"]]"
end
test "when %{full_lists: true}, order of list elements is ignored" do
assert RSM.matches?([["g", "h"], ["e", "f"]], efgh_list(), %{full_lists: true})
end
test "exactly matching lists match when %{exact_lists: true}" do
assert RSM.matches?([["e", "f"], ["g", "h"]], efgh_list(), %{exact_lists: true})
end
test "presence of unexpected list items causes match failure if %{exact_lists: true}" do
assert capture_log(fn ->
RSM.matches?([["e", "f"]], efgh_list(), %{exact_lists: true})
end) =~
"[error] [[\"e\", \"f\"], [\"g\", \"h\"]] does not match [[\"e\", \"f\"]]"
end
test "order of list items matters when %{exact_lists: true}" do
assert capture_log(fn ->
RSM.matches?([["g", "h"], ["e", "f"]], efgh_list(), %{exact_lists: true})
end) =~
"[error] [[\"e\", \"f\"], [\"g\", \"h\"]] does not match [[\"g\", \"h\"], [\"e\", \"f\"]]"
end
# TODO: Make this work
@tag :skip
test "order matters when matching list elements if %{ordered_lists: true}" do
refute RSM.matches?([["f", "e"]], efgh_list(), %{ordered_lists: true})
end
test "suppress_warnings: true disables error logging" do
expected = {:a, :b, :c}
actual = {:a, :b, :d}
assert capture_log(fn -> RSM.matches?(expected, actual, %{suppress_warnings: true}) end) == ""
assert capture_io(fn -> RSM.matches?(expected, actual, %{suppress_warnings: true}) end) == ""
end
test "Celtics matches? test" do
assert RSM.matches?(celtics_expected(), celtics_actual())
end
test "by default, structs don't match maps" do
parrish = %Person{
id: 1187,
fname: "Robert",
lname: "Parrish",
position: :center,
jersey_num: "00"
}
refute RSM.includes?(parrish, parrish |> RSM.convert_struct_to_map())
end
test "Celtics includes? finds a particular struct when present" do
assert RSM.includes?(
celtics_actual()[:players] |> Enum.at(1),
celtics_actual()[:players]
)
end
test "Celtics includes? finds map with keys when present" do
assert RSM.includes?(
%{fname: "Larry", lname: "Bird"},
celtics_actual()[:players]
)
end
test "Celtics includes? doesn't find map with keys when not present" do
refute RSM.includes?(
%{fname: "Magic", lname: "Johnson"},
celtics_actual()[:players]
)
end
test "Celtics includes? finds :any_struct when struct is present" do
assert RSM.includes?(
:any_struct,
celtics_actual()[:players]
)
end
defp celtics_expectation_functions() do
%{
players: &is_list/1,
team: %{
name: &is_binary/1,
nba_id: &is_integer/1,
greatest_player: :any_struct,
plays_at: %{
arena: %{
name: &is_binary/1,
location: %{"city" => &is_binary/1, "state" => &is_binary/1}
}
}
},
data_fetched_at: &is_binary/1
}
end
test "Celtics test with expectation functions" do
assert RSM.matches?(celtics_expectation_functions(), celtics_actual())
end
defp celtics_expectation_functions_w_regex() do
%{
players: &(length(&1) == 3),
team: %{
name: &(&1 in ["Bucks", "Celtics", "76ers", "Lakers", "Rockets", "Warriors"]),
nba_id: &(&1 >= 1 && &1 <= 30),
greatest_player: %Person{
id: &(&1 >= 0 && &1 <= 99),
fname: &Regex.match?(~r/[A-Z][a-z]{2,}/, &1),
lname: &Regex.match?(~r/[A-Z][a-z]{2,}/, &1),
position: &(&1 in [:center, :guard, :forward]),
jersey_num: &Regex.match?(~r/\d{1,2}/, &1),
born: :any_date
},
plays_at: %{
arena: %{
name: &(String.length(&1) > 3),
location: %{"city" => &is_binary/1, "state" => &Regex.match?(~r/[A-Z]{2}/, &1)}
}
}
},
data_fetched_at: &Regex.match?(~r/2018-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, &1)
}
end
test "Celtics test with expectation functions with regexes" do
assert RSM.matches?(celtics_expectation_functions_w_regex(), celtics_actual())
end
test ":multi allows multiple test criteria" do
expected = %{
players:
{:multi,
[&(length(&1) == 3), &Enum.all?(&1, fn player -> player.lname |> byte_size() >= 4 end)]}
}
assert RSM.matches?(expected, celtics_actual())
end
test "matches a single element of a list" do
expected = %{
players: [
%Person{id: 1187, fname: "Robert", lname: "Parrish", position: :center, jersey_num: "00"}
]
}
assert RSM.matches?(expected, celtics_actual())
end
test "matches two out of order elements within a list" do
expected = %{
players: [
%Person{id: 1033, fname: "Larry", lname: "Bird", position: :forward, jersey_num: "33"},
%Person{id: 1187, fname: "Robert", lname: "Parrish", position: :center, jersey_num: "00"}
]
}
assert RSM.matches?(expected, celtics_actual())
end
test "matches the full set of list elements when out of order & some matchers are functions" do
expected = %{
players: [
%Person{
id: &is_integer/1,
fname: "Larry",
lname: "Bird",
position: :forward,
jersey_num: "33"
},
%Person{
id: 979,
fname: &is_binary/1,
lname: "McHale",
position: :forward,
jersey_num: "32"
},
%Person{
id: 1187,
fname: "Robert",
lname: &(String.length(&1) > 4),
position: :center,
jersey_num: "00"
}
]
}
assert RSM.matches?(expected, celtics_actual())
end
test "doesn't match if Parrish's jersey number expectation is wrong ('0' instead of '00')" do
expected = %{
players: [
%Person{
id: &is_integer/1,
fname: "Larry",
lname: "Bird",
position: :forward,
jersey_num: "33"
},
%Person{
id: 979,
fname: &is_binary/1,
lname: "McHale",
position: :forward,
jersey_num: "32"
},
%Person{
id: 1187,
fname: "Robert",
lname: &(String.length(&1) > 4),
position: :center,
jersey_num: "0"
}
]
}
refute RSM.matches?(expected, celtics_actual(), %{suppress_warnings: true})
end
test "single-level, key-valued maps" do
expected = %{best_beatle: %{fname: "John", lname: "Lennon"}}
actual = %{best_beatle: %{fname: "John", lname: "Lennon"}}
assert RSM.matches?(expected, actual)
end
test "multi-level, key-valued maps" do
expected = %{best_beatle: %{fname: "John", lname: "Lennon"}}
actual = %{best_beatle: %{fname: "John", mname: "Winston", lname: "Lennon", born: 1940}}
assert RSM.matches?(expected, actual)
end
test "map with an expected value of :anything and an actual value of a list" do
expected = %{team: "Red Sox", players: :anything}
actual = %{
team: "Red Sox",
players: [
"Mookie Betts",
"Xander Bogaerts",
"Hanley Ramirez",
"Jackie Bradley Jr",
"Chris Sale",
"Rick Porcello",
"David Price"
]
}
assert RSM.matches?(expected, actual)
end
test "map with an expected value of :any_list and an actual value of a list" do
expected = %{team: "Red Sox", players: :any_list}
actual = %{
team: "Red Sox",
players: [
"Mookie Betts",
"Xander Bogaerts",
"Hanley Ramirez",
"Jackie Bradley Jr",
"Chris Sale",
"Rick Porcello",
"David Price"
]
}
assert RSM.matches?(expected, actual)
end
test "map with an expected value of :any_map and an actual value of a list" do
expected = %{team: "Red Sox", players: :any_map}
actual = %{
team: "Red Sox",
players: [
"Mookie Betts",
"Xander Bogaerts",
"Hanley Ramirez",
"Jackie Bradley Jr",
"Chris Sale",
"Rick Porcello",
"David Price"
]
}
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test "map with an expected value of :any_map and an actual value of a map" do
expected = %{team: "Red Sox", players: :any_map}
actual = %{
team: "Red Sox",
players: %{
right_field: "Mookie Betts",
third_base: "Xander Bogaerts",
dh: "Hanley Ramirez",
center_field: "Jackie Bradley Jr",
p1: "Chris Sale",
p3: "Rick Porcello",
p2: "David Price"
}
}
assert RSM.matches?(expected, actual)
end
test ":any_datetime matches a DateTime{}" do
assert RSM.matches?(:any_datetime, DateTime.utc_now())
end
test ":any_datetime doesn't match a UTCDateTime{}" do
refute RSM.matches?(:any_datetime, UTCDateTime.utc_now())
end
test ":any_datetime doesn't match a NaiveDateTime{}" do
refute RSM.matches?(:any_datetime, NaiveDateTime.utc_now())
end
test ":any_utc_datetime doesn't match a DateTime{}" do
refute RSM.matches?(:any_utc_datetime, DateTime.utc_now())
end
test ":any_utc_datetime matches a UTCDateTime{}" do
assert RSM.matches?(:any_utc_datetime, UTCDateTime.utc_now())
end
test ":any_utc_datetime doesn't match a NaiveDateTime{}" do
refute RSM.matches?(:any_utc_datetime, NaiveDateTime.utc_now())
end
test ":any_naive_datetime doesn't match a DateTime{}" do
refute RSM.matches?(:any_naive_datetime, DateTime.utc_now())
end
test ":any_naive_datetime matches a NaiveDateTime{}" do
assert RSM.matches?(:any_naive_datetime, NaiveDateTime.utc_now())
end
test ":any_naive_datetime doesn't match a UTCDateTime{}" do
refute RSM.matches?(:any_naive_datetime, UTCDateTime.utc_now())
end
test ":any_iso8601_datetime with :data_fetched_at" do
assert RSM.matches?(:any_iso8601_datetime, celtics_actual()[:data_fetched_at])
end
test ":any_iso8601_date" do
assert RSM.matches?(:any_iso8601_date, "2018-12-25")
end
test ":any_iso8601_date when invalid month" do
refute RSM.matches?(:any_iso8601_date, "2018-13-25")
end
test ":any_iso8601_date when invalid day" do
refute RSM.matches?(:any_iso8601_date, "2018-09-33")
end
test ":any_iso8601_time" do
assert RSM.matches?(:any_iso8601_time, "11:54:09")
end
test ":any_iso8601_time when invalid second" do
refute RSM.matches?(:any_iso8601_time, "11:54:69")
end
test ":any_iso8601_datetime with 'T' between date and time" do
assert RSM.matches?(:any_iso8601_datetime, "2018-12-25T13:51:11")
end
test ":any_iso8601_datetime with valid date/time in distant past" do
assert RSM.matches?(:any_iso8601_datetime, "1009-02-13 13:51:11")
end
test ":any_iso8601_datetime with valid date/time in far future" do
assert RSM.matches?(:any_iso8601_datetime, "9999-02-13 13:51:11")
end
test ":any_iso8601_datetime with invalid minute 61" do
refute RSM.matches?(:any_iso8601_datetime, "2017-02-13 13:61:11")
end
test ":any_iso8601_datetime with invalid second 60" do
refute RSM.matches?(:any_iso8601_datetime, "2017-02-13 13:11:60")
end
test ":any_iso8601_datetime with invalid hour 24" do
refute RSM.matches?(:any_iso8601_datetime, "2017-02-13T24:11:11")
end
test ":any_iso8601_datetime with invalid hour 35" do
refute RSM.matches?(:any_iso8601_datetime, "2017-02-13 35:11:11")
end
test "map with an expected value of :any_integer and an actual value of an integer" do
expected = %{team: "Red Sox", current_standing: :any_integer}
actual = %{team: "Red Sox", current_standing: 1}
assert RSM.matches?(expected, actual)
end
test "map with an expected value of :any_integer and an actual value of a non-integer" do
expected = %{team: "Red Sox", current_standing: :any_integer}
actual = %{team: "Red Sox", current_standing: "1"}
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] Key :current_standing is expected to have a value of :any_integer (according to %{current_standing: :any_integer, team: \"Red Sox\"}) but has a value of \"1\" (in %{current_standing: \"1\", team: \"Red Sox\"})"
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] %{current_standing: \"1\", team: \"Red Sox\"} does not match %{current_standing: :any_integer, team: \"Red Sox\"}"
end
test "map with an expected value of :any_binary and an actual value of a binary" do
expected = %{team: "Red Sox", current_standing: :any_binary}
actual = %{team: "Red Sox", current_standing: "1"}
assert RSM.matches?(expected, actual)
end
test "map with an expected value of :any_binary and an actual value of a non-binary" do
expected = %{team: "Red Sox", current_standing: :any_binary}
actual = %{team: "Red Sox", current_standing: 1}
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] %{current_standing: 1, team: \"Red Sox\"} does not match %{current_standing: :any_binary, team: \"Red Sox\"}"
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] Key :current_standing is expected to have a value of :any_binary (according to %{current_standing: :any_binary, team: \"Red Sox\"}) but has a value of 1 (in %{current_standing: 1, team: \"Red Sox\"})"
end
test "struct with an expected value of :any_binary and an actual value of a binary treated like an equivalent map" do
expected = %TestStruct{fname: "Larry", lname: :any_binary, hof: :any_boolean}
actual = %TestStruct{fname: "Larry", lname: "Bird", hof: true}
assert RSM.matches?(expected, actual)
end
test "structs of different types don't match" do
expected = %TestStruct{fname: "Larry", lname: "Bird", hof: true}
actual = %AnotherTestStruct{fname: "Larry", lname: "Bird", hof: true}
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] AnotherTestStruct does not match TestStruct"
end
test "map with an expected value of :any_atom and an actual value of an atom" do
expected = %{team: "Red Sox", current_standing: :any_atom}
actual = %{team: "Red Sox", current_standing: :first}
assert RSM.matches?(expected, actual)
end
test "map with an expected value of :any_atom and an actual value of a non-atom" do
expected = %{team: "Red Sox", current_standing: :any_atom}
actual = %{team: "Red Sox", current_standing: 1}
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] Key :current_standing is expected to have a value of :any_atom (according to %{current_standing: :any_atom, team: \"Red Sox\"}) but has a value of 1 (in %{current_standing: 1, team: \"Red Sox\"})"
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] %{current_standing: 1, team: \"Red Sox\"} does not match %{current_standing: :any_atom, team: \"Red Sox\"}"
end
test "basic logging" do
expected = %{team: "Red Sox", players: ["Mookie Betts", "Xander Bogaerts"]}
actual = %{team: "Red Sox", players: ["Mookie Betts"]}
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] Key :players is expected to have a value of [\"Mookie Betts\", \"Xander Bogaerts\"] (according to %{players: [\"Mookie Betts\", \"Xander Bogaerts\"], team: \"Red Sox\"}) but has a value of [\"Mookie Betts\"] (in %{players: [\"Mookie Betts\"], team: \"Red Sox\"})"
end
test "map with an expected value of :any_tuple and an actual value of a list" do
expected = %{team: "Red Sox", players: :any_tuple}
actual = %{
team: "Red Sox",
players: [
"Mookie Betts",
"Xander Bogaerts",
"Hanley Ramirez",
"Jackie Bradley Jr",
"Chris Sale",
"Rick Porcello",
"David Price"
]
}
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
~s/[error] Key :players is expected to have a value of :any_tuple (according to %{players: :any_tuple, team: "Red Sox"}) but has a value of ["Mookie Betts", "Xander Bogaerts", "Hanley Ramirez", "Jackie Bradley Jr", "Chris Sale", "Rick Porcello", "David Price"] (in %{players: ["Mookie Betts", "Xander Bogaerts", "Hanley Ramirez", "Jackie Bradley Jr", "Chris Sale", "Rick Porcello", "David Price"], team: "Red Sox"})/
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] %{players: [\"Mookie Betts\", \"Xander Bogaerts\", \"Hanley Ramirez\", \"Jackie Bradley Jr\", \"Chris Sale\", \"Rick Porcello\", \"David Price\"], team: \"Red Sox\"} does not match %{players: :any_tuple, team: \"Red Sox\"}"
end
test "map with an expected value of :any_tuple and an actual value of a tuple" do
expected = %{team: "Red Sox", players: :any_tuple}
actual = %{
team: "Red Sox",
players:
{"Mookie Betts", "Xander Bogaerts", "Hanley Ramirez", "Jackie Bradley Jr", "Chris Sale",
"Rick Porcello", "David Price"}
}
assert RSM.matches?(expected, actual, %{})
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) == ""
end
test "single-level, key-valued maps don't ignore differences between string & atom keys" do
expected = %{best_beatle: %{fname: "John", lname: "Lennon"}}
actual = %{"best_beatle" => %{fname: "John", lname: "Lennon"}}
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] Key :best_beatle not present in %{\"best_beatle\" => %{fname: \"John\", lname: \"Lennon\"}} but present in %{best_beatle: %{fname: \"John\", lname: \"Lennon\"}}"
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] %{\"best_beatle\" => %{fname: \"John\", lname: \"Lennon\"}} does not match %{best_beatle: %{fname: \"John\", lname: \"Lennon\"}}"
end
test "multi-level, key-valued maps don't ignore differences between string & atom keys" do
expected = %{best_beatle: %{fname: "John", lname: "Lennon"}}
actual = %{
best_beatle: %{"fname" => "John", "mname" => "Winston", "lname" => "Lennon", "born" => 1940}
}
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] Key :best_beatle is expected to have a value of %{fname: \"John\", lname: \"Lennon\"} (according to %{best_beatle: %{fname: \"John\", lname: \"Lennon\"}}) but has a value of %{\"born\" => 1940, \"fname\" => \"John\", \"lname\" => \"Lennon\", \"mname\" => \"Winston\"} (in %{best_beatle: %{\"born\" => 1940, \"fname\" => \"John\", \"lname\" => \"Lennon\", \"mname\" => \"Winston\"}})"
assert capture_log(fn -> RSM.matches?(expected, actual, %{}) end) =~
"[error] %{best_beatle: %{\"born\" => 1940, \"fname\" => \"John\", \"lname\" => \"Lennon\", \"mname\" => \"Winston\"}} does not match %{best_beatle: %{fname: \"John\", lname: \"Lennon\"}}"
end
test "single-level, key-valued maps ignore differences between string & atom keys when standardize_keys: true" do
expected = %{best_beatle: %{fname: "John", lname: "Lennon"}}
actual = %{"best_beatle" => %{fname: "John", lname: "Lennon"}}
assert RSM.matches?(expected, actual, %{standardize_keys: true})
end
test "multi-level, key-valued maps ignore differences between string & atom keys when standardize_keys: true" do
expected = %{best_beatle: %{"fname" => "John", "lname" => "Lennon"}}
actual = %{"best_beatle" => %{fname: "John", mname: "Winston", lname: "Lennon", born: 1940}}
assert RSM.matches?(expected, actual, %{standardize_keys: true})
end
test "single-level list when identical" do
expected = ["apple", "banana", "cherry"]
actual = ["apple", "banana", "cherry"]
assert RSM.matches?(expected, actual)
end
test "single-level list when actual has extra values" do
expected = ["apple", "banana", "cherry"]
actual = ["apple", "banana", "cherry", "strawberry"]
assert RSM.matches?(expected, actual)
end
test "single-level list when expected has extra values" do
expected = ["apple", "banana", "cherry", "strawberry"]
actual = ["apple", "banana", "cherry"]
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test "multi-level lists when expected has extra string value" do
expected = ["apple", "banana", ["cherry", "grape"], "strawberry"]
actual = ["apple", "banana", ["cherry", "grape"]]
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test "multi-level lists when expected has extra string and list values" do
expected = ["apple", "banana", ["cherry", "grape"], "strawberry", ["peach", "apricot"]]
actual = ["apple", "banana", ["cherry", "grape"]]
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test "multi-level lists when actual has extra string value" do
expected = ["apple", "banana", ["cherry", "grape"]]
actual = ["apple", "banana", ["cherry", "grape"], "strawberry"]
assert RSM.matches?(expected, actual)
end
test "multi-level lists when actual has extra string and list values" do
expected = ["apple", "banana", ["cherry", "grape"]]
actual = ["apple", "banana", ["cherry", "grape"], "strawberry", ["peach", "apricot"]]
assert RSM.matches?(expected, actual)
end
test "multi-level, mixed map & list data structures" do
expected = %{
best_beatle: %{fname: "John", lname: "Lennon", cities: ["Liverpool", "New York"]}
}
actual = %{
best_beatle: %{
fname: "John",
mname: "Winston",
lname: "Lennon",
born: 1940,
cities: ["Liverpool", "New York"]
}
}
assert RSM.matches?(expected, actual)
end
test "expected values of :anything are ignored" do
expected = %{best_beatle: %{fname: "John", mname: :anything, lname: "Lennon"}}
actual = %{best_beatle: %{fname: "John", mname: "Winston", lname: "Lennon", born: 1940}}
assert RSM.matches?(expected, actual)
end
test "expected values of :anything must exist in actual" do
expected = %{best_beatle: %{fname: "John", mname: :anything, lname: "Lennon"}}
actual = %{best_beatle: %{fname: "John", lname: "Lennon", born: 1940}}
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test "single-level tuple" do
expected = {1, "banana"}
actual = {1, "banana"}
assert RSM.matches?(expected, actual)
end
test "single-level tuple when not matching" do
expected = {1, "banana"}
actual = {2, "banana"}
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test "multi-level tuple" do
expected = {1, {"banana", "bananas"}}
actual = {1, {"banana", "bananas"}}
assert RSM.matches?(expected, actual)
end
test "multi-level tuple when not matching" do
expected = {1, {"banana", "yellow"}}
actual = {1, {"banana", "green"}}
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test "single-level tuple with :anything" do
expected = {1, :anything}
actual = {1, "banana"}
assert RSM.matches?(expected, actual)
end
test ":any_time matches Time values" do
expected = :any_time
actual = ~T[11:13:12.032]
assert RSM.matches?(expected, actual)
end
test ":any_time doesn't match Date values" do
expected = :any_time
actual = ~D[2018-11-09]
refute RSM.matches?(expected, actual)
end
test ":any_date matches Date values" do
expected = :any_date
actual = ~D[2018-11-09]
assert RSM.matches?(expected, actual)
end
test ":any_date doesn't match Time values" do
expected = :any_date
actual = ~T[11:13:12.032]
refute RSM.matches?(expected, actual)
end
test ":any_pos_integer matches 3" do
expected = :any_pos_integer
actual = 3
assert RSM.matches?(expected, actual)
end
test ":any_pos_integer doesn't match 3.5" do
expected = :any_pos_integer
actual = 3.5
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_pos_integer doesn't match 0" do
expected = :any_pos_integer
actual = 0
refute RSM.matches?(expected, actual)
end
test ":any_pos_integer doesn't match -4" do
expected = :any_pos_integer
actual = -4
refute RSM.matches?(expected, actual)
end
test ":any_non_neg_integer matches 3" do
expected = :any_non_neg_integer
actual = 3
assert RSM.matches?(expected, actual)
end
test ":any_non_neg_integer doesn't match 3.5" do
expected = :any_non_neg_integer
actual = 3.5
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_non_neg_integer matches 0" do
expected = :any_non_neg_integer
actual = 0
assert RSM.matches?(expected, actual)
end
test ":any_non_neg_integer doesn't match -4" do
expected = :any_non_neg_integer
actual = -4
refute RSM.matches?(expected, actual)
end
test ":any_float doesn't match 3" do
expected = :any_float
actual = 3
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_float matches 3.5" do
expected = :any_float
actual = 3.5
assert RSM.matches?(expected, actual)
end
test ":any_float doesn't match 0" do
expected = :any_float
actual = 0
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_float matches 0.0" do
expected = :any_float
actual = 0.0
assert RSM.matches?(expected, actual)
end
test ":any_float matches -3.5" do
expected = :any_float
actual = -3.5
assert RSM.matches?(expected, actual)
end
test ":any_float doesn't match -4" do
expected = :any_float
actual = -4
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_pos_float doesn't match 3" do
expected = :any_pos_float
actual = 3
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_pos_float matches 3.5" do
expected = :any_pos_float
actual = 3.5
assert RSM.matches?(expected, actual)
end
test ":any_pos_float doesn't match 0" do
expected = :any_pos_float
actual = 0
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_pos_float doesn't match 0.0" do
expected = :any_pos_float
actual = 0.0
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_pos_float doesn't match -3.5" do
expected = :any_pos_float
actual = -3.5
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_pos_float doesn't match -4" do
expected = :any_pos_float
actual = -4
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_non_neg_float doesn't match 3" do
expected = :any_non_neg_float
actual = 3
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_non_neg_float matches 3.5" do
expected = :any_non_neg_float
actual = 3.5
assert RSM.matches?(expected, actual)
end
test ":any_non_neg_float doesn't match 0" do
expected = :any_non_neg_float
actual = 0
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_non_neg_float matches 0.0" do
expected = :any_non_neg_float
actual = 0.0
assert RSM.matches?(expected, actual)
end
test ":any_non_neg_float doesn't match -3.5" do
expected = :any_non_neg_float
actual = -3.5
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_non_neg_float doesn't match -4" do
expected = :any_non_neg_float
actual = -4
refute RSM.matches?(expected, actual, %{suppress_warnings: true})
end
test ":any_number matches 3" do
expected = :any_number
actual = 3
assert RSM.matches?(expected, actual)
end
test ":any_number matches 3.5" do
expected = :any_number
actual = 3.5
assert RSM.matches?(expected, actual)
end
test ":any_number matches 0" do
expected = :any_number
actual = 0
assert RSM.matches?(expected, actual)
end
test ":any_number matches 0.0" do
expected = :any_number
actual = 0.0
assert RSM.matches?(expected, actual)
end
test ":any_number matches -3.5" do
expected = :any_number
actual = -3.5
assert RSM.matches?(expected, actual)
end
test ":any_number matches -4" do
expected = :any_number
actual = -4
assert RSM.matches?(expected, actual)
end