Skip to content

Commit

Permalink
Merge pull request #2 from Songmu/testing
Browse files Browse the repository at this point in the history
introduce teble driven tests
  • Loading branch information
Songmu committed Jun 17, 2017
2 parents 6d1adb3 + 52ece03 commit be535bc
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 84 deletions.
102 changes: 60 additions & 42 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,77 @@ func TestData2Map(t *testing.T) {
}
}

type ss struct {
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height *float64 `ltsv:"height"`
Weight float32
Memo string `ltsv:"-"`
}

func pfloat64(f float64) *float64 {
return &f
}

var decodeTests = []struct {
Name string
Input string
Output *ss
}{
{
Name: "Simple",
Input: "user:songmu\tage:36\theight:169.1\tweight:66.6",
Output: &ss{
User: "songmu",
Age: 36,
Height: pfloat64(169.1),
Weight: 66.6,
},
},
{
Name: "Default values",
Input: "user:songmu\tage:36",
Output: &ss{
User: "songmu",
Age: 36,
Height: nil,
Weight: 0.0,
},
},
{
Name: "Hyphen and empty string as null number",
Input: "user:songmu\tage:\theight:-",
Output: &ss{
User: "songmu",
Age: 0,
Height: nil,
Weight: 0.0,
},
},
}

func TestUnmarshal(t *testing.T) {
m := make(map[string]string)
Unmarshal([]byte("hoge: fuga\tpiyo: piyo"), &m)

expect := map[string]string{
"hoge": "fuga",
"piyo": "piyo",
}
if !reflect.DeepEqual(m, expect) {
t.Errorf("result of data2map not expected: %#v", m)
t.Errorf("unmarsharl error:\n out: %+v\n want: %+v", m, expect)
}

type ss struct {
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height *float64 `ltsv:"height"`
Weight float32
}
s := &ss{}
Unmarshal([]byte("user:songmu\tage:36\theight:169.1\tweight:66.6"), s)
hei := 169.1
expect2 := &ss{
User: "songmu",
Age: 36,
Height: &hei,
Weight: 66.6,
}
if !reflect.DeepEqual(s, expect2) {
t.Errorf("result of data2map not expected: %#v", s)
}
for _, tt := range decodeTests {
t.Logf("testing: %s\n", tt.Name)
s := &ss{}

s2 := &ss{}
Unmarshal([]byte("user:songmu\tage:36"), s2)
expect3 := &ss{
User: "songmu",
Age: 36,
// Height: nil,
Weight: 0.0,
}
if !reflect.DeepEqual(s2, expect3) {
t.Errorf("result of data2map not expected: %#v", s2)
}
err := Unmarshal([]byte(tt.Input), s)
if err != nil {
t.Errorf("%s(err): error should be nil but: %+v", tt.Name, err)
}

s3 := &ss{}
Unmarshal([]byte("user:songmu\tage:-\theight:-"), s3)
expect4 := &ss{
User: "songmu",
Age: 0,
// Height: nil,
Weight: 0.0,
}
if !reflect.DeepEqual(s3, expect4) {
t.Errorf("result of data2map not expected: %#v", s3)
if !reflect.DeepEqual(s, tt.Output) {
t.Errorf("%s:\n out: %+v\n want: %+v", tt.Name, s, tt.Output)
}
}

}
106 changes: 64 additions & 42 deletions encode_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,70 @@
package ltsv

import "testing"
import (
"fmt"
"testing"
)

func TestMarshal(t *testing.T) {
data := map[string]string{
"hoge": "fuga",
"piyo": "piyo",
}
expect1 := []byte("hoge:fuga\tpiyo:piyo")
expect2 := []byte("piyo:piyo\thoge:fuga")
r, _ := Marshal(data)
if string(r) != string(expect1) && string(r) != string(expect2) {
t.Errorf("result is not expected: %s", string(r))
}

type ss struct {
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height *float64 `ltsv:"height"`
Weight float32
Memo string `ltsv:"-"`
}
he := 169.1
s := &ss{
User: "songmu",
Age: 36,
Height: &he,
Weight: 66.6,
Memo: "songmu.jp",
}
expect := []byte("user:songmu\tage:36\theight:169.1\tweight:66.6")
buf, _ := Marshal(s)
if string(buf) != string(expect) {
t.Errorf("result is not expected: %s", string(buf))
}
var encodeTests = []struct {
Name string
Input interface{}
Output string
Check func(s string) error
}{
{
Name: "map",
Input: map[string]string{
"hoge": "fuga",
"piyo": "piyo",
},
Check: func(s string) error {
expect1 := "hoge:fuga\tpiyo:piyo"
expect2 := "piyo:piyo\thoge:fuga"
if s != expect1 && s != expect2 {
return fmt.Errorf("result is not expected: %s", s)
}
return nil
},
},
{
Name: "Simple",
Input: &ss{
User: "songmu",
Age: 36,
Height: pfloat64(169.1),
Weight: 66.6,
Memo: "songmu.jp",
},
Output: "user:songmu\tage:36\theight:169.1\tweight:66.6",
},
{
Name: "Omit memo",
Input: &ss{
User: "songmu",
Age: 36,
Memo: "songmu.jp",
},
Output: "user:songmu\tage:36\tweight:0",
},
}

s = &ss{
User: "songmu",
Age: 36,
Memo: "songmu.jp",
}
expect = []byte("user:songmu\tage:36\tweight:0")
buf, _ = Marshal(s)
if string(buf) != string(expect) {
t.Errorf("result is not expected: %s", string(buf))
func TestMarshal(t *testing.T) {
for _, tt := range encodeTests {
t.Logf("testing: %s\n", tt.Name)
buf, err := Marshal(tt.Input)
if err != nil {
t.Errorf("%s(err): error should be nil but: %+v", tt.Name, err)
}
s := string(buf)
if tt.Check != nil {
err := tt.Check(s)
if err != nil {
t.Errorf("%s: %s", tt.Name, err)
}
} else {
if s != tt.Output {
t.Errorf("%s: out=%s, want=%s", tt.Name, s, tt.Output)
}
}
}
}

0 comments on commit be535bc

Please sign in to comment.