| ... | ... | @@ -1602,13 +1602,6 @@ test "zig fmt: imports" { |
| 1602 | 1602 | ); |
| 1603 | 1603 | } |
| 1604 | 1604 | |
| 1605 | | test "zig fmt: extern function" { |
| 1606 | | try testCanonical( |
| 1607 | | \\extern fn puts(s: &const u8) c_int; |
| 1608 | | \\ |
| 1609 | | ); |
| 1610 | | } |
| 1611 | | |
| 1612 | 1605 | test "zig fmt: global declarations" { |
| 1613 | 1606 | try testCanonical( |
| 1614 | 1607 | \\const a = b; |
| ... | ... | @@ -1758,6 +1751,21 @@ test "zig fmt: call expression" { |
| 1758 | 1751 | ); |
| 1759 | 1752 | } |
| 1760 | 1753 | |
| 1754 | test "zig fmt: var args" { |
| 1755 | try testCanonical( |
| 1756 | \\fn print(args: ...) void {} |
| 1757 | \\ |
| 1758 | ); |
| 1759 | } |
| 1760 | |
| 1761 | test "zig fmt: extern function" { |
| 1762 | try testCanonical( |
| 1763 | \\extern fn puts(s: &const u8) c_int; |
| 1764 | \\extern "c" fn puts(s: &const u8) c_int; |
| 1765 | \\ |
| 1766 | ); |
| 1767 | } |
| 1768 | |
| 1761 | 1769 | test "zig fmt: values" { |
| 1762 | 1770 | try testCanonical( |
| 1763 | 1771 | \\test "values" { |
| ... | ... | @@ -1897,6 +1905,41 @@ test "zig fmt: enum declaration" { |
| 1897 | 1905 | ); |
| 1898 | 1906 | } |
| 1899 | 1907 | |
| 1908 | test "zig fmt: union declaration" { |
| 1909 | try testCanonical( |
| 1910 | \\const U = union { |
| 1911 | \\ Int: u8, |
| 1912 | \\ Float: f32, |
| 1913 | \\ Bool: bool, |
| 1914 | \\}; |
| 1915 | \\ |
| 1916 | \\const Ue = union(enum) { |
| 1917 | \\ Int: u8, |
| 1918 | \\ Float: f32, |
| 1919 | \\ Bool: bool, |
| 1920 | \\}; |
| 1921 | \\ |
| 1922 | \\const E = enum { |
| 1923 | \\ Int, |
| 1924 | \\ Float, |
| 1925 | \\ Bool, |
| 1926 | \\}; |
| 1927 | \\ |
| 1928 | \\const Ue2 = union(E) { |
| 1929 | \\ Int: u8, |
| 1930 | \\ Float: f32, |
| 1931 | \\ Bool: bool, |
| 1932 | \\}; |
| 1933 | \\ |
| 1934 | \\const Eu = extern union { |
| 1935 | \\ Int: u8, |
| 1936 | \\ Float: f32, |
| 1937 | \\ Bool: bool, |
| 1938 | \\}; |
| 1939 | \\ |
| 1940 | ); |
| 1941 | } |
| 1942 | |
| 1900 | 1943 | test "zig fmt: container initializers" { |
| 1901 | 1944 | try testCanonical( |
| 1902 | 1945 | \\const a1 = []u8{ }; |
| ... | ... | @@ -1907,6 +1950,301 @@ test "zig fmt: container initializers" { |
| 1907 | 1950 | ); |
| 1908 | 1951 | } |
| 1909 | 1952 | |
| 1953 | test "zig fmt: switch" { |
| 1954 | try testCanonical( |
| 1955 | \\test "switch" { |
| 1956 | \\ switch (0) { |
| 1957 | \\ 0 => {}, |
| 1958 | \\ 1 => unreachable, |
| 1959 | \\ 2, 3 => {}, |
| 1960 | \\ 4 ... 7 => {}, |
| 1961 | \\ 1 + 4 * 3 + 22 => {}, |
| 1962 | \\ else => { |
| 1963 | \\ const a = 1; |
| 1964 | \\ const b = a; |
| 1965 | \\ }, |
| 1966 | \\ } |
| 1967 | \\ |
| 1968 | \\ const res = switch (0) { |
| 1969 | \\ 0 => 0, |
| 1970 | \\ 1 => 2, |
| 1971 | \\ else => 4, |
| 1972 | \\ }; |
| 1973 | \\ |
| 1974 | \\ const Union = union(enum) { |
| 1975 | \\ Int: i64, |
| 1976 | \\ Float: f64, |
| 1977 | \\ }; |
| 1978 | \\ |
| 1979 | \\ const u = Union { .Int = 0 }; |
| 1980 | \\ switch (u) { |
| 1981 | \\ Union.Int => |int| {}, |
| 1982 | \\ Union.Float => |*float| unreachable, |
| 1983 | \\ } |
| 1984 | \\} |
| 1985 | \\ |
| 1986 | ); |
| 1987 | } |
| 1988 | |
| 1989 | test "zig fmt: while" { |
| 1990 | try testCanonical( |
| 1991 | \\test "while" { |
| 1992 | \\ while (10 < 1) { |
| 1993 | \\ unreachable; |
| 1994 | \\ } |
| 1995 | \\ |
| 1996 | \\ while (10 < 1) |
| 1997 | \\ unreachable; |
| 1998 | \\ |
| 1999 | \\ var i: usize = 0; |
| 2000 | \\ while (i < 10) : (i += 1) { |
| 2001 | \\ continue; |
| 2002 | \\ } |
| 2003 | \\ |
| 2004 | \\ i = 0; |
| 2005 | \\ while (i < 10) : (i += 1) |
| 2006 | \\ continue; |
| 2007 | \\ |
| 2008 | \\ i = 0; |
| 2009 | \\ var j usize = 0; |
| 2010 | \\ while (i < 10) : ({ i += 1; j += 1; }) { |
| 2011 | \\ continue; |
| 2012 | \\ } |
| 2013 | \\ |
| 2014 | \\ var a: ?u8 = 2; |
| 2015 | \\ while (a) |v| : (a = null) { |
| 2016 | \\ continue; |
| 2017 | \\ } |
| 2018 | \\ |
| 2019 | \\ while (a) |v| : (a = null) |
| 2020 | \\ unreachable; |
| 2021 | \\ |
| 2022 | \\ label: while (10 < 0) { |
| 2023 | \\ unreachable; |
| 2024 | \\ } |
| 2025 | \\ |
| 2026 | \\ const res = while (0 < 10) { |
| 2027 | \\ break 7; |
| 2028 | \\ } else { |
| 2029 | \\ unreachable; |
| 2030 | \\ } |
| 2031 | \\ |
| 2032 | \\ var a: error!u8 = 0; |
| 2033 | \\ while (a) |v| { |
| 2034 | \\ a = error.Err; |
| 2035 | \\ } else |err| { |
| 2036 | \\ i = 1; |
| 2037 | \\ } |
| 2038 | \\ |
| 2039 | \\ comptime var k: usize = 0; |
| 2040 | \\ inline while (i < 10) (i += 1) |
| 2041 | \\ j += 2; |
| 2042 | \\} |
| 2043 | \\ |
| 2044 | ); |
| 2045 | } |
| 2046 | |
| 2047 | test "zig fmt: for" { |
| 2048 | try testCanonical( |
| 2049 | \\test "for" { |
| 2050 | \\ const a = []u8{ 1, 2, 3 }; |
| 2051 | \\ for (a) |v| { |
| 2052 | \\ continue; |
| 2053 | \\ } |
| 2054 | \\ |
| 2055 | \\ for (a) |v| |
| 2056 | \\ continue; |
| 2057 | \\ |
| 2058 | \\ for (a) |*v| |
| 2059 | \\ continue; |
| 2060 | \\ |
| 2061 | \\ for (a) |v, i| { |
| 2062 | \\ continue; |
| 2063 | \\ } |
| 2064 | \\ |
| 2065 | \\ for (a) |v, i| |
| 2066 | \\ continue; |
| 2067 | \\ |
| 2068 | \\ const res = for (a) |v, i| { |
| 2069 | \\ breal v; |
| 2070 | \\ } else { |
| 2071 | \\ unreachable; |
| 2072 | \\ } |
| 2073 | \\ |
| 2074 | \\ var num: usize = 0; |
| 2075 | \\ inline for (a) |v, i| { |
| 2076 | \\ num += v; |
| 2077 | \\ num += i; |
| 2078 | \\ } |
| 2079 | \\} |
| 2080 | \\ |
| 2081 | ); |
| 2082 | } |
| 2083 | |
| 2084 | test "zig fmt: if" { |
| 2085 | try testCanonical( |
| 2086 | \\test "if" { |
| 2087 | \\ if (10 < 0) { |
| 2088 | \\ unreachable; |
| 2089 | \\ } |
| 2090 | \\ |
| 2091 | \\ if (10 < 0) |
| 2092 | \\ unreachable; |
| 2093 | \\ |
| 2094 | \\ if (10 < 0) { |
| 2095 | \\ unreachable; |
| 2096 | \\ } else { |
| 2097 | \\ const a = 20; |
| 2098 | \\ } |
| 2099 | \\ |
| 2100 | \\ if (10 < 0) { |
| 2101 | \\ unreachable; |
| 2102 | \\ } else if (5 < 0) { |
| 2103 | \\ unreachable; |
| 2104 | \\ } else { |
| 2105 | \\ const a = 20; |
| 2106 | \\ } |
| 2107 | \\ |
| 2108 | \\ const is_world_broken = if (10 < 0) true else false; |
| 2109 | \\ |
| 2110 | \\ const a: ?u8 = 10; |
| 2111 | \\ const b: ?u8 = null; |
| 2112 | \\ if (a) |v| { |
| 2113 | \\ const some = v; |
| 2114 | \\ } else if (b) |*v| { |
| 2115 | \\ unreachable; |
| 2116 | \\ } else { |
| 2117 | \\ const some = 10; |
| 2118 | \\ } |
| 2119 | \\ |
| 2120 | \\ const non_null_a = if (a) |v| v else 0; |
| 2121 | \\ |
| 2122 | \\ const a_err: error!u8 = 0; |
| 2123 | \\ if (a_err) |v| { |
| 2124 | \\ const p = v; |
| 2125 | \\ } else |err| { |
| 2126 | \\ unreachable; |
| 2127 | \\ } |
| 2128 | \\} |
| 2129 | \\ |
| 2130 | ); |
| 2131 | } |
| 2132 | |
| 2133 | test "zig fmt: defer" { |
| 2134 | try testCanonical( |
| 2135 | \\test "defer" { |
| 2136 | \\ var i: usize = 0; |
| 2137 | \\ defer i = 1; |
| 2138 | \\ defer { |
| 2139 | \\ i += 2; |
| 2140 | \\ i *= i; |
| 2141 | \\ } |
| 2142 | \\ |
| 2143 | \\ errdefer i += 3; |
| 2144 | \\ errdefer { |
| 2145 | \\ i += 2; |
| 2146 | \\ i /= i; |
| 2147 | \\ } |
| 2148 | \\} |
| 2149 | \\ |
| 2150 | ); |
| 2151 | } |
| 2152 | |
| 2153 | test "zig fmt: catch" { |
| 2154 | try testCanonical( |
| 2155 | \\test "catch" { |
| 2156 | \\ const a: error!u8 = 0; |
| 2157 | \\ _ = a catch return; |
| 2158 | \\ _ = a catch |err| return; |
| 2159 | \\} |
| 2160 | \\ |
| 2161 | ); |
| 2162 | } |
| 2163 | |
| 2164 | test "zig fmt: comptime" { |
| 2165 | try testCanonical( |
| 2166 | \\fn a() u8 { |
| 2167 | \\ return 5; |
| 2168 | \\} |
| 2169 | \\ |
| 2170 | \\fn b(comptime i: u8) u8 { |
| 2171 | \\ return i; |
| 2172 | \\} |
| 2173 | \\ |
| 2174 | \\const av = comptime a(); |
| 2175 | \\const av2 = comptime blk: { |
| 2176 | \\ var res = a(); |
| 2177 | \\ res *= b(2); |
| 2178 | \\ break :blk res; |
| 2179 | \\}; |
| 2180 | \\ |
| 2181 | \\comptime { |
| 2182 | \\ _ = a(); |
| 2183 | \\} |
| 2184 | \\ |
| 2185 | \\test "comptime" { |
| 2186 | \\ const av3 = comptime a(); |
| 2187 | \\ const av4 = comptime blk: { |
| 2188 | \\ var res = a(); |
| 2189 | \\ res *= a(); |
| 2190 | \\ break :blk res; |
| 2191 | \\ }; |
| 2192 | \\ |
| 2193 | \\ comptime var i = 0; |
| 2194 | \\ comptime { |
| 2195 | \\ i = a(); |
| 2196 | \\ i += b(i); |
| 2197 | \\ } |
| 2198 | \\} |
| 2199 | \\ |
| 2200 | ); |
| 2201 | } |
| 2202 | |
| 2203 | test "zig fmt: fn type" { |
| 2204 | try testCanonical( |
| 2205 | \\fn a(i: u8) u8 { |
| 2206 | \\ return i + 1; |
| 2207 | \\} |
| 2208 | \\ |
| 2209 | \\const ap: fn(u8) u8 = a; |
| 2210 | \\ |
| 2211 | ); |
| 2212 | } |
| 2213 | |
| 2214 | test "zig fmt: inline asm" { |
| 2215 | try testCanonical( |
| 2216 | \\pub fn syscall1(number: usize, arg1: usize) usize { |
| 2217 | \\ return asm volatile ("syscall" |
| 2218 | \\ : [ret] "={rax}" (-> usize) |
| 2219 | \\ : [number] "{rax}" (number), |
| 2220 | \\ [arg1] "{rdi}" (arg1) |
| 2221 | \\ : "rcx", "r11"); |
| 2222 | \\} |
| 2223 | \\ |
| 2224 | ); |
| 2225 | } |
| 2226 | |
| 2227 | test "zig fmt: coroutines" { |
| 2228 | try testCanonical( |
| 2229 | \\async fn simpleAsyncFn() void { |
| 2230 | \\ x += 1; |
| 2231 | \\ suspend; |
| 2232 | \\ x += 1; |
| 2233 | \\ suspend |p| { |
| 2234 | \\ } |
| 2235 | \\ const p = async simpleAsyncFn() cache unreachable; |
| 2236 | \\ await p; |
| 2237 | \\} |
| 2238 | \\ |
| 2239 | \\test "coroutine suspend, resume, cancel" { |
| 2240 | \\ const p = try async<std.debug.global_allocator> testAsyncSeq(); |
| 2241 | \\ resume p; |
| 2242 | \\ cancel p; |
| 2243 | \\} |
| 2244 | \\ |
| 2245 | ); |
| 2246 | } |
| 2247 | |
| 1910 | 2248 | test "zig fmt: zig fmt" { |
| 1911 | 2249 | try testCanonical(@embedFile("ast.zig")); |
| 1912 | 2250 | try testCanonical(@embedFile("index.zig")); |