github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/scanners/azure/arm/parser/armjson/decode_meta_test.go (about)

     1  package armjson
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  type TestParent struct {
    13  	Child *TestChild `json:"child"`
    14  }
    15  
    16  type TestChild struct {
    17  	Name   string
    18  	Line   int
    19  	Column int
    20  }
    21  
    22  func (t *TestChild) UnmarshalJSONWithMetadata(node Node) error {
    23  	t.Line = node.Range().Start.Line
    24  	t.Column = node.Range().Start.Column
    25  	return node.Decode(&t.Name)
    26  }
    27  
    28  func Test_DecodeWithMetadata(t *testing.T) {
    29  	example := []byte(`
    30  {
    31  	"child": "secret"
    32  }
    33  `)
    34  	var parent TestParent
    35  	metadata := types.NewTestMetadata()
    36  	require.NoError(t, Unmarshal(example, &parent, &metadata))
    37  	assert.Equal(t, 3, parent.Child.Line)
    38  	assert.Equal(t, 12, parent.Child.Column)
    39  	assert.Equal(t, "secret", parent.Child.Name)
    40  }