Renamed BlockDigest's ToString() to String() to implement fmt.Stringer() interface...
authormishaz <misha@curoverse.com>
Tue, 27 Jan 2015 01:06:21 +0000 (01:06 +0000)
committerTom Clegg <tom@curoverse.com>
Fri, 13 Feb 2015 21:25:31 +0000 (16:25 -0500)
sdk/go/blockdigest/blockdigest.go
sdk/go/blockdigest/blockdigest_test.go

index 0742839720c764788ccae6e505d37640e91655e8..9b818d365303ac6805c15be534400a0c3c854448 100644 (file)
@@ -15,7 +15,7 @@ type BlockDigest struct {
        l uint64
 }
 
-func (d *BlockDigest) ToString() (s string) {
+func (d BlockDigest) String() string {
        return fmt.Sprintf("%016x%016x", d.h, d.l)
 }
 
index 9081bb846c5f023928ab57bfbd52eaeafb14ad8a..068a1385aef5e4f714e5282235da621fb4e970b4 100644 (file)
@@ -1,6 +1,7 @@
 package blockdigest
 
 import (
+       "fmt"
        "strings"
        "testing"
 )
@@ -13,8 +14,8 @@ func expectValidDigestString(t *testing.T, s string) {
 
        expected := strings.ToLower(s)
                
-       if expected != bd.ToString() {
-               t.Fatalf("Expected %s to be returned by FromString(%s).ToString() but instead we received %s", expected, s, bd.ToString())
+       if expected != bd.String() {
+               t.Fatalf("Expected %s to be returned by FromString(%s).String() but instead we received %s", expected, s, bd.String())
        }
 }
 
@@ -43,3 +44,36 @@ func TestBlockDigestWorksAsMapKey(t *testing.T) {
        bd := AssertFromString("01234567890123456789abcdefabcdef")
        m[bd] = 5
 }
+
+func TestBlockDigestGetsPrettyPrintedByPrintf(t *testing.T) {
+       input := "01234567890123456789abcdefabcdef"
+       prettyPrinted := fmt.Sprintf("%v", AssertFromString(input))
+       if prettyPrinted != input {
+               t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as " +
+                       "\"%s\", but instead it was printed as %s",
+                       input, input, prettyPrinted)
+       }
+}
+
+func TestBlockDigestGetsPrettyPrintedByPrintfInNestedStructs(t *testing.T) {
+       input := "01234567890123456789abcdefabcdef"
+       value := 42
+       nested := struct{
+               // Fun trivia fact: If this field was called "digest" instead of
+               // "Digest", then it would not be exported and String() would
+               // never get called on it and our output would look very
+               // different.
+               Digest BlockDigest
+               value int
+       }{
+               AssertFromString(input),
+               value,
+       }
+       prettyPrinted := fmt.Sprintf("%+v", nested)
+       expected := fmt.Sprintf("{Digest:%s value:%d}", input, value)
+       if prettyPrinted != expected {
+               t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as " +
+                       "\"%s\", but instead it was printed as %s",
+                       input, expected, prettyPrinted)
+       }
+}