-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
94 lines (86 loc) · 2.53 KB
/
parser_test.go
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
// Copyright 2017 Tomas Machalek <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vertigo
import (
"context"
"fmt"
"path"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
type TestingProcessor struct {
paragraphs []*Structure
newLines []*Structure
marks []*Structure
data []*Token
}
func (tp *TestingProcessor) ProcToken(token *Token, line int, err error) error {
fmt.Println("TOKEN: ", token)
tp.data = append(tp.data, token)
return nil
}
func (tp *TestingProcessor) ProcStruct(strc *Structure, line int, err error) error {
fmt.Println("STRUCT: ", strc)
switch strc.Name {
case "doc":
case "p":
tp.paragraphs = append(tp.paragraphs, strc)
case "nl":
tp.newLines = append(tp.newLines, strc)
case "m":
tp.marks = append(tp.marks, strc)
}
return nil
}
func (tp *TestingProcessor) ProcStructClose(strc *StructureClose, line int, err error) error {
fmt.Println("SCLOSE: ", strc)
return nil
}
func TestParseVerticalFile(t *testing.T) {
_, fileName, _, ok := runtime.Caller(0)
if !ok {
assert.Fail(t, "failed to determine vertical-generator script")
}
filePath := path.Join(path.Dir(fileName), "scripts/genvert.py")
conf := ParserConf{
InputFilePath: "| /usr/bin/python3 " + filePath,
StructAttrAccumulator: "nil",
}
tp := &TestingProcessor{
paragraphs: make([]*Structure, 0, 20),
newLines: make([]*Structure, 0, 20),
marks: make([]*Structure, 0, 20),
data: make([]*Token, 0, 20),
}
ctx := context.Background()
err := ParseVerticalFile(ctx, &conf, tp)
assert.NoError(t, err)
assert.Equal(t, 2, len(tp.paragraphs))
for i, p := range tp.paragraphs {
assert.Equal(t, fmt.Sprintf("par%d", i+1), p.Attrs["id"])
}
assert.Equal(t, 4, len(tp.newLines))
for _, nl := range tp.newLines {
assert.Equal(t, "nl", nl.Name)
assert.True(t, nl.IsEmpty)
assert.Equal(t, 0, len(nl.Attrs))
}
assert.Equal(t, 4, len(tp.marks))
for _, m := range tp.marks {
assert.Equal(t, "m", m.Name)
assert.True(t, m.IsEmpty)
assert.Equal(t, 0, len(m.Attrs))
}
}