github.com/diamondburned/arikawa/v2@v2.1.0/utils/json/enum/enum_test.go (about)

     1  package enum
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestInt8ToJSON(t *testing.T) {
     9  	testCases := []struct {
    10  		name   string
    11  		src    Enum
    12  		expect []byte
    13  	}{
    14  		{
    15  			name:   "null",
    16  			src:    Null,
    17  			expect: []byte("null"),
    18  		},
    19  		{
    20  			name:   "value",
    21  			src:    12,
    22  			expect: []byte("12"),
    23  		},
    24  	}
    25  
    26  	for _, c := range testCases {
    27  		t.Run(c.name, func(t *testing.T) {
    28  			actual := ToJSON(c.src)
    29  
    30  			if !reflect.DeepEqual(actual, c.expect) {
    31  				t.Errorf("expected nullable.Int8ToJSON to return: %+v, but got: %+v", c.expect, actual)
    32  			}
    33  		})
    34  	}
    35  }
    36  
    37  func TestInt8FromJSON(t *testing.T) {
    38  	testCases := []struct {
    39  		name   string
    40  		src    []byte
    41  		expect Enum
    42  		err    bool
    43  	}{
    44  		{
    45  			name:   "null",
    46  			src:    []byte("null"),
    47  			expect: Null,
    48  			err:    false,
    49  		},
    50  		{
    51  			name:   "value",
    52  			src:    []byte("12"),
    53  			expect: 12,
    54  			err:    false,
    55  		},
    56  		{
    57  			name:   "invalid input",
    58  			src:    []byte("NaN"),
    59  			expect: 0,
    60  			err:    true,
    61  		},
    62  	}
    63  
    64  	for _, c := range testCases {
    65  		t.Run(c.name, func(t *testing.T) {
    66  			actual, err := FromJSON(c.src)
    67  
    68  			if c.err {
    69  				if err == nil {
    70  					t.Error("expected nullable.Int8FromJSON to return an error, but it did not")
    71  				}
    72  			} else {
    73  				if !reflect.DeepEqual(actual, c.expect) {
    74  					t.Errorf("expected nullable.Int8FromJSON to return: %+v, but got: %+v", c.expect, actual)
    75  				}
    76  
    77  				if err != nil {
    78  					t.Errorf("nullable.Int8FromJSON returned an error: %s", err.Error())
    79  				}
    80  			}
    81  		})
    82  	}
    83  }