Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for HCL parser #151

Merged
merged 1 commit into from May 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
155 changes: 155 additions & 0 deletions parsers/hcl/hcl_test.go
@@ -0,0 +1,155 @@
package hcl

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHCL_Unmarshal(t *testing.T) {

hclParserWithFlatten := Parser(true)
hclParserWithoutFlatten := Parser(false)

testCases := []struct {
name string
input []byte
output map[string]interface{}
isErr bool
function HCL
}{
{
name: "Empty HCL - With faltten",
input: []byte(`{}`),
function: *hclParserWithFlatten,
output: map[string]interface{}{},
},
{
name: "Empty HCL - Without flatten",
input: []byte(`{}`),
function: *hclParserWithoutFlatten,
output: map[string]interface{}{},
},
{
name: "Valid HCL - With faltten",
input: []byte(`resource "aws_instance" "example" {
count = 2 # meta-argument first
ami = "abc123"
instance_type = "t2.micro"
lifecycle { # meta-argument block last
create_before_destroy = true
}
}`),
function: *hclParserWithFlatten,
output: map[string]interface{}{
"resource": map[string]interface{}{
"aws_instance": map[string]interface{}{
"example": map[string]interface{}{
"ami": "abc123",
"count": 2,
"instance_type": "t2.micro",
"lifecycle": map[string]interface{}{
"create_before_destroy": true,
},
},
},
},
},
},
{
name: "Valid HCL - Without faltten",
input: []byte(`resource "aws_instance" "example" {
count = 2 # meta-argument first
ami = "abc123"
instance_type = "t2.micro"
lifecycle { # meta-argument block last
create_before_destroy = true
}
}`),
function: *hclParserWithoutFlatten,
output: map[string]interface{}{
"resource": []map[string]interface{}{{
"aws_instance": []map[string]interface{}{{
"example": []map[string]interface{}{{
"ami": "abc123",
"count": 2,
"instance_type": "t2.micro",
"lifecycle": []map[string]interface{}{{
"create_before_destroy": true},
},
}},
}},
}},
},
},
{
name: "Invalid HCL - With missing parenthesis",
input: []byte(`resource "aws_instance" "example" {
ami = "abc123"
`),
function: *hclParserWithFlatten,
isErr: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out, err := tc.function.Unmarshal(tc.input)
if tc.isErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tc.output, out)
}
})
}
}

func TestHCL_Marshal(t *testing.T) {

hclParserWithFlatten := Parser(true)
// hclParserWithoutFlatten := Parser(false)

testCases := []struct {
name string
input map[string]interface{}
isErr bool
function HCL
}{
{
name: "Empty HCL",
input: map[string]interface{}{},
isErr: true,
function: *hclParserWithFlatten,
},
{
name: "Complex HCL",
input: map[string]interface{}{
"resource": []map[string]interface{}{{
"aws_instance": []map[string]interface{}{{
"example": []map[string]interface{}{{
"ami": "abc123",
"count": 2,
"instance_type": "t2.micro",
"lifecycle": []map[string]interface{}{{
"create_before_destroy": true},
},
}},
}},
}},
},
isErr: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.function.Marshal(tc.input)
if tc.isErr {
assert.EqualError(t, err, "HCL marshalling is not supported")
} else {
assert.Nil(t, err)
}
})
}
}