github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/variables_reserved_test.go (about)

     1  package lang_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/lmorg/murex/test"
     9  )
    10  
    11  func TestVarSelf(t *testing.T) {
    12  	tests := []test.MurexTest{
    13  		// TTY
    14  		{
    15  			Block: `
    16  				function TestVarSelf {
    17  					$SELF -> [TTY]
    18  				}
    19  				TestVarSelf
    20  			`,
    21  			Stdout: "false",
    22  		},
    23  
    24  		// Method
    25  		{
    26  			Block: `
    27  				function TestVarSelf {
    28  					$SELF -> [Method]
    29  				}
    30  				TestVarSelf
    31  			`,
    32  			Stdout: "false",
    33  		},
    34  		{
    35  			Block: `
    36  				function TestVarSelf {
    37  					$SELF -> [Method]
    38  				}
    39  				out foobar -> TestVarSelf
    40  			`,
    41  			Stdout: "true",
    42  		},
    43  
    44  		// Not
    45  		{
    46  			Block: `
    47  				function TestVarSelf {
    48  					$SELF -> [Not]
    49  				}
    50  				TestVarSelf
    51  			`,
    52  			Stdout: "false",
    53  		},
    54  		{
    55  			Block: `
    56  				function !TestVarSelf {
    57  					$SELF -> [Not]
    58  				}
    59  				!TestVarSelf
    60  			`,
    61  			Stdout: "true",
    62  		},
    63  
    64  		// Background
    65  		{
    66  			Block: `
    67  				function TestVarSelf {
    68  					$SELF -> [Background]
    69  				}
    70  				TestVarSelf
    71  			`,
    72  			Stdout: "false",
    73  		},
    74  		{
    75  			Block: `
    76  				function TestVarSelf {
    77  					$SELF -> [Background]
    78  				}
    79  				bg { TestVarSelf }
    80  				sleep 1
    81  			`,
    82  			Stdout: "true",
    83  		},
    84  	}
    85  
    86  	test.RunMurexTests(tests, t)
    87  }
    88  
    89  func TestVarArgs(t *testing.T) {
    90  	tests := []test.MurexTest{
    91  		{
    92  			Block: `
    93  				function TestVarArgs {
    94  					out @ARGS
    95  				}
    96  				TestVarArgs
    97  			`,
    98  			Stdout: "TestVarArgs\n",
    99  		},
   100  		{
   101  			Block: `
   102  				function TestVarArgs {
   103  					out @ARGS
   104  				}
   105  				TestVarArgs 1 2 3
   106  			`,
   107  			Stdout: "TestVarArgs 1 2 3\n",
   108  		},
   109  		{
   110  			Block: `
   111  				function TestVarArgs {
   112  					out @ARGS
   113  				}
   114  				TestVarArgs 1   2   3
   115  			`,
   116  			Stdout: "TestVarArgs 1 2 3\n",
   117  		},
   118  		{
   119  			Block: `
   120  				function TestVarArgs {
   121  					out $ARGS
   122  				}
   123  				TestVarArgs 1   2   3
   124  			`,
   125  			Stdout: `["TestVarArgs","1","2","3"]` + "\n",
   126  		},
   127  	}
   128  
   129  	test.RunMurexTests(tests, t)
   130  }
   131  
   132  func TestVarParams(t *testing.T) {
   133  	tests := []test.MurexTest{
   134  		{
   135  			Block: `
   136  				function TestVarParams {
   137  					out $0
   138  				}
   139  				TestVarParams
   140  			`,
   141  			Stdout: "^TestVarParams\n$",
   142  		},
   143  		{
   144  			Block: `
   145  				function TestVarParams {
   146  					out $0
   147  				}
   148  				TestVarParams 1 2 3
   149  			`,
   150  			Stdout: "^TestVarParams\n$",
   151  		},
   152  		{
   153  			Block: `
   154  				function TestVarParams {
   155  					out @PARAMS
   156  				}
   157  				TestVarParams
   158  			`,
   159  			Stderr:  "array '@PARAMS' is empty\n",
   160  			ExitNum: 1,
   161  		},
   162  		{
   163  			Block: `
   164  				function TestVarParams {
   165  					out @PARAMS
   166  				}
   167  				TestVarParams 1 2 3
   168  			`,
   169  			Stdout: "^1 2 3\n$",
   170  		},
   171  		{
   172  			Block: `
   173  				function TestVarParams {
   174  					out @PARAMS
   175  				}
   176  				TestVarParams 1   2   3
   177  			`,
   178  			Stdout: "^1 2 3\n$",
   179  		},
   180  		{
   181  			Block: `
   182  				function TestVarParams {
   183  					out $PARAMS
   184  				}
   185  				TestVarParams 1   2   3
   186  			`,
   187  			Stdout: `\["1","2","3"\]`,
   188  		},
   189  	}
   190  
   191  	test.RunMurexTestsRx(tests, t)
   192  }
   193  
   194  func TestVarHostname(t *testing.T) {
   195  	hostname := func() string {
   196  		s, err := os.Hostname()
   197  		if err != nil {
   198  			t.Errorf(err.Error())
   199  		}
   200  		return s
   201  	}
   202  
   203  	tests := []test.MurexTest{
   204  		{
   205  			Block:  `out $HOSTNAME`,
   206  			Stdout: fmt.Sprintf("%s\n", hostname()),
   207  		},
   208  	}
   209  
   210  	test.RunMurexTests(tests, t)
   211  }
   212  
   213  func TestVarPwd(t *testing.T) {
   214  	pwd := func() string {
   215  		s, err := os.Getwd()
   216  		if err != nil {
   217  			t.Errorf(err.Error())
   218  		}
   219  		return s
   220  	}
   221  
   222  	tests := []test.MurexTest{
   223  		{
   224  			Block:  `out $PWD`,
   225  			Stdout: fmt.Sprintf("%s\n", pwd()),
   226  		},
   227  	}
   228  
   229  	test.RunMurexTests(tests, t)
   230  }