Skip to content

Commit

Permalink
test: fix ExampleSStruct_patterns
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Aug 30, 2022
1 parent 9a8d707 commit eebb2f4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
26 changes: 12 additions & 14 deletions td/example_cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2889,23 +2889,21 @@ func ExampleCmpSStruct_patterns() {
secret: "5ecr3T",
}

ok := td.Cmp(t, got,
td.Struct(Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`= *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`=~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
`! [A-Z]*`: td.Ignore(), // private fields
}),
ok := td.CmpSStruct(t, got, Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`= *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`=~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
`! [A-Z]*`: td.Ignore(), // private fields
},
"mix shell & regexp patterns")
fmt.Println("Patterns match only remaining fields:", ok)

ok = td.Cmp(t, got,
td.Struct(Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`1 = *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`2 =~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
`3 !~ ^[A-Z]`: td.Ignore(), // private fields
}),
ok = td.CmpSStruct(t, got, Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`1 = *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`2 =~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
`3 !~ ^[A-Z]`: td.Ignore(), // private fields
},
"ordered patterns")
fmt.Println("Ordered patterns match only remaining fields:", ok)

Expand Down
26 changes: 12 additions & 14 deletions td/example_t_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2889,23 +2889,21 @@ func ExampleT_SStruct_patterns() {
secret: "5ecr3T",
}

ok := t.Cmp(got,
td.Struct(Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`= *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`=~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
`! [A-Z]*`: td.Ignore(), // private fields
}),
ok := t.SStruct(got, Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`= *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`=~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
`! [A-Z]*`: td.Ignore(), // private fields
},
"mix shell & regexp patterns")
fmt.Println("Patterns match only remaining fields:", ok)

ok = t.Cmp(got,
td.Struct(Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`1 = *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`2 =~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
`3 !~ ^[A-Z]`: td.Ignore(), // private fields
}),
ok = t.SStruct(got, Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`1 = *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`2 =~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
`3 !~ ^[A-Z]`: td.Ignore(), // private fields
},
"ordered patterns")
fmt.Println("Ordered patterns match only remaining fields:", ok)

Expand Down
4 changes: 2 additions & 2 deletions td/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3529,7 +3529,7 @@ func ExampleSStruct_patterns() {
}

ok := td.Cmp(t, got,
td.Struct(Person{Lastname: "Foo"}, td.StructFields{
td.SStruct(Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`= *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`=~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
Expand All @@ -3539,7 +3539,7 @@ func ExampleSStruct_patterns() {
fmt.Println("Patterns match only remaining fields:", ok)

ok = td.Cmp(t, got,
td.Struct(Person{Lastname: "Foo"}, td.StructFields{
td.SStruct(Person{Lastname: "Foo"}, td.StructFields{
`DeletedAt`: nil,
`1 = *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model
`2 =~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt
Expand Down
7 changes: 6 additions & 1 deletion tools/gen_funcs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,12 @@ package td

while ($examples =~ /^func Example($funcs_reg)(_\w+)?\(\) \{\n(.*?)^\}$/gms)
{
push(@{$funcs{$1}{examples}}, { name => $2 // '', code => $3 });
my($op, $name, $code) = ($1, $2, $3);
if ($code =~ /\btd\.$op\(/)
{
# Only copy example for which an operator use is found
push(@{$funcs{$op}{examples}}, { name => $name // '', code => $code });
}
}

{
Expand Down

0 comments on commit eebb2f4

Please sign in to comment.