github.com/enetx/g@v1.0.80/tests/format_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/enetx/g"
     7  )
     8  
     9  func TestStringFormatting(t *testing.T) {
    10  	// Test Sprintf
    11  	formatted := g.Sprintf("%s is %d years old", "John", 30)
    12  	expected := g.NewString("John is 30 years old")
    13  	if formatted != expected {
    14  		t.Errorf("Sprintf formatting incorrect. Expected: %s, Got: %s", expected, formatted)
    15  	}
    16  
    17  	// Test Sprint
    18  	sprinted := g.Sprint("Hello", "World", 42)
    19  	expected = g.NewString("HelloWorld42")
    20  	if sprinted != expected {
    21  		t.Errorf("Sprint formatting incorrect. Expected: %s, Got: %s", expected, sprinted)
    22  	}
    23  }
    24  
    25  func TestStringFormat(t *testing.T) {
    26  	// Test case
    27  	values := g.Map[string, any]{
    28  		"name": "John",
    29  		"age":  30,
    30  		"city": "New York",
    31  	}
    32  
    33  	format := "Hello, my name is {name}. I am {age} years old and live in {city}."
    34  	formatted := g.Format(format, values)
    35  	expected := g.NewString("Hello, my name is John. I am 30 years old and live in New York.")
    36  
    37  	if formatted != expected {
    38  		t.Errorf("Format function incorrect. Expected: %s, Got: %s", expected, formatted)
    39  	}
    40  }