github.com/yanndegat/hiera@v0.6.8/api/key_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	require "github.com/lyraproj/dgo/dgo_test"
     8  	"github.com/lyraproj/dgo/vf"
     9  	"github.com/yanndegat/hiera/api"
    10  )
    11  
    12  func ExampleNewKey_simple() {
    13  	key := api.NewKey(`simple`)
    14  	fmt.Printf(`%s, %d`, key.Source(), len(key.Parts()))
    15  	// Output: simple, 1
    16  }
    17  
    18  func ExampleNewKey_dotted() {
    19  	key := api.NewKey(`a.b.c`)
    20  	fmt.Printf(`%s, %d`, key.Source(), len(key.Parts()))
    21  	// Output: a.b.c, 3
    22  }
    23  
    24  func ExampleNewKey_dotted_int() {
    25  	key := api.NewKey(`a.3`)
    26  	fmt.Printf(`%T`, key.Parts()[1])
    27  	// Output: int
    28  }
    29  
    30  func ExampleNewKey_quoted() {
    31  	key := api.NewKey(`'a.b.c'`)
    32  	fmt.Printf(`%s, %d`, key.Source(), len(key.Parts()))
    33  	// Output: 'a.b.c', 1
    34  }
    35  
    36  func ExampleNewKey_doubleQuoted() {
    37  	key := api.NewKey(`"a.b.c"`)
    38  	fmt.Printf(`%s, %d`, key.Source(), len(key.Parts()))
    39  	// Output: "a.b.c", 1
    40  }
    41  
    42  func ExampleNewKey_quotedDot() {
    43  	key := api.NewKey(`a.'b.c'`)
    44  	fmt.Printf(`%s, %d, %s`, key.Source(), len(key.Parts()), key.Parts()[1])
    45  	// Output: a.'b.c', 2, b.c
    46  }
    47  
    48  func TestNewKey_quotedDotX(t *testing.T) {
    49  	key := api.NewKey(`a.'b.c'.d`)
    50  	require.Equal(t, 3, len(key.Parts()))
    51  	require.Equal(t, `b.c`, key.Parts()[1])
    52  }
    53  
    54  func TestNewKey_quotedQuote(t *testing.T) {
    55  	key := api.NewKey(`a.b.'c"d"e'`)
    56  	require.Equal(t, `c"d"e`, key.Parts()[2])
    57  }
    58  
    59  func TestNewKey_doubleQuotedQuote(t *testing.T) {
    60  	key := api.NewKey(`a.b."c'd'e"`)
    61  	require.Equal(t, `c'd'e`, key.Parts()[2])
    62  }
    63  
    64  func TestNewKey_unterminatedQuoted(t *testing.T) {
    65  	require.Panic(t, func() { api.NewKey(`a.b."c`) }, `unterminated quote in key 'a\.b\."c'`)
    66  }
    67  
    68  func TestNewKey_empty(t *testing.T) {
    69  	require.Panic(t, func() { api.NewKey(``) }, `key '' contains an empty segment`)
    70  }
    71  
    72  func TestNewKey_emptySegment(t *testing.T) {
    73  	require.Panic(t, func() { api.NewKey(`a..b`) }, `key 'a\.\.b' contains an empty segment`)
    74  }
    75  
    76  func TestNewKey_emptySegmentStart(t *testing.T) {
    77  	require.Panic(t, func() { api.NewKey(`a.`) }, `key 'a\.' contains an empty segment`)
    78  }
    79  
    80  func TestNewKey_emptySegmentEnd(t *testing.T) {
    81  	require.Panic(t, func() { api.NewKey(`.b`) }, `key '\.b' contains an empty segment`)
    82  }
    83  
    84  func TestNewKey_firstSegmentIndex(t *testing.T) {
    85  	require.Panic(t, func() { api.NewKey(`1.a`) }, `key '1\.a' first segment cannot be an index`)
    86  }
    87  
    88  func TestKey_Bury_dotted(t *testing.T) {
    89  	v := api.NewKey(`a.b.c`).Bury(vf.String(`x`))
    90  	require.Equal(t, vf.Map(`b`, vf.Map(`c`, `x`)), v)
    91  }
    92  
    93  func TestKey_Bury_dotted_int(t *testing.T) {
    94  	v := api.NewKey(`a.3`).Bury(vf.String(`x`))
    95  	require.Equal(t, vf.Map(3, `x`), v)
    96  }
    97  
    98  func TestKey_Bury_untouched(t *testing.T) {
    99  	v := api.NewKey(`a`).Bury(vf.String(`x`))
   100  	require.Equal(t, `x`, v)
   101  }