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

     1  package dsl
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestStringSub(t *testing.T) {
    10  	var (
    11  		ctx = NewCtx(context.Background())
    12  		bs  = NewBindings()
    13  		s   = `'{@@bindings.go}'`
    14  	)
    15  
    16  	got, err := bs.StringSub(ctx, s)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	if !strings.HasPrefix(got, `'/*`) {
    21  		t.Fatal(got)
    22  	}
    23  }
    24  
    25  func TestBindingsSet(t *testing.T) {
    26  	bs := NewBindings()
    27  	check := func(err error, key, want string) {
    28  		if err != nil {
    29  			t.Fatalf("key %s -> <%v> != %s", key, err, want)
    30  		}
    31  		got, have := (*bs)[key]
    32  		if !have {
    33  			t.Fatalf("key %s -> %s != %s", key, got, want)
    34  		}
    35  	}
    36  
    37  	err := bs.Set(`like="tacos"`)
    38  	check(err, "like", "tacos")
    39  
    40  	err = bs.Set(`like=tacos`)
    41  	check(err, "like", "tacos")
    42  
    43  	if err = bs.Set(`like=42`); err != nil {
    44  		t.Fatal(err)
    45  	} else {
    46  		x, have := (*bs)["like"]
    47  		if !have {
    48  			t.Fatal("like")
    49  		}
    50  		switch vv := x.(type) {
    51  		case float64:
    52  			if vv != 42 {
    53  				t.Fatal(vv)
    54  			}
    55  		default:
    56  			t.Fatalf("%T: %v", x, x)
    57  		}
    58  	}
    59  
    60  	if err = bs.Set(`like={"want":"chips"}`); err != nil {
    61  		t.Fatal(err)
    62  	} else {
    63  		x, have := (*bs)["like"]
    64  		if !have {
    65  			t.Fatal("like")
    66  		}
    67  		switch vv := x.(type) {
    68  		case map[string]interface{}:
    69  			x, have := vv["want"]
    70  			if !have {
    71  				t.Fatal(err)
    72  			}
    73  			switch vv := x.(type) {
    74  			case string:
    75  				if vv != "chips" {
    76  					t.Fatal(vv)
    77  				}
    78  			default:
    79  				t.Fatalf("%T: %v", x, x)
    80  			}
    81  		default:
    82  			t.Fatalf("%T: %v", x, x)
    83  		}
    84  	}
    85  
    86  	if err = bs.Set(`liketacos`); err == nil {
    87  		t.Fatal("should have complained")
    88  	}
    89  }