github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/utils/convert_test.go (about)

     1  package utils_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  
    10  	"github.com/clubpay/ronykit/kit/utils"
    11  )
    12  
    13  var _ = Describe("ParseNumeric tests", func() {
    14  	DescribeTable(
    15  		"inputs of multiple types",
    16  		func(in any, xv float64, xs string) {
    17  			on := utils.ParseNumeric(in)
    18  			Expect(on.Value()).To(BeNumerically("~", xv-1e-6, xv+1e-6))
    19  			Expect(on.String()).To(BeIdenticalTo(xs))
    20  			Expect(json.Marshal(on)).To(BeEquivalentTo(fmt.Sprintf("%q", xs)))
    21  		},
    22  		Entry("string", "13.14", 13.14, "13.14"),
    23  		Entry("float64", 13.14, 13.14, "13.14"),
    24  		Entry("float32", float32(13.14), 13.14, "13.14"),
    25  		Entry("int", 13, 13.0, "13.00"),
    26  		Entry("int64", int64(13), 13.0, "13.00"),
    27  	)
    28  })
    29  
    30  var _ = Describe("Numeric tests", func() {
    31  	DescribeTable(
    32  		"decoded from json",
    33  		func(str string, xv float64, xs string) {
    34  			type medium struct {
    35  				Field utils.Numeric `json:"f"`
    36  			}
    37  			m := new(medium)
    38  
    39  			err := json.Unmarshal([]byte(str), m)
    40  			Expect(err).ShouldNot(HaveOccurred())
    41  
    42  			Expect(m.Field.Value()).To(BeNumerically("~", xv-1e-6, xv+1e-6))
    43  			Expect(m.Field.String()).To(BeIdenticalTo(xs))
    44  		},
    45  		Entry("string field", `{"f": "13.14"}`, 13.14, "13.14"),
    46  		Entry("float field", `{"f": 13.14}`, 13.14, "13.14"),
    47  		Entry("int field", `{"f": 13}`, 13.00, "13.00"),
    48  	)
    49  
    50  	DescribeTable(
    51  		"with specific precision",
    52  		func(in utils.Numeric, prec int, xv float64, xs string) {
    53  			Expect(in.WithPrecision(prec).Value()).To(BeNumerically("~", xv-1e-6, xv+1e-6))
    54  			Expect(in.WithPrecision(prec).String()).To(BeIdenticalTo(xs))
    55  		},
    56  		Entry("increased", utils.ParseNumeric("13.14"), 3, 13.14, "13.140"),
    57  		Entry("decreased", utils.ParseNumeric("13.14"), 1, 13.14, "13.1"),
    58  		Entry("unchanged", utils.ParseNumeric("13.14"), 2, 13.14, "13.14"),
    59  		Entry("unset", utils.ParseNumeric("13.14"), -1, 13.14, "13.14"),
    60  	)
    61  
    62  	DescribeTable(
    63  		"string truncate",
    64  		func(s string, maxSize int, xs string) {
    65  			Expect(utils.StrTruncate(s, maxSize)).To(BeIdenticalTo(xs))
    66  		},
    67  		Entry("test", utils.StrTruncate("Merci Marcel Tiong Bahru", 13), 13, "Merci Marcel "),
    68  		Entry("test", utils.StrTruncate("Merci Marcel Tiong Bahru", 4), 4, "Merc"),
    69  		Entry("test", utils.StrTruncate("", 3), 3, ""),
    70  		Entry("test", utils.StrTruncate("Merci Marcel Tiong Bahru", 1), 1, "M"),
    71  		Entry("test", utils.StrTruncate("Merci Marcel Tiong Bahru", 0), 0, ""),
    72  		Entry("test", utils.StrTruncate("Merci Marcel Tiong Bahru", -1), -1, ""),
    73  		Entry("test", utils.StrTruncate(" ", 0), 0, ""),
    74  		Entry("test", utils.StrTruncate(" ", 1), 1, " "),
    75  	)
    76  })