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

     1  package lang
     2  
     3  /*
     4  	This test library relates to the testing framework within the murex
     5  	language itself rather than Go's test framework within the murex project.
     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  	"github.com/lmorg/murex/lang/ref"
    18  	"github.com/lmorg/murex/lang/stdio"
    19  	"github.com/lmorg/murex/utils"
    20  )
    21  
    22  func utBlock(plan *UnitTestPlan, fileRef *ref.File, block []rune, stdin []byte, dt string, property string, function string, results *TestResults, passed *bool) {
    23  	fork := ShellProcess.Fork(F_FUNCTION | F_CREATE_STDIN | F_CREATE_STDERR | F_CREATE_STDOUT)
    24  	fork.IsMethod = true
    25  	fork.Name.Set("(unit test " + property + ")")
    26  	fork.Stdin.SetDataType(dt)
    27  	_, err := fork.Stdin.Write(stdin)
    28  	if err != nil {
    29  		utAddReport(results, fileRef, plan, function, TestError, tMsgWriteErr(property, err))
    30  		*passed = false
    31  		return
    32  	}
    33  
    34  	exitNum, err := fork.Execute(block)
    35  	if err != nil {
    36  		utAddReport(results, fileRef, plan, function, TestError, tMsgCompileErr(property, err))
    37  		*passed = false
    38  		return
    39  	}
    40  
    41  	if exitNum == 0 {
    42  		utAddReport(results, fileRef, plan, function, TestInfo, tMsgExitNumZero(property))
    43  	} else {
    44  		utAddReport(results, fileRef, plan, function, TestFailed, tMsgExitNumNotZero(property, exitNum))
    45  		*passed = false
    46  	}
    47  
    48  	utReadAllOut(fork.Stdout, results, plan, fileRef, property, function, passed)
    49  	utReadAllErr(fork.Stderr, results, plan, fileRef, property, function, passed)
    50  }
    51  
    52  func utReadAllOut(std stdio.Io, results *TestResults, plan *UnitTestPlan, fileRef *ref.File, property string, function string, passed *bool) {
    53  	b, err := std.ReadAll()
    54  	if err != nil {
    55  		utAddReport(results, fileRef, plan, function, TestError,
    56  			tMsgReadErr("stdout", property, err))
    57  		*passed = false
    58  		return
    59  	}
    60  
    61  	if len(b) != 0 {
    62  		utAddReport(results, fileRef, plan, function, TestInfo,
    63  			tMsgStdout(property, utils.CrLfTrim(b)))
    64  	}
    65  }
    66  
    67  func utReadAllErr(std stdio.Io, results *TestResults, plan *UnitTestPlan, fileRef *ref.File, property string, function string, passed *bool) {
    68  	b, err := std.ReadAll()
    69  	if err != nil {
    70  		utAddReport(results, fileRef, plan, function, TestError,
    71  			tMsgReadErr("stderr", property, err))
    72  		*passed = false
    73  		return
    74  	}
    75  
    76  	if len(b) != 0 {
    77  		utAddReport(results, fileRef, plan, function, TestFailed,
    78  			tMsgStdout(property, utils.CrLfTrim(b)))
    79  		*passed = false
    80  	}
    81  }