From 8a1e1c05e64873e349f395013e07f6b1a20b19fe Mon Sep 17 00:00:00 2001 From: Daniel Hess Date: Fri, 20 Oct 2017 21:53:24 -0700 Subject: [PATCH] moving examples to godocs --- README.md | 64 +-------------------------------------- postal_examples_test.go | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 63 deletions(-) create mode 100644 postal_examples_test.go diff --git a/README.md b/README.md index ca21944..c6a5599 100644 --- a/README.md +++ b/README.md @@ -17,69 +17,7 @@ go get github.com/gomicro/postal ``` # Usage -Compare a mail record - -``` -func testMailing(t *testing.T) { - host := "mailhost.com:25" - auth := smtp.PlainAuth("", "username", "password", "mailhost.com") - from := "dev@gomicro.io" - to := []string{"foo@bar.com"} - body := []byte{"hello world"} - - p := postal.New() - SendMail := p.Mailer() - - err := SendMail(host, auth, from, to, body) - if err != nil { - t.Error(err.Error()) - } - - if p.Mailed() != 1 { - t.Errorf("expected 1 mailed, got %v mailed", p.Mailed()) - } - - records := p.MailRecords() - if len(records) != 1 { - t.Errorf("expected 1 record, got %v record", len(records)) - } - - r := records[0] - if r.Host != host { - t.Errorf("expected %v, got %v", host, r.Host) - } - - if r.From != from { - t.Errorf("expected %v, got %v", from, r.From) - } - - if r.To != to { - t.Errorf("expected %v, got %v", to, r.To) - } - - if r.Body != body { - t.Errorf("expected %v, got %v", string(body), string(r.Body)) - } -} -``` - -Get an error from sending an email - -``` -func testMailing(t *testing.T) { - auth := smtp.PlainAuth("", "username", "password", "mailhost.com") - - p := postal.New() - p.SetError(fmt.Errorf("something's not quite right here")) - SendMail := p.Mailer() - - err := SendMail("mailhost.com:25", auth, "dev@gomicro.io", []string{"foo@bar.com"}, []byte("Hello world")) - - if err == nil { - t.Errorf("Expected error, and got nil") - } -} -``` +See the [examples](https://godoc.org/github.com/gomicro/postal#pkg-examples) within the docs for ways to use the library. # Versioning The library will be versioned in accordance with [Semver 2.0.0](http://semver.org). See the [releases](https://github.com/gomicro/postal/releases) section for the latest version. Until version 1.0.0 the libary is considered to be unstable. diff --git a/postal_examples_test.go b/postal_examples_test.go new file mode 100644 index 0000000..3ba7225 --- /dev/null +++ b/postal_examples_test.go @@ -0,0 +1,67 @@ +package postal_test + +import ( + "fmt" + "net/smtp" + "testing" + + "github.com/gomicro/postal" +) + +func ExamplePostal() { + // This would normally be provided by the testing function + var t *testing.T + + host := "mailhost.com:25" + auth := smtp.PlainAuth("", "username", "password", "mailhost.com") + from := "dev@gomicro.io" + to := []string{"foo@bar.com"} + body := []byte("hello world") + + p := postal.New() + SendMail := p.Mailer() + + err := SendMail(host, auth, from, to, body) + if err != nil { + t.Error(err.Error()) + } + + if p.Mailed() != 1 { + t.Errorf("expected 1 mailed, got %v mailed", p.Mailed()) + } + + records := p.MailRecords() + if len(records) != 1 { + t.Errorf("expected 1 record, got %v record", len(records)) + } + + r := records[0] + if r.Host != host { + t.Errorf("expected %v, got %v", host, r.Host) + } + + if r.From != from { + t.Errorf("expected %v, got %v", from, r.From) + } + + if string(r.Body) != string(body) { + t.Errorf("expected %v, got %v", string(body), string(r.Body)) + } +} + +func ExamplePostal_error() { + // This would normally be provided by the testing function + var t *testing.T + + auth := smtp.PlainAuth("", "username", "password", "mailhost.com") + + p := postal.New() + p.SetError(fmt.Errorf("something's not quite right here")) + SendMail := p.Mailer() + + err := SendMail("mailhost.com:25", auth, "dev@gomicro.io", []string{"foo@bar.com"}, []byte("Hello world")) + + if err == nil { + t.Errorf("Expected error, and got nil") + } +}