| 8 | ) |
| 9 | |
| 10 | func TestPHPFunctionGenerator_Generate(t *testing.T) { |
| 11 | tests := []struct { |
| 12 | name string |
| 13 | function phpFunction |
| 14 | contains []string // Strings that should be present in the output |
| 15 | }{ |
| 16 | { |
| 17 | name: "simple string function", |
| 18 | function: phpFunction{ |
| 19 | Name: "greet", |
| 20 | ReturnType: phpString, |
| 21 | Params: []phpParameter{ |
| 22 | {Name: "name", PhpType: phpString}, |
| 23 | }, |
| 24 | }, |
| 25 | contains: []string{ |
| 26 | "PHP_FUNCTION(greet)", |
| 27 | "zend_string *name = NULL;", |
| 28 | "Z_PARAM_STR(name)", |
| 29 | "zend_string *result = go_greet(name);", |
| 30 | "RETURN_STR(result)", |
| 31 | }, |
| 32 | }, |
| 33 | { |
| 34 | name: "function with default parameter", |
| 35 | function: phpFunction{ |
| 36 | Name: "calculate", |
| 37 | ReturnType: phpInt, |
| 38 | Params: []phpParameter{ |
| 39 | {Name: "base", PhpType: phpInt}, |
| 40 | {Name: "multiplier", PhpType: phpInt, HasDefault: true, DefaultValue: "2"}, |
| 41 | }, |
| 42 | }, |
| 43 | contains: []string{ |
| 44 | "PHP_FUNCTION(calculate)", |
| 45 | "zend_long base = 0;", |
| 46 | "zend_long multiplier = 2;", |
| 47 | "ZEND_PARSE_PARAMETERS_START(1, 2)", |
| 48 | "Z_PARAM_OPTIONAL", |
| 49 | "Z_PARAM_LONG(base)", |
| 50 | "Z_PARAM_LONG(multiplier)", |
| 51 | }, |
| 52 | }, |
| 53 | { |
| 54 | name: "void function", |
| 55 | function: phpFunction{ |
| 56 | Name: "doSomething", |
| 57 | ReturnType: phpVoid, |
| 58 | Params: []phpParameter{ |
| 59 | {Name: "action", PhpType: phpString}, |
| 60 | }, |
| 61 | }, |
| 62 | contains: []string{ |
| 63 | "PHP_FUNCTION(doSomething)", |
| 64 | "go_doSomething(action);", |
| 65 | }, |
| 66 | }, |
| 67 | { |