github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/variables/expand_test.go (about)

     1  package variables
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/lang"
     7  	"github.com/lmorg/murex/lang/types"
     8  	"github.com/lmorg/murex/test/count"
     9  	"github.com/lmorg/murex/utils/home"
    10  )
    11  
    12  var (
    13  	testString = "|.|$foo|.|$bar|.|~|.|"
    14  	expString  = "|.|oof|.|rab|.|" + home.MyDir + "|.|"
    15  )
    16  
    17  // TestExpand tests the ExpandString function
    18  func TestExpand(t *testing.T) {
    19  	count.Tests(t, 3) // 3 tests in 1 string
    20  
    21  	lang.InitEnv()
    22  
    23  	err := lang.GlobalVariables.Set(lang.ShellProcess, "foo", "oof", types.String)
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  
    28  	err = lang.GlobalVariables.Set(lang.ShellProcess, "bar", "rab", types.String)
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  
    33  	r := Expand([]rune(testString))
    34  	if string(r) != expString {
    35  		t.Error("String didn't expand as expected")
    36  		t.Log("  testString:", testString)
    37  		t.Log("  expString: ", expString)
    38  		t.Log("  string(r): ", string(r))
    39  	}
    40  }
    41  
    42  // TestExpandString tests the ExpandString function
    43  func TestExpandString(t *testing.T) {
    44  	count.Tests(t, 3) // 3 tests in 1 string
    45  
    46  	lang.InitEnv()
    47  
    48  	err := lang.GlobalVariables.Set(lang.ShellProcess, "foo", "oof", types.String)
    49  	if err != nil {
    50  		t.Error(err)
    51  	}
    52  
    53  	err = lang.GlobalVariables.Set(lang.ShellProcess, "bar", "rab", types.String)
    54  	if err != nil {
    55  		t.Error(err)
    56  	}
    57  
    58  	s := ExpandString(testString)
    59  	if s != expString {
    60  		t.Error("String didn't expand as expected")
    61  		t.Log("  testString:", testString)
    62  		t.Log("  expString :", expString)
    63  		t.Log("  s:         ", s)
    64  	}
    65  }
    66  
    67  // TestCompare checks the Expand and ExpandString functions returns the same data (albeit in different data types)
    68  func TestCompare(t *testing.T) {
    69  	count.Tests(t, 1) // test comparison
    70  
    71  	r := Expand([]rune(testString))
    72  	s := ExpandString(testString)
    73  
    74  	if string(r) != s {
    75  		t.Error("Expand and ExpandString are not the same after data type conversion")
    76  	}
    77  }