...

Source file src/github.com/leodido/go-urn/urn_test.go

Documentation: github.com/leodido/go-urn

     1  package urn
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestDefaultPrefixWhenString(t *testing.T) {
    11  	u := &URN{
    12  		ID: "pippo",
    13  		SS: "pluto",
    14  	}
    15  
    16  	assert.Equal(t, "urn:pippo:pluto", u.String())
    17  }
    18  
    19  func TestParseSignature(t *testing.T) {
    20  	urn, ok := Parse([]byte(``))
    21  	assert.Nil(t, urn)
    22  	assert.False(t, ok)
    23  }
    24  
    25  func TestJSONMarshaling(t *testing.T) {
    26  	t.Run("roundtrip", func(t *testing.T) {
    27  		// Marshal
    28  		expected := URN{ID: "oid", SS: "1.2.3.4"}
    29  		bytes, err := json.Marshal(expected)
    30  		if !assert.NoError(t, err) {
    31  			return
    32  		}
    33  		// Unmarshal
    34  		var actual URN
    35  		err = json.Unmarshal(bytes, &actual)
    36  		if !assert.NoError(t, err) {
    37  			return
    38  		}
    39  		assert.Equal(t, expected.String(), actual.String())
    40  	})
    41  
    42  	t.Run("invalid URN", func(t *testing.T) {
    43  		var actual URN
    44  		err := json.Unmarshal([]byte(`"not a URN"`), &actual)
    45  		assert.EqualError(t, err, "invalid URN: not a URN")
    46  	})
    47  
    48  	t.Run("empty", func(t *testing.T) {
    49  		var actual URN
    50  		err := actual.UnmarshalJSON(nil)
    51  		assert.EqualError(t, err, "unexpected end of JSON input")
    52  	})
    53  }
    54  

View as plain text