github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/builtin_fn_str_test.go (about)

     1  package eval_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/markusbkk/elvish/pkg/eval"
     7  	. "github.com/markusbkk/elvish/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  		thatOutputErrorIsBubbled("to-string str"),
    33  	)
    34  }
    35  
    36  func TestBase(t *testing.T) {
    37  	Test(t,
    38  		That(`base 2 1 3 4 16 255`).Puts("1", "11", "100", "10000", "11111111"),
    39  		That(`base 16 42 233`).Puts("2a", "e9"),
    40  		That(`base 1 1`).Throws(ErrBadBase),
    41  		That(`base 37 10`).Throws(ErrBadBase),
    42  		thatOutputErrorIsBubbled("base 2 1"),
    43  	)
    44  }
    45  
    46  func TestWcswidth(t *testing.T) {
    47  	Test(t,
    48  		That(`wcswidth 你好`).Puts(4),
    49  		That(`-override-wcwidth x 10; wcswidth 1x2x; -override-wcwidth x 1`).
    50  			Puts(22),
    51  	)
    52  }
    53  
    54  func TestEawk(t *testing.T) {
    55  	Test(t,
    56  		That(`echo "  ax  by cz  \n11\t22 33" | eawk {|@a| put $a[-1] }`).
    57  			Puts("cz", "33"),
    58  		// Bad input type
    59  		That(`num 42 | eawk {|@a| fail "this should not run" }`).
    60  			Throws(ErrInputOfEawkMustBeString),
    61  		// Propagation of exception
    62  		That(`
    63  			to-lines [1 2 3 4] | eawk {|@a|
    64  				if (==s 3 $a[1]) {
    65  					fail "stop eawk"
    66  				}
    67  				put $a[1]
    68  			}
    69  		`).Puts("1", "2").Throws(FailError{"stop eawk"}),
    70  		// break
    71  		That(`
    72  			to-lines [" a" "b\tc " "d" "e"] | eawk {|@a|
    73  				if (==s d $a[1]) {
    74  					break
    75  				} else {
    76  					put $a[-1]
    77  				}
    78  			}
    79  		`).Puts("a", "c"),
    80  		// continue
    81  		That(`
    82  			to-lines [" a" "b\tc " "d" "e"] | eawk {|@a|
    83  				if (==s d $a[1]) {
    84  					continue
    85  				} else {
    86  					put $a[-1]
    87  				}
    88  			}
    89  		`).Puts("a", "c", "e"),
    90  	)
    91  }