-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
74 lines (68 loc) · 1.68 KB
/
parser_test.go
File metadata and controls
74 lines (68 loc) · 1.68 KB
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
package cssx
import "testing"
func TestParsePlainSelector(t *testing.T) {
ast, err := Parse(".product")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
sel, ok := ast.Expr.(*SelectorExpr)
if !ok {
t.Fatalf("expected SelectorExpr, got %T", ast.Expr)
}
if sel.Raw != ".product" {
t.Fatalf("expected selector '.product', got %q", sel.Raw)
}
}
func TestParseExpression(t *testing.T) {
ast, err := Parse(":text(:first(h1))")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
call, ok := ast.Expr.(*CallExpr)
if !ok {
t.Fatalf("expected CallExpr, got %T", ast.Expr)
}
if call.Name != "text" || len(call.Args) != 1 {
t.Fatalf("unexpected call: %#v", call)
}
}
func TestParsePipelineSugar(t *testing.T) {
ast, err := Parse(".section .item >> :nth(2) >> :text()")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
pipe, ok := ast.Expr.(*PipelineExpr)
if !ok {
t.Fatalf("expected PipelineExpr, got %T", ast.Expr)
}
if pipe.Base == nil || pipe.Base.Raw != ".section .item" {
t.Fatalf("unexpected base selector: %#v", pipe.Base)
}
if len(pipe.Calls) != 2 {
t.Fatalf("expected 2 calls, got %d", len(pipe.Calls))
}
}
func TestParseMixedArgOrderAllowed(t *testing.T) {
ast, err := Parse(":foo(:bar(a), \"x\")")
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
if _, ok := ast.Expr.(*CallExpr); !ok {
t.Fatalf("expected CallExpr, got %T", ast.Expr)
}
}
func TestParseErrors(t *testing.T) {
cases := []string{
":",
":text(",
":text(:first(h1)",
":attr(\"href\" :first(a))",
" ",
".section >> text()",
}
for _, input := range cases {
if _, err := Parse(input); err == nil {
t.Fatalf("expected error for %q", input)
}
}
}