Skip to content
Permalink
Browse files

Add tests for cmd package

  • Loading branch information
ArmanMazdaee committed Dec 19, 2018
1 parent c0224f1 commit 6781a4d9a4d250875fde29e3016ac42e2b383610
Showing with 139 additions and 0 deletions.
  1. +43 −0 cmd/cmd_test.go
  2. +26 −0 cmd/generate_example_test.go
  3. +26 −0 cmd/generate_schema_test.go
  4. +27 −0 cmd/render_test.go
  5. +11 −0 cmd/validate_test.go
  6. +6 −0 example.html
@@ -0,0 +1,43 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
)

func compareJSONFiles(file1, file2 string) (bool, error) {
data1, err := ioutil.ReadFile(file1)
if err != nil {
return false, fmt.Errorf("could not read file1: %s", err)
}
data2, err := ioutil.ReadFile(file2)
if err != nil {
return false, fmt.Errorf("could not read file2: %s", err)
}
var json1 interface{}
err = json.Unmarshal(data1, &json1)
if err != nil {
return false, fmt.Errorf("could not unmarshal json1: %s", err)
}
var json2 interface{}
err = json.Unmarshal(data2, &json2)
if err != nil {
return false, fmt.Errorf("could not unmarshal json2: %s", err)
}
return reflect.DeepEqual(json1, json2), nil
}

func compareFiles(file1, file2 string) (bool, error) {
data1, err := ioutil.ReadFile(file1)
if err != nil {
return false, fmt.Errorf("could not read file1: %s", err)
}
data2, err := ioutil.ReadFile(file2)
if err != nil {
return false, fmt.Errorf("could not read file2: %s", err)
}
return bytes.Equal(data1, data2), nil
}
@@ -0,0 +1,26 @@
package cmd

import (
"io/ioutil"
"os"
"testing"
)

func TestGenerateExampleRunValid(t *testing.T) {
tempFile, err := ioutil.TempFile(os.TempDir(), "resumic-example-*.json")
defer os.Remove(tempFile.Name())
if err != nil {
t.Fatalf("Could not generate temp file: %s", err)
}
err = generateExampleRun(generateExampleCmd, []string{tempFile.Name()})
if err != nil {
t.Fatalf("Could not generate example, %s", err)
}
r, err := compareJSONFiles("../example.json", tempFile.Name())
if err != nil {
t.Fatalf("could not compare example.json and generated schema: %s", err)
}
if !r {
t.Fatal("example.json and generated example are not same")
}
}
@@ -0,0 +1,26 @@
package cmd

import (
"io/ioutil"
"os"
"testing"
)

func TestGenerateSchemaRunValid(t *testing.T) {
tempFile, err := ioutil.TempFile(os.TempDir(), "resumic-schema-*.json")
defer os.Remove(tempFile.Name())
if err != nil {
t.Fatalf("Could not generate temp file: %s", err)
}
err = generateSchemaRun(generateSchemaCmd, []string{tempFile.Name()})
if err != nil {
t.Fatalf("Could not generate schema, %s", err)
}
r, err := compareJSONFiles("../schema.json", tempFile.Name())
if err != nil {
t.Fatalf("could not compare schema.json and generated schema: %s", err)
}
if !r {
t.Fatal("schema.json and generated schema are not same")
}
}
@@ -0,0 +1,27 @@
package cmd

import (
"io/ioutil"
"os"
"testing"
)

func TestRenderRun(t *testing.T) {
resumePath := "../example.json"
htmlPath := "../example.html"
tempFile, err := ioutil.TempFile(os.TempDir(), "resumic-html-*.json")
if err != nil {
t.Fatalf("Could not make temp file: %s", err)
}
err = renderRun(renderCmd, []string{resumePath, tempFile.Name()})
if err != nil {
t.Fatalf("Could not render the resume html: %s", err)
}
r, err := compareFiles(htmlPath, tempFile.Name())
if err != nil {
t.Fatalf("Could not compare the html and temp file: %s", err)
}
if !r {
t.Fatalf("%s and generated html are not same", htmlPath)
}
}
@@ -0,0 +1,11 @@
package cmd

import "testing"

func TestValidateRunValid(t *testing.T) {
resumeFile := "../example.json"
err := validateRun(validateCmd, []string{resumeFile})
if err != nil {
t.Fatalf("Expected valid state, got error, %s", err)
}
}
@@ -0,0 +1,6 @@
<html>
<body>

John Doe - Software Engineer
</body>
</html>

0 comments on commit 6781a4d

Please sign in to comment.
You can’t perform that action at this time.