github.com/mithrandie/csvq@v1.18.1/lib/constant/main_test.go (about)

     1  package constant
     2  
     3  import (
     4  	"errors"
     5  	"math"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/mithrandie/csvq/lib/parser"
    10  	"github.com/mithrandie/csvq/lib/value"
    11  )
    12  
    13  func TestGet(t *testing.T) {
    14  	Definition["TEST"] = map[string]interface{}{
    15  		"INVALID_TYPE": uint8(3),
    16  	}
    17  
    18  	defer func() {
    19  		delete(Definition, "Unused")
    20  	}()
    21  
    22  	expr := parser.Constant{
    23  		Space: "integer",
    24  		Name:  "max",
    25  	}
    26  	var expect value.Primary = value.NewInteger(math.MaxInt64)
    27  
    28  	ret, err := Get(expr)
    29  	if err != nil {
    30  		t.Errorf("unexpected error %q", err)
    31  	}
    32  
    33  	if !reflect.DeepEqual(ret, expect) {
    34  		t.Errorf("result = %s, want %s", ret.String(), expect.String())
    35  	}
    36  
    37  	expr = parser.Constant{
    38  		Space: "math",
    39  		Name:  "pi",
    40  	}
    41  	expect = value.NewFloat(math.Pi)
    42  
    43  	ret, err = Get(expr)
    44  	if err != nil {
    45  		t.Errorf("unexpected error %q", err)
    46  	}
    47  
    48  	if !reflect.DeepEqual(ret, expect) {
    49  		t.Errorf("result = %s, want %s", ret.String(), expect.String())
    50  	}
    51  
    52  	expr = parser.Constant{
    53  		Space: "NotDefined",
    54  		Name:  "pi",
    55  	}
    56  	expectError := ErrUndefined
    57  
    58  	_, err = Get(expr)
    59  	if err == nil {
    60  		t.Errorf("no error, want error %q", expectError.Error())
    61  	}
    62  	if !errors.Is(err, expectError) {
    63  		t.Errorf("error %q, want error %q", err.Error(), expectError.Error())
    64  	}
    65  
    66  	expr = parser.Constant{
    67  		Space: "math",
    68  		Name:  "NotDefined",
    69  	}
    70  
    71  	_, err = Get(expr)
    72  	if err == nil {
    73  		t.Errorf("no error, want error %q", expectError.Error())
    74  	}
    75  	if !errors.Is(err, expectError) {
    76  		t.Errorf("error %q, want error %q", err.Error(), expectError.Error())
    77  	}
    78  
    79  	expr = parser.Constant{
    80  		Space: "test",
    81  		Name:  "invalid_type",
    82  	}
    83  	expectError = ErrInvalidType
    84  
    85  	_, err = Get(expr)
    86  	if err == nil {
    87  		t.Errorf("no error, want error %q", expectError.Error())
    88  	}
    89  	if !errors.Is(err, expectError) {
    90  		t.Errorf("error %q, want error %q", err.Error(), expectError.Error())
    91  	}
    92  }
    93  
    94  func TestCount(t *testing.T) {
    95  	oldDef := Definition
    96  
    97  	Definition = map[string]map[string]interface{}{}
    98  	Definition["CAT1"] = map[string]interface{}{
    99  		"VAL1": 1,
   100  	}
   101  	Definition["CAT2"] = map[string]interface{}{
   102  		"VAL2": 2,
   103  		"VAL3": 3,
   104  	}
   105  
   106  	defer func() {
   107  		Definition = oldDef
   108  	}()
   109  
   110  	ret := Count()
   111  	if ret != 3 {
   112  		t.Errorf("result = %d, want %d", ret, 3)
   113  	}
   114  }