Created
September 18, 2023 09:54
-
-
Save tqmvt/c14cc321b1d770299ba069b93807a7c8 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