Renamed BlockDigest's ToString() to String() to implement fmt.Stringer() interface...
[arvados.git] / sdk / go / blockdigest / blockdigest_test.go
1 package blockdigest
2
3 import (
4         "fmt"
5         "strings"
6         "testing"
7 )
8
9 func expectValidDigestString(t *testing.T, s string) {
10         bd, err := FromString(s)
11         if err != nil {
12                 t.Fatalf("Expected %s to produce a valid BlockDigest but instead got error: %v", s, err)
13         }
14
15         expected := strings.ToLower(s)
16                 
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())
19         }
20 }
21
22 func expectInvalidDigestString(t *testing.T, s string) {
23         _, err := FromString(s)
24         if err == nil {
25                 t.Fatalf("Expected %s to be an invalid BlockDigest, but did not receive an error", s)
26         }
27 }
28
29 func TestValidDigestStrings(t *testing.T) {
30         expectValidDigestString(t, "01234567890123456789abcdefabcdef")
31         expectValidDigestString(t, "01234567890123456789ABCDEFABCDEF")
32         expectValidDigestString(t, "01234567890123456789AbCdEfaBcDeF")
33 }
34
35 func TestInvalidDigestStrings(t *testing.T) {
36         expectInvalidDigestString(t, "01234567890123456789abcdefabcdeg")
37         expectInvalidDigestString(t, "01234567890123456789abcdefabcde")
38         expectInvalidDigestString(t, "01234567890123456789abcdefabcdefa")
39         expectInvalidDigestString(t, "g1234567890123456789abcdefabcdef")
40 }
41
42 func TestBlockDigestWorksAsMapKey(t *testing.T) {
43         m := make(map[BlockDigest]int)
44         bd := AssertFromString("01234567890123456789abcdefabcdef")
45         m[bd] = 5
46 }
47
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)
55         }
56 }
57
58 func TestBlockDigestGetsPrettyPrintedByPrintfInNestedStructs(t *testing.T) {
59         input := "01234567890123456789abcdefabcdef"
60         value := 42
61         nested := struct{
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
65                 // different.
66                 Digest BlockDigest
67                 value int
68         }{
69                 AssertFromString(input),
70                 value,
71         }
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)
78         }
79 }