github.com/zlyuancn/zstr@v0.0.0-20230412074414-14d6b645962f/template_test.go (about)

     1  /*
     2  -------------------------------------------------
     3     Author :       Zhang Fan
     4     date:         2020/11/25
     5     Description :
     6  -------------------------------------------------
     7  */
     8  
     9  package zstr
    10  
    11  import (
    12  	"testing"
    13  )
    14  
    15  const testShort = `a=@a b={@b}`
    16  
    17  const testLong = `balabalabalabalabalabalabalabalabal
    18  balabalabalabala  @a    balabalabalabalabalabalabalab
    19  balabalabalabalabalab  {@b}   labalabalabalabalabalab
    20  balabalab  {@c}   alabalaalabala @a @c abalabalabalab
    21  balabalabalabalabalabalabalabalabalabalabalabalabalab
    22  balabalabalabalab  {@d}   labalabalabalabalabalabalab`
    23  
    24  const testShortSql = `
    25  select * from a where $a $b $c
    26  `
    27  const testLongSql = `
    28  select * from a where
    29  {&u.phone_number like}
    30      {&u.user_name like}
    31      &dev.district_id
    32      &u.gender
    33  	{&u.create_time >=}
    34  	&u.create_time
    35  	{&u.create_time <}
    36  	&u.create_time
    37      #start_time
    38      {#start_time}
    39      {#end_time}
    40      #end_time
    41      {#a}
    42      #a
    43  	#b
    44      {#b}
    45      &au.is_ugc
    46  	{&c in}
    47  	{&d in}
    48  	{&e notin}
    49  	{&f in}
    50  	{&g like}
    51  	{&h like}
    52      &dev.device_platform
    53  group by u.id
    54  limit #size offset {#start a};`
    55  
    56  var testData = map[string]interface{}{
    57  	"":                 "xxx",
    58  	"u.create_time":    "uc",
    59  	"u.create_time[1]": "uc1",
    60  	"u.create_time[2]": "uc2",
    61  	"u.create_time[0]": "uc0",
    62  	"start_time":       "st",
    63  	"start_time[1]":    "st1",
    64  	"end_time[1]":      "et[1]",
    65  	"a[0]":             "av0",
    66  	"b":                "bv",
    67  	"b[1]":             "bv0",
    68  	"d":                "dv",
    69  	"e[0]":             []string{"ev0", "ev1"},
    70  	"g":                "gv",
    71  }
    72  
    73  func BenchmarkRenderShort(b *testing.B) {
    74  	b.RunParallel(func(pb *testing.PB) {
    75  		for pb.Next() {
    76  			_ = Render(testShort, testData)
    77  		}
    78  	})
    79  }
    80  
    81  func BenchmarkSqlRenderShort(b *testing.B) {
    82  	b.RunParallel(func(pb *testing.PB) {
    83  		for pb.Next() {
    84  			_ = SqlRender(testShortSql, testData)
    85  		}
    86  	})
    87  }
    88  
    89  func BenchmarkSqlParseShort(b *testing.B) {
    90  	b.RunParallel(func(pb *testing.PB) {
    91  		for pb.Next() {
    92  			_, _, _ = SqlParse(testShortSql, testData)
    93  		}
    94  	})
    95  }
    96  
    97  func BenchmarkRenderLong(b *testing.B) {
    98  	b.RunParallel(func(pb *testing.PB) {
    99  		for pb.Next() {
   100  			_ = Render(testLong, testData)
   101  		}
   102  	})
   103  }
   104  
   105  func BenchmarkSqlRenderLong(b *testing.B) {
   106  	b.RunParallel(func(pb *testing.PB) {
   107  		for pb.Next() {
   108  			_ = SqlRender(testLongSql, testData)
   109  		}
   110  	})
   111  }
   112  
   113  func BenchmarkSqlParseLong(b *testing.B) {
   114  	b.RunParallel(func(pb *testing.PB) {
   115  		for pb.Next() {
   116  			_, _, _ = SqlParse(testLongSql, testData)
   117  		}
   118  	})
   119  }
   120  
   121  func TestReplaceTemplateVars(t *testing.T) {
   122  	data := map[string]string{
   123  		"name":  "John",
   124  		"age":   "30",
   125  		"email": "john@example.com",
   126  		"x[2]":  "X",
   127  	}
   128  
   129  	cases := []struct {
   130  		input    string
   131  		expected string
   132  	}{
   133  		{"Hello, { @name}!", "Hello, John!"},
   134  		{"{@name} is {@age } years old.", "John is 30 years old."},
   135  		{"Contact me at { @email }.", "Contact me at john@example.com."},
   136  		{"{ @foo } {@bar} {@baz}", "  "},
   137  		{"{ @name }{@age}{@email}", "John30john@example.com"},
   138  		{"{@name} {@age} {@email} {@foo}", "John 30 john@example.com "},
   139  		{"{@name} { @ age } {@email} {@foo}", "John { @ age } john@example.com "},
   140  		{"{@x} @x @x", " @x X"},
   141  	}
   142  
   143  	for _, c := range cases {
   144  		actual := Render(c.input, data)
   145  		if actual != c.expected {
   146  			t.Errorf("Render(%q, fn) == %q, expected %q",
   147  				c.input, actual, c.expected)
   148  		}
   149  	}
   150  }