Last active
July 7, 2018 03:24
-
-
Save valeriofarias/d4d8c6f0f9b2351f3efa to your computer and use it in GitHub Desktop.
FizzBuzz with Unit Test written using GoLang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fizzbuzz | |
import "strconv" | |
func Fizzbuzz(n int) string { | |
var name string = "" | |
if n%3 == 0 { | |
name = "Fizz" | |
} | |
if n%5 == 0 { | |
name += "Buzz" | |
} | |
if name == "" { | |
name = strconv.Itoa(n) | |
} | |
return name | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fizzbuzz | |
import "testing" | |
func TestFizzbuzz(t *testing.T) { | |
cases := []struct { | |
in int | |
want string | |
}{ | |
{1, "1"}, | |
{2, "2"}, | |
{3, "Fizz"}, | |
{4, "4"}, | |
{5, "Buzz"}, | |
{15, "FizzBuzz"}, | |
{30, "FizzBuzz"}, | |
} | |
for _, tt := range cases { | |
actual := Fizzbuzz(tt.in) | |
if actual != tt.want { | |
t.Errorf("Fizzbuzz(%d): expected %q, actual %q", tt.in, tt.want, actual) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment