Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions week04/Entertainer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
)

var _ Entertainer = &Band{}
var _ Setuper = &Band{}
var _Teardowner = &Band{}

type Band struct {
IsSetup bool
IsTorndown bool
MinAudience int
PlayedFor int
}

func (a Band) Name() string {
return "The Thunders of Rock"
}

func (a Band) Validate(a Venue) error {
if b.Audience < a.MinAudience {
return fmt.Errorf("we don't play small bands")
}
return nil
}

func (a *Band) Perform(b Venue) error {
if err := a.Validate(b); err != nil {
return err
}
a.PlayedFor = b.Audience

return nil
}

func (a *Band) Setup(b Venue) error {
if a.IsSetup {
return fmt.Errorf("we already set up your material")

}
if err := a.Validate(b); err != nil {
return err

}
a.IsSetup = true
return nil

}

func (a *Band) Teardown(a Venue) error {
if a.IsTorndown {
return fmt.Errorf("we already tore down our material")

}
a.IsTorndown = true
return nil
}

var _Entertainer = Poet{}

type Poet struct{}

func (c Poet) Name() string {
return "Maybelle Marie"

}

func (c Poet) Perform(a Venue) error {
if b.Audience == 1 {
return fmt.Errorf("i'm not playing for just the employees")

}
return nil
}
54 changes: 53 additions & 1 deletion week04/venue.go
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@

package main

import (
"fmt"
"io"
)

type Venue struct { //We will need to define a venue type
Audience int // We will need to take the number of audience members within this program.
Log io.Writer
}

func (a *Venue) Entertain(audience int, acts ...Entertainer) error { // *Venue is used as the pointer. This takes the Venue type and interfaces implement an Entertain method on *Venue.
if len(acts) == 0 {
return fmt.Errorf("there are no entertainers to perform")

}
a.Audience = audience
for _, act := range acts {
if err := a.play(act); err != nil {
return err
}
}
return nil
}

func (a Venue) play(act Entertainer) error {
name := act.Name()

if b, ok := act.(Setuper); ok { // The venue should check each entertainer to see if it implements the Setuper interface.
if err := b.Setup(a); err != nil {
return fmt.Errorf("%s: %w", name, err)

}
fmt.Fprint(a.Log, "%s has completed setup.\n", name) // Use the format, ("%s has completed setup. \n")

}
if err := act.Perform(a); err != nil {
return fmt.Errorf("%s: %w", name, err)
}
fmt.Fprint(a.Log, "%s has performed for %d people.\n", name, a.Audience) // Use the format, ("%s has performed for %d people. \n")

if c, ok := act.(Teardowner); ok { // The venue should check each entertainer to see if it implements the Teardowner interface.
if err := c.Teardown(a); err != nil {
return fmt.Errorf("%s: %w", name, err)

}
fmt.Fprintf(a.Log, "%s has completed teardown.\n", name) // Use the format, ("%s has completed teardown. \n")
}

return nil

}
129 changes: 129 additions & 0 deletions week04/venue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"fmt"
"bytes"
"log"
"strings"
"testing"

)

func Test_Venue_Logs(t *testing.T) { // The layout for testing is: Test(t *testing.T)
t.Parallel()

bb := &bytes.Buffer{}

v := &Venue{Log: bb}

err := v.Entertain(100, &Band{MinAudience: 50})

if err != nil {
t.Fatalf("expected no error, got%s", err)

}

act := strings.TrimSpace(bb.String())
exp := "The Thunders of rock has completed setup.
The Thunders of Rock has performed for 100 people.
The Thunders of Rock has completed teardown."

if act != exp {
t.Fatalf("expected %q, got %q", exp, act)

}
}
func Test_Venue_Entertain(t *testing.T) {
t.Parallel()

const allgood = "The Thunders of rock has completed setup.
The Thunders of Rock has performed for 45 people.
The Thunders of Rock has completed teardown.
Maybelle Marie has performed for 45 people."

table := {} struct {
acts []Entertainer
aud int
err bool
exp string
name string

}{
{
name: "setup error"
acts: []Entertainer{
&Band{IsSetup: true},

},
exp: "we already set up our material",
err: true,

},
{
name: "teardown error",
acts: []Entertainer{
&Band{IsTorndown: true},

},
exp: "we already tore down our material ",
err : true,

},
{
name: "play error",
acts: []Entertainer{
Poet{},

},
exp: "i'm not playing for just the employees",
err: true,
aud: 1,
},
{
name: "all good",
acts: []Entertainer{
&Band{},
Poet{},

},
aud: 45,
exp: allgood,
},

}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
bb := &bytes.Buffer{}
v := &Venue{Log: bb}

err := v.Entertain(tt.aud, tt.acts...)

if tt.err {
if err == nil {
log.Fatalf("expected error, got none")

}

act := err.Error()
if !strings.Contains(act, tt.exp) {
log.Fatalf("expected %q to contain %q", act, tt.exp)

}
return
}

if err != nil {
t.Fatalf("expected no error, got %s", err)

}

act := bb.String()
fmt.Println(act)
if !strings.Contains(act, tt.exp) {
log.Fatalf("expected %q to contain %q", act, tt.exp)

}

} )
}
}