File: parser_test.go

package info (click to toggle)
bingo 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,884 kB
  • sloc: sh: 258; makefile: 117
file content (95 lines) | stat: -rw-r--r-- 2,494 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
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.

package makefile

import (
	"fmt"
	"strings"
)

func ExampleParser_Parse_withComments() {
	contents := `
include github.com/tj/foo
# Stuff here:
#
#    :)
#
# Start the dev server.
start:
	@gopherjs -m -v serve --http :3000 github.com/tj/docs/client
.PHONY: start
# Start the API server.
api:
	@go run server/cmd/api/api.go
.PHONY: api
# Display dependency graph.
deps:
	@godepgraph github.com/tj/docs/client | dot -Tsvg | browser
.PHONY: deps
# Display size of dependencies.
#
# - foo
# - bar
# - baz
#
size:
	@gopherjs build client/*.go -m -o /tmp/out.js
	@du -h /tmp/out.js
	@gopher-count /tmp/out.js | sort -nr
.PHONY: size
.PHONY: dummy
# Just a comment.
# Just another comment.
dummy:
	@ls
`

	nodes, err := Parse(strings.NewReader(contents))
	if err != nil {
		panic(err)
	}

	for _, node := range nodes {
		fmt.Printf("%#v\n", node)
	}

	// Output:
	// makefile.Include{node:makefile.node{lines:[]int{1}}, Value:"github.com/tj/foo"}
	// makefile.Comment{node:makefile.node{lines:[]int{7}}, Target:"start", Value:"Stuff here:\n\n   :)\n\nStart the dev server.", Default:false}
	// makefile.Comment{node:makefile.node{lines:[]int{8, 11}}, Target:"api", Value:"Start the API server.", Default:false}
	// makefile.Comment{node:makefile.node{lines:[]int{15}}, Target:"deps", Value:"Display dependency graph.", Default:false}
	// makefile.Comment{node:makefile.node{lines:[]int{24}}, Target:"size", Value:"Display size of dependencies.\n\n- foo\n- bar\n- baz", Default:false}
	// makefile.Comment{node:makefile.node{lines:[]int{32}}, Target:"dummy", Value:"Just a comment.\nJust another comment.", Default:false}
}

func ExampleParser_Parse_withoutComments() {
	contents := `
include github.com/tj/foo
include github.com/tj/bar
include github.com/tj/something/here
start:
	@gopherjs -m -v serve --http :3000 github.com/tj/docs/client
.PHONY: start
api:
	@go run server/cmd/api/api.go
.PHONY: api
deps:
	@godepgraph github.com/tj/docs/client | dot -Tsvg | browser
.PHONY: deps
`

	nodes, err := Parse(strings.NewReader(contents))
	if err != nil {
		panic(err)
	}

	for _, node := range nodes {
		fmt.Printf("%#v\n", node)
	}

	// Output:
	// makefile.Include{node:makefile.node{lines:[]int{1}}, Value:"github.com/tj/foo"}
	// makefile.Include{node:makefile.node{lines:[]int{2}}, Value:"github.com/tj/bar"}
	// makefile.Include{node:makefile.node{lines:[]int{3}}, Value:"github.com/tj/something/here"}
}