-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_test.go
More file actions
1294 lines (962 loc) · 37 KB
/
Copy pathhtml_test.go
File metadata and controls
1294 lines (962 loc) · 37 KB
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
package testastic_test
import (
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"github.com/monkescience/testastic"
)
type htmlStringer string
func (s htmlStringer) String() string {
return string(s)
}
type architectureHTMLExactMatcher struct {
want string
calls *atomic.Int32
}
func (m architectureHTMLExactMatcher) Match(actual any) bool {
m.calls.Add(1)
value, ok := actual.(string)
return ok && value == m.want
}
func (m architectureHTMLExactMatcher) String() string {
return "{{architectureHTMLExact " + m.want + "}}"
}
func TestAssertHTML(t *testing.T) {
t.Parallel()
t.Run("exact match", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with exact content
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/exact_match.html",
`<div class="card"><span>Hello</span></div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure, got: %s", mt.message)
}
})
t.Run("exact match full document", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with a full HTML document
mt := &mockT{}
actual := `<!DOCTYPE html><html><head><title>Test</title></head>` +
`<body><p>Hello</p></body></html>`
// when: asserting with matching full document
testastic.AssertHTML(mt, "testdata/html/full_document.html", actual)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure, got: %s", mt.message)
}
})
t.Run("with anyString matcher", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with anyString matcher in text content
mt := &mockT{}
// when: asserting with any string in the span
testastic.AssertHTML(mt, "testdata/html/with_anystring.html",
`<div class="card"><span>Hello World</span></div>`)
// then: the test passes (matcher accepts any string)
if mt.failed {
t.Errorf("expected no failure with anyString matcher, got: %s", mt.message)
}
})
t.Run("with regex matcher", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with regex matcher
mt := &mockT{}
// when: asserting with a value matching the regex
testastic.AssertHTML(mt, "testdata/html/with_regex.html",
`<div><span>user-123</span></div>`)
// then: the test passes (regex matches)
if mt.failed {
t.Errorf("expected no failure with regex matcher, got: %s", mt.message)
}
})
t.Run("with regex matcher fails", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with regex matcher
mt := &mockT{}
// when: asserting with a value not matching the regex
testastic.AssertHTML(mt, "testdata/html/with_regex_fails.html",
`<div><span>invalid-format</span></div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure with non-matching regex")
}
})
t.Run("with ignore matcher", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with ignore matcher
mt := &mockT{}
actual := `<div><span class="timestamp">2024-01-01 12:00:00</span>` +
`<span>Content</span></div>`
// when: asserting with any value in the ignored span
testastic.AssertHTML(mt, "testdata/html/with_ignore.html", actual)
// then: the test passes (ignored content is not compared)
if mt.failed {
t.Errorf("expected no failure with ignore matcher, got: %s", mt.message)
}
})
t.Run("matcher in attribute", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with matcher in an attribute
mt := &mockT{}
// when: asserting with any string in the attribute
testastic.AssertHTML(mt, "testdata/html/matcher_in_attribute.html",
`<div data-id="abc-123"><span>Content</span></div>`)
// then: the test passes (matcher accepts any string)
if mt.failed {
t.Errorf("expected no failure with matcher in attribute, got: %s", mt.message)
}
})
t.Run("missing element", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with two span elements
mt := &mockT{}
// when: asserting with HTML missing the second span
testastic.AssertHTML(mt, "testdata/html/missing_element.html",
`<div><span>First</span></div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure for missing element")
}
})
t.Run("extra element", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with one span element
mt := &mockT{}
// when: asserting with HTML containing an extra span
testastic.AssertHTML(mt, "testdata/html/extra_element.html",
`<div><span>First</span><span>Second</span></div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure for extra element")
}
})
t.Run("wrong tag", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with a span element
mt := &mockT{}
// when: asserting with HTML using a different tag
testastic.AssertHTML(mt, "testdata/html/wrong_tag.html",
`<div><p>Content</p></div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure for wrong tag")
}
})
t.Run("wrong attribute", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with a specific class attribute
mt := &mockT{}
// when: asserting with HTML using a different class value
testastic.AssertHTML(mt, "testdata/html/wrong_attribute.html",
`<div class="box"><span>Content</span></div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure for wrong attribute value")
}
})
t.Run("missing attribute", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with class and id attributes
mt := &mockT{}
// when: asserting with HTML missing the id attribute
testastic.AssertHTML(mt, "testdata/html/missing_attribute.html",
`<div class="card"><span>Content</span></div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure for missing attribute")
}
})
t.Run("extra attribute", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with only class attribute
mt := &mockT{}
// when: asserting with HTML containing an extra id attribute
testastic.AssertHTML(mt, "testdata/html/extra_attribute.html",
`<div class="card" id="extra"><span>Content</span></div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure for extra attribute")
}
})
t.Run("whitespace normalization", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with normalized whitespace
mt := &mockT{}
// when: asserting with HTML containing extra whitespace
testastic.AssertHTML(mt, "testdata/html/whitespace.html",
`<div><span>Hello World</span></div>`)
// then: the test passes (whitespace is normalized by default)
if mt.failed {
t.Errorf("expected whitespace to be normalized, got: %s", mt.message)
}
})
t.Run("preserve whitespace", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with specific whitespace
mt := &mockT{}
actual := `<div><span>Hello World</span></div>`
// when: asserting with PreserveWhitespace option
testastic.AssertHTML(mt, "testdata/html/whitespace_preserve.html", actual,
testastic.PreserveWhitespace())
// then: the test fails (whitespace differences are detected)
if !mt.failed {
t.Error("expected failure with PreserveWhitespace option")
}
})
t.Run("preserve whitespace-only text nodes", func(t *testing.T) {
t.Parallel()
// given: expected inline elements separated by a whitespace-only text node
expectedFile := writeHTMLExpected(t, `<div><span>a</span> <span>b</span></div>`)
mt := &mockT{}
// when: actual HTML omits the separating whitespace
testastic.AssertHTML(mt, expectedFile, `<div><span>a</span><span>b</span></div>`,
testastic.PreserveWhitespace())
// then: preserving whitespace reports the missing text node
if !mt.failed {
t.Error("expected a missing inline space to fail when whitespace is preserved")
}
})
t.Run("ignore comments", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with a comment
mt := &mockT{}
actual := `<div><!-- different comment --><span>Content</span></div>`
// when: asserting with IgnoreHTMLComments option
testastic.AssertHTML(mt, "testdata/html/with_comment.html", actual,
testastic.IgnoreHTMLComments())
// then: the test passes (comments are ignored)
if mt.failed {
t.Errorf("expected comments to be ignored, got: %s", mt.message)
}
})
t.Run("failure diff omits ignored comments", func(t *testing.T) {
t.Parallel()
// given: differing comments alongside a real paragraph difference
expectedFile := writeHTMLExpected(t,
`<div><!-- ignored-expected-comment --><p>old</p></div>`,
)
mt := &mockT{}
// when: comparing with comments ignored
testastic.AssertHTML(mt, expectedFile,
`<div><!-- ignored-actual-comment --><p>new</p></div>`,
testastic.IgnoreHTMLComments(),
)
// then: the diff contains only the real paragraph difference
if !mt.failed {
t.Fatal("expected the paragraph difference to fail")
}
if strings.Contains(mt.message, "ignored-expected-comment") ||
strings.Contains(mt.message, "ignored-actual-comment") {
t.Errorf("expected ignored comments to be absent from the diff, got: %s", mt.message)
}
if !strings.Contains(mt.message, "<p>old</p>") || !strings.Contains(mt.message, "<p>new</p>") {
t.Errorf("expected the paragraph difference to remain in the diff, got: %s", mt.message)
}
})
t.Run("failure diff omits removed formatted comment", func(t *testing.T) {
t.Parallel()
// given: indented expected HTML with a comment absent from actual HTML
expectedFile := writeHTMLExpected(t,
"<div>\n <!-- ignored-expected-comment -->\n <p>old</p>\n</div>",
)
// when: another semantic difference makes the assertion fail
mt := &mockT{}
testastic.AssertHTML(mt, expectedFile, "<div>\n <p>new</p>\n</div>",
testastic.IgnoreHTMLComments(),
)
// then: neither the comment nor its split indentation appears in the diff
if !mt.failed {
t.Fatal("expected the paragraph difference to fail")
}
if strings.Contains(mt.message, "ignored-expected-comment") || strings.Contains(mt.message, "\n- \n") {
t.Errorf("expected the removed comment to be absent from the diff, got: %s", mt.message)
}
if !strings.Contains(mt.message, "<p>old</p>") || !strings.Contains(mt.message, "<p>new</p>") {
t.Errorf("expected the paragraph difference to remain in the diff, got: %s", mt.message)
}
})
t.Run("ignore elements", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with a script element
mt := &mockT{}
actual := `<div><script>console.log('different')</script><span>Content</span></div>`
// when: asserting with IgnoreElements option for script
testastic.AssertHTML(mt, "testdata/html/with_script.html", actual,
testastic.IgnoreElements("script"))
// then: the test passes (script element is ignored)
if mt.failed {
t.Errorf("expected script element to be ignored, got: %s", mt.message)
}
})
t.Run("ignore attributes", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with class and data-testid attributes
mt := &mockT{}
actual := `<div class="box" data-testid="different"><span>Content</span></div>`
// when: asserting with IgnoreAttributes option
testastic.AssertHTML(mt, "testdata/html/ignore_attributes.html", actual,
testastic.IgnoreAttributes("class", "data-testid"))
// then: the test passes (specified attributes are ignored)
if mt.failed {
t.Errorf("expected attributes to be ignored, got: %s", mt.message)
}
})
t.Run("ignore child order", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with ordered children
expectedFile := filepath.Join(t.TempDir(), "ignore-child-order.html")
err := os.WriteFile(expectedFile, []byte("<ul><li>first</li><li>second</li></ul>"), 0o644)
if err != nil {
t.Fatalf("write expected file: %v", err)
}
mt := &mockT{}
actual := `<ul><li>second</li><li>first</li></ul>`
// when: asserting with IgnoreChildOrder
testastic.AssertHTML(mt, expectedFile, actual, testastic.IgnoreChildOrder())
// then: the children are compared without order sensitivity
if mt.failed {
t.Errorf("expected child order to be ignored, got: %s", mt.message)
}
})
t.Run("ignore child order at", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with one ordered and one unordered list
expectedFile := filepath.Join(t.TempDir(), "ignore-child-order-at.html")
err := os.WriteFile(expectedFile, []byte(
"<div><ol><li>one</li><li>two</li></ol><ul><li>a</li><li>b</li></ul></div>",
), 0o644)
if err != nil {
t.Fatalf("write expected file: %v", err)
}
mt := &mockT{}
actual := `<div><ol><li>one</li><li>two</li></ol><ul><li>b</li><li>a</li></ul></div>`
// when: asserting with IgnoreChildOrderAt for the ul path
testastic.AssertHTML(mt, expectedFile, actual, testastic.IgnoreChildOrderAt("html > body > div > ul"))
// then: only the targeted subtree ignores child order
if mt.failed {
t.Errorf("expected child order to be ignored at the targeted path, got: %s", mt.message)
}
})
t.Run("ignore attribute at", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with an attribute that changes at one path
expectedFile := filepath.Join(t.TempDir(), "ignore-attribute-at.html")
err := os.WriteFile(expectedFile, []byte(`<div data-id="expected"><span>Content</span></div>`), 0o644)
if err != nil {
t.Fatalf("write expected file: %v", err)
}
mt := &mockT{}
actual := `<div data-id="actual"><span>Content</span></div>`
// when: asserting with IgnoreAttributeAt for the data-id attribute
testastic.AssertHTML(mt, expectedFile, actual, testastic.IgnoreAttributeAt("html > body > div@data-id"))
// then: the targeted attribute is ignored
if mt.failed {
t.Errorf("expected attribute to be ignored at the targeted path, got: %s", mt.message)
}
})
t.Run("ignore fields by path", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with a subtree that should be ignored
expectedFile := filepath.Join(t.TempDir(), "ignore-fields-path.html")
err := os.WriteFile(expectedFile, []byte(`<div><span>expected</span><p>stable</p></div>`), 0o644)
if err != nil {
t.Fatalf("write expected file: %v", err)
}
mt := &mockT{}
actual := `<div><span>actual</span><p>stable</p></div>`
// when: asserting with IgnoreFields for the span path
testastic.AssertHTML(mt, expectedFile, actual, testastic.IgnoreFields("html > body > div > span"))
// then: the targeted subtree is excluded from comparison
if mt.failed {
t.Errorf("expected ignored HTML field path to be skipped, got: %s", mt.message)
}
})
t.Run("create expected file", func(t *testing.T) {
t.Parallel()
// given: a non-existent expected file path
dir := t.TempDir()
expectedFile := filepath.Join(dir, "new-expected.html")
mt := &mockT{}
actual := `<div class="card"><span>Content</span></div>`
// when: asserting with HTMLUpdate option
testastic.AssertHTML(mt, expectedFile, actual, testastic.Update())
// then: the test passes and the file is created
if mt.failed {
t.Errorf("expected no failure when creating file, got: %s", mt.message)
}
content, err := os.ReadFile(expectedFile)
if err != nil {
t.Fatalf("expected file was not created: %v", err)
}
if !strings.Contains(string(content), "card") {
t.Errorf("expected file content incorrect: %s", content)
}
})
t.Run("create expected file in nested directory", func(t *testing.T) {
t.Parallel()
// given: a non-existent expected file path in nested directories
dir := t.TempDir()
expectedFile := filepath.Join(dir, "nested", "path", "new-expected.html")
mt := &mockT{}
actual := `<div class="card"><span>Content</span></div>`
// when: asserting with update option
testastic.AssertHTML(mt, expectedFile, actual, testastic.Update())
// then: the test passes and the nested file is created
if mt.failed {
t.Errorf("expected no failure when creating nested file, got: %s", mt.message)
}
content, err := os.ReadFile(expectedFile)
if err != nil {
t.Fatalf("expected nested file was not created: %v", err)
}
if !strings.Contains(string(content), "card") {
t.Errorf("expected file content incorrect: %s", content)
}
})
t.Run("byte slice input", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file and actual as []byte
mt := &mockT{}
// when: asserting with []byte input
testastic.AssertHTML(mt, "testdata/html/bytes.html",
[]byte(`<div><span>Hello</span></div>`))
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with []byte input, got: %s", mt.message)
}
})
t.Run("reader input", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file and actual as io.Reader
mt := &mockT{}
// when: asserting with io.Reader input
testastic.AssertHTML(mt, "testdata/html/bytes_reader.html",
strings.NewReader(`<article><span>Hello Reader</span></article>`))
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with io.Reader input, got: %s", mt.message)
}
})
t.Run("stringer input", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file and actual as fmt.Stringer
mt := &mockT{}
actual := htmlStringer(`<div><span>Hello</span></div>`)
// when: asserting with fmt.Stringer input
testastic.AssertHTML(mt, "testdata/html/bytes.html", actual)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with fmt.Stringer input, got: %s", mt.message)
}
})
t.Run("nested elements", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with nested elements
mt := &mockT{}
// when: asserting with matching nested structure
testastic.AssertHTML(mt, "testdata/html/nested.html",
`<div><ul><li>Item 1</li><li>Item 2</li></ul></div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with nested elements, got: %s", mt.message)
}
})
t.Run("void elements", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with void elements
mt := &mockT{}
// when: asserting with matching void elements
testastic.AssertHTML(mt, "testdata/html/void_elements.html",
`<div><img src="test.jpg"><br><input type="text"></div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with void elements, got: %s", mt.message)
}
})
t.Run("embedded matcher in attribute", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded matcher in attribute
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_matcher_attr.html",
`<div style="border-left: 6px solid #ff0000;">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded matcher, got: %s", mt.message)
}
})
t.Run("embedded regex in attribute", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded regex in attribute
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_regex_attr.html",
`<div style="border-left: 6px solid #ff0000;">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded regex, got: %s", mt.message)
}
})
t.Run("embedded matcher mismatch", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded regex that won't match
mt := &mockT{}
// when: asserting with non-matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_regex_attr_mismatch.html",
`<div style="border-left: 6px solid red;">Content</div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure with non-matching embedded regex")
}
})
t.Run("multiple embedded matchers", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with multiple embedded matchers
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/multiple_embedded.html",
`<div data-info="user-john-id-12345">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with multiple embedded matchers, got: %s", mt.message)
}
})
t.Run("embedded matcher in text content", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded matcher in text content
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_text.html",
`<div>Hello World, your ID is 12345</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded matcher in text, got: %s", mt.message)
}
})
t.Run("embedded anyInt", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded anyInt matcher
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_anyint.html",
`<div data-count="Total: 42 items">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded anyInt, got: %s", mt.message)
}
})
t.Run("embedded anyFloat", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded anyFloat matcher
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_anyfloat.html",
`<div data-price="Price: $19.99">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded anyFloat, got: %s", mt.message)
}
})
t.Run("embedded anyBool", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded anyBool matcher
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_anybool.html",
`<div data-state="enabled=true">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded anyBool, got: %s", mt.message)
}
})
t.Run("embedded anyValue", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded anyValue matcher
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_anyvalue.html",
`<div data-info="key=anything-here-123">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded anyValue, got: %s", mt.message)
}
})
t.Run("embedded ignore", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded ignore matcher
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_ignore.html",
`<div data-timestamp="created=2024-01-15T10:30:00Z">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded ignore, got: %s", mt.message)
}
})
t.Run("embedded oneOf", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded oneOf matcher
mt := &mockT{}
// when: asserting with matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_oneof.html",
`<div class="btn btn-secondary">Content</div>`)
// then: the test passes
if mt.failed {
t.Errorf("expected no failure with embedded oneOf, got: %s", mt.message)
}
})
t.Run("embedded oneOf mismatch", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file with embedded oneOf matcher
mt := &mockT{}
// when: asserting with non-matching HTML
testastic.AssertHTML(mt, "testdata/html/embedded_oneof_limited.html",
`<div class="btn btn-danger">Content</div>`)
// then: the test fails
if !mt.failed {
t.Error("expected failure with non-matching oneOf value")
}
})
t.Run("with message", func(t *testing.T) {
t.Parallel()
// given: an expected HTML file and mismatched actual HTML
mt := &mockT{}
// when: asserting with a custom message
testastic.AssertHTML(mt, "testdata/html/wrong_tag.html", `<div><p>Content</p></div>`,
testastic.Message("custom html message"))
// then: the failure includes the custom message
if !mt.failed {
t.Error("expected failure for wrong HTML tag")
}
if !strings.Contains(mt.message, "custom html message") {
t.Errorf("expected custom message in failure, got: %s", mt.message)
}
})
}
func TestAssertHTML_StandaloneTypedMatchers(t *testing.T) {
t.Parallel()
// given: standalone typed matchers in text and attribute values
expectedFile := filepath.Join(t.TempDir(), "standalone-typed-matchers.html")
expected := `<div data-count="{{anyInt}}" data-price="{{anyFloat}}" data-enabled="{{anyBool}}">` +
`<span>{{anyInt}}</span><strong>{{anyFloat}}</strong><em>{{anyBool}}</em></div>`
err := os.WriteFile(expectedFile, []byte(expected), 0o644)
if err != nil {
t.Fatalf("write expected file: %v", err)
}
mt := &mockT{}
actual := `<div data-count="42" data-price="19.99" data-enabled="true">` +
`<span>-7</span><strong>3.14</strong><em>false</em></div>`
// when: asserting with matching HTML string values
testastic.AssertHTML(mt, expectedFile, actual)
// then: typed matchers treat HTML text and attributes as textual values
if mt.failed {
t.Errorf("expected standalone typed HTML matchers to pass, got: %s", mt.message)
}
}
func TestAssertHTML_UpdateEscapesGeneratedHTML(t *testing.T) {
t.Parallel()
t.Run("create expected file roundtrips escaped content", func(t *testing.T) {
t.Parallel()
// given: HTML containing text and attribute values that require escaping
expectedFile := filepath.Join(t.TempDir(), "created.html")
actual := `<div title="a " b">Tom & Jerry <friends></div>`
mt := &mockT{}
// when: creating the expected file in update mode
testastic.AssertHTML(mt, expectedFile, actual, testastic.Update())
if mt.failed {
t.Fatalf("expected create to pass, got: %s", mt.message)
}
verify := &mockT{}
// then: the generated expected file remains valid and matches the same actual HTML
testastic.AssertHTML(verify, expectedFile, actual)
if verify.failed {
t.Errorf("expected generated HTML to roundtrip, got: %s", verify.message)
}
})
t.Run("update expected file roundtrips escaped content", func(t *testing.T) {
t.Parallel()
// given: an existing expected file that will be updated
expectedFile := filepath.Join(t.TempDir(), "updated.html")
err := os.WriteFile(expectedFile, []byte(`<div title="old">old</div>`), 0o644)
if err != nil {
t.Fatalf("write expected file: %v", err)
}
actual := `<div title="a " b">Tom & Jerry <friends></div>`
mt := &mockT{}
// when: updating the expected file
testastic.AssertHTML(mt, expectedFile, actual, testastic.Update())
if mt.failed {
t.Fatalf("expected update to pass, got: %s", mt.message)
}
verify := &mockT{}
// then: the updated expected file remains valid and matches the same actual HTML
testastic.AssertHTML(verify, expectedFile, actual)
if verify.failed {
t.Errorf("expected updated HTML to roundtrip, got: %s", verify.message)
}
})
}
func TestAssertHTML_UpdateDoesNotRenderIndentationNodes(t *testing.T) {
t.Parallel()
// given: indented actual HTML and a missing expected file
expectedFile := filepath.Join(t.TempDir(), "created.html")
actual := "<div>\n <span>Content</span>\n</div>"
// when: creating the expected file in update mode
mt := &mockT{}
testastic.AssertHTML(mt, expectedFile, actual, testastic.Update())
if mt.failed {
t.Fatalf("expected create to pass, got: %s", mt.message)
}
content, err := os.ReadFile(expectedFile)
if err != nil {
t.Fatalf("read expected file: %v", err)
}
// then: source indentation does not create blank lines in canonical output
want := "<html>\n <head></head>\n <body>\n <div>\n <span>Content</span>\n" +
" </div>\n </body>\n</html>"
if string(content) != want {
t.Errorf("generated HTML mismatch:\n%s", content)
}
}
func TestAssertHTML_UnsupportedOptions(t *testing.T) {
t.Parallel()
t.Run("json options rejected", func(t *testing.T) {
t.Parallel()
// given: JSON/YAML-only options passed to AssertHTML
mt := &mockT{}
// when: asserting with unsupported options
testastic.AssertHTML(mt, "testdata/html/exact_match_unsupported_options.html",
`<section class="card"><span>Hello Unsupported</span></section>`, testastic.IgnoreArrayOrder())
// then: the test fatals with a message listing the unsupported option
if !mt.fatal {
t.Error("expected fatal error for unsupported options")
}
expectedMsg := "testastic: unsupported options for AssertHTML: IgnoreArrayOrder"
if mt.message != expectedMsg {
t.Errorf("expected message %q, got: %q", expectedMsg, mt.message)
}
})
t.Run("supported options accepted", func(t *testing.T) {
t.Parallel()
// given: supported options for AssertHTML
mt := &mockT{}
// when: asserting with IgnoreHTMLComments
testastic.AssertHTML(mt, "testdata/html/exact_match_supported_options.html",
`<div class="panel"><span>Hello Options</span></div>`, testastic.IgnoreHTMLComments())
// then: the test passes without unsupported option error