Skip to content

Commit

Permalink
Merge pull request #1 from wneessen/subcap-test
Browse files Browse the repository at this point in the history
Added README.md and test coverage to subject_capitalize
  • Loading branch information
wneessen authored Oct 2, 2022
2 parents dbf2580 + 402e042 commit 46bb5ff
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions subject_capitalize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
SPDX-FileCopyrightText: 2022 Winni Neessen <[email protected]>
SPDX-License-Identifier: CC0-1.0
-->

## Capitalize your subject based on a given language

This is a simple middlware that makes use of the powerful [golang.org/x/text/cases](https://golang.org/x/text/cases)
library. It will read the currently set subject of the `mail.Msg` and use the `cases` library to capitalize the
subject based on the given language.

### Example
```go
package main

import (
"fmt"
"github.com/wneessen/go-mail"
"github.com/wneessen/go-mail-middleware/subject_capitalize"
"golang.org/x/text/language"
"os"
)

func main() {
m := mail.NewMsg(mail.WithMiddleware(subcap.New(language.English)))
m.Subject("this is a test message")
if err := m.WriteToFile("testmail.eml"); err != nil {
fmt.Printf("failed to write mail message to file: %s\n", err)
os.Exit(1)
}
}
```
33 changes: 33 additions & 0 deletions subject_capitalize/subject_capitalize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: 2022 Winni Neessen <[email protected]>
//
// SPDX-License-Identifier: MIT

package subcap

import (
"bytes"
"fmt"
"github.com/wneessen/go-mail"
"golang.org/x/text/language"
"strings"
"testing"
)

func TestNew(t *testing.T) {
mw := New(language.English)
if fmt.Sprintf("%s", mw.l) != "en" {
t.Errorf("New() failed. Expected language: %q, got: %q", "en", fmt.Sprintf("%s", mw.l))
}
}

func TestMiddleware_Handle(t *testing.T) {
m := mail.NewMsg(mail.WithMiddleware(New(language.English)))
m.Subject("this is a test")
buf := bytes.Buffer{}
if _, err := m.WriteTo(&buf); err != nil {
t.Errorf("failed to write mail message to buffer: %s", err)
}
if !strings.Contains(buf.String(), "This Is A Test") {
t.Errorf("middleware failed. Expected: %q in subject, got: %q", "This Is A Test", buf.String())
}
}

0 comments on commit 46bb5ff

Please sign in to comment.