1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-04 17:18:23 -05:00
denoland-deno/flags/tests/bool.ts

159 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-01-01 22:46:17 -05:00
import { test, assertEqual } from "../../testing/mod.ts";
import { parse } from "../index.ts";
2018-12-19 13:06:31 -05:00
test(function flagBooleanDefaultFalse() {
2018-12-24 10:28:01 -05:00
const argv = parse(["moo"], {
boolean: ["t", "verbose"],
default: { verbose: false, t: false }
});
assertEqual(argv, {
verbose: false,
t: false,
_: ["moo"]
});
assertEqual(typeof argv.verbose, "boolean");
assertEqual(typeof argv.t, "boolean");
2018-12-19 13:06:31 -05:00
});
test(function booleanGroups() {
2018-12-24 10:28:01 -05:00
const argv = parse(["-x", "-z", "one", "two", "three"], {
boolean: ["x", "y", "z"]
});
assertEqual(argv, {
x: true,
y: false,
z: true,
_: ["one", "two", "three"]
});
assertEqual(typeof argv.x, "boolean");
assertEqual(typeof argv.y, "boolean");
assertEqual(typeof argv.z, "boolean");
2018-12-19 13:06:31 -05:00
});
test(function booleanAndAliasWithChainableApi() {
2018-12-24 10:28:01 -05:00
const aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const opts = {
herp: { alias: "h", boolean: true }
};
const aliasedArgv = parse(aliased, {
boolean: "herp",
alias: { h: "herp" }
});
const propertyArgv = parse(regular, {
boolean: "herp",
alias: { h: "herp" }
});
const expected = {
herp: true,
h: true,
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
2018-12-19 13:06:31 -05:00
});
test(function booleanAndAliasWithOptionsHash() {
2018-12-24 10:28:01 -05:00
const aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const opts = {
alias: { h: "herp" },
boolean: "herp"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const expected = {
herp: true,
h: true,
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
2018-12-19 13:06:31 -05:00
});
test(function booleanAndAliasArrayWithOptionsHash() {
2018-12-24 10:28:01 -05:00
const aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const alt = ["--harp", "derp"];
const opts = {
alias: { h: ["herp", "harp"] },
boolean: "h"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const altPropertyArgv = parse(alt, opts);
const expected = {
harp: true,
herp: true,
h: true,
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
assertEqual(altPropertyArgv, expected);
2018-12-19 13:06:31 -05:00
});
test(function booleanAndAliasUsingExplicitTrue() {
2018-12-24 10:28:01 -05:00
const aliased = ["-h", "true"];
const regular = ["--herp", "true"];
const opts = {
alias: { h: "herp" },
boolean: "h"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const expected = {
herp: true,
h: true,
_: []
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
2018-12-19 13:06:31 -05:00
});
// regression, see https://github.com/substack/node-optimist/issues/71
// boolean and --x=true
test(function booleanAndNonBoolean() {
2018-12-24 10:28:01 -05:00
const parsed = parse(["--boool", "--other=true"], {
boolean: "boool"
});
assertEqual(parsed.boool, true);
assertEqual(parsed.other, "true");
const parsed2 = parse(["--boool", "--other=false"], {
boolean: "boool"
});
assertEqual(parsed2.boool, true);
assertEqual(parsed2.other, "false");
2018-12-19 13:06:31 -05:00
});
test(function booleanParsingTrue() {
2018-12-24 10:28:01 -05:00
const parsed = parse(["--boool=true"], {
default: {
boool: false
},
boolean: ["boool"]
});
assertEqual(parsed.boool, true);
2018-12-19 13:06:31 -05:00
});
test(function booleanParsingFalse() {
2018-12-24 10:28:01 -05:00
const parsed = parse(["--boool=false"], {
default: {
boool: true
},
boolean: ["boool"]
});
assertEqual(parsed.boool, false);
2018-12-19 13:06:31 -05:00
});