...

Source file src/github.com/drone/envsubst/v2/funcs_test.go

Documentation: github.com/drone/envsubst/v2

     1  package envsubst
     2  
     3  import "testing"
     4  
     5  func Test_len(t *testing.T) {
     6  	got, want := toLen("Hello World"), "11"
     7  	if got != want {
     8  		t.Errorf("Expect len function to return %s, got %s", want, got)
     9  	}
    10  }
    11  
    12  func Test_lower(t *testing.T) {
    13  	got, want := toLower("Hello World"), "hello world"
    14  	if got != want {
    15  		t.Errorf("Expect lower function to return %s, got %s", want, got)
    16  	}
    17  }
    18  
    19  func Test_lowerFirst(t *testing.T) {
    20  	got, want := toLowerFirst("HELLO WORLD"), "hELLO WORLD"
    21  	if got != want {
    22  		t.Errorf("Expect lowerFirst function to return %s, got %s", want, got)
    23  	}
    24  	defer func() {
    25  		if recover() != nil {
    26  			t.Errorf("Expect empty string does not panic lowerFirst")
    27  		}
    28  	}()
    29  	toLowerFirst("")
    30  }
    31  
    32  func Test_upper(t *testing.T) {
    33  	got, want := toUpper("Hello World"), "HELLO WORLD"
    34  	if got != want {
    35  		t.Errorf("Expect upper function to return %s, got %s", want, got)
    36  	}
    37  }
    38  
    39  func Test_upperFirst(t *testing.T) {
    40  	got, want := toUpperFirst("hello world"), "Hello world"
    41  	if got != want {
    42  		t.Errorf("Expect upperFirst function to return %s, got %s", want, got)
    43  	}
    44  	defer func() {
    45  		if recover() != nil {
    46  			t.Errorf("Expect empty string does not panic upperFirst")
    47  		}
    48  	}()
    49  	toUpperFirst("")
    50  }
    51  
    52  func Test_default(t *testing.T) {
    53  	got, want := toDefault("Hello World", "Hola Mundo"), "Hello World"
    54  	if got != want {
    55  		t.Errorf("Expect default function uses variable value")
    56  	}
    57  
    58  	got, want = toDefault("", "Hola Mundo"), "Hola Mundo"
    59  	if got != want {
    60  		t.Errorf("Expect default function uses default value, when variable empty. Got %s, Want %s", got, want)
    61  	}
    62  
    63  	got, want = toDefault("", "Hola Mundo", "-Bonjour le monde", "-Halló heimur"), "Hola Mundo-Bonjour le monde-Halló heimur"
    64  	if got != want {
    65  		t.Errorf("Expect default function to use concatenated args when variable empty. Got %s, Want %s", got, want)
    66  	}
    67  }
    68  
    69  func Test_substr(t *testing.T) {
    70  	got, want := toSubstr("123456789123456789", "0", "8"), "12345678"
    71  	if got != want {
    72  		t.Errorf("Expect substr function to cut from beginning to length")
    73  	}
    74  
    75  	got, want = toSubstr("123456789123456789", "1", "8"), "23456789"
    76  	if got != want {
    77  		t.Errorf("Expect substr function to cut from offset to length")
    78  	}
    79  
    80  	got, want = toSubstr("123456789123456789", "9"), "123456789"
    81  	if got != want {
    82  		t.Errorf("Expect substr function to cut beginnging with offset")
    83  	}
    84  
    85  	got, want = toSubstr("123456789123456789", "9", "50"), "123456789"
    86  	if got != want {
    87  		t.Errorf("Expect substr function to ignore length if out of bound")
    88  	}
    89  
    90  	got, want = toSubstr("123456789123456789", "-3", "2"), "78"
    91  	if got != want {
    92  		t.Errorf("Expect substr function to count negative offsets from the end")
    93  	}
    94  
    95  	got, want = toSubstr("123456789123456789", "-300", "3"), "123"
    96  	if got != want {
    97  		t.Errorf("Expect substr function to cut from the beginning to length for negative offsets exceeding string length")
    98  	}
    99  	
   100  	got, want = toSubstr("12345678", "9", "1"), ""
   101  	if got != want {
   102  		t.Errorf("Expect substr function to cut entire string if pos is itself out of bound")
   103  	}
   104  }
   105  

View as plain text