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

     1  package lang_test
     2  
     3  /*
     4  	This test library relates to using the Go testing framework to test murex's
     5  	framework for unit testing shell scripts.
     6  
     7  	The naming convention here is basically the inverse of Go's test naming
     8  	convention. ie Go source files will be named "test_unit.go" (because
     9  	calling it unit_test.go would mean it's a Go test rather than murex test)
    10  	and the code is named UnitTestPlans (etc) rather than TestUnitPlans (etc)
    11  	because the latter might suggest they would be used by `go test`. This
    12  	naming convention is a little counterintuitive but it at least avoids
    13  	naming conflicts with `go test`.
    14  */
    15  
    16  import (
    17  	"encoding/json"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/lmorg/murex/lang"
    22  	"github.com/lmorg/murex/lang/ref"
    23  	"github.com/lmorg/murex/test/count"
    24  )
    25  
    26  func TestRunTestNotATest(t *testing.T) {
    27  	count.Tests(t, 1)
    28  
    29  	lang.InitEnv()
    30  	lang.ShellProcess.Config.Set("test", "auto-report", false, nil)
    31  
    32  	fileRef := &ref.File{
    33  		Source: &ref.Source{
    34  			Filename: "foobar.mx",
    35  			Module:   "foo/bar",
    36  			DateTime: time.Now(),
    37  		},
    38  	}
    39  
    40  	plan := &lang.UnitTestPlan{
    41  		StdoutMatch: "foobar\n",
    42  	}
    43  
    44  	lang.MxFunctions.Define("foobar", nil, []rune("out foobar"), fileRef)
    45  
    46  	ut := new(lang.UnitTests)
    47  	ut.Add("random_string_that_shouldnt_exist_kjhadgkjsdhgfksdahfgsadhjsdfjksadfhs", plan, fileRef)
    48  
    49  	if ut.Run(lang.ShellProcess, "foobar") {
    50  		t.Error("TestRunTestNotATest passed when expected to fail")
    51  	} else {
    52  		b, err := json.MarshalIndent(lang.ShellProcess.Tests.Results.Dump(), "", "    ")
    53  		if err != nil {
    54  			panic(err)
    55  		}
    56  		t.Logf("Test report:\n%s", b)
    57  	}
    58  }
    59  
    60  func TestRunTestNotAFunction(t *testing.T) {
    61  	count.Tests(t, 1)
    62  
    63  	lang.InitEnv()
    64  	lang.ShellProcess.Config.Set("test", "auto-report", false, nil)
    65  
    66  	fileRef := &ref.File{
    67  		Source: &ref.Source{
    68  			Filename: "foobar.mx",
    69  			Module:   "foo/bar",
    70  			DateTime: time.Now(),
    71  		},
    72  	}
    73  
    74  	plan := &lang.UnitTestPlan{
    75  		StdoutMatch: "foobar\n",
    76  	}
    77  
    78  	lang.MxFunctions.Define("foobar", nil, []rune("out foobar"), fileRef)
    79  
    80  	ut := new(lang.UnitTests)
    81  	ut.Add("random_string_that_shouldnt_exist_kjhadgkjsdhgfksdahfgsadhjsdfjksadfhs", plan, fileRef)
    82  
    83  	if ut.Run(lang.ShellProcess, "random_string_that_shouldnt_exist_kjhadgkjsdhgfksdahfgsadhjsdfjksadfhs") {
    84  		t.Error("TestRunTestNotATest passed when expected to fail")
    85  	} else {
    86  		b, err := json.MarshalIndent(lang.ShellProcess.Tests.Results.Dump(), "", "    ")
    87  		if err != nil {
    88  			panic(err)
    89  		}
    90  		t.Logf("Test report:\n%s", b)
    91  	}
    92  }