github.com/aaronlehmann/figtree@v1.0.1/rawoption_test.go (about)

     1  package figtree
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	yaml "gopkg.in/yaml.v3"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestOptionInterface(t *testing.T) {
    13  	f := func(_ option) bool {
    14  		return true
    15  	}
    16  
    17  	assert.True(t, f(&BoolOption{}))
    18  	assert.True(t, f(&ByteOption{}))
    19  	assert.True(t, f(&Complex128Option{}))
    20  	assert.True(t, f(&Complex64Option{}))
    21  	assert.True(t, f(&ErrorOption{}))
    22  	assert.True(t, f(&Float32Option{}))
    23  	assert.True(t, f(&Float64Option{}))
    24  	assert.True(t, f(&IntOption{}))
    25  	assert.True(t, f(&Int16Option{}))
    26  	assert.True(t, f(&Int32Option{}))
    27  	assert.True(t, f(&Int64Option{}))
    28  	assert.True(t, f(&Int8Option{}))
    29  	assert.True(t, f(&RuneOption{}))
    30  	assert.True(t, f(&StringOption{}))
    31  	assert.True(t, f(&UintOption{}))
    32  	assert.True(t, f(&Uint16Option{}))
    33  	assert.True(t, f(&Uint32Option{}))
    34  	assert.True(t, f(&Uint64Option{}))
    35  	assert.True(t, f(&Uint8Option{}))
    36  	assert.True(t, f(&UintptrOption{}))
    37  }
    38  
    39  func TestStringOptionYAML(t *testing.T) {
    40  	s := ""
    41  	err := yaml.Unmarshal([]byte(`""`), &s)
    42  	assert.Nil(t, err)
    43  	assert.Equal(t, s, "")
    44  
    45  	type testType struct {
    46  		String StringOption `yaml:"string,omitempty"`
    47  	}
    48  	tt := testType{}
    49  
    50  	err = yaml.Unmarshal([]byte(`string: ""`), &tt)
    51  	assert.Nil(t, err)
    52  	assert.Equal(t, StringOption{Source: "yaml", Value: "", Defined: true}, tt.String)
    53  
    54  	tt = testType{}
    55  	err = yaml.Unmarshal([]byte(`string: "value"`), &tt)
    56  	assert.Nil(t, err)
    57  	assert.Equal(t, StringOption{Source: "yaml", Value: "value", Defined: true}, tt.String)
    58  }
    59  
    60  func TestStringOptionJSON(t *testing.T) {
    61  	type testType struct {
    62  		String StringOption `json:"string,omitempty"`
    63  	}
    64  	tt := testType{}
    65  
    66  	err := json.Unmarshal([]byte(`{"string": ""}`), &tt)
    67  	assert.Nil(t, err)
    68  	assert.Equal(t, StringOption{Source: "json", Value: "", Defined: true}, tt.String)
    69  
    70  	tt = testType{}
    71  	err = json.Unmarshal([]byte(`{"string": "value"}`), &tt)
    72  	assert.Nil(t, err)
    73  	assert.Equal(t, StringOption{Source: "json", Value: "value", Defined: true}, tt.String)
    74  }
    75  
    76  func TestBoolOptionYAML(t *testing.T) {
    77  	type testType struct {
    78  		Bool BoolOption `yaml:"bool,omitempty"`
    79  	}
    80  	tt := testType{}
    81  
    82  	err := yaml.Unmarshal([]byte(`bool: true`), &tt)
    83  	assert.Nil(t, err)
    84  	assert.Equal(t, BoolOption{Source: "yaml", Value: true, Defined: true}, tt.Bool)
    85  
    86  	tt = testType{}
    87  	err = yaml.Unmarshal([]byte(`bool: false`), &tt)
    88  	assert.Nil(t, err)
    89  	assert.Equal(t, BoolOption{Source: "yaml", Value: false, Defined: true}, tt.Bool)
    90  
    91  	tt = testType{
    92  		Bool: NewBoolOption(true),
    93  	}
    94  	err = yaml.Unmarshal([]byte(`bool: false`), &tt)
    95  	assert.Nil(t, err)
    96  	assert.Equal(t, BoolOption{Source: "yaml", Value: false, Defined: true}, tt.Bool)
    97  }