9 func expectValidDigestString(t *testing.T, s string) {
10 bd, err := FromString(s)
12 t.Fatalf("Expected %s to produce a valid BlockDigest but instead got error: %v", s, err)
15 expected := strings.ToLower(s)
17 if expected != bd.String() {
18 t.Fatalf("Expected %s to be returned by FromString(%s).String() but instead we received %s", expected, s, bd.String())
22 func expectInvalidDigestString(t *testing.T, s string) {
23 _, err := FromString(s)
25 t.Fatalf("Expected %s to be an invalid BlockDigest, but did not receive an error", s)
29 func TestValidDigestStrings(t *testing.T) {
30 expectValidDigestString(t, "01234567890123456789abcdefabcdef")
31 expectValidDigestString(t, "01234567890123456789ABCDEFABCDEF")
32 expectValidDigestString(t, "01234567890123456789AbCdEfaBcDeF")
35 func TestInvalidDigestStrings(t *testing.T) {
36 expectInvalidDigestString(t, "01234567890123456789abcdefabcdeg")
37 expectInvalidDigestString(t, "01234567890123456789abcdefabcde")
38 expectInvalidDigestString(t, "01234567890123456789abcdefabcdefa")
39 expectInvalidDigestString(t, "g1234567890123456789abcdefabcdef")
42 func TestBlockDigestWorksAsMapKey(t *testing.T) {
43 m := make(map[BlockDigest]int)
44 bd := AssertFromString("01234567890123456789abcdefabcdef")
48 func TestBlockDigestGetsPrettyPrintedByPrintf(t *testing.T) {
49 input := "01234567890123456789abcdefabcdef"
50 prettyPrinted := fmt.Sprintf("%v", AssertFromString(input))
51 if prettyPrinted != input {
52 t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as " +
53 "\"%s\", but instead it was printed as %s",
54 input, input, prettyPrinted)
58 func TestBlockDigestGetsPrettyPrintedByPrintfInNestedStructs(t *testing.T) {
59 input := "01234567890123456789abcdefabcdef"
62 // Fun trivia fact: If this field was called "digest" instead of
63 // "Digest", then it would not be exported and String() would
64 // never get called on it and our output would look very
69 AssertFromString(input),
72 prettyPrinted := fmt.Sprintf("%+v", nested)
73 expected := fmt.Sprintf("{Digest:%s value:%d}", input, value)
74 if prettyPrinted != expected {
75 t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as " +
76 "\"%s\", but instead it was printed as %s",
77 input, expected, prettyPrinted)