Skip to content

Commit

Permalink
use assert.deepStrictEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Jan 10, 2024
1 parent 54ecc17 commit f15d1f9
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 117 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
},
"devDependencies": {
"sanctuary-maybe": "2.1.x",
"sanctuary-scripts": "7.0.x",
"sanctuary-show": "3.0.x",
"sanctuary-type-classes": "13.0.x"
"sanctuary-scripts": "7.0.x"
},
"scripts": {
"doctest": "sanctuary-doctest",
Expand Down
220 changes: 106 additions & 114 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import assert from 'assert';
import {deepStrictEqual as eq} from 'assert';

import test from 'oletus';
import Maybe from 'sanctuary-maybe';
import show from 'sanctuary-show';
import Z from 'sanctuary-type-classes';

import {Flag, Option, Left, Right, Pair, parseArgs} from 'sanctuary-argv';


const {Nothing, Just} = Maybe;

// eq :: a -> a -> Undefined !
const eq = actual => expected => {
assert.strictEqual (show (actual), show (expected));
assert.strictEqual (Z.equals (actual, expected), true);
};

// type Options = { color :: Boolean
// , email :: Maybe String
// , words :: Array String }
Expand All @@ -34,7 +26,7 @@ const color = Flag (options => ({...options, color: true}));
const email = Option (email =>
email.includes ('@')
? Right (options => ({...options, email: Just (email)}))
: Left (`${show (email)} is not a valid email address`)
: Left (`${JSON.stringify (email)} is not a valid email address`)
);

// words :: Handler Options
Expand All @@ -53,217 +45,217 @@ const updaters = {
const parse = args => parseArgs (updaters) (Pair (defaultOptions) (args));

test ('[success] no arguments', () => {
eq (parse ([]))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse ([]),
Right (Pair ({color: false, email: Nothing, words: []})
([])));
});

test ('[success] long options', () => {
eq (parse (['--color']))
(Right (Pair ({color: true, email: Nothing, words: []})
eq (parse (['--color']),
Right (Pair ({color: true, email: Nothing, words: []})
([])));

eq (parse (['--colour']))
(Right (Pair ({color: true, email: Nothing, words: []})
eq (parse (['--colour']),
Right (Pair ({color: true, email: Nothing, words: []})
([])));

eq (parse (['--email', '[email protected]']))
(Right (Pair ({color: false, email: Just ('[email protected]'), words: []})
eq (parse (['--email', '[email protected]']),
Right (Pair ({color: false, email: Just ('[email protected]'), words: []})
([])));

eq (parse (['--email', '[email protected]', '--email', '[email protected]']))
(Right (Pair ({color: false, email: Just ('[email protected]'), words: []})
eq (parse (['--email', '[email protected]', '--email', '[email protected]']),
Right (Pair ({color: false, email: Just ('[email protected]'), words: []})
([])));

eq (parse (['--word', 'foo']))
(Right (Pair ({color: false, email: Nothing, words: ['foo']})
eq (parse (['--word', 'foo']),
Right (Pair ({color: false, email: Nothing, words: ['foo']})
([])));

eq (parse (['--word', 'foo', '--word', 'bar']))
(Right (Pair ({color: false, email: Nothing, words: ['foo', 'bar']})
eq (parse (['--word', 'foo', '--word', 'bar']),
Right (Pair ({color: false, email: Nothing, words: ['foo', 'bar']})
([])));

eq (parse (['--word', 'foo', '--word', 'bar', '--word', 'baz']))
(Right (Pair ({color: false, email: Nothing, words: ['foo', 'bar', 'baz']})
eq (parse (['--word', 'foo', '--word', 'bar', '--word', 'baz']),
Right (Pair ({color: false, email: Nothing, words: ['foo', 'bar', 'baz']})
([])));

eq (parse (['--word', 'foo', '--color', '--word', 'bar', '--email', '[email protected]', '--word', 'baz']))
(Right (Pair ({color: true, email: Just ('[email protected]'), words: ['foo', 'bar', 'baz']})
eq (parse (['--word', 'foo', '--color', '--word', 'bar', '--email', '[email protected]', '--word', 'baz']),
Right (Pair ({color: true, email: Just ('[email protected]'), words: ['foo', 'bar', 'baz']})
([])));

eq (parse (['--word', '--']))
(Right (Pair ({color: false, email: Nothing, words: ['--']})
eq (parse (['--word', '--']),
Right (Pair ({color: false, email: Nothing, words: ['--']})
([])));

eq (parse (['--word', '--color']))
(Right (Pair ({color: false, email: Nothing, words: ['--color']})
eq (parse (['--word', '--color']),
Right (Pair ({color: false, email: Nothing, words: ['--color']})
([])));
});

test ('[success] short options (separate)', () => {
eq (parse (['-c', '-e', '[email protected]', '-w', 'foo', '-w', 'bar', '-w', 'baz']))
(Right (Pair ({color: true, email: Just ('[email protected]'), words: ['foo', 'bar', 'baz']})
eq (parse (['-c', '-e', '[email protected]', '-w', 'foo', '-w', 'bar', '-w', 'baz']),
Right (Pair ({color: true, email: Just ('[email protected]'), words: ['foo', 'bar', 'baz']})
([])));

eq (parse (['-e', '[email protected]', '--email', '[email protected]']))
(Right (Pair ({color: false, email: Just ('[email protected]'), words: []})
eq (parse (['-e', '[email protected]', '--email', '[email protected]']),
Right (Pair ({color: false, email: Just ('[email protected]'), words: []})
([])));

eq (parse (['--email', '[email protected]', '-e', '[email protected]']))
(Right (Pair ({color: false, email: Just ('[email protected]'), words: []})
eq (parse (['--email', '[email protected]', '-e', '[email protected]']),
Right (Pair ({color: false, email: Just ('[email protected]'), words: []})
([])));

eq (parse (['-w', 'short', '--word', 'long', '-w', 'short', '--word', 'long']))
(Right (Pair ({color: false, email: Nothing, words: ['short', 'long', 'short', 'long']})
eq (parse (['-w', 'short', '--word', 'long', '-w', 'short', '--word', 'long']),
Right (Pair ({color: false, email: Nothing, words: ['short', 'long', 'short', 'long']})
([])));

eq (parse (['-w', '--']))
(Right (Pair ({color: false, email: Nothing, words: ['--']})
eq (parse (['-w', '--']),
Right (Pair ({color: false, email: Nothing, words: ['--']})
([])));

eq (parse (['-w', '-c']))
(Right (Pair ({color: false, email: Nothing, words: ['-c']})
eq (parse (['-w', '-c']),
Right (Pair ({color: false, email: Nothing, words: ['-c']})
([])));
});

test ('[success] short options (grouped)', () => {
eq (parse (['-ce', '[email protected]']))
(Right (Pair ({color: true, email: Just ('[email protected]'), words: []})
eq (parse (['-ce', '[email protected]']),
Right (Pair ({color: true, email: Just ('[email protected]'), words: []})
([])));
});

test ('[success] positional arguments', () => {
eq (parse (['foo']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['foo']),
Right (Pair ({color: false, email: Nothing, words: []})
(['foo'])));

eq (parse (['foo', 'bar']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['foo', 'bar']),
Right (Pair ({color: false, email: Nothing, words: []})
(['foo', 'bar'])));

eq (parse (['foo', 'bar', 'baz']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['foo', 'bar', 'baz']),
Right (Pair ({color: false, email: Nothing, words: []})
(['foo', 'bar', 'baz'])));

eq (parse (['foo', 'bar', 'baz', '-']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['foo', 'bar', 'baz', '-']),
Right (Pair ({color: false, email: Nothing, words: []})
(['foo', 'bar', 'baz', '-'])));

eq (parse (['foo', 'bar', 'baz', '--']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['foo', 'bar', 'baz', '--']),
Right (Pair ({color: false, email: Nothing, words: []})
(['foo', 'bar', 'baz', '--'])));

eq (parse (['--']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--']),
Right (Pair ({color: false, email: Nothing, words: []})
([])));

eq (parse (['--', 'foo']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--', 'foo']),
Right (Pair ({color: false, email: Nothing, words: []})
(['foo'])));

eq (parse (['--', 'foo', 'bar']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--', 'foo', 'bar']),
Right (Pair ({color: false, email: Nothing, words: []})
(['foo', 'bar'])));

eq (parse (['--', 'foo', 'bar', 'baz']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--', 'foo', 'bar', 'baz']),
Right (Pair ({color: false, email: Nothing, words: []})
(['foo', 'bar', 'baz'])));

eq (parse (['--', '-', '--', '---']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--', '-', '--', '---']),
Right (Pair ({color: false, email: Nothing, words: []})
(['-', '--', '---'])));

eq (parse (['--', '--color', '--email', '[email protected]', '--word', 'foo']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--', '--color', '--email', '[email protected]', '--word', 'foo']),
Right (Pair ({color: false, email: Nothing, words: []})
(['--color', '--email', '[email protected]', '--word', 'foo'])));

eq (parse (['--', '--foo', '--bar', '--baz']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--', '--foo', '--bar', '--baz']),
Right (Pair ({color: false, email: Nothing, words: []})
(['--foo', '--bar', '--baz'])));

eq (parse (['--', '-x', '-y', '-z']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--', '-x', '-y', '-z']),
Right (Pair ({color: false, email: Nothing, words: []})
(['-x', '-y', '-z'])));

eq (parse (['--', '-']))
(Right (Pair ({color: false, email: Nothing, words: []})
eq (parse (['--', '-']),
Right (Pair ({color: false, email: Nothing, words: []})
(['-'])));
});

test ('[failure] unrecognized long options', () => {
eq (parse (['--foo']))
(Left ('--foo is unrecognized'));
eq (parse (['--foo']),
Left ('--foo is unrecognized'));

eq (parse (['--foo', '--bar']))
(Left ('--foo is unrecognized'));
eq (parse (['--foo', '--bar']),
Left ('--foo is unrecognized'));

eq (parse (['--color', '--foo']))
(Left ('--foo is unrecognized'));
eq (parse (['--color', '--foo']),
Left ('--foo is unrecognized'));

eq (parse (['--bar', '--color']))
(Left ('--bar is unrecognized'));
eq (parse (['--bar', '--color']),
Left ('--bar is unrecognized'));
});

test ('[failure] lonely dash', () => {
eq (parse (['-']))
(Left ('- is not a valid flag or option name'));
eq (parse (['-']),
Left ('- is not a valid flag or option name'));
});

test ('[failure] unrecognized short options (separate)', () => {
eq (parse (['-d', '-c']))
(Left ('-d is unrecognized'));
eq (parse (['-d', '-c']),
Left ('-d is unrecognized'));

eq (parse (['-c', '-d']))
(Left ('-d is unrecognized'));
eq (parse (['-c', '-d']),
Left ('-d is unrecognized'));

eq (parse (['-X']))
(Left ('-X is unrecognized'));
eq (parse (['-X']),
Left ('-X is unrecognized'));

eq (parse (['-0']))
(Left ('-0 is unrecognized'));
eq (parse (['-0']),
Left ('-0 is unrecognized'));
});

test ('[failure] unrecognized short options (grouped)', () => {
eq (parse (['-dc']))
(Left ('-d is unrecognized'));
eq (parse (['-dc']),
Left ('-d is unrecognized'));

eq (parse (['-cd']))
(Left ('-d is unrecognized'));
eq (parse (['-cd']),
Left ('-d is unrecognized'));
});

test ('[failure] unspecified long options', () => {
eq (parse (['--email']))
(Left ('--email requires a value'));
eq (parse (['--email']),
Left ('--email requires a value'));

eq (parse (['--word']))
(Left ('--word requires a value'));
eq (parse (['--word']),
Left ('--word requires a value'));
});

test ('[failure] unspecified short options (separate)', () => {
eq (parse (['-c', '-e']))
(Left ('-e requires a value'));
eq (parse (['-c', '-e']),
Left ('-e requires a value'));
});

test ('[failure] unspecified short options (grouped)', () => {
eq (parse (['-ce']))
(Left ('-e requires a value'));
eq (parse (['-ce']),
Left ('-e requires a value'));

eq (parse (['-ec']))
(Left ('-e requires a value'));
eq (parse (['-ec']),
Left ('-e requires a value'));
});

test ('[failure] invalid long option values', () => {
eq (parse (['--email', '--color']))
(Left ('"--color" is not a valid email address'));
eq (parse (['--email', '--color']),
Left ('"--color" is not a valid email address'));

eq (parse (['--email', '--color', '--xxx']))
(Left ('"--color" is not a valid email address'));
eq (parse (['--email', '--color', '--xxx']),
Left ('"--color" is not a valid email address'));
});

test ('[failure] invalid short option values', () => {
eq (parse (['-e', '-c']))
(Left ('"-c" is not a valid email address'));
eq (parse (['-e', '-c']),
Left ('"-c" is not a valid email address'));

eq (parse (['-e', '-c', '-x']))
(Left ('"-c" is not a valid email address'));
eq (parse (['-e', '-c', '-x']),
Left ('"-c" is not a valid email address'));
});

0 comments on commit f15d1f9

Please sign in to comment.