github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/structs/function_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/lmorg/murex/lang"
    10  	"github.com/lmorg/murex/lang/ref"
    11  	"github.com/lmorg/murex/test/count"
    12  )
    13  
    14  func init() {
    15  	rand.Seed(time.Now().UnixNano())
    16  }
    17  
    18  func TestAliases(t *testing.T) {
    19  	alias := "TestAlias"
    20  
    21  	count.Tests(t, 3)
    22  
    23  	lang.InitEnv()
    24  
    25  	if lang.GlobalAliases.Exists(alias) {
    26  		t.Fatalf("Expecting '%s' not to exist, yet it does", alias)
    27  	}
    28  
    29  	p := lang.NewTestProcess()
    30  	p.Name.Set("alias")
    31  	p.Parameters.DefineParsed([]string{alias + "=foobar"})
    32  	err := cmdAlias(p)
    33  	if err != nil {
    34  		t.Fatalf("Error calling cmdAlias(): %s", err.Error())
    35  	}
    36  
    37  	if !lang.GlobalAliases.Exists(alias) {
    38  		t.Fatalf("Expecting '%s' to be created, it did not", alias)
    39  	}
    40  
    41  	p = lang.NewTestProcess()
    42  	p.Name.Set("!alias")
    43  	p.IsNot = true
    44  	p.Parameters.DefineParsed([]string{alias})
    45  	err = cmdUnalias(p)
    46  	if err != nil {
    47  		t.Fatalf("Error calling cmdAlias() for the 2nd time: %s", err.Error())
    48  	}
    49  
    50  	if lang.GlobalAliases.Exists(alias) {
    51  		t.Fatalf("Expecting '%s' to be destroyed, it still exists", alias)
    52  	}
    53  }
    54  
    55  func TestFunction(t *testing.T) {
    56  	fn := "TestFunction"
    57  
    58  	count.Tests(t, 3)
    59  
    60  	lang.InitEnv()
    61  
    62  	if lang.MxFunctions.Exists(fn) {
    63  		t.Fatalf("Expecting '%s' not to exist, yet it does", fn)
    64  	}
    65  
    66  	p := lang.NewTestProcess()
    67  	p.Name.Set("function")
    68  	p.Parameters.DefineParsed([]string{fn, "{ test }"})
    69  	err := cmdFunc(p)
    70  	if err != nil {
    71  		t.Fatalf("Error calling cmdFunc(): %s", err.Error())
    72  	}
    73  
    74  	if !lang.MxFunctions.Exists(fn) {
    75  		t.Fatalf("Expecting '%s' to be created, it did not", fn)
    76  	}
    77  
    78  	p = lang.NewTestProcess()
    79  	p.Name.Set("!function")
    80  	p.IsNot = true
    81  	p.Parameters.DefineParsed([]string{fn})
    82  	err = cmdUnfunc(p)
    83  	if err != nil {
    84  		t.Fatalf("Error calling cmdFunc() for the 2nd time: %s", err.Error())
    85  	}
    86  
    87  	if lang.MxFunctions.Exists(fn) {
    88  		t.Fatalf("Expecting '%s' to be destroyed, it still exists", fn)
    89  	}
    90  }
    91  
    92  func TestPrivate(t *testing.T) {
    93  	fn := "TestPrivate"
    94  	mod := fmt.Sprintf("GoTest-%d", rand.Int())
    95  
    96  	count.Tests(t, 2)
    97  
    98  	lang.InitEnv()
    99  
   100  	if lang.PrivateFunctions.ExistsString(fn, mod) {
   101  		t.Fatalf("Expecting '%s/%s' not to exist, yet it does", mod, fn)
   102  	}
   103  
   104  	p := lang.NewTestProcess()
   105  	p.Name.Set("function")
   106  	p.FileRef = &ref.File{
   107  		Source: &ref.Source{
   108  			Module: mod,
   109  		},
   110  	}
   111  
   112  	p.Parameters.DefineParsed([]string{fn, "{ test }"})
   113  	err := cmdPrivate(p)
   114  	if err != nil {
   115  		t.Fatalf("Error calling cmdPrivate(): %s", err.Error())
   116  	}
   117  
   118  	if !lang.PrivateFunctions.ExistsString(fn, mod) {
   119  		t.Fatalf("Expecting '%s/%s' to be created, it did not", mod, fn)
   120  	}
   121  }