UNPKG

2.97 kBtext/coffeescriptView Raw
1makeGrammar = require '../index.js'
2
3describe "Atom Syntax Tools", ->
4
5 describe "when you use macros in regexes", ->
6 inputGrammar =
7 scopeName: "source.my-grammar-1"
8 macros:
9 nested_item: /{ident}\[{item_getter}\]/
10 ident: /[a-zA-Z_]\w*/
11 digits: /\d+/
12 item_getter: /(?:{digits}|{ident})/
13
14 patterns: [
15 { match: /// {ident} /// }
16 { match: /// {nested_item} /// }
17 ]
18
19 it "expands simple macros", ->
20 g = makeGrammar inputGrammar
21 expect(g.patterns[0].match).toBe("[a-zA-Z_]\\w*")
22
23 it "expands nested macros", ->
24 g = makeGrammar inputGrammar
25 expect(g.patterns[1].match).toBe("[a-zA-Z_]\\w*\\[(?:\\d+|[a-zA-Z_]\\w*)\\]")
26
27
28 describe "when you want to keep your grammar short", ->
29
30 inputGrammar =
31 scopeName: "source.my-grammar"
32
33 patterns: [
34 "#block"
35 {i: "#another-one"}
36 ]
37
38 repository:
39 block:
40 n: "string.quoted.double.my-grammar"
41 N: "constant.character.escape.my-grammar"
42 b: /// begin here ///
43 c: { 1: "hello" }
44 e: /// end here ///
45 C: { 2: "world" }
46 p: [ "#match" ]
47
48 match:
49 m: /// match ///
50 c: { 1: "hello world" }
51
52 it "lets you abbreviate name with n", ->
53 g = makeGrammar inputGrammar
54 expect(g.repository.block.patterns[0].name).toBe("string.quoted.double.my-grammar")
55
56 it "lets you abbreviate contentName with N", ->
57 g = makeGrammar inputGrammar
58 expect(g.repository.block.patterns[0].contentName).toBe("constant.character.escape.my-grammar")
59
60 it "lets you abbreviate begin with b", ->
61 g = makeGrammar inputGrammar
62 expect(g.repository.block.patterns[0].begin).toBe("beginhere")
63
64 it "lets you abbreviate end with e", ->
65 g = makeGrammar inputGrammar
66 expect(g.repository.block.patterns[0].end).toBe("endhere")
67
68 it "lets you abbreviate beginCaptures with c, if you used begin regex", ->
69 g = makeGrammar inputGrammar
70 expect(g.repository.block.patterns[0].beginCaptures).toEqual {1: { "name": "hello.my-grammar"} }
71
72 it "lets you abbreviate endCaptures with C", ->
73 g = makeGrammar inputGrammar
74 expect(g.repository.block.patterns[0].endCaptures).toEqual {2: { name: "world.my-grammar"} }
75
76 it "lets you use a string instead of object with include key", ->
77 g = makeGrammar inputGrammar
78 expect(g.patterns[0]).toEqual {include: "#block"}
79
80 it "lets you abbreviate include with i", ->
81 g = makeGrammar inputGrammar
82 expect(g.patterns[1].include).toBe "#another-one"
83
84 it "lets you abbreviate match with m", ->
85 g = makeGrammar inputGrammar
86 expect(g.repository.match.patterns[0].match).toBe "match"
87
88 it "lets you abbreviate captures for match with c", ->
89 g = makeGrammar inputGrammar
90 expect(g.repository.match.patterns[0].captures).toEqual {1: {name: "hello world.my-grammar"}}