File: header_test.go

package info (click to toggle)
golang-github-chaseadamsio-goorgeous 2.0.0%2Bgit20171126.dcf1ef8-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 208 kB
  • sloc: makefile: 3
file content (113 lines) | stat: -rw-r--r-- 3,615 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package goorgeous

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

func TestExtractOrgHeaders(t *testing.T) {
	source := "./testdata/test.org"
	golden := "./testdata/test.orgheaders.golden"
	content, err := os.Open(source)
	if err != nil {
		t.Fatalf("Could not open file %s: %s", source, err)
	}
	r := bufio.NewReader(content)
	fm, err := ExtractOrgHeaders(r)
	if err != nil {
		t.Fatalf("Could not extract org headers: %s", err)
	}

	if *update {
		if err := ioutil.WriteFile(golden, fm, 0644); err != nil {
			t.Errorf("failed to write %s file: %s", golden, err)
		}
		return
	}

	gld, err := ioutil.ReadFile(golden)
	if err != nil {
		t.Fatalf("failed to read %s file: %s", golden, err)
	}

	if !bytes.Equal(fm, gld) {
		t.Errorf("ExtractOrgHeaders() from %s = %s\nwants: %s", source, string(fm), gld)
	}
}

func TestOrgHeaders(t *testing.T) {
	testCases := map[string]struct {
		in       string
		expected map[string]interface{}
	}{
		"no-content-headers": {"#title:\n",
			map[string]interface{}{},
		},
		"one-content-header": {"#title: my org content\n#+author:",
			map[string]interface{}{},
		},
		"basic-happy-path": {"#+title: my org mode content\n#+author: Chase Adams\n#+description: This is my description!",
			map[string]interface{}{
				"title":       "my org mode content",
				"author":      "Chase Adams",
				"description": "This is my description!",
			}},
		"basic-happy-path-new-content-after": {"#+title: my org mode content\n#+author: Chase Adams\n#+description: This is my description!\n* This shouldn't get captured!",
			map[string]interface{}{
				"title":       "my org mode content",
				"author":      "Chase Adams",
				"description": "This is my description!",
			}},
		"basic-happy-path-with-tags": {"#+title: my org mode tags content\n#+author: Chase Adams\n#+description: This is my description!\n#+tags: org-content org-mode hugo\n",
			map[string]interface{}{
				"title":       "my org mode tags content",
				"author":      "Chase Adams",
				"description": "This is my description!",
				"tags":        []string{"org-content", "org-mode", "hugo"},
			}},
		"basic-happy-path-with-categories": {"#+title: my org mode tags content\n#+author: Chase Adams\n#+description: This is my description!\n#+categories: org-content org-mode hugo\n",
			map[string]interface{}{
				"title":       "my org mode tags content",
				"author":      "Chase Adams",
				"description": "This is my description!",
				"categories":  []string{"org-content", "org-mode", "hugo"},
			}},
		"basic-happy-path-with-aliases": {"#+title: my org mode tags content\n#+author: Chase Adams\n#+description: This is my description!\n#+aliases: /org/content /org/mode /hugo\n",
			map[string]interface{}{
				"title":       "my org mode tags content",
				"author":      "Chase Adams",
				"description": "This is my description!",
				"aliases":     []string{"/org/content", "/org/mode", "/hugo"},
			}},
	}

	for caseName, tc := range testCases {
		out, err := OrgHeaders([]byte(tc.in))
		if err != nil {
			t.Fatalf("OrgHeaders() failed: %s", err)
		}
		for k, v := range tc.expected {
			switch out[k].(type) {
			case []string:
				outSlice := out[k].([]string)
				vSlice := v.([]string)
				for idx, val := range outSlice {
					if val != vSlice[idx] {
						t.Errorf("%s OrgHeaders() %v = %v\n wants: %v\n", caseName, k, out[k], tc.expected[k])
					}
				}
			case string:
				if out[k] != v {
					t.Errorf("%s OrgHeaders() %v = %v\n wants: %v\n", caseName, k, out[k], tc.expected[k])
				}
			case nil:
				t.Errorf("%s OrgHeaders() %v is nil", caseName, k)
			default:
			}
		}

	}
}