Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Factory Method Pattern 馃帀 #4

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to鈥
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -0,0 +1,57 @@
package patterns

import (
"fmt"
)

// Requester interface
type Requester interface {
Request(url string) string
}

const (
// ReactElement denote
ReactElement = iota
// VueElement denote
VueElement
// AngularElement denote
AngularElement
)

//GetRenderUI returns a pointer to a Requester interface
func GetRenderUI(m int) (Requester, error) {
switch m {
case ReactElement:
return new(ReactUI), nil
case VueElement:
return new(VueUI), nil
case AngularElement:
return new(AngularUI), nil
default:
return nil, fmt.Errorf("Renderer %d not recognized", m)
}
}

// ReactUI struct
type ReactUI struct{}

// VueUI struct
type VueUI struct{}

// AngularUI struct
type AngularUI struct{}

// Request method on ReactUI struct
func (ui *ReactUI) Request(url string) string {
return fmt.Sprintf("Request URL %s by using React\n", url)
}

// Request method on VueUI struct
func (ui *VueUI) Request(url string) string {
return fmt.Sprintf("Request URL %s by using Vue\n", url)
}

// Request method on AngularUI struct
func (ui *AngularUI) Request(url string) string {
return fmt.Sprintf("Request URL %s by using Angular\n", url)
}
@@ -0,0 +1,53 @@
package patterns

import (
"strings"
"testing"
)

func TestGetRenderReactUI(t *testing.T) {
ui, err := GetRenderUI(ReactElement)
if err != nil {
t.Fatal("A ui of type 'ReactElement' must exist")
}

msg := ui.Request("react")
if !strings.Contains(msg, "Request URL react by using React") {
t.Error("The React ui message wasn't correct")
}
t.Log("LOG:", msg)
}

func TestGetRenderVueUI(t *testing.T) {
ui, err := GetRenderUI(VueElement)
if err != nil {
t.Fatal("A ui of type 'VueElement' must exist")
}

msg := ui.Request("vue")
if !strings.Contains(msg, "Request URL vue by using Vue") {
t.Error("The Vue ui message wasn't correct")
}
t.Log("LOG:", msg)
}

func TestGetRenderAngularUI(t *testing.T) {
ui, err := GetRenderUI(AngularElement)
if err != nil {
t.Fatal("A ui of type 'AngularElement' must exist")
}

msg := ui.Request("angular")
if !strings.Contains(msg, "Request URL angular by using Angular") {
t.Error("The Angular ui message wasn't correct")
}
t.Log("LOG:", msg)
}

func TestGetPaymentMethodNonExistent(t *testing.T) {
_, err := GetRenderUI(20)
if err == nil {
t.Error("A ui with ID 20 must return an error")
}
t.Log("LOG:", err)
}
ProTip! Use n and p to navigate between commits in a pull request.
You can鈥檛 perform that action at this time.