|
| 1 | +// Copyright (c) 2018 Palantir Technologies. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package binary_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "github.com/palantir/pkg/binary" |
| 14 | +) |
| 15 | + |
| 16 | +func TestBinary_Marshal(t *testing.T) { |
| 17 | + for _, test := range []struct { |
| 18 | + Name string |
| 19 | + Input []byte |
| 20 | + Output []byte |
| 21 | + }{ |
| 22 | + { |
| 23 | + Name: "hello world", |
| 24 | + Input: []byte(`hello world`), |
| 25 | + Output: []byte(`"aGVsbG8gd29ybGQ="`), |
| 26 | + }, |
| 27 | + } { |
| 28 | + t.Run(test.Name, func(t *testing.T) { |
| 29 | + out, err := json.Marshal(binary.New(test.Input)) |
| 30 | + assert.NoError(t, err) |
| 31 | + assert.Equal(t, string(test.Output), string(out)) |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestBinary_Unmarshal(t *testing.T) { |
| 37 | + for _, test := range []struct { |
| 38 | + Name string |
| 39 | + Input []byte |
| 40 | + Output []byte |
| 41 | + }{ |
| 42 | + { |
| 43 | + Name: "hello world", |
| 44 | + Input: []byte(`"aGVsbG8gd29ybGQ="`), |
| 45 | + Output: []byte(`hello world`), |
| 46 | + }, |
| 47 | + } { |
| 48 | + t.Run(test.Name, func(t *testing.T) { |
| 49 | + var bin binary.Binary |
| 50 | + err := json.Unmarshal(test.Input, &bin) |
| 51 | + assert.NoError(t, err) |
| 52 | + bytes, err := bin.Bytes() |
| 53 | + assert.NoError(t, err) |
| 54 | + assert.Equal(t, string(test.Output), string(bytes)) |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
0 commit comments