()
| 226 | |
| 227 | |
| 228 | def test_function_basics(): |
| 229 | |
| 230 | # Test init fail |
| 231 | assert_raises(TypeError, Function) # no args |
| 232 | assert_raises(ValueError, Function, 3) # need string |
| 233 | |
| 234 | # Test init success 1 |
| 235 | fun = Function('void main(){}') |
| 236 | assert_equal(fun.name, 'main') |
| 237 | assert len(fun.template_vars) == 0 |
| 238 | |
| 239 | # Test init success with template vars |
| 240 | fun = Function('void main(){$foo; $bar;}') |
| 241 | assert_equal(fun.name, 'main') |
| 242 | assert len(fun.template_vars) == 2 |
| 243 | assert_in('foo', fun.template_vars) |
| 244 | |
| 245 | # Test that `var in fun` syntax works as well |
| 246 | assert 'foo' in fun |
| 247 | assert 'bar' in fun |
| 248 | assert 'baz' not in fun |
| 249 | |
| 250 | assert_in('bar', fun.template_vars) |
| 251 | |
| 252 | # Test setting verbatim expressions |
| 253 | assert_raises(KeyError, fun.__setitem__, 'bla', '33') # no such template |
| 254 | fun['foo'] = '33' |
| 255 | fun['bar'] = 'bla bla' |
| 256 | assert_is(type(fun['foo']), TextExpression) |
| 257 | assert_equal(fun['foo'].expression(None), '33') |
| 258 | assert_is(type(fun['bar']), TextExpression) |
| 259 | assert_equal(fun['bar'].expression(None), 'bla bla') |
| 260 | |
| 261 | # Test setting call expressions |
| 262 | fun = Function('void main(){\n$foo;\n$bar;\n$spam(XX);\n$eggs(YY);\n}') |
| 263 | trans = Function('float transform_scale(float x) {return x+1.0;}') |
| 264 | assert_raises(TypeError, trans) # requires 1 arg |
| 265 | assert_raises(TypeError, trans, '1', '2') |
| 266 | fun['foo'] = trans('2') |
| 267 | fun['bar'] = trans('3') |
| 268 | fun['spam'] = trans |
| 269 | fun['eggs'] = trans |
| 270 | # |
| 271 | for name in ['foo', 'bar']: |
| 272 | assert_is(type(fun[name]), FunctionCall) |
| 273 | assert_equal(fun[name].function, trans) |
| 274 | assert_in(trans, fun.dependencies()) |
| 275 | for name in ['spam', 'eggs']: |
| 276 | assert_equal(fun[name], trans) |
| 277 | |
| 278 | # |
| 279 | text = fun.compile() |
| 280 | assert_in('\ntransform_scale(2);\n', text) |
| 281 | assert_in('\ntransform_scale(3);\n', text) |
| 282 | assert_in('\ntransform_scale(XX);\n', text) |
| 283 | assert_in('\ntransform_scale(YY);\n', text) |
| 284 | |
| 285 | # test pre/post assignments |
nothing calls this directly
no test coverage detected
searching dependent graphs…