Skip to content

Commit 3ebe7c8

Browse files
committed
test: added tests to the codegen components (increased coverage to be >85%)
Signed-off-by: Frederic BIDON <[email protected]>
1 parent 0ac64a1 commit 3ebe7c8

File tree

11 files changed

+3193
-110
lines changed

11 files changed

+3193
-110
lines changed

codegen/internal/generator/domains/domains_test.go

Lines changed: 120 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ type makeDomainIndexCase struct {
233233
checkDomains func(*testing.T, Index)
234234
}
235235

236-
//nolint:gocognit,gocyclo,cyclop // this is temporary accepted extra complexity. Should refactor with externalized asserting functions
237236
func makeDomainIndexCases() iter.Seq[makeDomainIndexCase] {
238237
return slices.Values([]makeDomainIndexCase{
239238
{
@@ -291,83 +290,8 @@ func makeDomainIndexCases() iter.Seq[makeDomainIndexCase] {
291290
testPackage: {Package: assertPkg},
292291
}
293292
}(),
294-
checkMetadata: func(t *testing.T, index Index) {
295-
t.Helper()
296-
297-
if index.Tool() != "testify-codegen" {
298-
t.Errorf("Expected tool 'testify-codegen', got %q", index.Tool())
299-
}
300-
if index.Copyright() != "Copyright 2025" {
301-
t.Errorf("Expected copyright 'Copyright 2025', got %q", index.Copyright())
302-
}
303-
if index.Receiver() != "Assertions" {
304-
t.Errorf("Expected receiver 'Assertions', got %q", index.Receiver())
305-
}
306-
if index.RootPackage() != testRepo {
307-
t.Errorf("Expected root package '%s', got %q", testRepo, index.RootPackage())
308-
}
309-
},
310-
checkDomains: func(t *testing.T, index Index) {
311-
t.Helper()
312-
313-
domainCount := 0
314-
for domain, entry := range index.Entries() {
315-
domainCount++
316-
317-
switch domain {
318-
case "equal":
319-
if entry.Description() != "Equality assertions" {
320-
t.Errorf("Expected description 'Equality assertions', got %q", entry.Description())
321-
}
322-
if len(entry.Functions()) != 1 || entry.Functions()[0].Name != "Equal" {
323-
t.Error("Expected Equal function in equal domain")
324-
}
325-
if len(entry.Consts()) != 1 || entry.Consts()[0].Name != "SomeConst" {
326-
t.Error("Expected SomeConst in equal domain")
327-
}
328-
329-
case "boolean":
330-
if entry.Description() != "Boolean assertions" {
331-
t.Errorf("Expected description 'Boolean assertions', got %q", entry.Description())
332-
}
333-
if len(entry.Functions()) != 1 || entry.Functions()[0].Name != "True" {
334-
t.Error("Expected True function in boolean domain")
335-
}
336-
if len(entry.Vars()) != 1 || entry.Vars()[0].Name != "SomeVar" {
337-
t.Error("Expected SomeVar in boolean domain")
338-
}
339-
340-
case "testing":
341-
if len(entry.Types()) != 1 || entry.Types()[0].Name != "TestingT" {
342-
t.Error("Expected TestingT type in testing domain")
343-
}
344-
345-
case "common":
346-
if entry.Description() != "Other uncategorized helpers" {
347-
t.Errorf("Expected description 'Other uncategorized helpers', got %q", entry.Description())
348-
}
349-
if len(entry.Functions()) != 1 || entry.Functions()[0].Name != "Helper" {
350-
t.Error("Expected Helper function in common domain")
351-
}
352-
if len(entry.Types()) != 1 || entry.Types()[0].Name != "H" {
353-
t.Error("Expected H type in common domain")
354-
}
355-
if len(entry.Vars()) != 1 || entry.Vars()[0].Name != "SomeOtherVar" {
356-
t.Error("Expected SomeOtherVar in common domain")
357-
}
358-
if len(entry.Consts()) != 1 || entry.Consts()[0].Name != "SomeOtherConst" {
359-
t.Error("Expected SomeOtherConst in common domain")
360-
}
361-
362-
default:
363-
t.Errorf("Unexpected domain: %s", domain)
364-
}
365-
}
366-
367-
if domainCount != 4 {
368-
t.Errorf("Expected 4 domains, got %d", domainCount)
369-
}
370-
},
293+
checkMetadata: checkCompleteIndexMetadata,
294+
checkDomains: checkCompleteIndexDomains,
371295
},
372296
{
373297
name: "dangling domain description without declarations",
@@ -399,25 +323,128 @@ func makeDomainIndexCases() iter.Seq[makeDomainIndexCase] {
399323
testPackage: {Package: pkg},
400324
}
401325
}(),
402-
checkDomains: func(t *testing.T, index Index) {
403-
t.Helper()
404-
405-
domainCount := 0
406-
for domain := range index.Entries() {
407-
domainCount++
408-
if domain == "phantom" {
409-
t.Error("Dangling domain 'phantom' should not appear in entries")
410-
}
411-
}
412-
413-
if domainCount != 1 {
414-
t.Errorf("Expected 1 domain (equal), got %d", domainCount)
415-
}
416-
},
326+
checkDomains: checkDanglingDomainExclusion,
417327
},
418328
})
419329
}
420330

331+
/* Domain index assertion helpers */
332+
333+
func checkCompleteIndexMetadata(t *testing.T, index Index) {
334+
t.Helper()
335+
336+
if index.Tool() != "testify-codegen" {
337+
t.Errorf("Expected tool 'testify-codegen', got %q", index.Tool())
338+
}
339+
if index.Copyright() != "Copyright 2025" {
340+
t.Errorf("Expected copyright 'Copyright 2025', got %q", index.Copyright())
341+
}
342+
if index.Receiver() != "Assertions" {
343+
t.Errorf("Expected receiver 'Assertions', got %q", index.Receiver())
344+
}
345+
if index.RootPackage() != testRepo {
346+
t.Errorf("Expected root package '%s', got %q", testRepo, index.RootPackage())
347+
}
348+
}
349+
350+
func checkCompleteIndexDomains(t *testing.T, index Index) {
351+
t.Helper()
352+
353+
checkers := map[string]func(*testing.T, Entry){
354+
"equal": checkEqualDomain,
355+
"boolean": checkBooleanDomain,
356+
"testing": checkTestingDomain,
357+
"common": checkCommonDomain,
358+
}
359+
360+
domainCount := 0
361+
for domain, entry := range index.Entries() {
362+
domainCount++
363+
364+
if checker, ok := checkers[domain]; ok {
365+
checker(t, entry)
366+
} else {
367+
t.Errorf("Unexpected domain: %s", domain)
368+
}
369+
}
370+
371+
if domainCount != 4 {
372+
t.Errorf("Expected 4 domains, got %d", domainCount)
373+
}
374+
}
375+
376+
func checkEqualDomain(t *testing.T, entry Entry) {
377+
t.Helper()
378+
379+
if entry.Description() != "Equality assertions" {
380+
t.Errorf("Expected description 'Equality assertions', got %q", entry.Description())
381+
}
382+
if len(entry.Functions()) != 1 || entry.Functions()[0].Name != "Equal" {
383+
t.Error("Expected Equal function in equal domain")
384+
}
385+
if len(entry.Consts()) != 1 || entry.Consts()[0].Name != "SomeConst" {
386+
t.Error("Expected SomeConst in equal domain")
387+
}
388+
}
389+
390+
func checkBooleanDomain(t *testing.T, entry Entry) {
391+
t.Helper()
392+
393+
if entry.Description() != "Boolean assertions" {
394+
t.Errorf("Expected description 'Boolean assertions', got %q", entry.Description())
395+
}
396+
if len(entry.Functions()) != 1 || entry.Functions()[0].Name != "True" {
397+
t.Error("Expected True function in boolean domain")
398+
}
399+
if len(entry.Vars()) != 1 || entry.Vars()[0].Name != "SomeVar" {
400+
t.Error("Expected SomeVar in boolean domain")
401+
}
402+
}
403+
404+
func checkTestingDomain(t *testing.T, entry Entry) {
405+
t.Helper()
406+
407+
if len(entry.Types()) != 1 || entry.Types()[0].Name != "TestingT" {
408+
t.Error("Expected TestingT type in testing domain")
409+
}
410+
}
411+
412+
func checkCommonDomain(t *testing.T, entry Entry) {
413+
t.Helper()
414+
415+
if entry.Description() != "Other uncategorized helpers" {
416+
t.Errorf("Expected description 'Other uncategorized helpers', got %q", entry.Description())
417+
}
418+
if len(entry.Functions()) != 1 || entry.Functions()[0].Name != "Helper" {
419+
t.Error("Expected Helper function in common domain")
420+
}
421+
if len(entry.Types()) != 1 || entry.Types()[0].Name != "H" {
422+
t.Error("Expected H type in common domain")
423+
}
424+
if len(entry.Vars()) != 1 || entry.Vars()[0].Name != "SomeOtherVar" {
425+
t.Error("Expected SomeOtherVar in common domain")
426+
}
427+
if len(entry.Consts()) != 1 || entry.Consts()[0].Name != "SomeOtherConst" {
428+
t.Error("Expected SomeOtherConst in common domain")
429+
}
430+
}
431+
432+
func checkDanglingDomainExclusion(t *testing.T, index Index) {
433+
t.Helper()
434+
435+
domainCount := 0
436+
for domain := range index.Entries() {
437+
domainCount++
438+
if domain == "phantom" {
439+
t.Error("Dangling domain 'phantom' should not appear in entries")
440+
}
441+
}
442+
443+
if domainCount != 1 {
444+
t.Errorf("Expected 1 domain (equal), got %d", domainCount)
445+
}
446+
}
447+
421448
type domainIndexSortingCase struct {
422449
name string
423450
docs map[string]model.Document

0 commit comments

Comments
 (0)