github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zutil/args_test.go (about) 1 package zutil 2 3 import ( 4 "database/sql" 5 "fmt" 6 "strings" 7 "testing" 8 9 "github.com/sohaha/zlsgo" 10 "github.com/sohaha/zlsgo/zstring" 11 ) 12 13 func TestArgs(t *testing.T) { 14 tt := zlsgo.NewTest(t) 15 16 sum := NewInt32(0) 17 18 moreNum := []interface{}{"abc " + zstring.Pad("", 88, "? ", zstring.PadRight) + " |[]", "abc " + zstring.Pad("", 88, "$? ", zstring.PadRight)} 19 for _, num := range strings.Split(zstring.Rand(88), "") { 20 moreNum = append(moreNum, num) 21 } 22 moreNum[0] = fmt.Sprintf("abc "+zstring.Pad("", 88, "? ", zstring.PadRight)+"|%v", moreNum[2:]) 23 tests := [][]interface{}{ 24 {"abc ? ?|[123 321]", "abc $? $?", 123, 321}, 25 {"abc ? ?|[123 123]", "abc $? $0", 123, 321}, 26 {"abc ? |[456]", "abc $0 ", 456}, 27 {"abc ? |[]", "abc ? ", 456}, 28 {"abc |[]", "abc $1 ", 123}, 29 {"abc ?-?|[6 123]", "abc ${s}-${a}", Named("a", 123), Named("s", func() interface{} { return sum.Load() })}, 30 {"abc |[]", "abc ${unknown} ", 123}, 31 {"abc $ |[]", "abc $$ ", 123}, 32 {"abc$|[]", "abc$", 123}, 33 {"abc ? ? ? ? |[123 456 123 456]", "abc $? $? $0 $? ", 123, 456, 789}, 34 moreNum, 35 } 36 for _, c := range tests { 37 sum.Add(1) 38 args := NewArgs() 39 for i := 2; i < len(c); i++ { 40 args.Var(c[i]) 41 } 42 query, values := args.Compile(c[1].(string)) 43 t.Log(query, values) 44 actual := fmt.Sprintf("%v|%v", query, values) 45 tt.Equal(c[0].(string), actual) 46 } 47 } 48 49 func TestArgsOnlyNamed(t *testing.T) { 50 tt := zlsgo.NewTest(t) 51 52 tests := [][]interface{}{ 53 {"abc $? $?|[]", "abc $? $?", 123, 321}, 54 {"abc $0 |[]", "abc $0 ", 456}, 55 {"abc $a |[]", "abc $$a ", Named("a", 123)}, 56 {"abc ? |[123]", "abc ${a} ", Named("a", 123), Named("a", 321)}, 57 {"abc |[]", "abc ${xxx}"}, 58 {"abc @sql_a |[{{} sql_a 123}]", "abc ${a} ", Named("a", sql.Named("sql_a", "123")), Named("ab", sql.Named("sql_a", "123"))}, 59 {"abc ?-?|[99 123]", "abc ${s}-${a}", Named("a", 123), Named("s", 99)}, 60 {"abc ?-?|[s.k__ 123]", "abc ${s.k}-${a}", Named("a", 123), Named("s.*", func(k string) interface{} { 61 return k + "__" 62 })}, 63 } 64 for _, c := range tests { 65 args := NewArgs(WithOnlyNamed()) 66 for i := 2; i < len(c); i++ { 67 args.Var(c[i]) 68 } 69 query, values := args.Compile(c[1].(string)) 70 t.Log(query, values) 71 actual := fmt.Sprintf("%v|%v", query, values) 72 tt.Equal(c[0].(string), actual) 73 } 74 } 75 76 func TestArgsString(t *testing.T) { 77 tt := zlsgo.NewTest(t) 78 79 tests := [][]interface{}{ 80 {"123", "$0", 123}, 81 {"abc 123 321", "abc $? $?", 123, 321}, 82 {"abc 123 321", "abc $? $?", 123, 321}, 83 {"abc 123 ", "abc ${a} ", Named("a", sql.Named("sql_a", "123"))}, 84 {"abc 99-123", "abc ${s}-${a}", Named("a", 123), Named("s", 99)}, 85 {"abc 99${a}123", "abc ${s}$${a}${a}", Named("a", 123), Named("s", 99)}, 86 {"abc s.k__-123", "abc ${s.k}-${a}", Named("a", 123), Named("s.*", func(k string) interface{} { 87 return k + "__" 88 })}, 89 } 90 for _, c := range tests { 91 args := NewArgs() 92 for i := 2; i < len(c); i++ { 93 args.Var(c[i]) 94 } 95 result := args.CompileString(c[1].(string)) 96 t.Log(result) 97 tt.Equal(c[0].(string), result) 98 } 99 }