Debian Codegraph @

ast/literal_test.go

raw symbols SCIP
1 // Copyright (c) HashiCorp, Inc.
2 // SPDX-License-Identifier: MPL-2.0
3
4 package ast
5
6 import (
7 "fmt"
8 "reflect"
9 "testing"
10 )
11
12 func TestLiteralNodeType(t *testing.T) {
13 c := &LiteralNode{Typex: TypeString}
14 actual, err := c.Type(nil)
15 if err != nil {
16 t.Fatalf("err: %s", err)
17 }
18 if actual != TypeString {
19 t.Fatalf("bad: %s", actual)
20 }
21 }
22
23 func TestNewLiteralNode(t *testing.T) {
24 tests := []struct {
25 Value interface{}
26 Expected *LiteralNode
27 }{
28 {
29 1,
30 &LiteralNode{
31 Value: 1,
32 Typex: TypeInt,
33 },
34 },
35 {
36 1.0,
37 &LiteralNode{
38 Value: 1.0,
39 Typex: TypeFloat,
40 },
41 },
42 {
43 true,
44 &LiteralNode{
45 Value: true,
46 Typex: TypeBool,
47 },
48 },
49 {
50 "hi",
51 &LiteralNode{
52 Value: "hi",
53 Typex: TypeString,
54 },
55 },
56 }
57
58 for _, test := range tests {
59 t.Run(fmt.Sprintf("%T", test.Value), func(t *testing.T) {
60 inPos := Pos{
61 Column: 2,
62 Line: 3,
63 Filename: "foo",
64 }
65 node, err := NewLiteralNode(test.Value, inPos)
66
67 if err != nil {
68 t.Fatalf("error: %s", err)
69 }
70
71 if got, want := node.Typex, test.Expected.Typex; want != got {
72 t.Errorf("got type %s; want %s", got, want)
73 }
74 if got, want := node.Value, test.Expected.Value; want != got {
75 t.Errorf("got value %#v; want %#v", got, want)
76 }
77 if got, want := node.Posx, inPos; !reflect.DeepEqual(got, want) {
78 t.Errorf("got position %#v; want %#v", got, want)
79 }
80 })
81 }
82 }