...

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

Documentation: github.com/leodido/go-urn

     1  package urn
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  type equivalenceTestCase struct {
    11  	eq bool
    12  	lx []byte
    13  	rx []byte
    14  }
    15  
    16  var equivalenceTests = []equivalenceTestCase{
    17  	{
    18  		true,
    19  		[]byte("urn:foo:a123%2C456"),
    20  		[]byte("URN:FOO:a123%2c456"),
    21  	},
    22  	{
    23  		true,
    24  		[]byte("urn:example:a123%2Cz456"),
    25  		[]byte("URN:EXAMPLE:a123%2cz456"),
    26  	},
    27  	{
    28  		true,
    29  		[]byte("urn:foo:AbC123%2C456"),
    30  		[]byte("URN:FOO:AbC123%2c456"),
    31  	},
    32  	{
    33  		true,
    34  		[]byte("urn:foo:AbC123%2C456%1f"),
    35  		[]byte("URN:FOO:AbC123%2c456%1f"),
    36  	},
    37  	{
    38  		true,
    39  		[]byte("URN:foo:a123,456"),
    40  		[]byte("urn:foo:a123,456"),
    41  	},
    42  	{
    43  		true,
    44  		[]byte("URN:foo:a123,456"),
    45  		[]byte("urn:FOO:a123,456"),
    46  	},
    47  	{
    48  		true,
    49  		[]byte("urn:foo:a123,456"),
    50  		[]byte("urn:FOO:a123,456"),
    51  	},
    52  	{
    53  		true,
    54  		[]byte("urn:ciao:%2E"),
    55  		[]byte("urn:ciao:%2e"),
    56  	},
    57  	{
    58  		false,
    59  		[]byte("urn:foo:A123,456"),
    60  		[]byte("URN:foo:a123,456"),
    61  	},
    62  	{
    63  		false,
    64  		[]byte("urn:foo:A123,456"),
    65  		[]byte("urn:foo:a123,456"),
    66  	},
    67  	{
    68  		false,
    69  		[]byte("urn:foo:A123,456"),
    70  		[]byte("urn:FOO:a123,456"),
    71  	},
    72  	{
    73  		false,
    74  		[]byte("urn:example:a123%2Cz456"),
    75  		[]byte("urn:example:a123,z456"),
    76  	},
    77  	{
    78  		false,
    79  		[]byte("urn:example:A123,z456"),
    80  		[]byte("urn:example:a123,Z456"),
    81  	},
    82  }
    83  
    84  var equivalenceTests8141 = []equivalenceTestCase{
    85  	{
    86  		false,
    87  		[]byte("urn:example:a123,z456/bar"),
    88  		[]byte("urn:example:a123,z456/foo"),
    89  	},
    90  	{
    91  		true,
    92  		[]byte("urn:example:a123,z456?+abc"),
    93  		[]byte("urn:example:a123,z456?=xyz"),
    94  	},
    95  	{
    96  		true,
    97  		[]byte("urn:example:a123,z456#789"),
    98  		[]byte("urn:example:a123,z456?=xyz"),
    99  	},
   100  }
   101  
   102  func lexicalEqual(t *testing.T, ii int, tt equivalenceTestCase, os ...Option) {
   103  	t.Helper()
   104  	urnlx, oklx := Parse(tt.lx, os...)
   105  	urnrx, okrx := Parse(tt.rx, os...)
   106  
   107  	if oklx && okrx {
   108  		assert.True(t, urnlx.Equal(urnlx))
   109  		assert.True(t, urnrx.Equal(urnrx))
   110  
   111  		if tt.eq {
   112  			assert.True(t, urnlx.Equal(urnrx), ierror(ii))
   113  			assert.True(t, urnrx.Equal(urnlx), ierror(ii))
   114  		} else {
   115  			assert.False(t, urnlx.Equal(urnrx), ierror(ii))
   116  			assert.False(t, urnrx.Equal(urnlx), ierror(ii))
   117  		}
   118  	} else {
   119  		t.Log("Something wrong in the testing table ...")
   120  	}
   121  }
   122  
   123  func TestLexicalEquivalence(t *testing.T) {
   124  	for ii, tt := range equivalenceTests {
   125  		lexicalEqual(t, ii, tt, WithParsingMode(RFC2141Only))
   126  	}
   127  
   128  	// The r-component, q-component, and f-component not taken into account for purposes of URN-equivalence
   129  	// See [RFC8141#3.2](https://datatracker.ietf.org/doc/html/rfc8141#section-3.2)
   130  	for ii, tt := range append(equivalenceTests, equivalenceTests8141...) {
   131  		lexicalEqual(t, ii, tt, WithParsingMode(RFC8141Only))
   132  	}
   133  }
   134  
   135  func TestEqualNil(t *testing.T) {
   136  	u, ok := Parse([]byte("urn:hello:world"))
   137  	require.NotNil(t, u)
   138  	require.True(t, ok)
   139  	require.False(t, u.Equal(nil))
   140  }
   141  

View as plain text