| 10 | ) |
| 11 | |
| 12 | func TestConstantParser(t *testing.T) { |
| 13 | tests := []struct { |
| 14 | name string |
| 15 | input string |
| 16 | expect int |
| 17 | assert func(t *testing.T, constants []phpConstant) |
| 18 | }{ |
| 19 | { |
| 20 | name: "single constant", |
| 21 | input: `package main |
| 22 | |
| 23 | //export_php:const |
| 24 | const MyConstant = "test_value"`, |
| 25 | expect: 1, |
| 26 | assert: func(t *testing.T, cs []phpConstant) { |
| 27 | c := cs[0] |
| 28 | assert.Equal(t, "MyConstant", c.Name) |
| 29 | assert.Equal(t, `"test_value"`, c.Value) |
| 30 | assert.Equal(t, phpString, c.PhpType) |
| 31 | assert.False(t, c.IsIota) |
| 32 | }, |
| 33 | }, |
| 34 | { |
| 35 | name: "multiple constants", |
| 36 | input: `package main |
| 37 | |
| 38 | //export_php:const |
| 39 | const FirstConstant = "first" |
| 40 | |
| 41 | //export_php:const |
| 42 | const SecondConstant = 42 |
| 43 | |
| 44 | //export_php:const |
| 45 | const ThirdConstant = true`, |
| 46 | expect: 3, |
| 47 | assert: func(t *testing.T, cs []phpConstant) { |
| 48 | names := []string{"FirstConstant", "SecondConstant", "ThirdConstant"} |
| 49 | values := []string{`"first"`, "42", "true"} |
| 50 | types := []phpType{phpString, phpInt, phpBool} |
| 51 | for i, c := range cs { |
| 52 | assert.Equal(t, names[i], c.Name) |
| 53 | assert.Equal(t, values[i], c.Value) |
| 54 | assert.Equal(t, types[i], c.PhpType) |
| 55 | } |
| 56 | }, |
| 57 | }, |
| 58 | { |
| 59 | name: "iota constant", |
| 60 | input: `package main |
| 61 | |
| 62 | //export_php:const |
| 63 | const IotaConstant = iota`, |
| 64 | expect: 1, |
| 65 | assert: func(t *testing.T, cs []phpConstant) { |
| 66 | c := cs[0] |
| 67 | assert.Equal(t, "IotaConstant", c.Name) |
| 68 | assert.True(t, c.IsIota) |
| 69 | assert.Equal(t, "0", c.Value) |