@@ -6,61 +6,61 @@ import { DEFAULT_CONFIG } from '../src/runtime/smartscript/config'
6
6
describe ( 'Mathematical Notation Support' , ( ) => {
7
7
const config = DEFAULT_CONFIG
8
8
const patterns = createPatterns ( config )
9
-
9
+
10
10
describe ( 'LaTeX-style Notation' , ( ) => {
11
11
it ( 'should support both x_n and x_{n} formats for subscripts' , ( ) => {
12
12
const pattern = patterns . mathSub
13
-
13
+
14
14
// Single character without braces (common in math)
15
15
expect ( 'x_n' . match ( pattern ) ) . toEqual ( [ 'x_n' ] )
16
16
expect ( 'x_i' . match ( pattern ) ) . toEqual ( [ 'x_i' ] )
17
17
expect ( 'x_j' . match ( pattern ) ) . toEqual ( [ 'x_j' ] )
18
18
expect ( 'a_0' . match ( pattern ) ) . toEqual ( [ 'a_0' ] )
19
-
19
+
20
20
// Single character with braces (also valid)
21
21
expect ( 'x_{n}' . match ( pattern ) ) . toEqual ( [ 'x_{n}' ] )
22
22
expect ( 'x_{i}' . match ( pattern ) ) . toEqual ( [ 'x_{i}' ] )
23
-
23
+
24
24
// Multi-character requires braces
25
25
expect ( 'x_{10}' . match ( pattern ) ) . toEqual ( [ 'x_{10}' ] )
26
26
expect ( 'x_{n+1}' . match ( pattern ) ) . toEqual ( [ 'x_{n+1}' ] )
27
27
expect ( 'x_{ij}' . match ( pattern ) ) . toEqual ( [ 'x_{ij}' ] )
28
28
} )
29
-
29
+
30
30
it ( 'should support both x^n and x^{n} formats for superscripts' , ( ) => {
31
31
const pattern = patterns . mathSuper
32
-
32
+
33
33
// Single character without braces (common in math)
34
34
expect ( 'x^2' . match ( pattern ) ) . toEqual ( [ 'x^2' ] )
35
35
expect ( 'x^n' . match ( pattern ) ) . toEqual ( [ 'x^n' ] )
36
36
expect ( 'x^i' . match ( pattern ) ) . toEqual ( [ 'x^i' ] )
37
37
expect ( 'e^x' . match ( pattern ) ) . toEqual ( [ 'e^x' ] )
38
-
38
+
39
39
// Single character with braces (also valid)
40
40
expect ( 'x^{2}' . match ( pattern ) ) . toEqual ( [ 'x^{2}' ] )
41
41
expect ( 'x^{n}' . match ( pattern ) ) . toEqual ( [ 'x^{n}' ] )
42
-
42
+
43
43
// Multi-character requires braces
44
44
expect ( 'x^{10}' . match ( pattern ) ) . toEqual ( [ 'x^{10}' ] )
45
45
expect ( 'x^{n+1}' . match ( pattern ) ) . toEqual ( [ 'x^{n+1}' ] )
46
46
expect ( 'x^{-1}' . match ( pattern ) ) . toEqual ( [ 'x^{-1}' ] )
47
47
} )
48
48
} )
49
-
49
+
50
50
describe ( 'Real-world Mathematical Expressions' , ( ) => {
51
51
it ( 'should handle common mathematical sequences' , ( ) => {
52
52
const combined = createCombinedPattern ( patterns , config )
53
-
53
+
54
54
const sequence = 'The sequence a_1, a_2, ..., a_n converges'
55
55
const matches = sequence . match ( combined )
56
56
expect ( matches ) . toContain ( 'a_1' )
57
57
expect ( matches ) . toContain ( 'a_2' )
58
58
expect ( matches ) . toContain ( 'a_n' )
59
59
} )
60
-
60
+
61
61
it ( 'should handle polynomial expressions' , ( ) => {
62
62
const combined = createCombinedPattern ( patterns , config )
63
-
63
+
64
64
// Note: Without spaces, a_nx^n only matches a_n (x^n is preceded by letter n)
65
65
// This is correct behavior to avoid false matches
66
66
const polynomial = 'P(x) = a_0 + a_1x + a_2x^2 + ... + a_n x^n'
@@ -72,90 +72,90 @@ describe('Mathematical Notation Support', () => {
72
72
expect ( matches ) . toContain ( 'a_n' )
73
73
expect ( matches ) . toContain ( 'x^n' )
74
74
} )
75
-
75
+
76
76
it ( 'should handle matrix notation' , ( ) => {
77
77
const combined = createCombinedPattern ( patterns , config )
78
-
78
+
79
79
const matrix = 'Matrix A has elements a_{ij} where i goes from 1 to m and j from 1 to n'
80
80
const matches = matrix . match ( combined )
81
81
expect ( matches ) . toContain ( 'a_{ij}' )
82
82
} )
83
-
83
+
84
84
it ( 'should handle exponential and logarithmic expressions' , ( ) => {
85
85
const combined = createCombinedPattern ( patterns , config )
86
-
86
+
87
87
const expressions = [
88
88
{ text : 'e^x is the exponential function' , expected : [ 'e^x' ] } ,
89
89
{ text : 'e^{2x} grows faster' , expected : [ 'e^{2x}' ] } ,
90
90
{ text : 'The function f_2(x) is defined' , expected : [ 'f_2' ] } ,
91
91
{ text : 'Sequence a_n converges' , expected : [ 'a_n' ] } ,
92
92
]
93
-
93
+
94
94
expressions . forEach ( ( { text, expected } ) => {
95
95
const matches = text . match ( combined )
96
96
if ( expected . length > 0 ) {
97
97
expect ( matches ) . toBeTruthy ( )
98
- expected . forEach ( exp => {
98
+ expected . forEach ( ( exp ) => {
99
99
expect ( matches ) . toContain ( exp )
100
100
} )
101
101
}
102
102
} )
103
103
} )
104
104
} )
105
-
105
+
106
106
describe ( 'Edge Cases and Non-matches' , ( ) => {
107
107
it ( 'should not match underscores in file names or identifiers' , ( ) => {
108
108
const combined = createCombinedPattern ( patterns , config )
109
-
109
+
110
110
// These should NOT match the math patterns
111
111
const fileNames = [
112
112
'file_name.txt' ,
113
113
'my_variable' ,
114
114
'snake_case_function' ,
115
115
'__init__' ,
116
116
]
117
-
118
- fileNames . forEach ( name => {
117
+
118
+ fileNames . forEach ( ( name ) => {
119
119
const matches = name . match ( combined )
120
120
expect ( matches ) . toBeNull ( )
121
121
} )
122
122
} )
123
-
123
+
124
124
it ( 'should not match when pattern is incomplete' , ( ) => {
125
125
const combined = createCombinedPattern ( patterns , config )
126
-
126
+
127
127
// These should NOT match
128
128
const incomplete = [
129
- 'x_' , // No character after underscore
130
- 'x^' , // No character after caret
131
- '_n' , // Underscore at start
132
- '^2' , // Caret at start
129
+ 'x_' , // No character after underscore
130
+ 'x^' , // No character after caret
131
+ '_n' , // Underscore at start
132
+ '^2' , // Caret at start
133
133
]
134
-
135
- incomplete . forEach ( text => {
134
+
135
+ incomplete . forEach ( ( text ) => {
136
136
const matches = text . match ( combined )
137
137
expect ( matches ) . toBeNull ( )
138
138
} )
139
139
} )
140
-
140
+
141
141
it ( 'should handle mixed subscripts and superscripts' , ( ) => {
142
142
const combined = createCombinedPattern ( patterns , config )
143
-
143
+
144
144
// Note: x_i^2 is tricky - after matching x_i, we're left with ^2
145
145
// But i^2 also matches because i is a standalone letter
146
146
const mixed = 'The term x_i represents x_i squared'
147
147
const matches = mixed . match ( combined )
148
148
expect ( matches ) . toContain ( 'x_i' )
149
149
expect ( matches ) . toHaveLength ( 2 ) // Two x_i matches
150
-
150
+
151
151
// For x_i^2, better to have spaces: x_i ^2
152
152
const mixed2 = 'The term x_i ^2 equals x_i squared'
153
153
const matches2 = mixed2 . match ( combined )
154
154
expect ( matches2 ) . toContain ( 'x_i' )
155
155
expect ( matches2 ) . toHaveLength ( 2 ) // Two x_i, ^2 won't match without variable
156
156
} )
157
157
} )
158
-
158
+
159
159
describe ( 'Processing Results' , ( ) => {
160
160
it ( 'should correctly process single-letter subscripts' , ( ) => {
161
161
const result = processMatch ( 'x_n' )
@@ -166,7 +166,7 @@ describe('Mathematical Notation Support', () => {
166
166
expect ( result . parts [ 1 ] . type ) . toBe ( 'sub' )
167
167
expect ( result . parts [ 1 ] . content ) . toBe ( 'n' )
168
168
} )
169
-
169
+
170
170
it ( 'should correctly process single-letter superscripts' , ( ) => {
171
171
const result = processMatch ( 'x^n' )
172
172
expect ( result . modified ) . toBe ( true )
@@ -176,19 +176,19 @@ describe('Mathematical Notation Support', () => {
176
176
expect ( result . parts [ 1 ] . type ) . toBe ( 'super' )
177
177
expect ( result . parts [ 1 ] . content ) . toBe ( 'n' )
178
178
} )
179
-
179
+
180
180
it ( 'should correctly extract content from braced expressions' , ( ) => {
181
181
const result1 = processMatch ( 'x_{n+1}' )
182
182
expect ( result1 . modified ) . toBe ( true )
183
183
expect ( result1 . parts ) . toHaveLength ( 2 )
184
184
expect ( result1 . parts [ 0 ] . content ) . toBe ( 'x' )
185
185
expect ( result1 . parts [ 1 ] . content ) . toBe ( 'n+1' ) // Braces removed
186
-
186
+
187
187
const result2 = processMatch ( 'x^{2n}' )
188
188
expect ( result2 . modified ) . toBe ( true )
189
189
expect ( result2 . parts ) . toHaveLength ( 2 )
190
190
expect ( result2 . parts [ 0 ] . content ) . toBe ( 'x' )
191
191
expect ( result2 . parts [ 1 ] . content ) . toBe ( '2n' ) // Braces removed
192
192
} )
193
193
} )
194
- } )
194
+ } )
0 commit comments