2024-06-01 16:31:50 +02:00
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
2024-06-10 20:37:58 +02:00
// t_builtin-string_test.go
2024-06-01 16:31:50 +02:00
package expr
import (
"errors"
"testing"
)
func TestFuncString ( t * testing . T ) {
2024-06-10 20:37:58 +02:00
section := "Builtin-String"
2024-06-01 16:31:50 +02:00
inputs := [ ] inputType {
2024-06-10 20:37:58 +02:00
/* 1 */ { ` builtin "string"; strJoin("-", "one", "two", "three") ` , "one-two-three" , nil } ,
/* 2 */ { ` builtin "string"; strJoin("-", ["one", "two", "three"]) ` , "one-two-three" , nil } ,
/* 3 */ { ` builtin "string"; ls= ["one", "two", "three"]; strJoin("-", ls) ` , "one-two-three" , nil } ,
/* 4 */ { ` builtin "string"; ls= ["one", "two", "three"]; strJoin(1, ls) ` , nil , errors . New ( ` strJoin(): the "separator" parameter must be a string, got a integer (1) ` ) } ,
/* 5 */ { ` builtin "string"; ls= ["one", 2, "three"]; strJoin("-", ls) ` , nil , errors . New ( ` strJoin(): expected string, got integer (2) ` ) } ,
/* 6 */ { ` builtin "string"; "<"+strTrim(" bye bye ")+">" ` , "<bye bye>" , nil } ,
/* 7 */ { ` builtin "string"; strSub("0123456789", 1,2) ` , "12" , nil } ,
/* 8 */ { ` builtin "string"; strSub("0123456789", -3,2) ` , "78" , nil } ,
/* 9 */ { ` builtin "string"; strSub("0123456789", -3) ` , "789" , nil } ,
/* 10 */ { ` builtin "string"; strSub("0123456789") ` , "0123456789" , nil } ,
/* 11 */ { ` builtin "string"; strStartsWith("0123456789", "xyz", "012") ` , true , nil } ,
/* 12 */ { ` builtin "string"; strStartsWith("0123456789", "xyz", "0125") ` , false , nil } ,
/* 13 */ { ` builtin "string"; strStartsWith("0123456789") ` , nil , errors . New ( ` strStartsWith(): too few params -- expected 2 or more, got 1 ` ) } ,
/* 14 */ { ` builtin "string"; strEndsWith("0123456789", "xyz", "789") ` , true , nil } ,
/* 15 */ { ` builtin "string"; strEndsWith("0123456789", "xyz", "0125") ` , false , nil } ,
/* 16 */ { ` builtin "string"; strEndsWith("0123456789") ` , nil , errors . New ( ` strEndsWith(): too few params -- expected 2 or more, got 1 ` ) } ,
/* 17 */ { ` builtin "string"; strSplit("one-two-three", "-") ` , newListA ( "one" , "two" , "three" ) , nil } ,
/* 18 */ { ` builtin "string"; strJoin("-", [1, "two", "three"]) ` , nil , errors . New ( ` strJoin(): expected string, got integer (1) ` ) } ,
/* 19 */ { ` builtin "string"; strJoin() ` , nil , errors . New ( ` strJoin(): too few params -- expected 1 or more, got 0 ` ) } ,
2024-06-01 16:31:50 +02:00
/* 69 */ / * { ` builtin "string"; $$global ` , ` vars : {
}
funcs : {
add ( any = 0 ... ) - > number ,
dec ( any ) - > decimal ,
endsWithStr ( source , suffix ) - > boolean ,
fract ( any , denominator = 1 ) - > fraction ,
import ( ... ) - > any ,
importAll ( ... ) - > any ,
int ( any ) - > integer ,
isBool ( any ) - > boolean ,
isDec ( any ) - > boolean ,
isDict ( any ) - > boolean ,
isFloat ( any ) - > boolean ,
isFract ( any ) - > boolean ,
isInt ( any ) - > boolean ,
isList ( any ) - > boolean ,
isNil ( any ) - > boolean ,
isString ( any ) - > boolean ,
joinStr ( separator , item = "" ... ) - > string ,
mul ( any = 1 ... ) - > number ,
splitStr ( source , separator = "" , count = - 1 ) - > list of string ,
startsWithStr ( source , prefix ) - > boolean ,
subStr ( source , start = 0 , count = - 1 ) - > string ,
trimStr ( source ) - > string
}
` , nil } , * /
}
//t.Setenv("EXPR_PATH", ".")
2024-06-02 12:33:32 +02:00
// parserTestSpec(t, section, inputs, 19)
2024-06-25 10:59:03 +02:00
runTestSuite ( t , section , inputs )
2024-06-01 16:31:50 +02:00
}