Skip to content

Instantly share code, notes, and snippets.

@maykonchagas
Last active November 23, 2020 18:18
Show Gist options
  • Save maykonchagas/50ebf215fd9fde3bbb95f3c2ea9f0bbb to your computer and use it in GitHub Desktop.
Save maykonchagas/50ebf215fd9fde3bbb95f3c2ea9f0bbb to your computer and use it in GitHub Desktop.
// This is a "stub" file. It's a little start on your solution.
// It's not a complete solution though; you have to write some code.
// Package twofer should have a package comment that summarizes what it's about.
// https://golang.org/doc/effective_go.html#commentary
package twofer
import "fmt"
// ShareWith should have a comment documenting it.
func ShareWith(name string) string {
// Write some code here to pass the test suite.
// Then remove all the stock comments.
// They're here to help you get started but they only clutter a finished solution.
// If you leave them in, reviewers may protest!
h := ""
if h == "Maykon" {
fmt.Printf("One for %v, one for me.", h)
} else {
fmt.Println("One for you, one for me.")
}
return ""
}
### two_for_test.go
package twofer
import "testing"
// Define a function ShareWith(string) string.
var tests = []struct {
name, expected string
}{
{"", "One for you, one for me."},
{"Alice", "One for Alice, one for me."},
{"Bob", "One for Bob, one for me."},
}
func TestShareWith(t *testing.T) {
for _, test := range tests {
if observed := ShareWith(test.name); observed != test.expected {
t.Fatalf("ShareWith(%s) = \"%v\", want \"%v\"", test.name, observed, test.expected)
}
}
}
func BenchmarkShareWith(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range tests {
ShareWith(test.name)
}
}
}
@benmezger
Copy link

package main

import (
	"fmt"
	"testing"
)

func ShareWith(name string) string {
	// Write some code here to pass the test suite.
	// Then remove all the stock comments.
	// They're here to help you get started but they only clutter a finished solution.
	// If you leave them in, reviewers may protest!

	if name != "" {
		return fmt.Sprintf("One for %v, one for me.", name)
	}
	return fmt.Sprintf("One for you, one for me.")
}

var tests = []struct {
	name, expected string
}{
	{"", "One for you, one for me."},
	{"Alice", "One for Alice, one for me."},
	{"Bob", "One for Bob, one for me."},
}

func TestShareWith(t *testing.T) {
	for _, test := range tests {
		if observed := ShareWith(test.name); observed != test.expected {
			t.Fatalf("ShareWith(%s) = \"%v\", want \"%v\"", test.name, observed, test.expected)
		}
	}
}

func BenchmarkShareWith(b *testing.B) {
	for i := 0; i < b.N; i++ {

		for _, test := range tests {
			ShareWith(test.name)
		}

	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment