Skip to content

Commit

Permalink
feat: unit tests for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickkirschen committed May 29, 2024
1 parent 415a3d1 commit 4798af7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example/my-manifest.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
apiVersion: example.com/v1alpha1
kind: MyManifest

metadata:
name: my-manifest

spec:
message: hello, world
79 changes: 79 additions & 0 deletions manifesto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package manifesto

import (
"io"
"strings"
"testing"
)

type MySpec struct {
Message string `yaml:"message" json:"message"`
}

func TestParseFile(t *testing.T) {
manifest := ParseFile("example/my-manifest.yaml", &MySpec{}, &MySpec{})
spec := manifest.Spec.(*MySpec)

want := manifest.Metadata.Name == "my-manifest" && spec.Message == "hello, world"

if !want {
t.Fatalf("Unable to parse manifest from file")
}
}

func TestParseReader(t *testing.T) {
r := io.NopCloser(strings.NewReader(`apiVersion: example.com/v1alpha1
kind: MyManifest
metadata:
name: my-manifest
spec:
message: hello, world
`))
manifest := ParseReader(r, &MySpec{}, &MySpec{})

spec := manifest.Spec.(*MySpec)

want := manifest.Metadata.Name == "my-manifest" && spec.Message == "hello, world"

if !want {
t.Fatalf("Unable to parse manifest from reader")
}
}

func TestParseString(t *testing.T) {
s := `apiVersion: example.com/v1alpha1
kind: MyManifest
metadata:
name: my-manifest
spec:
message: hello, world
`
manifest := ParseString(s, &MySpec{}, &MySpec{})

spec := manifest.Spec.(*MySpec)

want := manifest.Metadata.Name == "my-manifest" && spec.Message == "hello, world"

if !want {
t.Fatalf("Unable to parse manifest from string")
}
}

func TestParseBytes(t *testing.T) {
b := []byte(`apiVersion: example.com/v1alpha1
kind: MyManifest
metadata:
name: my-manifest
spec:
message: hello, world
`)
manifest := ParseBytes(b, &MySpec{}, &MySpec{})

spec := manifest.Spec.(*MySpec)

want := manifest.Metadata.Name == "my-manifest" && spec.Message == "hello, world"

if !want {
t.Fatalf("Unable to parse manifest from bytes")
}
}

0 comments on commit 4798af7

Please sign in to comment.