Mocking first pass
							parent
							
								
									51259ce82f
								
							
						
					
					
						commit
						aa7df72415
					
				| @ -0,0 +1,3 @@ | |||||||
|  | module mocking | ||||||
|  | 
 | ||||||
|  | go 1.21.0 | ||||||
											
												Binary file not shown.
											
										
									
								| @ -0,0 +1,45 @@ | |||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"io" | ||||||
|  | 	"os" | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | const finalWord = "Go!" | ||||||
|  | 
 | ||||||
|  | // const countDownStart = 3
 | ||||||
|  | 
 | ||||||
|  | type Sleeper interface { | ||||||
|  | 	Sleep() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // type DefaultSleeper struct{}
 | ||||||
|  | 
 | ||||||
|  | // func (d *DefaultSleeper) Sleep() {
 | ||||||
|  | // 	time.Sleep(1 * time.Second)
 | ||||||
|  | // }
 | ||||||
|  | 
 | ||||||
|  | type ConfigurableSleeper struct { | ||||||
|  | 	duration time.Duration | ||||||
|  | 	sleep    func(time.Duration) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (c *ConfigurableSleeper) Sleep() { | ||||||
|  | 	c.sleep(c.duration) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func Countdown(out io.Writer, sleeper Sleeper, count int) { | ||||||
|  | 	for i := count; i > 0; i-- { | ||||||
|  | 		fmt.Fprint(out, i, "\n") | ||||||
|  | 		sleeper.Sleep() | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	fmt.Fprint(out, finalWord, "\n") | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func main() { | ||||||
|  | 	sleeper := &ConfigurableSleeper{1 * time.Second, time.Sleep} | ||||||
|  | 	Countdown(os.Stdout, sleeper, 3) | ||||||
|  | } | ||||||
| @ -0,0 +1,118 @@ | |||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"bytes" | ||||||
|  | 	"reflect" | ||||||
|  | 	"testing" | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | const write = "write" | ||||||
|  | const sleep = "sleep" | ||||||
|  | 
 | ||||||
|  | type SpyCountdownOperations struct { | ||||||
|  | 	Calls []string | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (s *SpyCountdownOperations) Sleep() { | ||||||
|  | 	s.Calls = append(s.Calls, sleep) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (s *SpyCountdownOperations) Write(p []byte) (n int, err error) { | ||||||
|  | 	s.Calls = append(s.Calls, write) | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestCountdownRuns(t *testing.T) { | ||||||
|  | 
 | ||||||
|  | 	t.Run("Prints 3 to Go!", func(t *testing.T) { | ||||||
|  | 		count := 3 | ||||||
|  | 		buffer := &bytes.Buffer{} | ||||||
|  | 		sleeper := &SpyCountdownOperations{} | ||||||
|  | 		Countdown(buffer, sleeper, count) | ||||||
|  | 
 | ||||||
|  | 		got := buffer.String() | ||||||
|  | 		// ` backtick allows strings with preserved newlines.
 | ||||||
|  | 		want := `3 | ||||||
|  | 2 | ||||||
|  | 1 | ||||||
|  | Go! | ||||||
|  | ` | ||||||
|  | 
 | ||||||
|  | 		if got != want { | ||||||
|  | 			t.Errorf("got %q want %q", got, want) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if len(sleeper.Calls) != 3 { | ||||||
|  | 			t.Errorf("Not enough calls to sleeper, want 3 got %q", sleeper.Calls) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	t.Run("Prints 4 to Go!", func(t *testing.T) { | ||||||
|  | 		count := 4 | ||||||
|  | 		buffer := &bytes.Buffer{} | ||||||
|  | 		sleeper := &SpyCountdownOperations{} | ||||||
|  | 		Countdown(buffer, sleeper, count) | ||||||
|  | 
 | ||||||
|  | 		got := buffer.String() | ||||||
|  | 		// ` backtick allows strings with preserved newlines.
 | ||||||
|  | 		want := `4 | ||||||
|  | 3 | ||||||
|  | 2 | ||||||
|  | 1 | ||||||
|  | Go! | ||||||
|  | ` | ||||||
|  | 
 | ||||||
|  | 		if got != want { | ||||||
|  | 			t.Errorf("got %q want %q", got, want) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if len(sleeper.Calls) != 4 { | ||||||
|  | 			t.Errorf("Not enough calls to sleeper, want 3 got %q", sleeper.Calls) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	t.Run("sleep before every print", func(t *testing.T) { | ||||||
|  | 		count := 3 | ||||||
|  | 		sleeper := &SpyCountdownOperations{} | ||||||
|  | 		Countdown(sleeper, sleeper, count) | ||||||
|  | 
 | ||||||
|  | 		want := []string{ | ||||||
|  | 			write, | ||||||
|  | 			sleep, | ||||||
|  | 			write, | ||||||
|  | 			sleep, | ||||||
|  | 			write, | ||||||
|  | 			sleep, | ||||||
|  | 			write, | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if !reflect.DeepEqual(want, sleeper.Calls) { | ||||||
|  | 			t.Errorf("wanted calls %v got %v", want, sleeper.Calls) | ||||||
|  | 
 | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type SpyTimeSleeper struct { | ||||||
|  | 	durationSlept time.Duration | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (s *SpyTimeSleeper) Sleep(duration time.Duration) { | ||||||
|  | 	s.durationSlept = duration | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestConfigurableSleeper(t *testing.T) { | ||||||
|  | 	sleepTime := 5 * time.Second | ||||||
|  | 	spyTime := &SpyTimeSleeper{} | ||||||
|  | 	sleeper := ConfigurableSleeper{sleepTime, spyTime.Sleep} | ||||||
|  | 	sleeper.Sleep() | ||||||
|  | 
 | ||||||
|  | 	if spyTime.durationSlept != sleepTime { | ||||||
|  | 		t.Errorf("should have slept for %v  but slept for %v", spyTime.durationSlept, sleepTime) | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | module git.runcible.io/androiddrew/mytablewriter | ||||||
|  | 
 | ||||||
|  | go 1.21.0 | ||||||
|  | 
 | ||||||
|  | require ( | ||||||
|  | 	github.com/mattn/go-runewidth v0.0.9 // indirect | ||||||
|  | 	github.com/olekukonko/tablewriter v0.0.5 // indirect | ||||||
|  | ) | ||||||
| @ -0,0 +1,4 @@ | |||||||
|  | github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= | ||||||
|  | github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= | ||||||
|  | github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= | ||||||
|  | github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= | ||||||
| @ -0,0 +1,20 @@ | |||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"os" | ||||||
|  | 
 | ||||||
|  | 	"github.com/olekukonko/tablewriter" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func main() { | ||||||
|  | 	data := [][]string{ | ||||||
|  | 		[]string{"Alfred", "15", "10/20", "(10.32, 56.21, 30.25)"}, | ||||||
|  | 		[]string{"Beelzebub", "30", "30/50", "(11.32, 57.21, 31.25)"}, | ||||||
|  | 		[]string{"Pokey", "19", "20/20", "(12.32, 54.21, 33.25)"}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	table := tablewriter.NewWriter(os.Stdout) | ||||||
|  | 	table.SetHeader([]string{"Name", "Speed", "Power", "Location"}) | ||||||
|  | 	table.AppendBulk(data) | ||||||
|  | 	table.Render() | ||||||
|  | } | ||||||
											
												Binary file not shown.
											
										
									
								
					Loading…
					
					
				
		Reference in New Issue