authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-03-31 15:39:51+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-03-31 15:39:51+02:00
logcda35093537221abe7c148c57b3a28892046b8b2
tree014791a894f9e46921a51653dbe8c6c4af485c23
parent26e56f2fab7c56829d51db1afb10acb0306a101f

Added test cases to cover all of zigs syntax


1 files changed, 345 insertions(+), 7 deletions(-)

std/zig/parser.zig+345-7
......@@ -1602,13 +1602,6 @@ test "zig fmt: imports" {
16021602 );
16031603}
16041604
1605test "zig fmt: extern function" {
1606 try testCanonical(
1607 \\extern fn puts(s: &const u8) c_int;
1608 \\
1609 );
1610}
1611
16121605test "zig fmt: global declarations" {
16131606 try testCanonical(
16141607 \\const a = b;
......@@ -1758,6 +1751,21 @@ test "zig fmt: call expression" {
17581751 );
17591752}
17601753
1754test "zig fmt: var args" {
1755 try testCanonical(
1756 \\fn print(args: ...) void {}
1757 \\
1758 );
1759}
1760
1761test "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
17611769test "zig fmt: values" {
17621770 try testCanonical(
17631771 \\test "values" {
......@@ -1897,6 +1905,41 @@ test "zig fmt: enum declaration" {
18971905 );
18981906}
18991907
1908test "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
19001943test "zig fmt: container initializers" {
19011944 try testCanonical(
19021945 \\const a1 = []u8{ };
......@@ -1907,6 +1950,301 @@ test "zig fmt: container initializers" {
19071950 );
19081951}
19091952
1953test "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
1989test "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
2047test "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
2084test "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
2133test "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
2153test "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
2164test "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
2203test "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
2214test "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
2227test "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
19102248test "zig fmt: zig fmt" {
19112249 try testCanonical(@embedFile("ast.zig"));
19122250 try testCanonical(@embedFile("index.zig"));