github.com/Comcast/plax@v0.8.32/dsl/test_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  	"context"
    23  	"fmt"
    24  	"io/ioutil"
    25  	"testing"
    26  
    27  	"gopkg.in/yaml.v3"
    28  )
    29  
    30  func TestWanted(t *testing.T) {
    31  	ctx := NewCtx(context.Background())
    32  
    33  	tst := NewTest(ctx, "a", nil)
    34  	tst.Priority = 42
    35  	t.Run("priority-negative", func(t *testing.T) {
    36  		if !tst.Wanted(ctx, -1, nil, nil) {
    37  			t.Fatal(-1)
    38  		}
    39  	})
    40  	t.Run("priority-high", func(t *testing.T) {
    41  		if !tst.Wanted(ctx, 52, nil, nil) {
    42  			t.Fatal(52)
    43  		}
    44  	})
    45  	t.Run("priority-low", func(t *testing.T) {
    46  		if tst.Wanted(ctx, 1, nil, nil) {
    47  			t.Fatal(1)
    48  		}
    49  	})
    50  }
    51  
    52  func TestTestIdFromPathname(t *testing.T) {
    53  	var (
    54  		pathname = "here/test-1.yaml"
    55  		want     = "here/test-1"
    56  		got      = TestIdFromPathname(pathname)
    57  	)
    58  	if want != got {
    59  		t.Fatal(got)
    60  	}
    61  }
    62  
    63  func testFromFile(t *testing.T, filename string) (*Test, *Errors) {
    64  	var (
    65  		ctx0, cancel = context.WithCancel(context.Background())
    66  		ctx          = NewCtx(ctx0)
    67  	)
    68  	defer cancel()
    69  
    70  	bs, err := ioutil.ReadFile(filename)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	bs, err = IncludeYAML(ctx, bs)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	tst := NewTest(ctx, filename, nil)
    81  	tst.Dir = "../demos"
    82  
    83  	if err := yaml.Unmarshal(bs, &tst); err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	if err := tst.Init(ctx); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	if errs := tst.Validate(ctx); errs != nil {
    92  		var acc string
    93  		for i, err := range errs {
    94  			acc += fmt.Sprintf("  %02d. %s\n", i, err)
    95  		}
    96  		t.Fatal(errs)
    97  	}
    98  
    99  	errs := tst.Run(ctx)
   100  
   101  	if err := tst.Close(ctx); err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	return tst, errs
   106  
   107  }
   108  
   109  func TestFinally(t *testing.T) {
   110  	tst, _ := testFromFile(t, "../demos/finally.yaml")
   111  
   112  	if problem, have := tst.State["problem"]; have {
   113  		t.Fatal(problem)
   114  	}
   115  
   116  	if x, have := tst.State["n"]; !have {
   117  		t.Fatal("No n")
   118  	} else if n, is := x.(int64); !is {
   119  		t.Fatalf("n is %T", x)
   120  	} else if n != 3 {
   121  		t.Fatal(n)
   122  	}
   123  }