github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/internal/sh/builtin/len_test.go (about)

     1  package builtin_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/madlambda/nash/internal/sh/internal/fixture"
     8  )
     9  
    10  func TestLen(t *testing.T) {
    11  	sh, cleanup := fixture.SetupShell(t)
    12  	defer cleanup()
    13  
    14  	var out bytes.Buffer
    15  
    16  	sh.SetStdout(&out)
    17  
    18  	err := sh.Exec(
    19  		"test len",
    20  		`var a = (1 2 3 4 5 6 7 8 9 0)
    21  		 var len_a <= len($a)
    22  		 echo -n $len_a`,
    23  	)
    24  
    25  	if err != nil {
    26  		t.Error(err)
    27  		return
    28  	}
    29  
    30  	if "10" != string(out.Bytes()) {
    31  		t.Errorf("String differs: '%s' != '%s'", "10", string(out.Bytes()))
    32  		return
    33  	}
    34  
    35  	// Having to reset is a bad example of why testing N stuff together
    36  	// is asking for trouble :-).
    37  	out.Reset()
    38  
    39  	err = sh.Exec(
    40  		"test len fail",
    41  		`a = "test"
    42  		 var l <= len($a)
    43  		 echo -n $l
    44  		`,
    45  	)
    46  
    47  	if err != nil {
    48  		t.Error(err)
    49  		return
    50  	}
    51  
    52  	if "4" != string(out.Bytes()) {
    53  		t.Errorf("String differs: '%s' != '%s'", "4", string(out.Bytes()))
    54  		return
    55  	}
    56  }