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

     1  package gsql_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mitranim/gg"
     7  	s "github.com/mitranim/gg/gsql"
     8  	gtest "github.com/mitranim/gg/gtest"
     9  )
    10  
    11  func TestRune_IsNull(t *testing.T) {
    12  	defer gtest.Catch(t)
    13  
    14  	gtest.True(s.Rune(0).IsNull())
    15  	gtest.False(s.Rune(1).IsNull())
    16  }
    17  
    18  func TestRune_IsNotNull(t *testing.T) {
    19  	defer gtest.Catch(t)
    20  
    21  	gtest.False(s.Rune(0).IsNotNull())
    22  	gtest.True(s.Rune(1).IsNotNull())
    23  }
    24  
    25  func TestRune_Clear(t *testing.T) {
    26  	defer gtest.Catch(t)
    27  
    28  	var tar s.Rune
    29  	gtest.Zero(tar)
    30  
    31  	tar = '👍'
    32  	gtest.NotZero(tar)
    33  
    34  	tar.Clear()
    35  	gtest.Zero(tar)
    36  
    37  	tar.Clear()
    38  	gtest.Zero(tar)
    39  }
    40  
    41  func TestRune_String(t *testing.T) {
    42  	defer gtest.Catch(t)
    43  
    44  	gtest.Str(s.Rune(0), ``)
    45  	gtest.Str(s.Rune('👍'), `👍`)
    46  }
    47  
    48  func BenchmarkRune_String(b *testing.B) {
    49  	for ind := 0; ind < b.N; ind++ {
    50  		gg.Nop1(s.Rune('👍'))
    51  	}
    52  }
    53  
    54  func TestRune_Append(t *testing.T) {
    55  	defer gtest.Catch(t)
    56  
    57  	buf := gg.ToBytes(`init_`)
    58  
    59  	gtest.Equal(s.Rune(0).AppendTo(buf), buf)
    60  	gtest.Equal(s.Rune(0).AppendTo(nil), nil)
    61  
    62  	gtest.Equal(s.Rune('👍').AppendTo(buf), gg.ToBytes(`init_👍`))
    63  	gtest.Equal(s.Rune('👍').AppendTo(nil), gg.ToBytes(`👍`))
    64  }
    65  
    66  func TestRune_Parse(t *testing.T) {
    67  	defer gtest.Catch(t)
    68  	testRuneParse((*s.Rune).Parse)
    69  }
    70  
    71  func testRuneParse(fun func(*s.Rune, string) error) {
    72  	gtest.ErrorStr(`unable to parse "ab" as char: too many chars`, fun(new(s.Rune), `ab`))
    73  	gtest.ErrorStr(`unable to parse "abc" as char: too many chars`, fun(new(s.Rune), `abc`))
    74  	gtest.ErrorStr(`unable to parse "👍👎" as char: too many chars`, fun(new(s.Rune), `👍👎`))
    75  
    76  	var tar s.Rune
    77  
    78  	gtest.NoError(fun(&tar, `🙂`))
    79  	gtest.Eq(tar, '🙂')
    80  
    81  	gtest.NoError(fun(&tar, ``))
    82  	gtest.Zero(tar)
    83  }
    84  
    85  func BenchmarkRune_Parse_empty(b *testing.B) {
    86  	var tar s.Rune
    87  
    88  	for ind := 0; ind < b.N; ind++ {
    89  		gg.Try(tar.Parse(``))
    90  	}
    91  }
    92  
    93  func BenchmarkRune_Parse_non_empty(b *testing.B) {
    94  	var tar s.Rune
    95  
    96  	for ind := 0; ind < b.N; ind++ {
    97  		gg.Try(tar.Parse(`🙂`))
    98  	}
    99  }
   100  
   101  func TestRune_MarshalText(t *testing.T) {
   102  	defer gtest.Catch(t)
   103  
   104  	encode := func(src s.Rune) string {
   105  		return gg.ToString(gg.Try1(src.MarshalText()))
   106  	}
   107  
   108  	gtest.Eq(encode(0), ``)
   109  	gtest.Eq(encode('👍'), `👍`)
   110  }
   111  
   112  func BenchmarkRune_MarshalText(b *testing.B) {
   113  	for ind := 0; ind < b.N; ind++ {
   114  		gg.Nop2(s.Rune('👍').MarshalText())
   115  	}
   116  }
   117  
   118  func TestRune_UnmarshalText(t *testing.T) {
   119  	defer gtest.Catch(t)
   120  	testRuneParse(charUnmarshalText)
   121  }
   122  
   123  func charUnmarshalText(tar *s.Rune, src string) error {
   124  	return tar.UnmarshalText(gg.ToBytes(src))
   125  }
   126  
   127  func BenchmarkRune_UnmarshalText(b *testing.B) {
   128  	var tar s.Rune
   129  
   130  	for ind := 0; ind < b.N; ind++ {
   131  		gg.Try(tar.UnmarshalText(gg.ToBytes(`👍`)))
   132  	}
   133  }
   134  
   135  func TestRune_MarshalJSON(t *testing.T) {
   136  	defer gtest.Catch(t)
   137  
   138  	encode := func(src s.Rune) string {
   139  		return gg.ToString(gg.Try1(src.MarshalJSON()))
   140  	}
   141  
   142  	gtest.Eq(encode(0), `null`)
   143  	gtest.Eq(encode('👍'), `"👍"`)
   144  }
   145  
   146  func BenchmarkRune_MarshalJSON_empty(b *testing.B) {
   147  	for ind := 0; ind < b.N; ind++ {
   148  		gg.Nop2(s.Rune(0).MarshalJSON())
   149  	}
   150  }
   151  
   152  func BenchmarkRune_MarshalJSON_non_empty(b *testing.B) {
   153  	for ind := 0; ind < b.N; ind++ {
   154  		gg.Nop2(s.Rune('👍').MarshalJSON())
   155  	}
   156  }
   157  
   158  func TestRune_UnmarshalJSON(t *testing.T) {
   159  	defer gtest.Catch(t)
   160  
   161  	testRuneParse(charUnmarshalJson)
   162  
   163  	gtest.ErrorStr(
   164  		`cannot unmarshal number into Go value of type string`,
   165  		new(s.Rune).UnmarshalJSON(gg.ToBytes(`123`)),
   166  	)
   167  
   168  	{
   169  		tar := s.Rune('👍')
   170  		gtest.NoError(tar.UnmarshalJSON(nil))
   171  		gtest.Zero(tar)
   172  	}
   173  
   174  	{
   175  		tar := s.Rune('👍')
   176  		gtest.NoError(tar.UnmarshalJSON(gg.ToBytes(`null`)))
   177  		gtest.Zero(tar)
   178  	}
   179  }
   180  
   181  func charUnmarshalJson(tar *s.Rune, src string) error {
   182  	return tar.UnmarshalJSON(gg.JsonBytes(src))
   183  }
   184  
   185  func BenchmarkRune_UnmarshalJSON_empty(b *testing.B) {
   186  	var tar s.Rune
   187  
   188  	for ind := 0; ind < b.N; ind++ {
   189  		gg.Try(tar.UnmarshalJSON(gg.ToBytes(`null`)))
   190  	}
   191  }
   192  
   193  func BenchmarkRune_UnmarshalJSON_non_empty(b *testing.B) {
   194  	var tar s.Rune
   195  
   196  	for ind := 0; ind < b.N; ind++ {
   197  		gg.Try(tar.UnmarshalJSON(gg.ToBytes(`"👍"`)))
   198  	}
   199  }
   200  
   201  func TestRune_Value(t *testing.T) {
   202  	defer gtest.Catch(t)
   203  
   204  	gtest.Zero(gg.Try1(s.Rune(0).Value()))
   205  	gtest.Equal(gg.Try1(s.Rune('👍').Value()), any(rune('👍')))
   206  }
   207  
   208  func TestRune_Scan(t *testing.T) {
   209  	t.Run(`clear`, func(t *testing.T) {
   210  		defer gtest.Catch(t)
   211  
   212  		test := func(src any) {
   213  			tar := s.Rune('👍')
   214  			gtest.NoError(tar.Scan(src))
   215  			gtest.Zero(tar)
   216  		}
   217  
   218  		test(nil)
   219  		test(string(``))
   220  		test([]byte(nil))
   221  		test([]byte{})
   222  		test(rune(0))
   223  		test(s.Rune(0))
   224  	})
   225  
   226  	t.Run(`unclear`, func(t *testing.T) {
   227  		defer gtest.Catch(t)
   228  
   229  		test := func(src any, exp s.Rune) {
   230  			var tar s.Rune
   231  			gtest.NoError(tar.Scan(src))
   232  			gtest.Eq(tar, exp)
   233  		}
   234  
   235  		test(string(`👍`), '👍')
   236  		test([]byte(`👍`), '👍')
   237  		test(rune('👍'), '👍')
   238  		test(s.Rune('👍'), '👍')
   239  	})
   240  }
   241  
   242  func BenchmarkRune_Scan_empty(b *testing.B) {
   243  	var tar s.Rune
   244  	var src []byte
   245  
   246  	for ind := 0; ind < b.N; ind++ {
   247  		gg.Try(tar.Scan(src))
   248  	}
   249  }
   250  
   251  func BenchmarkRune_Scan_non_empty(b *testing.B) {
   252  	var tar s.Rune
   253  	src := string(`👍`)
   254  
   255  	for ind := 0; ind < b.N; ind++ {
   256  		gg.Try(tar.Scan(src))
   257  	}
   258  }
   259  
   260  func Test_string_to_char_slice(t *testing.T) {
   261  	defer gtest.Catch(t)
   262  
   263  	gtest.Equal(
   264  		[]rune(`👍👎🙂😄`),
   265  		[]rune{'👍', '👎', '🙂', '😄'},
   266  	)
   267  
   268  	gtest.Equal(
   269  		[]s.Rune(`👍👎🙂😄`),
   270  		[]s.Rune{'👍', '👎', '🙂', '😄'},
   271  	)
   272  }