github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/builtin_fn_str_test.go (about)

     1  package eval_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "src.elv.sh/pkg/eval"
     7  	. "src.elv.sh/pkg/eval/evaltest"
     8  )
     9  
    10  func TestStringComparisonCommands(t *testing.T) {
    11  	Test(t,
    12  		That(`<s a b`).Puts(true),
    13  		That(`<s 2 10`).Puts(false),
    14  		That(`<=s a a`).Puts(true),
    15  		That(`<=s a b`).Puts(true),
    16  		That(`<=s b a`).Puts(false),
    17  		That(`==s haha haha`).Puts(true),
    18  		That(`==s 10 10.0`).Puts(false),
    19  		That(`!=s haha haha`).Puts(false),
    20  		That(`!=s 10 10.1`).Puts(true),
    21  		That(`>s a b`).Puts(false),
    22  		That(`>s 2 10`).Puts(true),
    23  		That(`>=s a a`).Puts(true),
    24  		That(`>=s a b`).Puts(false),
    25  		That(`>=s b a`).Puts(true),
    26  	)
    27  }
    28  
    29  func TestToString(t *testing.T) {
    30  	Test(t,
    31  		That(`to-string str (num 1) $true`).Puts("str", "1", "$true"),
    32  	)
    33  }
    34  
    35  func TestBase(t *testing.T) {
    36  	Test(t,
    37  		That(`base 2 1 3 4 16 255`).Puts("1", "11", "100", "10000", "11111111"),
    38  		That(`base 16 42 233`).Puts("2a", "e9"),
    39  		That(`base 1 1`).Throws(AnyError),   // no base-1
    40  		That(`base 37 10`).Throws(AnyError), // no letter for base-37
    41  	)
    42  }
    43  
    44  func TestWcswidth(t *testing.T) {
    45  	Test(t,
    46  		That(`wcswidth 你好`).Puts(4),
    47  		That(`-override-wcwidth x 10; wcswidth 1x2x; -override-wcwidth x 1`).
    48  			Puts(22),
    49  	)
    50  }
    51  
    52  func TestEawk(t *testing.T) {
    53  	Test(t,
    54  		That(`echo "  ax  by cz  \n11\t22 33" | eawk [@a]{ put $a[-1] }`).
    55  			Puts("cz", "33"),
    56  		// Bad input type
    57  		That(`num 42 | eawk [@a]{ fail "this should not run" }`).
    58  			Throws(ErrInputOfEawkMustBeString),
    59  		// Propagation of exception
    60  		That(`
    61  			to-lines [1 2 3 4] | eawk [@a]{
    62  				if (==s 3 $a[1]) {
    63  					fail "stop eawk"
    64  				}
    65  				put $a[1]
    66  			}
    67  		`).Puts("1", "2").Throws(FailError{"stop eawk"}),
    68  		// break
    69  		That(`
    70  			to-lines [" a" "b\tc " "d" "e"] | eawk [@a]{
    71  				if (==s d $a[1]) {
    72  					break
    73  				} else {
    74  					put $a[-1]
    75  				}
    76  			}
    77  		`).Puts("a", "c"),
    78  		// continue
    79  		That(`
    80  			to-lines [" a" "b\tc " "d" "e"] | eawk [@a]{
    81  				if (==s d $a[1]) {
    82  					continue
    83  				} else {
    84  					put $a[-1]
    85  				}
    86  			}
    87  		`).Puts("a", "c", "e"),
    88  	)
    89  }