|
| 1 | +from pytest import raises |
| 2 | + |
| 3 | + |
| 4 | +class TestTypeHints: |
| 5 | + def setup_class(cls): |
| 6 | + import cppyy |
| 7 | + |
| 8 | + cppyy.cppdef( |
| 9 | + r""" |
| 10 | + namespace TypeHints { |
| 11 | + int x = 10; |
| 12 | + template <typename T> |
| 13 | + struct MyTKlass{ |
| 14 | + T obj; |
| 15 | + }; |
| 16 | + struct MyKlass { |
| 17 | + std::string name = "MyKlass"; |
| 18 | + bool flag = false; |
| 19 | + std::vector<int> array = {}; |
| 20 | + float callme(std::string a, bool b, std::vector<int> c) { return 0.0f; } |
| 21 | + template <typename T> |
| 22 | + T callme(T v) { return v; } |
| 23 | + }; |
| 24 | + typedef MyKlass Klass; |
| 25 | + typedef MyTKlass<float> KlassFloat; |
| 26 | + float fn(std::string a, bool b, std::vector<int> c) { return 0.0f; } |
| 27 | + template <typename T> |
| 28 | + T tmpl_fn(T v) { return v; } |
| 29 | + } // namespace TypeHints |
| 30 | + int x = 10; |
| 31 | + double y = 10; |
| 32 | + unsigned short z = 10; |
| 33 | + char a = 'a'; |
| 34 | + signed char sa = 'a'; |
| 35 | + unsigned char usa = 'a'; |
| 36 | + template <typename T> |
| 37 | + struct MyTKlass{ |
| 38 | + T obj; |
| 39 | + }; |
| 40 | + struct MyKlass { |
| 41 | + std::string name = "MyKlass"; |
| 42 | + bool flag = false; |
| 43 | + std::vector<int> array = {}; |
| 44 | + float callme(std::string a, bool b, std::vector<int> c) { return 0.0f; } |
| 45 | + template <typename T> |
| 46 | + T callme(T v) { return v; } |
| 47 | + }; |
| 48 | + float callme(std::string a, bool b, std::vector<int> c) { return 0.0f; } |
| 49 | + template <typename T> |
| 50 | + T tmpl_fn(T v) { return v; } |
| 51 | + typedef MyKlass Klass; |
| 52 | + typedef MyTKlass<float> KlassFloat; |
| 53 | + """ |
| 54 | + ) |
| 55 | + |
| 56 | + def test_invalids(self): |
| 57 | + from cppyy import gbl, generate_typehints |
| 58 | + |
| 59 | + typehint = generate_typehints("x") |
| 60 | + assert "x: int\n" == typehint |
| 61 | + typehint = generate_typehints("y") |
| 62 | + assert "y: float\n" == typehint |
| 63 | + typehint = generate_typehints("z") |
| 64 | + assert "z: int\n" == typehint |
| 65 | + typehint = generate_typehints("a") |
| 66 | + assert "a: str\n" == typehint |
| 67 | + typehint = generate_typehints("sa") |
| 68 | + assert "sa: int\n" == typehint |
| 69 | + typehint = generate_typehints("usa") |
| 70 | + assert "usa: int\n" == typehint |
| 71 | + |
| 72 | + with raises(NotImplementedError) as err: |
| 73 | + generate_typehints("MyKlass") |
| 74 | + assert "class" in str(err) |
| 75 | + |
| 76 | + typehint = generate_typehints("callme") |
| 77 | + assert ( |
| 78 | + '@overload\ndef callme (a: "std.string", b: bool, c: "std.vector[int]") -> float: ...\n' |
| 79 | + == typehint |
| 80 | + ) |
| 81 | + |
| 82 | + typehint = generate_typehints("Klass") |
| 83 | + assert "Klass = MyKlass\n" == typehint |
| 84 | + |
| 85 | + typehint = generate_typehints("KlassFloat") |
| 86 | + assert "KlassFloat = \"MyTKlass[float]\"\n" == typehint |
| 87 | + |
| 88 | + with raises(NotImplementedError) as err: |
| 89 | + generate_typehints("MyTKlass") |
| 90 | + |
| 91 | + with raises(NotImplementedError) as err: |
| 92 | + generate_typehints("TypeHints") |
| 93 | + assert "namespace" in str(err) |
| 94 | + |
| 95 | + with raises(TypeError) as err: |
| 96 | + generate_typehints("unknown") |
| 97 | + assert "Unknown Type" in str(err) |
| 98 | + |
| 99 | + with raises(TypeError) as err: |
| 100 | + generate_typehints("TypeHints::x") |
0 commit comments