github.com/swaros/contxt/module/taskrun@v0.0.0-20240305083542-3dbd4436ac40/parshlp_test.go (about)

     1  package taskrun_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/swaros/contxt/module/taskrun"
     7  )
     8  
     9  func TestSplitArgsString(t *testing.T) {
    10  	line, args := taskrun.StringSplitArgs("function hello world", "arg")
    11  	if line != "function" {
    12  		t.Error("string shoul be function and not ", line)
    13  	}
    14  	if len(args) != 3 {
    15  		t.Error("unexpected count of args. expected 3", len(args))
    16  	}
    17  
    18  	if entr, ok := args["arg0"]; ok {
    19  		if entr != "function" {
    20  			t.Error("index 0 should be function. but not ", entr)
    21  		}
    22  	} else {
    23  		t.Error("expected mp entrie not exists arg0")
    24  	}
    25  
    26  	if entr, ok := args["arg1"]; ok {
    27  		if entr != "hello" {
    28  			t.Error("index 1 should be hello. but not ", entr)
    29  		}
    30  	} else {
    31  		t.Error("expected mp entrie not exists arg1")
    32  	}
    33  
    34  	if entr, ok := args["arg2"]; ok {
    35  		if entr != "world" {
    36  			t.Error("index 1 should be world. but not ", entr)
    37  		}
    38  	} else {
    39  		t.Error("expected mp entrie not exists arg2")
    40  	}
    41  }
    42  
    43  func TestParseArgs(t *testing.T) {
    44  	var args []string = []string{"check abc def", "mama check", "line", "line"}
    45  	result := taskrun.SplitArgs(args, "test", func(s string, m map[string]string) {
    46  		switch s {
    47  		case "check":
    48  
    49  			if entr, ok := m["test0"]; ok {
    50  				if entr != "check" {
    51  					t.Error("index 0 should be check. but not ", entr)
    52  				}
    53  			} else {
    54  				t.Error("expected mp entrie not exists test0")
    55  			}
    56  
    57  			if entr, ok := m["test1"]; ok {
    58  				if entr != "abc" {
    59  					t.Error("index 1 should be abc. but not ", entr)
    60  				}
    61  			} else {
    62  				t.Error("expected mp entrie not exists test1")
    63  			}
    64  
    65  			if entr, ok := m["test2"]; ok {
    66  				if entr != "def" {
    67  					t.Error("index 1 should be def. but not ", entr)
    68  				}
    69  			} else {
    70  				t.Error("expected mp entrie not exists test2")
    71  			}
    72  
    73  		case "mama":
    74  			if entr, ok := m["test0"]; ok {
    75  				if entr != "mama" {
    76  					t.Error("index 0 should be mama. but not ", entr)
    77  				}
    78  			} else {
    79  				t.Error("expected mp entrie not exists test0")
    80  			}
    81  
    82  			if entr, ok := m["test1"]; ok {
    83  				if entr != "check" {
    84  					t.Error("index 1 should be check. but not ", entr)
    85  				}
    86  			} else {
    87  				t.Error("expected mp entrie not exists test1")
    88  			}
    89  		}
    90  	})
    91  
    92  	if len(result) != 4 {
    93  		t.Error("unexpected length of result.", result)
    94  	}
    95  
    96  	if result[0] != "check" {
    97  		t.Error("unexpected cleared result for index 0")
    98  	}
    99  	if result[1] != "mama" {
   100  		t.Error("unexpected cleared result for index 1")
   101  	}
   102  	if result[2] != "line" {
   103  		t.Error("unexpected cleared result for index 2")
   104  	}
   105  	if result[3] != "line" {
   106  		t.Error("unexpected cleared result for index 3")
   107  	}
   108  }