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

     1  package taskrun_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/swaros/contxt/module/taskrun"
     7  )
     8  
     9  func TestSetAndGet(t *testing.T) {
    10  	taskrun.SetPH("lost1", "data")
    11  	value := taskrun.GetPH("lost1")
    12  	if value != "data" {
    13  		t.Error("unexpected result:'", value, "' expected was 'data'")
    14  	}
    15  
    16  	taskrun.ClearAll()
    17  	valueAfterClean := taskrun.GetPH("lost1")
    18  	if valueAfterClean != "" {
    19  		t.Error("unexpected result:'", valueAfterClean, "' should be empty after ClearAll")
    20  	}
    21  }
    22  
    23  func TestOverwrite(t *testing.T) {
    24  	taskrun.SetPH("check", "flower")
    25  	taskrun.SetPH("check", "main")
    26  	value := taskrun.GetPH("check")
    27  	if value != "main" {
    28  		t.Error("unexpected result:'", value, "' expected was 'main'")
    29  	}
    30  }
    31  
    32  func TestNotExists(t *testing.T) {
    33  	nonExists := taskrun.GetPH("whatever")
    34  	if nonExists != "" {
    35  		t.Error("unexpected result: [", nonExists, "] this sould be a empty string")
    36  	}
    37  
    38  }
    39  func TestBasicReplace(t *testing.T) {
    40  
    41  	taskrun.SetPH("test1", "here i am")
    42  	taskrun.SetPH("test2", "XXX")
    43  
    44  	testLine := "a: ${test1}"
    45  	testLine2 := "b: ${test2} and again ${test2}"
    46  
    47  	result := taskrun.HandlePlaceHolder(testLine)
    48  	if result != "a: here i am" {
    49  		t.Error("noting was replaced:'", testLine, "' => ", result)
    50  	}
    51  
    52  	result2 := taskrun.HandlePlaceHolder(testLine2)
    53  	if result2 != "b: XXX and again XXX" {
    54  		t.Error("noting was replaced:'", testLine2, "' => ", result2)
    55  	}
    56  
    57  }
    58  
    59  /* that seems an relic. but have to think about*/
    60  /*
    61  func TestAsync(t *testing.T) {
    62  	var wg sync.WaitGroup
    63  	wg.Add(1)
    64  	go WriteToMap(&wg, "async", "check2", 500, 100*time.Microsecond)
    65  	wg.Add(1)
    66  	go ReadFromMap(&wg, "async", 500, 80*time.Microsecond)
    67  	wg.Add(1)
    68  	go WriteToMap(&wg, "async", "check3", 300, 200*time.Microsecond)
    69  	wg.Add(1)
    70  	go ReadFromMap(&wg, "async", 280, 150*time.Microsecond)
    71  	wg.Wait()
    72  }
    73  
    74  func WriteToMap(waitGroup *sync.WaitGroup, key, value string, loops int, wait time.Duration) {
    75  	defer waitGroup.Done()
    76  	for i := 0; i < loops; i++ {
    77  		taskrun.SetPH(key, value)
    78  		time.Sleep(wait)
    79  	}
    80  }
    81  
    82  func ReadFromMap(waitGroup *sync.WaitGroup, key string, loops int, wait time.Duration) {
    83  	defer waitGroup.Done()
    84  	for i := 0; i < loops; i++ {
    85  		taskrun.GetPH(key)
    86  		time.Sleep(wait)
    87  	}
    88  }
    89  */