dimanche 31 juillet 2016

Example test of go templates fails with imported and not used: "testing"

As far as I can tell I'm following structure needed for 'go test' flawlessly. I don't see a discrepancy from tests I could run in other packages. 'go build' works fine. I'm getting

./HelloTemplate_test.go:3: imported and not used: "testing" ./HelloTemplate_test.go:5: undefined: Testing in Testing.T

What am I missing?

HelloTemplate.go

package templateprint

import "testing"

func TestRunTempl(t *Testing.T) {
    sweaters := Inventory{"wool", 17}
    tmpl := " items are made of "
    err := RunTempl(tmpl, sweaters)
    if err != nil {
        t.Error("Template failed ")
    }
}

HelloTemplate_test.go

package templateprint

import (
    "os"
    "text/template"
)

type Inventory struct {
    Material string
    Count    uint
}

func RunTempl(templ string, inv Inventory) error {
    tmpl, err := template.New("test").Parse(templ)
    if err != nil {
        return (err)
    }
    err = tmpl.Execute(os.Stdout, inv)
    if err != nil {
        return (err)
    }
    return nil
}

Aucun commentaire:

Enregistrer un commentaire