go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/llx/primitives_test.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package llx 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 "go.mondoo.com/cnquery/logger" 12 "go.mondoo.com/cnquery/types" 13 ) 14 15 func init() { 16 logger.InitTestEnv() 17 } 18 19 func TestPrimitiveBool(t *testing.T) { 20 a := &Primitive{Type: string(types.Bool), Value: bool2bytes(true)} 21 b := &Primitive{Type: string(types.Bool), Value: bool2bytes(false)} 22 assert.Equal(t, a, BoolPrimitive(true)) 23 assert.Equal(t, b, BoolPrimitive(false)) 24 } 25 26 func TestPrimitiveFloat(t *testing.T) { 27 a := &Primitive{Type: string(types.Float), Value: []byte{0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x28, 0x40}} 28 assert.Equal(t, a, FloatPrimitive(12.3)) 29 } 30 31 func TestPrimitiveInt(t *testing.T) { 32 a := &Primitive{Type: string(types.Int), Value: []byte{0xf6, 0x01}} 33 assert.Equal(t, a, IntPrimitive(123)) 34 } 35 36 func TestPrimitiveString(t *testing.T) { 37 a := &Primitive{Type: string(types.String), Value: []byte("hi")} 38 assert.Equal(t, a, StringPrimitive("hi")) 39 } 40 41 func TestPrimitiveRegex(t *testing.T) { 42 a := &Primitive{Type: string(types.Regex), Value: []byte(".*")} 43 assert.Equal(t, a, RegexPrimitive(".*")) 44 } 45 46 func TestPrimitiveTime(t *testing.T) { 47 a := &Primitive{Type: string(types.Time), Value: []byte{0x15, 0xcd, 0x5b, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15, 0xcd, 0x5b, 0x07}} 48 ut := time.Unix(123456789, 123456789) 49 assert.Equal(t, a, TimePrimitive(&ut)) 50 51 assert.Equal(t, NilPrimitive, TimePrimitive(nil)) 52 } 53 54 func TestPrimitiveArray(t *testing.T) { 55 a := &Primitive{Type: string(types.Array(types.Int)), Array: []*Primitive{IntPrimitive(123)}} 56 assert.Equal(t, a, ArrayPrimitive([]*Primitive{IntPrimitive(123)}, types.Int)) 57 } 58 59 func TestPrimitiveMap(t *testing.T) { 60 a := &Primitive{Type: string(types.Map(types.String, types.Int)), Map: map[string]*Primitive{"a": IntPrimitive(123)}} 61 assert.Equal(t, a, MapPrimitive(map[string]*Primitive{"a": IntPrimitive(123)}, types.Int)) 62 } 63 64 func TestPrimitiveFunction(t *testing.T) { 65 a := &Primitive{ 66 Type: string(types.Function(0, nil)), 67 Value: []byte{0xf6, 0x01}, 68 } 69 assert.Equal(t, a, FunctionPrimitiveV1(123)) 70 } 71 72 func TestPrimitiveNil(t *testing.T) { 73 t.Run("nil type", func(t *testing.T) { 74 p := &Primitive{ 75 Type: string(types.Nil), 76 } 77 assert.True(t, p.IsNil()) 78 }) 79 80 t.Run("string without value is an empty string (not nil)", func(t *testing.T) { 81 p := &Primitive{ 82 Type: string(types.String), 83 } 84 assert.False(t, p.IsNil()) 85 }) 86 87 t.Run("string with value is not nil", func(t *testing.T) { 88 p := StringPrimitive("hi") 89 assert.False(t, p.IsNil()) 90 }) 91 92 t.Run("map type without value is empty map (not nil)", func(t *testing.T) { 93 p := MapPrimitive(nil, types.Int) 94 assert.False(t, p.IsNil()) 95 }) 96 97 t.Run("map type with empty value is not nil", func(t *testing.T) { 98 p := MapPrimitive(map[string]*Primitive{}, types.Int) 99 assert.False(t, p.IsNil()) 100 }) 101 102 t.Run("array type without value is empty array (not nil)", func(t *testing.T) { 103 p := ArrayPrimitive(nil, types.Int) 104 assert.False(t, p.IsNil()) 105 }) 106 107 t.Run("array type with empty value is not nil", func(t *testing.T) { 108 p := ArrayPrimitive([]*Primitive{}, types.Int) 109 assert.False(t, p.IsNil()) 110 }) 111 } 112 113 func TestRange(t *testing.T) { 114 t.Run("single line", func(t *testing.T) { 115 r := RangePrimitive(NewRange().AddLine(12)) 116 assert.Equal(t, "12", r.LabelV2(nil)) 117 }) 118 119 t.Run("line range", func(t *testing.T) { 120 r := RangePrimitive(NewRange().AddLineRange(12, 18)) 121 assert.Equal(t, "12-18", r.LabelV2(nil)) 122 }) 123 124 t.Run("column range", func(t *testing.T) { 125 r := RangePrimitive(NewRange().AddColumnRange(12, 1, 28)) 126 assert.Equal(t, "12:1-28", r.LabelV2(nil)) 127 }) 128 129 t.Run("line and column range", func(t *testing.T) { 130 r := RangePrimitive(NewRange().AddLineColumnRange(12, 18, 1, 28)) 131 assert.Equal(t, "12:1-18:28", r.LabelV2(nil)) 132 }) 133 }