github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/test/string_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  	"github.com/v2pro/plz/msgfmt"
     7  	"fmt"
     8  	"github.com/v2pro/plz/countlog"
     9  	. "github.com/v2pro/plz/test"
    10  	. "github.com/v2pro/plz/test/must"
    11  )
    12  
    13  func Test_string(t *testing.T) {
    14  	t.Run("string => string", Case(func(ctx *countlog.Context) {
    15  		AssertEqual("ahellob", fmt.Sprintf("a%sb", "hello"))
    16  		AssertEqual("ahellob", msgfmt.Sprintf("a{key}b", "key", "hello"))
    17  	}))
    18  	t.Run("int => string", Case(func(ctx *countlog.Context) {
    19  		AssertEqual("%!s(int=100)", fmt.Sprintf("%s", 100))
    20  		AssertEqual("100", msgfmt.Sprintf("{key}", "key", 100))
    21  	}))
    22  	t.Run("bytes => string", Case(func(ctx *countlog.Context) {
    23  		AssertEqual("hello", fmt.Sprintf("%s", []byte("hello")))
    24  		AssertEqual("hello", msgfmt.Sprintf("{key}", "key", []byte("hello")))
    25  	}))
    26  	t.Run("printf", Case(func(ctx *countlog.Context) {
    27  		fmt.Printf("%s\n", "hello")
    28  		msgfmt.Printf("{key}\n", "key", "hello")
    29  	}))
    30  	t.Run("println", Case(func(ctx *countlog.Context) {
    31  		fmt.Println("hello", "world")
    32  		msgfmt.Println("hello", "world")
    33  	}))
    34  }
    35  
    36  func Benchmark_string_to_string(b *testing.B) {
    37  	b.ReportAllocs()
    38  	for i := 0; i < b.N; i++ {
    39  		msgfmt.Sprintf("{key}", "key", "hello")
    40  		//fmt.Sprintf("%s", "hello")
    41  	}
    42  }
    43  
    44  func Benchmark_time_now(b *testing.B) {
    45  	b.ReportAllocs()
    46  	for i := 0; i < b.N; i++ {
    47  		time.Now().String()
    48  	}
    49  }