github.com/Jeffail/benthos/v3@v3.65.0/lib/util/text/util_test.go (about)

     1  package text
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Jeffail/benthos/v3/lib/message"
     7  )
     8  
     9  func TestInterpolatedString(t *testing.T) {
    10  	str := NewInterpolatedString("foobar")
    11  	if exp, act := "foobar", str.Get(message.New(nil)); exp != act {
    12  		t.Errorf("Wrong result: %v != %v", act, exp)
    13  	}
    14  
    15  	str = NewInterpolatedString("")
    16  	if exp, act := "", str.Get(message.New(nil)); exp != act {
    17  		t.Errorf("Wrong result: %v != %v", act, exp)
    18  	}
    19  
    20  	str = NewInterpolatedString("foo${!json_field:foo.bar}bar")
    21  	if exp, act := "foonullbar", str.Get(message.New(nil)); exp != act {
    22  		t.Errorf("Wrong result: %v != %v", act, exp)
    23  	}
    24  	if exp, act := "foobazbar", str.Get(message.New([][]byte{
    25  		[]byte(`{"foo":{"bar":"baz"}}`),
    26  	})); exp != act {
    27  		t.Errorf("Wrong result: %v != %v", act, exp)
    28  	}
    29  }
    30  
    31  func TestInterpolatedBytes(t *testing.T) {
    32  	b := NewInterpolatedBytes([]byte("foobar"))
    33  	if exp, act := "foobar", string(b.Get(message.New(nil))); exp != act {
    34  		t.Errorf("Wrong result: %v != %v", act, exp)
    35  	}
    36  
    37  	b = NewInterpolatedBytes([]byte(""))
    38  	if exp, act := "", string(b.Get(message.New(nil))); exp != act {
    39  		t.Errorf("Wrong result: %v != %v", act, exp)
    40  	}
    41  
    42  	b = NewInterpolatedBytes([]byte(nil))
    43  	if exp, act := "", string(b.Get(message.New(nil))); exp != act {
    44  		t.Errorf("Wrong result: %v != %v", act, exp)
    45  	}
    46  
    47  	b = NewInterpolatedBytes(nil)
    48  	if exp, act := "", string(b.Get(message.New(nil))); exp != act {
    49  		t.Errorf("Wrong result: %v != %v", act, exp)
    50  	}
    51  
    52  	b = NewInterpolatedBytes([]byte("foo${!json_field:foo.bar}bar"))
    53  	if exp, act := "foonullbar", string(b.Get(message.New(nil))); exp != act {
    54  		t.Errorf("Wrong result: %v != %v", act, exp)
    55  	}
    56  	if exp, act := "foobazbar", string(b.Get(message.New([][]byte{
    57  		[]byte(`{"foo":{"bar":"baz"}}`),
    58  	}))); exp != act {
    59  		t.Errorf("Wrong result: %v != %v", act, exp)
    60  	}
    61  }