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

     1  package lang_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	_ "github.com/lmorg/murex/builtins"
     8  	"github.com/lmorg/murex/test"
     9  )
    10  
    11  /*
    12  	TestMurexTestingFramework tests murex's testing framework using Go's testing
    13  	framework (confused? Essentially murex shell scripts have a testing framework
    14  	leveredged via the `test` builtin. This can be used for testing and debugging
    15  	murex shell script. The concept behind them is that you place all of the test
    16  	code within the normal shell script and they sit there idle while murex is
    17  	running. However the moment you enable the testing flag (via `config`) those
    18  	test builtins start writing their results to the test report for your review)
    19  
    20  	This Go source file tests that murex's test builtins and report functions
    21  	work by testing the Go code that resides behind them.
    22  */
    23  
    24  func TestMurexTestDefine(t *testing.T) {
    25  	tests := []test.MurexTest{
    26  		{
    27  			Block: fmt.Sprintf(`
    28  					pipe: %s%d
    29  
    30  					function test.%s.%d {
    31  						test: <null> config enable auto-report !verbose
    32  						config: set test report-format json
    33  						config: set test report-pipe %s%d
    34  
    35  						test: define bob {
    36  							"StdoutMatch": "%s.%d\n"
    37  						}
    38  
    39  						out: <test_bob> "%s.%d"
    40  					}
    41  
    42  					bg <err> {
    43  						<%s%d> 
    44  					}
    45  
    46  					test.%s.%d
    47  					sleep: 2
    48  					!pipe: %s%d
    49  				`,
    50  				t.Name(), 0,
    51  				t.Name(), 0,
    52  				t.Name(), 0,
    53  				t.Name(), 0,
    54  				t.Name(), 0,
    55  				t.Name(), 0,
    56  				t.Name(), 0,
    57  				t.Name(), 0,
    58  			),
    59  			Stdout: fmt.Sprintf("%s.[0-9]+", t.Name()),
    60  			Stderr: "PASSED",
    61  		},
    62  		{
    63  			Block: fmt.Sprintf(`
    64  					pipe: %s%d
    65  
    66  					function test.%s.%d {
    67  						test: <null> config enable auto-report !verbose
    68  						config: set test report-format json
    69  						config: set test report-pipe %s%d
    70  
    71  						test: define bob {
    72  							"StderrMatch": "%s.%d\n",
    73  							"ExitNum": 1
    74  						}
    75  
    76  						err: <test_bob> "%s.%d"
    77  					}
    78  
    79  					bg {
    80  						<%s%d> 
    81  					}
    82  
    83  					test.%s.%d
    84  					sleep: 2
    85  					!pipe: %s%d
    86  				`,
    87  				t.Name(), 1,
    88  				t.Name(), 1,
    89  				t.Name(), 1,
    90  				t.Name(), 1,
    91  				t.Name(), 1,
    92  				t.Name(), 1,
    93  				t.Name(), 1,
    94  				t.Name(), 1,
    95  			),
    96  			Stderr: fmt.Sprintf("%s.[0-9]+", t.Name()),
    97  			Stdout: "PASSED",
    98  		},
    99  	}
   100  
   101  	test.RunMurexTestsRx(tests, t)
   102  }
   103  
   104  func TestMurexTestUnit(t *testing.T) {
   105  	tests := []test.MurexTest{
   106  		{
   107  			Block: fmt.Sprintf(`
   108  					test: unit function test.%s.%d {
   109  						"StdoutMatch": "%s.%d\n"
   110  					}
   111  
   112  					function test.%s.%d {
   113  						out: "%s.%d"
   114  					}
   115  
   116  					source { test: run test.%s.%d }
   117  				`,
   118  				t.Name(), 0,
   119  				t.Name(), 0,
   120  				t.Name(), 0,
   121  				t.Name(), 0,
   122  				t.Name(), 0,
   123  			),
   124  			Stdout: "PASSED",
   125  		},
   126  		{
   127  			Block: fmt.Sprintf(`
   128  					test: unit function test.%s.%d {
   129  						"StdoutRegex": "%s.%d"
   130  					}
   131  
   132  					function test.%s.%d {
   133  						out: "%s.%d"
   134  					}
   135  
   136  					source { test: run test.%s.%d }
   137  				`,
   138  				t.Name(), 1,
   139  				t.Name(), 1,
   140  				t.Name(), 1,
   141  				t.Name(), 1,
   142  				t.Name(), 1,
   143  			),
   144  			Stdout: "PASSED",
   145  		},
   146  		{
   147  			Block: fmt.Sprintf(`
   148  					test: unit function test.%s.%d {
   149  						"StderrMatch": "%s.%d\n",
   150  						"ExitNum": 1
   151  					}
   152  
   153  					function test.%s.%d {
   154  						err: "%s.%d"
   155  					}
   156  
   157  					source { test: run test.%s.%d }
   158  				`,
   159  				t.Name(), 2,
   160  				t.Name(), 2,
   161  				t.Name(), 2,
   162  				t.Name(), 2,
   163  				t.Name(), 2,
   164  			),
   165  			Stdout: "PASSED",
   166  		},
   167  		{
   168  			Block: fmt.Sprintf(`
   169  					test: unit function test.%s.%d {
   170  						"StderrRegex": "%s.%d",
   171  						"ExitNum": 1
   172  					}
   173  
   174  					function test.%s.%d {
   175  						err: "%s.%d"
   176  					}
   177  
   178  					source { test: run test.%s.%d }
   179  				`,
   180  				t.Name(), 3,
   181  				t.Name(), 3,
   182  				t.Name(), 3,
   183  				t.Name(), 3,
   184  				t.Name(), 3,
   185  			),
   186  			Stdout: "PASSED",
   187  		},
   188  	}
   189  
   190  	test.RunMurexTestsRx(tests, t)
   191  }