github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/std/memfile_test.go (about)

     1  package std
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestMemPackage_Validate(t *testing.T) {
    10  	tt := []struct {
    11  		name        string
    12  		mpkg        *MemPackage
    13  		errContains string
    14  	}{
    15  		{
    16  			"Correct",
    17  			&MemPackage{
    18  				Name:  "hey",
    19  				Path:  "gno.land/r/demo/hey",
    20  				Files: []*MemFile{{Name: "a.gno"}},
    21  			},
    22  			"",
    23  		},
    24  		{
    25  			"Unsorted",
    26  			&MemPackage{
    27  				Name:  "hey",
    28  				Path:  "gno.land/r/demo/hey",
    29  				Files: []*MemFile{{Name: "b.gno"}, {Name: "a.gno"}},
    30  			},
    31  			`mempackage "gno.land/r/demo/hey" has unsorted files`,
    32  		},
    33  		{
    34  			"Duplicate",
    35  			&MemPackage{
    36  				Name:  "hey",
    37  				Path:  "gno.land/r/demo/hey",
    38  				Files: []*MemFile{{Name: "a.gno"}, {Name: "a.gno"}},
    39  			},
    40  			`duplicate file name "a.gno"`,
    41  		},
    42  	}
    43  	for _, tc := range tt {
    44  		t.Run(tc.name, func(t *testing.T) {
    45  			err := tc.mpkg.Validate()
    46  			if tc.errContains == "" {
    47  				assert.NoError(t, err)
    48  			} else {
    49  				assert.ErrorContains(t, err, tc.errContains)
    50  			}
    51  		})
    52  	}
    53  }
    54  
    55  func TestRePkgOrRlmPath(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	testTable := []struct {
    59  		desc, in string
    60  		expected bool
    61  	}{
    62  		{
    63  			desc:     "Valid p",
    64  			in:       "gno.land/p/path/path",
    65  			expected: true,
    66  		},
    67  		{
    68  			desc:     "Valid r",
    69  			in:       "gno.land/r/path/path",
    70  			expected: true,
    71  		},
    72  		{
    73  			desc:     "Leading Underscore",
    74  			in:       "gno.land/r/path/_path",
    75  			expected: true,
    76  		},
    77  		{
    78  			desc:     "Trailing Underscore",
    79  			in:       "gno.land/r/path/path_",
    80  			expected: true,
    81  		},
    82  		{
    83  			desc:     "Underscore in Between",
    84  			in:       "gno.land/r/path/p_ath",
    85  			expected: true,
    86  		},
    87  		{
    88  			desc:     "Invalid With Underscore 1",
    89  			in:       "gno.land/r/path/_",
    90  			expected: false,
    91  		},
    92  		{
    93  			desc:     "Invalid With Underscore 2",
    94  			in:       "gno.land/r/path/_/_",
    95  			expected: false,
    96  		},
    97  		{
    98  			desc:     "Invalid With Underscore 3",
    99  			in:       "gno.land/r/path/__/path",
   100  			expected: false,
   101  		},
   102  		{
   103  			desc:     "Invalid With Hyphen",
   104  			in:       "gno.land/r/path/pa-th",
   105  			expected: false,
   106  		},
   107  		{
   108  			desc:     "Invalid x",
   109  			in:       "gno.land/x/path/path",
   110  			expected: false,
   111  		},
   112  		{
   113  			desc:     "Missing Path 1",
   114  			in:       "gno.land/p",
   115  			expected: false,
   116  		},
   117  		{
   118  			desc:     "Missing Path 2",
   119  			in:       "gno.land/p/",
   120  			expected: false,
   121  		},
   122  		{
   123  			desc:     "Invalid domain",
   124  			in:       "github.com/p/path/path",
   125  			expected: false,
   126  		},
   127  		{
   128  			desc:     "Special Character 1",
   129  			in:       "gno.land/p/p@th/abc/def",
   130  			expected: false,
   131  		},
   132  		{
   133  			desc:     "Special Character 2",
   134  			in:       "gno.land/p/p&th/abc/def",
   135  			expected: false,
   136  		},
   137  		{
   138  			desc:     "Special Character 3",
   139  			in:       "gno.land/p/p&%$#h/abc/def",
   140  			expected: false,
   141  		},
   142  		{
   143  			desc:     "Leading Number",
   144  			in:       "gno.land/p/1Path/abc/def",
   145  			expected: false,
   146  		},
   147  		{
   148  			desc:     "Uppercase Letters",
   149  			in:       "gno.land/p/PaTh/abc/def",
   150  			expected: false,
   151  		},
   152  		{
   153  			desc:     "Empty Path Part",
   154  			in:       "gno.land/p/path//def",
   155  			expected: false,
   156  		},
   157  		{
   158  			desc:     "Trailing Slash",
   159  			in:       "gno.land/p/path/abc/def/",
   160  			expected: false,
   161  		},
   162  		{
   163  			desc:     "Extra Slash(s)",
   164  			in:       "gno.land/p/path///abc/def",
   165  			expected: false,
   166  		},
   167  		{
   168  			desc:     "Valid Long path",
   169  			in:       "gno.land/r/very/very/very/long/path",
   170  			expected: true,
   171  		},
   172  		{
   173  			desc:     "Long Path With Special Character 1",
   174  			in:       "gno.land/r/very/very/very/long/p@th",
   175  			expected: false,
   176  		},
   177  		{
   178  			desc:     "Long Path With Special Character 2",
   179  			in:       "gno.land/r/very/very/v%ry/long/path",
   180  			expected: false,
   181  		},
   182  		{
   183  			desc:     "Long Path With Trailing Slash",
   184  			in:       "gno.land/r/very/very/very/long/path/",
   185  			expected: false,
   186  		},
   187  		{
   188  			desc:     "Long Path With Empty Path Part",
   189  			in:       "gno.land/r/very/very/very//long/path/",
   190  			expected: false,
   191  		},
   192  	}
   193  
   194  	for _, tc := range testTable {
   195  		tc := tc
   196  		t.Run(tc.desc, func(t *testing.T) {
   197  			t.Parallel()
   198  
   199  			assert.Equal(t, tc.expected, rePkgOrRlmPath.MatchString(tc.in))
   200  		})
   201  	}
   202  }