File tree Expand file tree Collapse file tree 4 files changed +107
-4
lines changed
tree-sitter-dscexpression/corpus Expand file tree Collapse file tree 4 files changed +107
-4
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,27 @@ mod tests {
67
67
assert_eq ! ( result, 123 ) ;
68
68
}
69
69
70
+ #[ test]
71
+ fn float ( ) {
72
+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
73
+ let err = parser. parse_and_execute ( "[int(1.0)]" , & Context :: new ( ) ) . unwrap_err ( ) ;
74
+ assert ! ( matches!( err, DscError :: Parser ( _) ) ) ;
75
+ }
76
+
77
+ #[ test]
78
+ fn incomplete_float_missing_digit ( ) {
79
+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
80
+ let err = parser. parse_and_execute ( "[int(.2)]" , & Context :: new ( ) ) . unwrap_err ( ) ;
81
+ assert ! ( matches!( err, DscError :: Parser ( _) ) ) ;
82
+ }
83
+
84
+ #[ test]
85
+ fn incomplete_float_missing_decimal ( ) {
86
+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
87
+ let err = parser. parse_and_execute ( "[int(2.)]" , & Context :: new ( ) ) . unwrap_err ( ) ;
88
+ assert ! ( matches!( err, DscError :: Parser ( _) ) ) ;
89
+ }
90
+
70
91
#[ test]
71
92
fn nested ( ) {
72
93
let mut parser = Statement :: new ( ) . unwrap ( ) ;
@@ -77,7 +98,7 @@ mod tests {
77
98
#[ test]
78
99
fn error ( ) {
79
100
let mut parser = Statement :: new ( ) . unwrap ( ) ;
80
- let err = parser. parse_and_execute ( "[int('foo')]" , & Context :: new ( ) ) . unwrap_err ( ) ;
101
+ let err = parser. parse_and_execute ( "[int('foo.1 ')]" , & Context :: new ( ) ) . unwrap_err ( ) ;
81
102
assert ! ( matches!( err, DscError :: FunctionArg ( _, _) ) ) ;
82
103
}
83
104
}
Original file line number Diff line number Diff line change @@ -36,13 +36,23 @@ impl Function {
36
36
///
37
37
/// This function will return an error if the function node is not valid.
38
38
pub fn new ( statement_bytes : & [ u8 ] , function : & Node ) -> Result < Self , DscError > {
39
- let Some ( function_name) = function. child_by_field_name ( "name" ) else {
39
+ let mut function_name = None ;
40
+ let mut function_args = None ;
41
+ let mut cursor = function. walk ( ) ;
42
+ for member in function. named_children ( & mut cursor) {
43
+ match member. kind ( ) {
44
+ "arguments" => function_args = Some ( member) ,
45
+ "functionName" => function_name = Some ( member) ,
46
+ "ERROR" => return Err ( DscError :: Parser ( "Found error node parsing function" . to_string ( ) ) ) ,
47
+ _ => { }
48
+ }
49
+ }
50
+ let Some ( name) = function_name else {
40
51
return Err ( DscError :: Parser ( "Function name node not found" . to_string ( ) ) ) ;
41
52
} ;
42
- let function_args = function. child_by_field_name ( "args" ) ;
43
53
let args = convert_args_node ( statement_bytes, & function_args) ?;
44
54
Ok ( Function {
45
- name : function_name . utf8_text ( statement_bytes) ?. to_string ( ) ,
55
+ name : name . utf8_text ( statement_bytes) ?. to_string ( ) ,
46
56
args} )
47
57
}
48
58
Original file line number Diff line number Diff line change @@ -110,3 +110,62 @@ Incomplete expression
110
110
(function
111
111
(functionName)))
112
112
(MISSING "]"))
113
+
114
+ =====
115
+ Invalid float argument
116
+ =====
117
+ [myFunction(a.1)]
118
+ ---
119
+
120
+ (statement
121
+ (expression
122
+ (function
123
+ (functionName)
124
+ (ERROR
125
+ (functionName))
126
+ (arguments
127
+ (number)))))
128
+
129
+ =====
130
+ Plus-sign number argument
131
+ =====
132
+ [myFunction(+1)]
133
+ ---
134
+
135
+ (statement
136
+ (expression
137
+ (function
138
+ (functionName)
139
+ (ERROR
140
+ (UNEXPECTED '+'))
141
+ (arguments
142
+ (number)))))
143
+
144
+ =====
145
+ Float input
146
+ =====
147
+ [myFunction(1234.5678)]
148
+ ---
149
+
150
+ (statement
151
+ (expression
152
+ (function
153
+ (functionName)
154
+ (ERROR
155
+ (number))
156
+ (arguments
157
+ (number)))))
158
+
159
+ =====
160
+ Float input starting with decimal
161
+ =====
162
+ [myFunction(.1)]
163
+ ---
164
+
165
+ (statement
166
+ (expression
167
+ (function
168
+ (functionName)
169
+ (ERROR)
170
+ (arguments
171
+ (number)))))
Original file line number Diff line number Diff line change @@ -101,3 +101,16 @@ Nested dot-notation
101
101
(memberName)))))
102
102
(memberAccess
103
103
(memberName))))
104
+
105
+ =====
106
+ Quotes float input
107
+ =====
108
+ [myFunction('1234.5678')]
109
+ ---
110
+
111
+ (statement
112
+ (expression
113
+ (function
114
+ (functionName)
115
+ (arguments
116
+ (string)))))
You can’t perform that action at this time.
0 commit comments