...

Source file src/github.com/pelletier/go-toml/keysparsing_test.go

Documentation: github.com/pelletier/go-toml

     1  package toml
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func testResult(t *testing.T, key string, expected []string) {
     9  	parsed, err := parseKey(key)
    10  	t.Logf("key=%s expected=%s parsed=%s", key, expected, parsed)
    11  	if err != nil {
    12  		t.Fatal("Unexpected error:", err)
    13  	}
    14  	if len(expected) != len(parsed) {
    15  		t.Fatal("Expected length", len(expected), "but", len(parsed), "parsed")
    16  	}
    17  	for index, expectedKey := range expected {
    18  		if expectedKey != parsed[index] {
    19  			t.Fatal("Expected", expectedKey, "at index", index, "but found", parsed[index])
    20  		}
    21  	}
    22  }
    23  
    24  func testError(t *testing.T, key string, expectedError string) {
    25  	res, err := parseKey(key)
    26  	if err == nil {
    27  		t.Fatalf("Expected error, but successfully parsed key %s", res)
    28  	}
    29  	if fmt.Sprintf("%s", err) != expectedError {
    30  		t.Fatalf("Expected error \"%s\", but got \"%s\".", expectedError, err)
    31  	}
    32  }
    33  
    34  func TestBareKeyBasic(t *testing.T) {
    35  	testResult(t, "test", []string{"test"})
    36  }
    37  
    38  func TestBareKeyDotted(t *testing.T) {
    39  	testResult(t, "this.is.a.key", []string{"this", "is", "a", "key"})
    40  }
    41  
    42  func TestDottedKeyBasic(t *testing.T) {
    43  	testResult(t, "\"a.dotted.key\"", []string{"a.dotted.key"})
    44  }
    45  
    46  func TestBaseKeyPound(t *testing.T) {
    47  	testError(t, "hello#world", "invalid bare key character: #")
    48  }
    49  
    50  func TestUnclosedSingleQuotedKey(t *testing.T) {
    51  	testError(t, "'", "unclosed single-quoted key")
    52  }
    53  
    54  func TestUnclosedDoubleQuotedKey(t *testing.T) {
    55  	testError(t, "\"", "unclosed double-quoted key")
    56  }
    57  
    58  func TestInvalidStartKeyCharacter(t *testing.T) {
    59  	testError(t, "/", "invalid key character: /")
    60  }
    61  
    62  func TestInvalidSpaceInKey(t *testing.T) {
    63  	testError(t, "invalid key", "invalid key character after whitespace: k")
    64  }
    65  
    66  func TestQuotedKeys(t *testing.T) {
    67  	testResult(t, `hello."foo".bar`, []string{"hello", "foo", "bar"})
    68  	testResult(t, `"hello!"`, []string{"hello!"})
    69  	testResult(t, `foo."ba.r".baz`, []string{"foo", "ba.r", "baz"})
    70  
    71  	// escape sequences must not be converted
    72  	testResult(t, `"hello\tworld"`, []string{`hello\tworld`})
    73  }
    74  
    75  func TestEmptyKey(t *testing.T) {
    76  	testError(t, ``, "empty key")
    77  	testError(t, ` `, "empty key")
    78  	testResult(t, `""`, []string{""})
    79  }
    80  

View as plain text