From 560b6233830e63008d5d165d5f7b1245b4bb371a Mon Sep 17 00:00:00 2001 From: asoseil Date: Mon, 20 Nov 2017 14:16:07 +0100 Subject: [PATCH] realize struct mock --- cli_test.go | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ realize_test.go | 13 +++++ 2 files changed, 142 insertions(+) create mode 100644 cli_test.go create mode 100644 realize_test.go diff --git a/cli_test.go b/cli_test.go new file mode 100644 index 0000000..49a6255 --- /dev/null +++ b/cli_test.go @@ -0,0 +1,129 @@ +package main + +import ( + "bytes" + "log" + "strings" + "testing" + "gopkg.in/urfave/cli.v2" + "errors" +) + +func (m *mockRealize) add() error{ + if mockResponse != nil { + return mockResponse.(error) + } + m.Projects = append(m.Projects, Project{Name:"One"}) + return nil +} + +func (m *mockRealize) setup() error{ + if mockResponse != nil { + return mockResponse.(error) + } + return nil +} + +func (m *mockRealize) start() error{ + if mockResponse != nil { + return mockResponse.(error) + } + return nil +} + +func (m *mockRealize) clean() error{ + if mockResponse != nil { + return mockResponse.(error) + } + return nil +} + +func (m *mockRealize) remove() error{ + if mockResponse != nil { + return mockResponse.(error) + } + m.Projects = []Project{} + return nil +} + +func TestAdd(t *testing.T) { + m := mockRealize{} + mockResponse = nil + if err := m.add(); err != nil{ + t.Fatal("Unexpected error") + } + if len(m.Projects) <= 0{ + t.Fatal("Unexpected error") + } + + m = mockRealize{} + m.Projects = []Project{{Name:"Default"}} + mockResponse = nil + if err := m.add(); err != nil{ + t.Fatal("Unexpected error") + } + if len(m.Projects) != 2{ + t.Fatal("Unexpected error") + } + + m = mockRealize{} + mockResponse = errors.New("error") + if err := m.clean(); err == nil{ + t.Fatal("Expected error") + } + if len(m.Projects) != 0{ + t.Fatal("Unexpected error") + } +} + +func TestStart(t *testing.T) { + m := mockRealize{} + mockResponse = nil + if err := m.add(); err != nil{ + t.Fatal("Unexpected error") + } +} + +func TestClean(t *testing.T) { + m := mockRealize{} + mockResponse = nil + if err := m.clean(); err != nil{ + t.Fatal("Unexpected error") + } + mockResponse = errors.New("error") + if err := m.clean(); err == nil{ + t.Fatal("Expected error") + } +} + +func TestRemove(t *testing.T) { + m := mockRealize{} + mockResponse = nil + if err := m.remove(); err != nil{ + t.Fatal("Unexpected error") + } + + m = mockRealize{} + mockResponse = nil + m.Projects = []Project{{Name:"Default"},{Name:"Default"}} + if err := m.remove(); err != nil{ + t.Fatal("Unexpected error") + } + if len(m.Projects) != 0{ + t.Fatal("Unexpected error") + } + + mockResponse = errors.New("error") + if err := m.clean(); err == nil{ + t.Fatal("Expected error") + } +} + +func TestVersion(t *testing.T) { + var buf bytes.Buffer + log.SetOutput(&buf) + r.version() + if !strings.Contains(buf.String(), RVersion) { + t.Fatal("Version expted", RVersion) + } +} diff --git a/realize_test.go b/realize_test.go new file mode 100644 index 0000000..9f18ccc --- /dev/null +++ b/realize_test.go @@ -0,0 +1,13 @@ +package main + +import "os" + +var mockResponse interface{} + +type mockRealize struct { + Settings Settings `yaml:"settings" json:"settings"` + Server Server `yaml:"server" json:"server"` + Schema `yaml:",inline"` + sync chan string + exit chan os.Signal +}