From ff6c4ce6a429507a997d498ad04050bd1280fe5f Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Thu, 13 Nov 2014 01:09:44 -0800 Subject: [PATCH] test(tour) ensure template renders without failure License: MIT Signed-off-by: Brian Tiger Chow --- cmd/ipfs2/tour.go | 43 +++++++++++++++++++++++++++++++----------- cmd/ipfs2/tour_test.go | 24 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 cmd/ipfs2/tour_test.go diff --git a/cmd/ipfs2/tour.go b/cmd/ipfs2/tour.go index efca847f7..e9f3226de 100644 --- a/cmd/ipfs2/tour.go +++ b/cmd/ipfs2/tour.go @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + "html/template" "io" "os" @@ -45,12 +46,17 @@ IPFS very quickly. To start, run: return nil, err } - topic := tour.TopicID(cfg.Tour.Last) + id := tour.TopicID(cfg.Tour.Last) if len(strs) > 0 { - topic = tour.TopicID(strs[0]) + id = tour.TopicID(strs[0]) } - err = tourShow(out, topic) + t, err := tourGet(id) + if err != nil { + return nil, err + } + + err = tourShow(out, t) if err != nil { return nil, err } @@ -72,14 +78,18 @@ var cmdIpfsTourNext = &cmds.Command{ return nil, err } - topic := tour.NextTopic(tour.TopicID(cfg.Tour.Last)) + id := tour.NextTopic(tour.TopicID(cfg.Tour.Last)) + topic, err := tourGet(id) + if err != nil { + return nil, err + } if err := tourShow(&w, topic); err != nil { return nil, err } // topic changed, not last. write it out. - if string(topic) != cfg.Tour.Last { - cfg.Tour.Last = string(topic) + if string(id) != cfg.Tour.Last { + cfg.Tour.Last = string(id) err := writeConfig(path, cfg) if err != nil { return nil, err @@ -147,14 +157,25 @@ func tourListCmd(w io.Writer, cfg *config.Config) { } } -func tourShow(w io.Writer, id tour.ID) error { +func tourShow(w io.Writer, t *tour.Topic) error { + tmpl := ` +Tour {{ .ID }} - {{ .Title }} + +{{ .Text }} + ` + ttempl, err := template.New("tour").Parse(tmpl) + if err != nil { + return err + } + return ttempl.Execute(w, t) +} + +func tourGet(id tour.ID) (*tour.Topic, error) { t, found := tour.Topics[id] if !found { - return fmt.Errorf("no topic with id: %s", id) + return nil, fmt.Errorf("no topic with id: %s", id) } - - fmt.Fprintf(w, "Tour %s - %s\n\n%s\n", t.ID, t.Title, t.Text) - return nil + return &t, nil } // TODO share func diff --git a/cmd/ipfs2/tour_test.go b/cmd/ipfs2/tour_test.go new file mode 100644 index 000000000..ae6089e56 --- /dev/null +++ b/cmd/ipfs2/tour_test.go @@ -0,0 +1,24 @@ +package main + +import ( + "bytes" + "testing" + + "github.com/jbenet/go-ipfs/tour" +) + +func TestParseTourTemplate(t *testing.T) { + topic := &tour.Topic{ + ID: "42", + Title: "IPFS CLI test files", + Text: `Welcome to the IPFS test files + This is where we test our beautiful command line interfaces + `, + } + var buf bytes.Buffer + err := tourShow(&buf, topic) + if err != nil { + t.Fatal(err) + } + t.Log(buf.String()) +}