quii / pepper

MIT License
17 stars 2 forks source link

Generate type-safe matchers for structs #3

Open quii opened 3 months ago

quii commented 3 months ago

Given a struct like this

type Page struct {
    Title       string
    Body        string
    Description string
    Private     bool
    Template    string
}

It would be rad if you could go generate matchers, and it should be possible as it's pretty boilerplate

func HasTemplate(expected string) Matcher[*cms.Page] {
    return func(got *cms.Page) MatchResult {
        return MatchResult{
            Description: "has template " + expected,
            Matches:     got.Template == expected,
            But:         "it was " + got.Template,
        }
    }
}

func IsPrivate() Matcher[*cms.Page] {
    return func(got *cms.Page) MatchResult {
        return MatchResult{
            Description: "is private",
            Matches:     got.Private,
            But:         "it was not private",
        }
    }
}

func HaveBody(expected string) Matcher[*cms.Page] {
    return func(got *cms.Page) MatchResult {
        return MatchResult{
            Description: "have body " + expected,
            Matches:     got.Body == expected,
            But:         "it was " + got.Body,
        }
    }
}

func HaveDescription(expected string) Matcher[*cms.Page] {
    return func(got *cms.Page) MatchResult {
        return MatchResult{
            Description: "have description " + expected,
            Matches:     got.Description == expected,
            But:         "it was " + got.Description,
        }
    }
}

func HaveTitle(expected string) Matcher[*cms.Page] {
    return func(got *cms.Page) MatchResult {
        return MatchResult{
            Description: "have title " + expected,
            Matches:     got.Title == expected,
            But:         "it was " + got.Title,
        }
    }
}
quii commented 3 months ago

That said, magic AI made all of these for me, so 🤷