...

Source file src/github.com/launchdarkly/go-server-sdk-evaluation/v2/internal/parse_hex_test.go

Documentation: github.com/launchdarkly/go-server-sdk-evaluation/v2/internal

     1  package internal
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  	"testing/quick"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestParseHexUint64IsEquivalentToStrconvParseUint(t *testing.T) {
    14  	validate := func(t *testing.T, s string) bool {
    15  		expectedResult, expectedError := strconv.ParseUint(s, 16, 64)
    16  		expectedOK := expectedError == nil
    17  		result, ok := ParseHexUint64([]byte(s))
    18  
    19  		if expectedResult != result || ok != expectedOK {
    20  			t.Errorf("For input %q, strconv returned (%v, %v) but ParseHexUint64 returned (%v, %v)",
    21  				s, expectedResult, expectedError, result, ok)
    22  			return false
    23  		}
    24  		return true
    25  	}
    26  
    27  	t.Run("for empty string", func(t *testing.T) { // quick.Check isn't guaranteed to produce an empty string
    28  		assert.True(t, validate(t, ""))
    29  	})
    30  
    31  	t.Run("for arbitrary strings", func(t *testing.T) {
    32  		randomStrings := func(s string) bool {
    33  			return validate(t, s)
    34  		}
    35  		if err := quick.Check(randomStrings, nil); err != nil {
    36  			t.Error(err)
    37  		}
    38  	})
    39  
    40  	t.Run("for hex representations of arbitrary uint64 values", func(t *testing.T) {
    41  		hexStrings := func(value uint64) bool {
    42  			hexString := fmt.Sprintf("%x", value)
    43  			return validate(t, strings.ToLower(hexString)) && validate(t, strings.ToUpper(hexString))
    44  		}
    45  		if err := quick.Check(hexStrings, nil); err != nil {
    46  			t.Error(err)
    47  		}
    48  	})
    49  }
    50  

View as plain text