#test command line inputs

1 messages · Page 1 of 1 (latest)

sturdy nymph
#

If I do a @TypeOf on command line inputs I see that they are of the type [][:0]u8.
In a test, how would I create a case for each of the possibilities?
Something like - const arg = [_][]u8{"--thing"}; - just falls over. Getting this syntax right is fiddly isn't it!?

indigo acorn
#

or if you really want it to be mutable, var args = [_][:0]u8{try std.testing.allocator.dupe(u8, "--thing")} and then pass &args

#

chances are you're not actually using the mutability tho, so the first option should be fine :)

sturdy nymph
#

I'm getting "error: expected type '[][:0]u8', found '[]const [:0]const u8'" - even trickier!

indigo acorn
#

you'll need to change the argument type of the function you're passing it to

sturdy nymph
#

The function I'm testing has a signature like this: error: expected type '[][:0]u8', found '[]const [:0]const u8'"

#

ok, change the signature, ok - i got it from doing @midnight moth

#

pub fn doArgs(args: []const [:0]const u8) !u8 {

#

this is the new signature of my little function

#

here's the test:

#

const args: []const [:0]const u8 = &.{"--help"};
try expect(doArgs(args) == 4);

#

but it gives this:

#

error: operator == not allowed for type

#

I checked that the function is returning a u8

indigo acorn
#

try doArgs

#

also use std.testing.expectEqual instead of expect, it'll print a more useful message :)

sturdy nymph
#

ok

#

mm, this is bothersome - when I debug print a zig build run I see that my state returned from doArgs is a 4 but when run from a test it gives back a 0. There's some difference when entering the function from the testing line 'const args..etc..' At least it's a bit further ahead now.

#

oh, maybe i need to add the default prog name as the first arg

indigo acorn
sturdy nymph
#

ah yes that was the badger

#

thanks for your time 🙂