github.com/zntrio/harp/v2@v2.0.9/pkg/template/engine/values_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package engine
    19  
    20  import (
    21  	"bytes"
    22  	"testing"
    23  	"text/template"
    24  )
    25  
    26  func TestReadValues(t *testing.T) {
    27  	doc := `# Test YAML parse
    28  poet: "Coleridge"
    29  title: "Rime of the Ancient Mariner"
    30  stanza:
    31    - "at"
    32    - "length"
    33    - "did"
    34    - cross
    35    - an
    36    - Albatross
    37  mariner:
    38    with: "crossbow"
    39    shot: "ALBATROSS"
    40  water:
    41    water:
    42      where: "everywhere"
    43      nor: "any drop to drink"
    44  `
    45  
    46  	data, err := ReadValues(bytes.NewBuffer([]byte(doc)))
    47  	if err != nil {
    48  		t.Fatalf("Error parsing bytes: %s", err)
    49  	}
    50  	matchValues(t, data)
    51  
    52  	tests := []string{`poet: "Coleridge"`, "# Just a comment", ""}
    53  
    54  	for _, tt := range tests {
    55  		data, err = ReadValues(bytes.NewBuffer([]byte(tt)))
    56  		if err != nil {
    57  			t.Fatalf("Error parsing bytes (%s): %s", tt, err)
    58  		}
    59  		if data == nil {
    60  			t.Errorf(`YAML string "%s" gave a nil map`, tt)
    61  		}
    62  	}
    63  }
    64  
    65  func TestTable(t *testing.T) {
    66  	doc := `
    67  title: "Moby Dick"
    68  chapter:
    69    one:
    70      title: "Loomings"
    71    two:
    72      title: "The Carpet-Bag"
    73    three:
    74      title: "The Spouter Inn"
    75  `
    76  	d, err := ReadValues(bytes.NewBuffer([]byte(doc)))
    77  	if err != nil {
    78  		t.Fatalf("Failed to parse the White Whale: %s", err)
    79  	}
    80  
    81  	if _, err := d.Table("title"); err == nil {
    82  		t.Fatalf("Title is not a table.")
    83  	}
    84  
    85  	if _, err := d.Table("chapter"); err != nil {
    86  		t.Fatalf("Failed to get the chapter table: %s\n%v", err, d)
    87  	}
    88  
    89  	if v, err := d.Table("chapter.one"); err != nil {
    90  		t.Errorf("Failed to get chapter.one: %s", err)
    91  	} else if v["title"] != "Loomings" {
    92  		t.Errorf("Unexpected title: %s", v["title"])
    93  	}
    94  
    95  	if _, err := d.Table("chapter.three"); err != nil {
    96  		t.Errorf("Chapter three is missing: %s\n%v", err, d)
    97  	}
    98  
    99  	if _, err := d.Table("chapter.OneHundredThirtySix"); err == nil {
   100  		t.Errorf("I think you mean 'Epilogue'")
   101  	}
   102  }
   103  
   104  func TestPathValue(t *testing.T) {
   105  	doc := `
   106  title: "Moby Dick"
   107  chapter:
   108    one:
   109      title: "Loomings"
   110    two:
   111      title: "The Carpet-Bag"
   112    three:
   113      title: "The Spouter Inn"
   114  `
   115  	d, err := ReadValues(bytes.NewBuffer([]byte(doc)))
   116  	if err != nil {
   117  		t.Fatalf("Failed to parse the White Whale: %s", err)
   118  	}
   119  
   120  	if v, err := d.PathValue("chapter.one.title"); err != nil {
   121  		t.Errorf("Got error instead of title: %s\n%v", err, d)
   122  	} else if v != "Loomings" {
   123  		t.Errorf("No error but got wrong value for title: %s\n%v", err, d)
   124  	}
   125  	if _, err := d.PathValue("chapter.one.doesnotexist"); err == nil {
   126  		t.Errorf("Non-existent key should return error: %s\n%v", err, d)
   127  	}
   128  	if _, err := d.PathValue("chapter.doesnotexist.one"); err == nil {
   129  		t.Errorf("Non-existent key in middle of path should return error: %s\n%v", err, d)
   130  	}
   131  	if _, err := d.PathValue(""); err == nil {
   132  		t.Error("Asking for the value from an empty path should yield an error")
   133  	}
   134  	if v, err := d.PathValue("title"); err == nil {
   135  		if v != "Moby Dick" {
   136  			t.Errorf("Failed to return values for root key title")
   137  		}
   138  	}
   139  }
   140  
   141  // -----------------------------------------------------------------------------
   142  
   143  func matchValues(t *testing.T, data map[string]interface{}) {
   144  	if data["poet"] != "Coleridge" {
   145  		t.Errorf("Unexpected poet: %s", data["poet"])
   146  	}
   147  
   148  	if o, err := ttpl("{{len .stanza}}", data); err != nil {
   149  		t.Errorf("len stanza: %s", err)
   150  	} else if o != "6" {
   151  		t.Errorf("Expected 6, got %s", o)
   152  	}
   153  
   154  	if o, err := ttpl("{{.mariner.shot}}", data); err != nil {
   155  		t.Errorf(".mariner.shot: %s", err)
   156  	} else if o != "ALBATROSS" {
   157  		t.Errorf("Expected that mariner shot ALBATROSS")
   158  	}
   159  
   160  	if o, err := ttpl("{{.water.water.where}}", data); err != nil {
   161  		t.Errorf(".water.water.where: %s", err)
   162  	} else if o != "everywhere" {
   163  		t.Errorf("Expected water water everywhere")
   164  	}
   165  }
   166  
   167  func ttpl(tpl string, v map[string]interface{}) (string, error) {
   168  	var b bytes.Buffer
   169  	tt := template.Must(template.New("t").Parse(tpl))
   170  	err := tt.Execute(&b, v)
   171  	return b.String(), err
   172  }