github.com/mitranim/gg@v0.1.17/opt_test.go (about)

     1  package gg_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mitranim/gg"
     7  	"github.com/mitranim/gg/gtest"
     8  )
     9  
    10  func TestOpt_MarshalJSON(t *testing.T) {
    11  	defer gtest.Catch(t)
    12  
    13  	type Type = gg.Opt[int]
    14  
    15  	gtest.Eq(gg.JsonString(gg.Zero[Type]()), `null`)
    16  	gtest.Eq(gg.JsonString(Type{Val: 123}), `null`)
    17  	gtest.Eq(gg.JsonString(gg.OptVal(123)), `123`)
    18  }
    19  
    20  func TestOpt_UnmarshalJSON(t *testing.T) {
    21  	defer gtest.Catch(t)
    22  
    23  	type Type = gg.Opt[int]
    24  
    25  	gtest.Zero(gg.JsonParseTo[Type](`null`))
    26  
    27  	gtest.Equal(
    28  		gg.JsonParseTo[Type](`123`),
    29  		gg.OptVal(123),
    30  	)
    31  }
    32  
    33  func BenchmarkOpt_String(b *testing.B) {
    34  	val := gg.OptVal(`str`)
    35  
    36  	for ind := 0; ind < b.N; ind++ {
    37  		gg.Nop1(val.String())
    38  	}
    39  }
    40  
    41  func TestOpt_Scan(t *testing.T) {
    42  	defer gtest.Catch(t)
    43  
    44  	type Type = gg.Opt[float64]
    45  
    46  	var tar Type
    47  	gtest.NoError(tar.Scan(float64(9.999999682655225e-18)))
    48  	gtest.Eq(tar.Val, 9.999999682655225e-18)
    49  
    50  	tar.Clear()
    51  	gtest.Zero(tar)
    52  	gtest.NoError(tar.Scan(`9.999999682655225e-18`))
    53  	gtest.Eq(tar.Val, 9.999999682655225e-18)
    54  }