...

Source file src/github.com/pelletier/go-toml/v2/localtime_test.go

Documentation: github.com/pelletier/go-toml/v2

     1  package toml_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/pelletier/go-toml/v2"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestLocalDate_AsTime(t *testing.T) {
    12  	d := toml.LocalDate{2021, 6, 8}
    13  	cast := d.AsTime(time.UTC)
    14  	require.Equal(t, time.Date(2021, time.June, 8, 0, 0, 0, 0, time.UTC), cast)
    15  }
    16  
    17  func TestLocalDate_String(t *testing.T) {
    18  	d := toml.LocalDate{2021, 6, 8}
    19  	require.Equal(t, "2021-06-08", d.String())
    20  }
    21  
    22  func TestLocalDate_MarshalText(t *testing.T) {
    23  	d := toml.LocalDate{2021, 6, 8}
    24  	b, err := d.MarshalText()
    25  	require.NoError(t, err)
    26  	require.Equal(t, []byte("2021-06-08"), b)
    27  }
    28  
    29  func TestLocalDate_UnmarshalMarshalText(t *testing.T) {
    30  	d := toml.LocalDate{}
    31  	err := d.UnmarshalText([]byte("2021-06-08"))
    32  	require.NoError(t, err)
    33  	require.Equal(t, toml.LocalDate{2021, 6, 8}, d)
    34  
    35  	err = d.UnmarshalText([]byte("what"))
    36  	require.Error(t, err)
    37  }
    38  
    39  func TestLocalTime_String(t *testing.T) {
    40  	d := toml.LocalTime{20, 12, 1, 2, 9}
    41  	require.Equal(t, "20:12:01.000000002", d.String())
    42  	d = toml.LocalTime{20, 12, 1, 0, 0}
    43  	require.Equal(t, "20:12:01", d.String())
    44  	d = toml.LocalTime{20, 12, 1, 0, 9}
    45  	require.Equal(t, "20:12:01.000000000", d.String())
    46  	d = toml.LocalTime{20, 12, 1, 100, 0}
    47  	require.Equal(t, "20:12:01.0000001", d.String())
    48  }
    49  
    50  func TestLocalTime_MarshalText(t *testing.T) {
    51  	d := toml.LocalTime{20, 12, 1, 2, 9}
    52  	b, err := d.MarshalText()
    53  	require.NoError(t, err)
    54  	require.Equal(t, []byte("20:12:01.000000002"), b)
    55  }
    56  
    57  func TestLocalTime_UnmarshalMarshalText(t *testing.T) {
    58  	d := toml.LocalTime{}
    59  	err := d.UnmarshalText([]byte("20:12:01.000000002"))
    60  	require.NoError(t, err)
    61  	require.Equal(t, toml.LocalTime{20, 12, 1, 2, 9}, d)
    62  
    63  	err = d.UnmarshalText([]byte("what"))
    64  	require.Error(t, err)
    65  
    66  	err = d.UnmarshalText([]byte("20:12:01.000000002 bad"))
    67  	require.Error(t, err)
    68  }
    69  
    70  func TestLocalTime_RoundTrip(t *testing.T) {
    71  	var d struct{ A toml.LocalTime }
    72  	err := toml.Unmarshal([]byte("a=20:12:01.500"), &d)
    73  	require.NoError(t, err)
    74  	require.Equal(t, "20:12:01.500", d.A.String())
    75  }
    76  
    77  func TestLocalDateTime_AsTime(t *testing.T) {
    78  	d := toml.LocalDateTime{
    79  		toml.LocalDate{2021, 6, 8},
    80  		toml.LocalTime{20, 12, 1, 2, 9},
    81  	}
    82  	cast := d.AsTime(time.UTC)
    83  	require.Equal(t, time.Date(2021, time.June, 8, 20, 12, 1, 2, time.UTC), cast)
    84  }
    85  
    86  func TestLocalDateTime_String(t *testing.T) {
    87  	d := toml.LocalDateTime{
    88  		toml.LocalDate{2021, 6, 8},
    89  		toml.LocalTime{20, 12, 1, 2, 9},
    90  	}
    91  	require.Equal(t, "2021-06-08T20:12:01.000000002", d.String())
    92  }
    93  
    94  func TestLocalDateTime_MarshalText(t *testing.T) {
    95  	d := toml.LocalDateTime{
    96  		toml.LocalDate{2021, 6, 8},
    97  		toml.LocalTime{20, 12, 1, 2, 9},
    98  	}
    99  	b, err := d.MarshalText()
   100  	require.NoError(t, err)
   101  	require.Equal(t, []byte("2021-06-08T20:12:01.000000002"), b)
   102  }
   103  
   104  func TestLocalDateTime_UnmarshalMarshalText(t *testing.T) {
   105  	d := toml.LocalDateTime{}
   106  	err := d.UnmarshalText([]byte("2021-06-08 20:12:01.000000002"))
   107  	require.NoError(t, err)
   108  	require.Equal(t, toml.LocalDateTime{
   109  		toml.LocalDate{2021, 6, 8},
   110  		toml.LocalTime{20, 12, 1, 2, 9},
   111  	}, d)
   112  
   113  	err = d.UnmarshalText([]byte("what"))
   114  	require.Error(t, err)
   115  
   116  	err = d.UnmarshalText([]byte("2021-06-08 20:12:01.000000002 bad"))
   117  	require.Error(t, err)
   118  }
   119  

View as plain text