I'm new to go and started to play around with testing. My method returns a []byte with a md5 hash in it.
func myHash(s string) []byte {
h := md5.New()
io.WriteString(h, s)
return h.Sum(nil)
}
It's working and the hashes look ok, but when I'm testing it with this method:
func TestMyHash(t *testing.T) {
s := "linux"
bf := ("e206a54e97690cce50cc872dd70ee896")
x := hashor(s)
if !bytes.Equal(x, []byte(bf)) {
t.Errorf("myHash ...")
}
}
It will always fail. First I thought it could be some issue with the casting of a string to []byte or vice versa, but after trying ot over and over again I just need to ask here.
Can you give me an example how to test my function? Do I miss something necessary?
Thanks in advance.
You are probably comparing the raw bytes of the hash with the hexadecimal formatted version of a hash. You might want to do something like this:
got := fmt.Sprintf("%034x", myHash("linux"))
want := "00e206a54e97690cce50cc872dd70ee896"
if got != want {
t.Errorf("got %q, want %q", got, want)
}