1
1
import attributeParser from '../src/public/app/services/attribute_parser.js' ;
2
2
import { describe , it , expect , execute } from './mini_test.js' ;
3
3
4
- describe ( "Lexer " , ( ) => {
4
+ describe ( "Lexing " , ( ) => {
5
5
it ( "simple label" , ( ) => {
6
- expect ( attributeParser . lexer ( "#label" ) . map ( t => t . text ) )
6
+ expect ( attributeParser . lex ( "#label" ) . map ( t => t . text ) )
7
+ . toEqual ( [ "#label" ] ) ;
8
+ } ) ;
9
+
10
+ it ( "simple label with trailing spaces" , ( ) => {
11
+ expect ( attributeParser . lex ( " #label " ) . map ( t => t . text ) )
7
12
. toEqual ( [ "#label" ] ) ;
8
13
} ) ;
9
14
10
15
it ( "inherited label" , ( ) => {
11
- expect ( attributeParser . lexer ( "#label(inheritable)" ) . map ( t => t . text ) )
16
+ expect ( attributeParser . lex ( "#label(inheritable)" ) . map ( t => t . text ) )
12
17
. toEqual ( [ "#label" , "(" , "inheritable" , ")" ] ) ;
13
18
14
- expect ( attributeParser . lexer ( "#label ( inheritable ) " ) . map ( t => t . text ) )
19
+ expect ( attributeParser . lex ( "#label ( inheritable ) " ) . map ( t => t . text ) )
15
20
. toEqual ( [ "#label" , "(" , "inheritable" , ")" ] ) ;
16
21
} ) ;
17
22
18
23
it ( "label with value" , ( ) => {
19
- expect ( attributeParser . lexer ( "#label=Hallo" ) . map ( t => t . text ) )
24
+ expect ( attributeParser . lex ( "#label=Hallo" ) . map ( t => t . text ) )
20
25
. toEqual ( [ "#label" , "=" , "Hallo" ] ) ;
21
26
} ) ;
22
27
23
28
it ( "label with value" , ( ) => {
24
- const tokens = attributeParser . lexer ( "#label=Hallo" ) ;
29
+ const tokens = attributeParser . lex ( "#label=Hallo" ) ;
25
30
expect ( tokens [ 0 ] . startIndex ) . toEqual ( 0 ) ;
26
31
expect ( tokens [ 0 ] . endIndex ) . toEqual ( 5 ) ;
27
32
} ) ;
28
33
29
34
it ( "relation with value" , ( ) => {
30
- expect ( attributeParser . lexer ( '~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM' ) . map ( t => t . text ) )
35
+ expect ( attributeParser . lex ( '~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM' ) . map ( t => t . text ) )
31
36
. toEqual ( [ "~relation" , "=" , "#root/RclIpMauTOKS/NFi2gL4xtPxM" ] ) ;
32
37
} ) ;
33
38
34
39
it ( "use quotes to define value" , ( ) => {
35
- expect ( attributeParser . lexer ( "#'label a'='hello\"` world'" ) . map ( t => t . text ) )
40
+ expect ( attributeParser . lex ( "#'label a'='hello\"` world'" ) . map ( t => t . text ) )
36
41
. toEqual ( [ "#label a" , "=" , 'hello"` world' ] ) ;
37
42
38
- expect ( attributeParser . lexer ( '#"label a" = "hello\'` world"' ) . map ( t => t . text ) )
43
+ expect ( attributeParser . lex ( '#"label a" = "hello\'` world"' ) . map ( t => t . text ) )
39
44
. toEqual ( [ "#label a" , "=" , "hello'` world" ] ) ;
40
45
41
- expect ( attributeParser . lexer ( '#`label a` = `hello\'" world`' ) . map ( t => t . text ) )
46
+ expect ( attributeParser . lex ( '#`label a` = `hello\'" world`' ) . map ( t => t . text ) )
42
47
. toEqual ( [ "#label a" , "=" , "hello'\" world" ] ) ;
43
48
} ) ;
44
49
} ) ;
45
50
46
51
describe ( "Parser" , ( ) => {
47
52
it ( "simple label" , ( ) => {
48
- const attrs = attributeParser . parser ( [ "#token" ] . map ( t => ( { text : t } ) ) ) ;
53
+ const attrs = attributeParser . parse ( [ "#token" ] . map ( t => ( { text : t } ) ) ) ;
49
54
50
55
expect ( attrs . length ) . toEqual ( 1 ) ;
51
56
expect ( attrs [ 0 ] . type ) . toEqual ( 'label' ) ;
@@ -55,7 +60,7 @@ describe("Parser", () => {
55
60
} ) ;
56
61
57
62
it ( "inherited label" , ( ) => {
58
- const attrs = attributeParser . parser ( [ "#token" , "(" , "inheritable" , ")" ] . map ( t => ( { text : t } ) ) ) ;
63
+ const attrs = attributeParser . parse ( [ "#token" , "(" , "inheritable" , ")" ] . map ( t => ( { text : t } ) ) ) ;
59
64
60
65
expect ( attrs . length ) . toEqual ( 1 ) ;
61
66
expect ( attrs [ 0 ] . type ) . toEqual ( 'label' ) ;
@@ -65,7 +70,7 @@ describe("Parser", () => {
65
70
} ) ;
66
71
67
72
it ( "label with value" , ( ) => {
68
- const attrs = attributeParser . parser ( [ "#token" , "=" , "val" ] . map ( t => ( { text : t } ) ) ) ;
73
+ const attrs = attributeParser . parse ( [ "#token" , "=" , "val" ] . map ( t => ( { text : t } ) ) ) ;
69
74
70
75
expect ( attrs . length ) . toEqual ( 1 ) ;
71
76
expect ( attrs [ 0 ] . type ) . toEqual ( 'label' ) ;
@@ -74,14 +79,14 @@ describe("Parser", () => {
74
79
} ) ;
75
80
76
81
it ( "relation" , ( ) => {
77
- let attrs = attributeParser . parser ( [ "~token" , "=" , "#root/RclIpMauTOKS/NFi2gL4xtPxM" ] . map ( t => ( { text : t } ) ) ) ;
82
+ let attrs = attributeParser . parse ( [ "~token" , "=" , "#root/RclIpMauTOKS/NFi2gL4xtPxM" ] . map ( t => ( { text : t } ) ) ) ;
78
83
79
84
expect ( attrs . length ) . toEqual ( 1 ) ;
80
85
expect ( attrs [ 0 ] . type ) . toEqual ( 'relation' ) ;
81
86
expect ( attrs [ 0 ] . name ) . toEqual ( "token" ) ;
82
87
expect ( attrs [ 0 ] . value ) . toEqual ( 'NFi2gL4xtPxM' ) ;
83
88
84
- attrs = attributeParser . parser ( [ "~token" , "=" , "#NFi2gL4xtPxM" ] . map ( t => ( { text : t } ) ) ) ;
89
+ attrs = attributeParser . parse ( [ "~token" , "=" , "#NFi2gL4xtPxM" ] . map ( t => ( { text : t } ) ) ) ;
85
90
86
91
expect ( attrs . length ) . toEqual ( 1 ) ;
87
92
expect ( attrs [ 0 ] . type ) . toEqual ( 'relation' ) ;
@@ -97,6 +102,9 @@ describe("error cases", () => {
97
102
98
103
expect ( ( ) => attributeParser . lexAndParse ( "#a&b/s" ) )
99
104
. toThrow ( `Attribute name "a&b/s" contains disallowed characters, only alphanumeric characters, colon and underscore are allowed.` ) ;
105
+
106
+ expect ( ( ) => attributeParser . lexAndParse ( "#" ) )
107
+ . toThrow ( `Attribute name is empty, please fill the name.` ) ;
100
108
} ) ;
101
109
} ) ;
102
110
0 commit comments