-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from ikawaha/develop
Release candidate
- Loading branch information
Showing
22 changed files
with
289 additions
and
47 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dict | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
"errors" | ||
"io" | ||
) | ||
|
||
// Info represents the dictionary info. | ||
type Info struct { | ||
Name string | ||
Src string | ||
} | ||
|
||
// ReadDictInfo reads gob encoded dictionary info and returns it. | ||
// | ||
// For backward compatibility, if a dictionary name is not defined or empty, it | ||
// returns UndefinedDictName. | ||
func ReadDictInfo(r io.Reader) *Info { | ||
if r == nil { | ||
return nil | ||
} | ||
var name string | ||
dec := gob.NewDecoder(r) | ||
if err := dec.Decode(&name); err != nil { | ||
return nil | ||
} | ||
var src string | ||
if err := dec.Decode(&src); err != nil { | ||
return nil | ||
} | ||
return &Info{Name: name, Src: src} | ||
} | ||
|
||
// WriteTo implements the io.WriteTo interface. | ||
func (d Info) WriteTo(w io.Writer) (n int64, err error) { | ||
if w == nil { | ||
return 0, errors.New("given writer is nil") | ||
} | ||
var b bytes.Buffer | ||
enc := gob.NewEncoder(&b) | ||
if err := enc.Encode(d.Name); err != nil { | ||
return 0, err | ||
} | ||
if err := enc.Encode(d.Src); err != nil { | ||
return 0, err | ||
} | ||
return b.WriteTo(w) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package dict | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestDictName_golden(t *testing.T) { | ||
in := Info{Name: "test_dict"} | ||
|
||
// Get gob encoded dictionary name. | ||
var gobName bytes.Buffer | ||
if _, err := in.WriteTo(&gobName); err != nil { | ||
t.Errorf("failed to get encoded name data: %v", err) | ||
} | ||
|
||
// Decode gob encoded dictionary name. | ||
out := ReadDictInfo(&gobName) | ||
|
||
// Assert be equal. | ||
if in.Name != out.Name { | ||
t.Errorf("want %v, got %v", in, out) | ||
} | ||
} | ||
|
||
func TestDictName_bad_input(t *testing.T) { | ||
t.Run("empty name", func(t *testing.T) { | ||
in := Info{Name: ""} | ||
|
||
// Get gob encoded dictionary name. | ||
var gobName bytes.Buffer | ||
if _, err := in.WriteTo(&gobName); err != nil { | ||
t.Errorf("failed to encode dict name: %v", err) | ||
} | ||
|
||
// Decode gob encoded dictionary name. | ||
got := ReadDictInfo(&gobName) | ||
if got.Name != "" { | ||
t.Errorf("empty name should return empty name. got %v", got) | ||
} | ||
}) | ||
|
||
t.Run("nil input", func(t *testing.T) { | ||
// Nil input shuold return default name. | ||
got := ReadDictInfo(nil) | ||
if got != nil { | ||
t.Errorf("nil input should return nil. got %v", got) | ||
} | ||
}) | ||
|
||
t.Run("bad gob data", func(t *testing.T) { | ||
// Bad gob data should return default name. | ||
got := ReadDictInfo(bytes.NewReader([]byte{0x00})) | ||
if got != nil { | ||
t.Errorf("bad gob data should return nil. got %v", got) | ||
} | ||
}) | ||
} | ||
|
||
func TestDictName_WriteTo(t *testing.T) { | ||
in := Info{Name: "test_dict"} | ||
|
||
// Nil writer should return error. | ||
_, err := in.WriteTo(nil) | ||
|
||
// Assert error. | ||
if err == nil { | ||
t.Error("nil writer should return error") | ||
} | ||
// Assert error message. | ||
want := "given writer is nil" | ||
if want != err.Error() { | ||
t.Errorf("want %v, got %v", want, err.Error()) | ||
} | ||
} |
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
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
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
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
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
Binary file not shown.
Binary file not shown.
Oops, something went wrong.