github.com/jdolitsky/cnab-go@v0.7.1-beta1/bundle/replacement/jsonreplacer_test.go (about)

     1  package replacement
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestCanReplaceInJSON(t *testing.T) {
    11  	source := `{
    12  	"a": 1,
    13  	"b": {
    14  		"c": "d",
    15  		"e": "f"
    16  	}
    17  }`
    18  	r := NewJSONReplacer("\t")
    19  	result, err := r.Replace(source, "b.c", "test")
    20  	if err != nil {
    21  		t.Fatalf("Replace failed: %s", err)
    22  	}
    23  
    24  	expected := strings.Replace(source, "d", "test", -1)
    25  
    26  	is := assert.New(t)
    27  	is.Equal(strings.TrimSpace(expected), strings.TrimSpace(result))
    28  }
    29  
    30  func TestJSONErrorIfPathNotFound(t *testing.T) {
    31  	source := `{
    32  	"a": 1,
    33  	"b": {
    34  		"c": "d",
    35  		"e": "f"
    36  	}
    37  }`
    38  	r := NewJSONReplacer("\t")
    39  
    40  	_, err := r.Replace(source, "b.c.d", "test")
    41  	if err != ErrSelectorNotFound {
    42  		t.Error("Expected path not found error for b.c.d")
    43  	}
    44  
    45  	_, err = r.Replace(source, "b.d", "test")
    46  	if err != ErrSelectorNotFound {
    47  		t.Error("Expected path not found error for b.d")
    48  	}
    49  }