summaryrefslogtreecommitdiffstats
path: root/write_test.go
blob: 8cca449954c236178c828ed5620b812776364ce2 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package cnm

import (
	"bytes"
	"testing"
)

var writeTests = map[string]*Document{
	"": &Document{},

	"title\n\tfoo bar\n": &Document{
		Title: "foo bar",
	},

	"title\n\tfoo bar baz\n" +
		"links\n\tqwe\\ asd zxc 123\n\tfoo\n" +
		"site\n\tfoo Foo\n\t\tbar\n\t\tbaz/qu\\ ux Test\n\t\t\t123\n\ttest\n": &Document{
		Title: "foo bar baz",
		Links: []Link{
			Link{"qwe asd", "zxc 123", ""},
			Link{"foo", "", ""},
		},
		Site: Site{
			Children: []Site{
				Site{
					Path: "foo",
					Name: "Foo",
					Children: []Site{
						Site{Path: "bar"},
						Site{
							Path: "baz/qu ux",
							Name: "Test",
							Children: []Site{
								Site{Path: "123"},
							},
						},
					},
				},
				Site{Path: "test"},
			},
		},
	},

	`title
	Test document
content
	section Test section
		text
			This is \n just a
		text pre
			  t  e 
			 s  t  
				
		raw text/plain
			of various \n features
		section of the
			table
				header
					text
						Column 1
					text
						Column 2
				row
					text
						CNM
					text
						document

						format
				row
					section
					list
						text
							ipsum
						list ordered
							list unordered
								text
									dolor

									sit amet
	embed text/cnm cnp://example.com/
		thing whatever
`: &Document{
		Title: "Test document",
		Content: &ContentBlock{
			name: "content",
			args: nil,
			children: []Block{
				&SectionBlock{ContentBlock{
					name: "section",
					args: []string{"Test", "section"},
					children: []Block{
						&TextBlock{
							Contents: TextPlainContents{[]string{
								"This is \n just a",
							}},
						},
						&TextBlock{
							Format: "pre",
							Contents: TextPreContents{
								"  t  e \n s  t  \n\t",
							},
						},
						&RawBlock{
							Syntax:   "text/plain",
							Contents: TextRawContents{"of various \\n features"},
						},
						&SectionBlock{ContentBlock{
							name: "section",
							args: []string{"of the"},
							children: []Block{
								&TableBlock{ContentBlock{
									name: "table",
									args: nil,
									children: []Block{
										&HeaderBlock{ContentBlock{
											name: "header",
											args: []string{},
											children: []Block{
												&TextBlock{
													Contents: TextPlainContents{[]string{
														"Column 1",
													}},
												},
												&TextBlock{
													Contents: TextPlainContents{[]string{
														"Column 2",
													}},
												},
											},
										}},
										&RowBlock{ContentBlock{
											name: "row",
											args: []string{},
											children: []Block{
												&TextBlock{
													Contents: TextPlainContents{[]string{
														"CNM",
													}},
												},
												&TextBlock{
													Contents: TextPlainContents{[]string{
														"document",
														"format",
													}},
												},
											},
										}},
										&RowBlock{ContentBlock{
											name: "row",
											args: []string{""},
											children: []Block{
												&SectionBlock{ContentBlock{
													name: "section",
													args: []string{"", "", ""},
												}},
												&ListBlock{ContentBlock{
													name: "list",
													args: nil,
													children: []Block{
														&TextBlock{
															Contents: TextPlainContents{[]string{
																"ipsum",
															}},
														},
														&ListBlock{ContentBlock{
															name: "list",
															args: []string{"ordered"},
															children: []Block{
																&ListBlock{ContentBlock{
																	name: "list",
																	args: []string{"unordered"},
																	children: []Block{
																		&TextBlock{
																			Contents: TextPlainContents{[]string{
																				"dolor",
																				"sit amet",
																			}},
																		},
																	},
																}},
															},
														}},
													},
												}},
											},
										}},
									},
								}},
							},
						}},
					},
				}},
				&EmbedBlock{
					Type:        "text/cnm",
					URL:         "cnp://example.com/",
					Description: "thing whatever",
				},
			},
		},
	},
}

func TestWrite(t *testing.T) {
	for k, v := range writeTests {
		t.Run(k, func(t *testing.T) {
			var buf bytes.Buffer
			err := v.Write(&buf)
			if err != nil {
				t.Fatalf("Write error: %v", err)
			}
			w := buf.String()
			if k != w {
				t.Log("====================")
				t.Log("expected:\n" + k)
				t.Log("--------------------")
				t.Log("     got:\n" + w)
				t.Log("====================")
				t.Fatal("Write: output did not match expected document")
			}
		})
	}
}