فهرست منبع

Update: Extensive test cases table

Leonardo Di Donato 8 سال پیش
والد
کامیت
cc62d9de2b
1فایلهای تغییر یافته به همراه56 افزوده شده و 16 حذف شده
  1. 56 16
      urn_test.go

+ 56 - 16
urn_test.go

@@ -1,19 +1,21 @@
 package urn
 
 import (
+	"strconv"
 	"testing"
 
-	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 )
 
-var tests = []struct {
+type testCase struct {
 	in   string // the input
 	ok   bool   // whether it is valid or not
 	obj  *URN   // a pointer to the resulting urn.URN instance
 	col  int    // the colum where the parsing error occurres
 	tree string // the tree representation
-}{
+}
+
+var tests = []testCase{
 	// ok
 	{"urn:simple:simple", true, &URN{ID: "simple", SS: "simple"}, -1, "(urn urn : (iD simple) : (sS simple) <EOF>)"},
 
@@ -31,6 +33,9 @@ var tests = []struct {
 
 	// ok - minimum urn
 	{"urn:a:b", true, &URN{ID: "a", SS: "b"}, -1, "(urn urn : (iD a) : (sS b) <EOF>)"},
+	{"urn:a::", true, &URN{ID: "a", SS: ":"}, -1, "(urn urn : (iD a) : (sS :) <EOF>)"},
+	{"urn:a:-", true, &URN{ID: "a", SS: "-"}, -1, "(urn urn : (iD a) : (sS -) <EOF>)"},
+	{"urn:a:%", true, &URN{ID: "a", SS: "%"}, -1, "(urn urn : (iD a) : (sS %) <EOF>)"},
 
 	// ok - URN prefix is case-insensitive
 	{"URN:simple:simple", true, &URN{ID: "simple", SS: "simple"}, -1, "(urn URN : (iD simple) : (sS simple) <EOF>)"},
@@ -52,48 +57,83 @@ var tests = []struct {
 	{"URN:a12:x", true, &URN{ID: "a12", SS: "x"}, -1, "(urn URN : (iD a12) : (sS x) <EOF>)"},
 	{"URN:cd2:x", true, &URN{ID: "cd2", SS: "x"}, -1, "(urn URN : (iD cd2) : (sS x) <EOF>)"},
 
-	// ok - ID can contain an hyphen but not in its first position
+	// ok - ID can contain an hyphen (not in its first position, see below)
 	{"URN:abcd-:x", true, &URN{ID: "abcd-", SS: "x"}, -1, "(urn URN : (iD abcd-) : (sS x) <EOF>)"},
 	{"URN:abcd-abcd:x", true, &URN{ID: "abcd-abcd", SS: "x"}, -1, "(urn URN : (iD abcd-abcd) : (sS x) <EOF>)"},
 	{"URN:a123-456z:x", true, &URN{ID: "a123-456z", SS: "x"}, -1, "(urn URN : (iD a123-456z) : (sS x) <EOF>)"},
 
-	// ok - SS can exactly contain the "urn" string
+	// ok - SS can contain the "urn" string, also be exactly equal to it
 	{"urn:urnx:urn", true, &URN{ID: "urnx", SS: "urn"}, -1, "(urn urn : (iD urnx) : (sS urn) <EOF>)"},
 	{"urn:urnurnurn:urn", true, &URN{ID: "urnurnurn", SS: "urn"}, -1, "(urn urn : (iD urnurnurn) : (sS urn) <EOF>)"},
+	{"urn:hey:urnurnurn", true, &URN{ID: "hey", SS: "urnurnurn"}, -1, "(urn urn : (iD hey) : (sS urnurnurn) <EOF>)"},
 
-	// ok - SS can contains multiple colons
+	// ok - SS can contains and discerns multiple colons, also at the end
 	{"urn:ciao:a:b:c", true, &URN{ID: "ciao", SS: "a:b:c"}, -1, "(urn urn : (iD ciao) : (sS a : b : c) <EOF>)"},
+	{"urn:aaa:x:y:", true, &URN{ID: "aaa", SS: "x:y:"}, -1, "(todo)"},
+	{"urn:aaa:x:y:", true, &URN{ID: "aaa", SS: "x:y:"}, -1, "(todo)"},
+
+	// ok - SS can contain (and also start with) some non-alphabetical characters
+	{"urn:ciao:-", true, &URN{ID: "ciao", SS: "-"}, -1, "(urn urn : (iD ciao) : (sS -) <EOF>)"},
+	{"urn:ciao::", true, &URN{ID: "ciao", SS: ":"}, -1, "(urn urn : (iD ciao) : (sS :) <EOF>)"},
+	{"urn:ciao:!", true, &URN{ID: "ciao", SS: "!"}, -1, "(urn urn : (iD ciao) : (sS !) <EOF>)"},
+	{"urn:ciao:!?", true, &URN{ID: "ciao", SS: "!?"}, -1, "(urn urn : (iD ciao) : (sS !?) <EOF>)"},
+	{"urn:ciao:-!:?-,:x", true, &URN{ID: "ciao", SS: "-!:?-,:x"}, -1, "(urn urn : (iD ciao) : (sS -!:?-,:x) <EOF>)"},
+	{"urn:ciao:###", true, &URN{ID: "ciao", SS: "###"}, -1, "(urn urn : (iD ciao) : (sS ###) <EOF>)"},
+	{"urn:ciao:#?!#(xyz)+a,b.*@g=$_'", true, &URN{ID: "ciao", SS: "#?!#(xyz)+a,b.*@g=$_'"}, -1, "(urn urn : (iD ciao) : (sS #?!#(xyz)+a,b.*@g=$_') <EOF>)"},
+
+	// ok - SS can contain (and also start with) hexadecimal representation of octets  // (todo)
 
 	// no - ID can not start with an hyphen
-	// {"URN:-xxx:x", false, nil, 5, ""}, // (fixme) - hyphen probably causes overlap between IDENTIFIER and CHARS
+	{"URN:-xxx:x", false, nil, 4, ""},
+	{"URN:---xxx:x", false, nil, 4, ""},
+
+	// no - ID can not start with a colon
+	{"urn::colon:nss", false, nil, 4, ""},
+	{"urn::::nss", false, nil, 4, ""},
 
 	// no - ID can not contains more than 32 characters
 	{"urn:abcdefghilmnopqrstuvzabcdefghilmn:specificstring", false, nil, 4 + 33, ""},
 
+	// no - ID can not contain special characters
+	{"URN:a!?:x", false, nil, 7, ""},
+	{"URN:#,:x", false, nil, 6, ""},
+	{"URN:bc'.@:x", false, nil, 9, ""},
+
 	// no - ID can not be equal to "urn"
 	{"urn:urn:NSS", false, nil, 4, ""},
 
+	// no - ID can not contain spaces
+	{"urn:white space:NSS", false, nil, 9, ""},
+
+	// no - SS can not contain spaces
+	{"urn:concat:no spaces", false, nil, 13, ""},
+
 	// no - Incomplete URNs
 	{"urn:", false, nil, 4, ""},
+	{"urn::", false, nil, 4, ""},
 	{"urn:a", false, nil, 5, ""},
 	{"urn:a:", false, nil, 6, ""},
-	// {"urn:a:x:", false, nil, 8, ""}, // (fixme) - probable issue with last colon
+}
+
+func herror(index int, test testCase) string {
+	return "Test case num. " + strconv.Itoa(index+1) + ", input \"" + test.in + "\""
 }
 
 func TestUrnParse(t *testing.T) {
-	for _, tt := range tests {
+	for ii, tt := range tests {
 		urn, err := Parse(tt.in)
 
 		if ok := err == nil; ok {
-			require.True(t, tt.ok)
+			require.True(t, tt.ok, herror(ii, tt))
+			require.Equal(t, tt.obj.ID, urn.ID, herror(ii, tt))
+			require.Equal(t, tt.obj.SS, urn.SS, herror(ii, tt))
+			// require.Equal(t, tt.tree, urn.Tree(), herror(ii, tt)) // (fixme) > flatten tree?
 			// Ignoring column testing since there is no error
-			assert.Equal(t, tt.obj.ID, urn.ID)
-			assert.Equal(t, tt.obj.SS, urn.SS)
-			assert.Equal(t, tt.tree, urn.Tree())
 		} else {
-			require.False(t, tt.ok)
-			assert.Equal(t, tt.col, err.(*Error).Column)
-			assert.Empty(t, urn)
+			require.False(t, tt.ok, herror(ii, tt))
+			require.Equal(t, tt.col, err.(*Error).Column, herror(ii, tt))
+			require.Empty(t, urn, herror(ii, tt))
+			//require.Empty(t, urn.Tree(), herror(ii, tt))
 		}
 	}
 }