github.com/splunk/dan1-qbec@v0.7.3/internal/vm/nativefuncs_test.go (about)

     1  // Copyright 2017 The kubecfg authors
     2  //
     3  //
     4  //    Licensed under the Apache License, Version 2.0 (the "License");
     5  //    you may not use this file except in compliance with the License.
     6  //    You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  //    Unless required by applicable law or agreed to in writing, software
    11  //    distributed under the License is distributed on an "AS IS" BASIS,
    12  //    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  //    See the License for the specific language governing permissions and
    14  //    limitations under the License.
    15  
    16  package vm
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/google/go-jsonnet"
    22  )
    23  
    24  // copied from original code at https://github.com/ksonnet/kubecfg/blob/master/utils/nativefuncs_test.go
    25  // and modified for use.
    26  
    27  // check there is no err, and a == b.
    28  func check(t *testing.T, err error, actual, expected string) {
    29  	if err != nil {
    30  		t.Errorf("Expected %q, got error: %q", expected, err.Error())
    31  	} else if actual != expected {
    32  		t.Errorf("Expected %q, got %q", expected, actual)
    33  	}
    34  }
    35  
    36  func TestParseJson(t *testing.T) {
    37  	vm := jsonnet.MakeVM()
    38  	registerNativeFuncs(vm)
    39  
    40  	_, err := vm.EvaluateSnippet("failtest", `std.native("parseJson")("barf{")`)
    41  	if err == nil {
    42  		t.Errorf("parseJson succeeded on invalid json")
    43  	}
    44  
    45  	x, err := vm.EvaluateSnippet("test", `std.native("parseJson")("null")`)
    46  	check(t, err, x, "null\n")
    47  
    48  	x, err = vm.EvaluateSnippet("test", `
    49      local a = std.native("parseJson")('{"foo": 3, "bar": 4}');
    50      a.foo + a.bar`)
    51  	check(t, err, x, "7\n")
    52  }
    53  
    54  func TestParseYaml(t *testing.T) {
    55  	vm := jsonnet.MakeVM()
    56  	registerNativeFuncs(vm)
    57  
    58  	_, err := vm.EvaluateSnippet("failtest", `std.native("parseYaml")("[barf")`)
    59  	if err == nil {
    60  		t.Errorf("parseYaml succeeded on invalid yaml")
    61  	}
    62  
    63  	x, err := vm.EvaluateSnippet("test", `std.native("parseYaml")("")`)
    64  	check(t, err, x, "[ ]\n")
    65  
    66  	x, err = vm.EvaluateSnippet("test", `
    67      local a = std.native("parseYaml")("foo:\n- 3\n- 4\n")[0];
    68      a.foo[0] + a.foo[1]`)
    69  	check(t, err, x, "7\n")
    70  
    71  	x, err = vm.EvaluateSnippet("test", `
    72      local a = std.native("parseYaml")("---\nhello\n---\nworld");
    73      a[0] + a[1]`)
    74  	check(t, err, x, "\"helloworld\"\n")
    75  }
    76  
    77  func TestRegexMatch(t *testing.T) {
    78  	vm := jsonnet.MakeVM()
    79  	registerNativeFuncs(vm)
    80  
    81  	_, err := vm.EvaluateSnippet("failtest", `std.native("regexMatch")("[f", "foo")`)
    82  	if err == nil {
    83  		t.Errorf("regexMatch succeeded with invalid regex")
    84  	}
    85  
    86  	x, err := vm.EvaluateSnippet("test", `std.native("regexMatch")("foo.*", "seafood")`)
    87  	check(t, err, x, "true\n")
    88  
    89  	x, err = vm.EvaluateSnippet("test", `std.native("regexMatch")("bar.*", "seafood")`)
    90  	check(t, err, x, "false\n")
    91  }
    92  
    93  func TestRegexSubst(t *testing.T) {
    94  	vm := jsonnet.MakeVM()
    95  	registerNativeFuncs(vm)
    96  
    97  	_, err := vm.EvaluateSnippet("failtest", `std.native("regexSubst")("[f", "foo", "bar")`)
    98  	if err == nil {
    99  		t.Errorf("regexSubst succeeded with invalid regex")
   100  	}
   101  
   102  	x, err := vm.EvaluateSnippet("test", `std.native("regexSubst")("a(x*)b", "-ab-axxb-", "T")`)
   103  	check(t, err, x, "\"-T-T-\"\n")
   104  
   105  	x, err = vm.EvaluateSnippet("test", `std.native("regexSubst")("a(x*)b", "-ab-axxb-", "${1}W")`)
   106  	check(t, err, x, "\"-W-xxW-\"\n")
   107  }
   108  
   109  func TestRegexQuoteMeta(t *testing.T) {
   110  	vm := jsonnet.MakeVM()
   111  	registerNativeFuncs(vm)
   112  	x, err := vm.EvaluateSnippet("test", `std.native("escapeStringRegex")("[f]")`)
   113  	check(t, err, x, `"\\[f\\]"`+"\n")
   114  }