github.com/Comcast/plax@v0.8.32/dsl/js_test.go (about)

     1  /*
     2   * Copyright 2021 Comcast Cable Communications Management, LLC
     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   * SPDX-License-Identifier: Apache-2.0
    17   */
    18  
    19  package dsl
    20  
    21  import (
    22  	"testing"
    23  )
    24  
    25  func TestJSExec(t *testing.T) {
    26  	ctx := NewCtx(nil)
    27  
    28  	t.Run("bindings", func(t *testing.T) {
    29  		x, err := JSExec(ctx, "1+x", map[string]interface{}{
    30  			"x": 2,
    31  		})
    32  		if err != nil {
    33  			t.Fatal(err)
    34  		}
    35  
    36  		switch vv := x.(type) {
    37  		case int64:
    38  			if vv != 3 {
    39  				t.Fatal(x)
    40  			}
    41  		default:
    42  			t.Fatal(x)
    43  		}
    44  	})
    45  
    46  	t.Run("now", func(t *testing.T) {
    47  		_, err := JSExec(ctx, "now()", nil)
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  	})
    52  
    53  	t.Run("tsMs", func(t *testing.T) {
    54  		_, err := JSExec(ctx, "tsMs(now())", nil)
    55  		if err != nil {
    56  			t.Fatal(err)
    57  		}
    58  	})
    59  
    60  	t.Run("matchhappy", func(t *testing.T) {
    61  		src := `
    62  var pat = {"want":"?x"};
    63  var msg = {"want":"queso"};
    64  var bs = {"?y":"guacamole"};
    65  var bss = match(pat, msg, bs);
    66  bss[0]["?x"] == "queso";
    67  `
    68  		x, err := JSExec(ctx, src, nil)
    69  		if err != nil {
    70  			t.Fatal(err)
    71  		}
    72  		b, is := x.(bool)
    73  		if !is {
    74  			t.Fatal(x)
    75  		}
    76  		if !b {
    77  			t.Fatal(b)
    78  		}
    79  	})
    80  
    81  	t.Run("matchsad", func(t *testing.T) {
    82  		src := `
    83  var pat = {"?danger":"?x","?bad":"?z"};
    84  var msg = {"want":"queso"};
    85  var bs = {"?y":"guacamole"};
    86  var bss = match(pat, msg, bs);
    87  bss[0]["?x"] == "queso";
    88  `
    89  		_, err := JSExec(ctx, src, nil)
    90  		if err == nil {
    91  			t.Fatal("should have complained (politely)")
    92  		}
    93  	})
    94  
    95  	t.Run("fail", func(t *testing.T) {
    96  		src := `fail("I don't like it.");`
    97  		_, err := JSExec(ctx, src, nil)
    98  		if err == nil {
    99  			t.Fatal("should have complained (politely)")
   100  		}
   101  		if x, is := err.(*Failure); !is {
   102  			t.Fatalf("wanted a %T and not a %T", x, err)
   103  		}
   104  	})
   105  
   106  	t.Run("nopanic", func(t *testing.T) {
   107  		// ToDo: Iterate over all exposed native Go functions?
   108  
   109  		src := `redactRegexp("[}");`
   110  		_, err := JSExec(ctx, src, nil)
   111  		if err == nil {
   112  			t.Fatal("should have complained (politely)")
   113  		}
   114  		if _, is := IsBroken(err); !is {
   115  			t.Fatalf("should have reported Broken instead of %T", err)
   116  		}
   117  	})
   118  
   119  }