github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/test_units_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 "fmt" 19 "sync/atomic" 20 "testing" 21 "time" 22 23 "github.com/lmorg/murex/lang" 24 "github.com/lmorg/murex/lang/ref" 25 "github.com/lmorg/murex/test/count" 26 ) 27 28 var uniq int32 29 30 type testUTPs struct { 31 Function string 32 TestBlock string 33 Passed bool 34 UTP lang.UnitTestPlan 35 } 36 37 func testRunTest(t *testing.T, plans []testUTPs) { 38 t.Helper() 39 count.Tests(t, len(plans)*2) 40 41 lang.InitEnv() 42 lang.ShellProcess.Config.Set("test", "auto-report", false, nil) 43 44 var pubPriv string 45 46 for i := range plans { 47 for j := 1; j < 3; j++ { // test public functions the private functions 48 49 fileRef := &ref.File{ 50 Source: &ref.Source{ 51 Filename: "foobar.mx", 52 Module: fmt.Sprintf("foobar/mod-%d-%d-%d", atomic.AddInt32(&uniq, 1), i, j), 53 DateTime: time.Now(), 54 }, 55 } 56 57 if j == 1 { 58 lang.MxFunctions.Define(plans[i].Function, nil, []rune(plans[i].TestBlock), fileRef) 59 pubPriv = "public" 60 } else { 61 lang.PrivateFunctions.Define(plans[i].Function, nil, []rune(plans[i].TestBlock), fileRef) 62 plans[i].Function = fileRef.Source.Module + "/" + plans[i].Function 63 pubPriv = "private" 64 } 65 66 ut := new(lang.UnitTests) 67 ut.Add(plans[i].Function, &plans[i].UTP, fileRef) 68 69 if ut.Run(lang.ShellProcess, plans[i].Function) != plans[i].Passed { 70 if plans[i].Passed { 71 t.Errorf("Unit test %s %d failed", pubPriv, i) 72 b, err := json.MarshalIndent(lang.ShellProcess.Tests.Results.Dump(), "", " ") 73 if err != nil { 74 panic(err) 75 } 76 77 t.Logf(" Test report:\n%s", b) 78 79 } else { 80 t.Errorf("Unit test %s %d passed when expected to fail", pubPriv, i) 81 } 82 } 83 lang.ShellProcess.Tests.Results = new(lang.TestResults) 84 } 85 } 86 }