Commit 9e88356282

Daniele Cocca <daniele.cocca@gmail.com>
2021-04-28 02:26:03
embedFile: change notation from [X:0] to [N:0]
This is for consistency with the documentation on sentinel-terminated {arrays,slices,pointers} which already use `N` for a comptime-inferred size rather than `X`. Also adds a behavioral test to assert that a string literal is returned.
1 parent 9be2f76
Changed files (3)
doc/langref.html.in
@@ -7447,7 +7447,7 @@ test "main" {
       {#see_also|@divFloor|@divExact#}
       {#header_close#}
       {#header_open|@embedFile#}
-      <pre>{#syntax#}@embedFile(comptime path: []const u8) *const [X:0]u8{#endsyntax#}</pre>
+      <pre>{#syntax#}@embedFile(comptime path: []const u8) *const [N:0]u8{#endsyntax#}</pre>
       <p>
       This function returns a compile time constant pointer to null-terminated,
       fixed-size array with length equal to the byte count of the file given by
test/behavior/bugs/3779.zig
@@ -29,3 +29,14 @@ test "@typeName() returns a string literal" {
     try std.testing.expectEqualStrings("TestType", type_name);
     try std.testing.expectEqualStrings("TestType", ptr_type_name[0..type_name.len]);
 }
+
+const actual_contents = @embedFile("3779_file_to_embed.txt");
+const ptr_actual_contents: [*:0]const u8 = actual_contents;
+const expected_contents = "hello zig\n";
+
+test "@embedFile() returns a string literal" {
+    try std.testing.expectEqual(*const [expected_contents.len:0]u8, @TypeOf(actual_contents));
+    try std.testing.expect(std.mem.eql(u8, expected_contents, actual_contents));
+    try std.testing.expectEqualStrings(expected_contents, actual_contents);
+    try std.testing.expectEqualStrings(expected_contents, ptr_actual_contents[0..actual_contents.len]);
+}
test/behavior/bugs/3779_file_to_embed.txt
@@ -0,0 +1,1 @@
+hello zig