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.
This commit is contained in:
Daniele Cocca 2021-04-28 01:26:03 +01:00 committed by Daniele Cocca
parent 9be2f76741
commit 9e88356282
3 changed files with 13 additions and 1 deletions

View File

@ -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

View File

@ -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]);
}

View File

@ -0,0 +1 @@
hello zig