|
10 | 10 | from Orange.data import (Table, Domain, StringVariable, |
11 | 11 | ContinuousVariable, DiscreteVariable) |
12 | 12 | from Orange.widgets.tests.base import WidgetTest |
| 13 | +from Orange.widgets.utils import vartype |
13 | 14 | from Orange.widgets.utils.itemmodels import PyListModel |
14 | 15 | from Orange.widgets.data.owfeatureconstructor import ( |
15 | 16 | DiscreteDescriptor, ContinuousDescriptor, StringDescriptor, |
16 | 17 | construct_variables, OWFeatureConstructor, |
17 | | - FeatureEditor, DiscreteFeatureEditor) |
| 18 | + FeatureEditor, DiscreteFeatureEditor, FeatureConstructorHandler) |
18 | 19 |
|
19 | 20 | from Orange.widgets.data.owfeatureconstructor import ( |
20 | 21 | freevars, validate_exp, FeatureFunc |
@@ -269,3 +270,45 @@ class TestFeatureEditor(unittest.TestCase): |
269 | 270 | def test_has_functions(self): |
270 | 271 | self.assertIs(FeatureEditor.FUNCTIONS["abs"], abs) |
271 | 272 | self.assertIs(FeatureEditor.FUNCTIONS["sqrt"], math.sqrt) |
| 273 | + |
| 274 | + |
| 275 | +class FeatureConstructorHandlerTests(unittest.TestCase): |
| 276 | + def test_handles_builtins_in_expression(self): |
| 277 | + self.assertTrue( |
| 278 | + FeatureConstructorHandler().is_valid_item( |
| 279 | + OWFeatureConstructor.descriptors, |
| 280 | + StringDescriptor("X", "str(A) + str(B)"), |
| 281 | + {"A": vartype(DiscreteVariable)}, |
| 282 | + {"B": vartype(DiscreteVariable)} |
| 283 | + ) |
| 284 | + ) |
| 285 | + |
| 286 | + # no variables is also ok |
| 287 | + self.assertTrue( |
| 288 | + FeatureConstructorHandler().is_valid_item( |
| 289 | + OWFeatureConstructor.descriptors, |
| 290 | + StringDescriptor("X", "str('foo')"), |
| 291 | + {}, |
| 292 | + {} |
| 293 | + ) |
| 294 | + ) |
| 295 | + |
| 296 | + # should fail on unknown variables |
| 297 | + self.assertFalse( |
| 298 | + FeatureConstructorHandler().is_valid_item( |
| 299 | + OWFeatureConstructor.descriptors, |
| 300 | + StringDescriptor("X", "str(X)"), |
| 301 | + {}, |
| 302 | + {} |
| 303 | + ) |
| 304 | + ) |
| 305 | + |
| 306 | + def test_handles_special_characters_in_var_names(self): |
| 307 | + self.assertTrue( |
| 308 | + FeatureConstructorHandler().is_valid_item( |
| 309 | + OWFeatureConstructor.descriptors, |
| 310 | + StringDescriptor("X", "A_2_f"), |
| 311 | + {"A.2 f": vartype(DiscreteVariable)}, |
| 312 | + {} |
| 313 | + ) |
| 314 | + ) |
0 commit comments