types.ha (2733B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 @test fn struct_union() void = { 5 roundtrip("export type foo = struct { 6 @offset(void) x: int, 7 y: int, 8 }; 9 10 export type bar = union { 11 x: int, 12 // docs docs docs 13 y: int, 14 }; 15 16 export type baz = struct { 17 embedded, 18 struct { 19 x: int, 20 y: int, 21 }, 22 }; 23 "); 24 roundtrip_reparse("export type foo = struct { x: int, y: int };\n" 25 "export type bar = union { x: int, y: int };\n" 26 "export type baz = struct { embedded, struct { x: int, y: int } };\n"); 27 }; 28 29 @test fn array_slice() void = { 30 roundtrip("export type foo = []int; 31 32 export type bar = [*]int; 33 34 export type baz = [_]int; 35 36 export type bat = [void]int; 37 "); 38 }; 39 40 @test fn enum_type() void = { 41 roundtrip("export type foo = enum { 42 X = void, 43 // foo 44 // bar 45 Y = void, 46 Z, // foo 47 Q, // bar 48 }; 49 50 export type bar = enum uint { 51 X = void, 52 Y = void, 53 Z, 54 Q, 55 }; 56 57 export type baz = enum rune { 58 X = void, 59 Y = void, 60 Z, 61 Q, 62 }; 63 "); 64 roundtrip_reparse("export type foo = enum { X, Y, Z };\n"); 65 }; 66 67 @test fn tuple() void = { 68 roundtrip("export type foo = (int, str); 69 70 export type bar = (a, b::c, d); 71 72 export type baz = (bat, foo::bar::baz, long_type_name, yet_another_very_long_type_name, 73 this_spans_multiple_lines, for_readability, never_gonna_give_you_up, 74 never_gonna_let_you_down); 75 "); 76 roundtrip_reparse("export type foo = (int, str,);\n"); 77 }; 78 79 @test fn tagged_union() void = { 80 roundtrip("export type foo = (size | void); 81 82 export type bar = (a | b::c | ...d); 83 84 export type baz = (bat | foo::bar::baz | long_type_name | yet_another_very_long_type_name | 85 this_spans_multiple_lines | for_readability | never_gonna_give_you_up | 86 never_gonna_let_you_down); 87 "); 88 }; 89 90 @test fn enum_comments() void = { 91 roundtrip("type foo = enum { 92 A, // comment 93 B, 94 C, 95 }; 96 "); 97 }; 98 99 @test fn func() void = { 100 roundtrip("export type foo = fn(int) void; 101 102 export type foo = fn(int...) void; 103 104 export type foo = fn(int, ...) void; 105 106 export type foo = fn( 107 long_param_name: long_type_name, 108 another_one: blablablabla, 109 this_spans: multiple_lines, 110 for_readability: and_stuff, 111 ) void; 112 113 export type foo = fn( 114 long_param_name: long_type_name, 115 another_one: blablablabla, 116 this_spans: multiple_lines, 117 for_readability: and_stuff... 118 ) void; 119 120 export type foo = fn( 121 long_param_name: long_type_name, 122 another_one: blablablabla, 123 this_spans: multiple_lines, 124 for_readability: and_stuff, 125 ... 126 ) void; 127 128 export type foo = fn( 129 long_type_name, 130 blablablabla, 131 multiple_lines, 132 and_stuff = 4, 133 ) void; 134 135 export type foo = fn( 136 long_type_name, 137 blablablabla, 138 multiple_lines, 139 and_stuff... 140 ) void; 141 142 export type foo = fn( 143 long_type_name, 144 blablablabla, 145 multiple_lines, 146 and_stuff, 147 ... 148 ) void; 149 "); 150 151 roundtrip_reparse("type foo = fn(int,) void;"); 152 };