014204cfb7
As recommended, an `internal/assert` package was added with a reduced set of assertions. All tests were then refactored to use the internal assertions. When more complex assertions were used, they have been rewritten using logic and the simplified assertions. Fancy formatting for failures was omitted. The `internal/assert/assertions.diff` function could be overwritten for better formatting. That is where diff libraries are used in other test suites. Refs: #872 Co-authored-by: Alex Mikitik <alex.mikitik@oracle.com>
119 lines
3.1 KiB
Go
119 lines
3.1 KiB
Go
package toml_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
"github.com/pelletier/go-toml/v2/internal/assert"
|
|
)
|
|
|
|
func TestLocalDate_AsTime(t *testing.T) {
|
|
d := toml.LocalDate{2021, 6, 8}
|
|
cast := d.AsTime(time.UTC)
|
|
assert.Equal(t, time.Date(2021, time.June, 8, 0, 0, 0, 0, time.UTC), cast)
|
|
}
|
|
|
|
func TestLocalDate_String(t *testing.T) {
|
|
d := toml.LocalDate{2021, 6, 8}
|
|
assert.Equal(t, "2021-06-08", d.String())
|
|
}
|
|
|
|
func TestLocalDate_MarshalText(t *testing.T) {
|
|
d := toml.LocalDate{2021, 6, 8}
|
|
b, err := d.MarshalText()
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, []byte("2021-06-08"), b)
|
|
}
|
|
|
|
func TestLocalDate_UnmarshalMarshalText(t *testing.T) {
|
|
d := toml.LocalDate{}
|
|
err := d.UnmarshalText([]byte("2021-06-08"))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, toml.LocalDate{2021, 6, 8}, d)
|
|
|
|
err = d.UnmarshalText([]byte("what"))
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestLocalTime_String(t *testing.T) {
|
|
d := toml.LocalTime{20, 12, 1, 2, 9}
|
|
assert.Equal(t, "20:12:01.000000002", d.String())
|
|
d = toml.LocalTime{20, 12, 1, 0, 0}
|
|
assert.Equal(t, "20:12:01", d.String())
|
|
d = toml.LocalTime{20, 12, 1, 0, 9}
|
|
assert.Equal(t, "20:12:01.000000000", d.String())
|
|
d = toml.LocalTime{20, 12, 1, 100, 0}
|
|
assert.Equal(t, "20:12:01.0000001", d.String())
|
|
}
|
|
|
|
func TestLocalTime_MarshalText(t *testing.T) {
|
|
d := toml.LocalTime{20, 12, 1, 2, 9}
|
|
b, err := d.MarshalText()
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, []byte("20:12:01.000000002"), b)
|
|
}
|
|
|
|
func TestLocalTime_UnmarshalMarshalText(t *testing.T) {
|
|
d := toml.LocalTime{}
|
|
err := d.UnmarshalText([]byte("20:12:01.000000002"))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, toml.LocalTime{20, 12, 1, 2, 9}, d)
|
|
|
|
err = d.UnmarshalText([]byte("what"))
|
|
assert.Error(t, err)
|
|
|
|
err = d.UnmarshalText([]byte("20:12:01.000000002 bad"))
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestLocalTime_RoundTrip(t *testing.T) {
|
|
var d struct{ A toml.LocalTime }
|
|
err := toml.Unmarshal([]byte("a=20:12:01.500"), &d)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "20:12:01.500", d.A.String())
|
|
}
|
|
|
|
func TestLocalDateTime_AsTime(t *testing.T) {
|
|
d := toml.LocalDateTime{
|
|
toml.LocalDate{2021, 6, 8},
|
|
toml.LocalTime{20, 12, 1, 2, 9},
|
|
}
|
|
cast := d.AsTime(time.UTC)
|
|
assert.Equal(t, time.Date(2021, time.June, 8, 20, 12, 1, 2, time.UTC), cast)
|
|
}
|
|
|
|
func TestLocalDateTime_String(t *testing.T) {
|
|
d := toml.LocalDateTime{
|
|
toml.LocalDate{2021, 6, 8},
|
|
toml.LocalTime{20, 12, 1, 2, 9},
|
|
}
|
|
assert.Equal(t, "2021-06-08T20:12:01.000000002", d.String())
|
|
}
|
|
|
|
func TestLocalDateTime_MarshalText(t *testing.T) {
|
|
d := toml.LocalDateTime{
|
|
toml.LocalDate{2021, 6, 8},
|
|
toml.LocalTime{20, 12, 1, 2, 9},
|
|
}
|
|
b, err := d.MarshalText()
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, []byte("2021-06-08T20:12:01.000000002"), b)
|
|
}
|
|
|
|
func TestLocalDateTime_UnmarshalMarshalText(t *testing.T) {
|
|
d := toml.LocalDateTime{}
|
|
err := d.UnmarshalText([]byte("2021-06-08 20:12:01.000000002"))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, toml.LocalDateTime{
|
|
toml.LocalDate{2021, 6, 8},
|
|
toml.LocalTime{20, 12, 1, 2, 9},
|
|
}, d)
|
|
|
|
err = d.UnmarshalText([]byte("what"))
|
|
assert.Error(t, err)
|
|
|
|
err = d.UnmarshalText([]byte("2021-06-08 20:12:01.000000002 bad"))
|
|
assert.Error(t, err)
|
|
}
|