52 lines
971 B
Go
52 lines
971 B
Go
package content
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"lukechampine.com/frand"
|
|
|
|
"github.com/mleku/transit/chk"
|
|
"github.com/mleku/transit/log"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
var err error
|
|
var b []byte
|
|
content := `this is some plain text
|
|
|
|
with a newline`
|
|
c := New(content, false)
|
|
log.I.F("%s", c.Val)
|
|
if b, err = json.Marshal(&c); chk.E(err) {
|
|
t.Fatal(err)
|
|
}
|
|
// log.I.F("%s", b)
|
|
c2 := &C{}
|
|
if err = json.Unmarshal(b, c2); chk.E(err) {
|
|
t.Fatal(err)
|
|
}
|
|
log.I.F("%s", c2.Val)
|
|
if !bytes.Equal(c.Val, c2.Val) {
|
|
t.Fatal("failed to encode/decode")
|
|
}
|
|
bin := make([]byte, frand.Intn(100)+16)
|
|
if _, err = frand.Read(bin); chk.E(err) {
|
|
t.Fatal(err)
|
|
}
|
|
bin1 := New(bin, true)
|
|
log.I.S(bin1)
|
|
if b, err = json.Marshal(bin1); chk.E(err) {
|
|
t.Fatal(err)
|
|
}
|
|
bin2 := &C{}
|
|
if err = json.Unmarshal(b, bin2); chk.E(err) {
|
|
t.Fatal(err)
|
|
}
|
|
log.I.S(bin1.Val, bin2.Val)
|
|
if !bytes.Equal(bin1.Val, bin2.Val) {
|
|
t.Fatal("failed to encode/decode")
|
|
}
|
|
}
|