Skip to content

Commit eaf0d98

Browse files
committed
add test
1 parent d138d7a commit eaf0d98

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

encode_test.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math"
88
"net"
99
"os"
10+
"strconv"
1011
"strings"
1112
"testing"
1213
"time"
@@ -399,11 +400,13 @@ type (
399400
food struct{ F []string }
400401
fun func()
401402
cplx complex128
403+
ints []int
402404

403405
sound2 struct{ S string }
404406
food2 struct{ F []string }
405407
fun2 func()
406408
cplx2 complex128
409+
ints2 []int
407410
)
408411

409412
// This is intentionally wrong (pointer receiver)
@@ -415,6 +418,26 @@ func (c cplx) MarshalText() ([]byte, error) {
415418
return []byte(fmt.Sprintf("(%f+%fi)", real(cplx), imag(cplx))), nil
416419
}
417420

421+
func intsValue(is []int) []byte {
422+
var buf bytes.Buffer
423+
buf.WriteByte('<')
424+
for i, v := range is {
425+
if i > 0 {
426+
buf.WriteByte(',')
427+
}
428+
buf.WriteString(strconv.Itoa(v))
429+
}
430+
buf.WriteByte('>')
431+
return buf.Bytes()
432+
}
433+
434+
func (is *ints) MarshalText() ([]byte, error) {
435+
if is == nil {
436+
return []byte("[]"), nil
437+
}
438+
return intsValue(*is), nil
439+
}
440+
418441
func (s *sound2) MarshalTOML() ([]byte, error) { return []byte("\"" + s.S + "\""), nil }
419442
func (f food2) MarshalTOML() ([]byte, error) {
420443
return []byte("[\"" + strings.Join(f.F, "\", \"") + "\"]"), nil
@@ -424,6 +447,13 @@ func (c cplx2) MarshalTOML() ([]byte, error) {
424447
cplx := complex128(c)
425448
return []byte(fmt.Sprintf("\"(%f+%fi)\"", real(cplx), imag(cplx))), nil
426449
}
450+
func (is *ints2) MarshalTOML() ([]byte, error) {
451+
// MarshalTOML must quote by self
452+
if is == nil {
453+
return []byte(`"[]"`), nil
454+
}
455+
return []byte(fmt.Sprintf(`"%s"`, intsValue(*is))), nil
456+
}
427457

428458
func TestEncodeTextMarshaler(t *testing.T) {
429459
x := struct {
@@ -435,6 +465,8 @@ func TestEncodeTextMarshaler(t *testing.T) {
435465
Food2 *food
436466
Complex cplx
437467
Fun fun
468+
Ints ints
469+
Ints2 *ints2
438470
}{
439471
Name: "Goblok",
440472
Sound: sound{"miauw"},
@@ -447,26 +479,28 @@ func TestEncodeTextMarshaler(t *testing.T) {
447479
Food2: &food{[]string{"chicken", "fish"}},
448480
Complex: complex(42, 666),
449481
Fun: func() { panic("x") },
482+
Ints: ints{1, 2, 3, 4},
483+
Ints2: &ints2{1, 2, 3, 4},
450484
}
451485

452486
var buf bytes.Buffer
453-
if err := NewEncoder(&buf).Encode(x); err != nil {
487+
if err := NewEncoder(&buf).Encode(&x); err != nil {
454488
t.Fatal(err)
455489
}
456490

457491
want := `Name = "Goblok"
492+
Sound = "miauw"
458493
Sound2 = "miauw"
459494
Food = "chicken, fish"
460495
Food2 = "chicken, fish"
461496
Complex = "(42.000000+666.000000i)"
462497
Fun = "why would you do this?"
498+
Ints = "<1,2,3,4>"
499+
Ints2 = "<1,2,3,4>"
463500
464501
[Labels]
465502
color = "black"
466503
type = "cat"
467-
468-
[Sound]
469-
S = "miauw"
470504
`
471505

472506
if buf.String() != want {

0 commit comments

Comments
 (0)