@@ -823,3 +823,172 @@ test('tokens: strict:false with -- --', () => {
823
823
const { tokens } = parseArgs ( { strict : false , args, tokens : true } ) ;
824
824
assert . deepStrictEqual ( tokens , expectedTokens ) ;
825
825
} ) ;
826
+
827
+ test ( 'default must be a boolean when option type is boolean' , ( ) => {
828
+ const args = [ ] ;
829
+ const options = { alpha : { type : 'boolean' , default : 'not a boolean' } } ;
830
+ assert . throws ( ( ) => {
831
+ parseArgs ( { args, options } ) ;
832
+ } , / " o p t i o n s \. a l p h a \. d e f a u l t " p r o p e r t y m u s t b e o f t y p e b o o l e a n /
833
+ ) ;
834
+ } ) ;
835
+
836
+ test ( 'default must accept undefined value' , ( ) => {
837
+ const args = [ ] ;
838
+ const options = { alpha : { type : 'boolean' , default : undefined } } ;
839
+ const result = parseArgs ( { args, options } ) ;
840
+ const expected = {
841
+ values : {
842
+ __proto__ : null ,
843
+ } ,
844
+ positionals : [ ]
845
+ } ;
846
+ assert . deepStrictEqual ( result , expected ) ;
847
+ } ) ;
848
+
849
+ test ( 'default must be a boolean array when option type is boolean and multiple' , ( ) => {
850
+ const args = [ ] ;
851
+ const options = { alpha : { type : 'boolean' , multiple : true , default : 'not an array' } } ;
852
+ assert . throws ( ( ) => {
853
+ parseArgs ( { args, options } ) ;
854
+ } , / " o p t i o n s \. a l p h a \. d e f a u l t " p r o p e r t y m u s t b e a n i n s t a n c e o f A r r a y /
855
+ ) ;
856
+ } ) ;
857
+
858
+ test ( 'default must be a boolean array when option type is string and multiple is true' , ( ) => {
859
+ const args = [ ] ;
860
+ const options = { alpha : { type : 'boolean' , multiple : true , default : [ true , true , 42 ] } } ;
861
+ assert . throws ( ( ) => {
862
+ parseArgs ( { args, options } ) ;
863
+ } , / " o p t i o n s \. a l p h a \. d e f a u l t \[ 2 \] " p r o p e r t y m u s t b e o f t y p e b o o l e a n /
864
+ ) ;
865
+ } ) ;
866
+
867
+ test ( 'default must be a string when option type is string' , ( ) => {
868
+ const args = [ ] ;
869
+ const options = { alpha : { type : 'string' , default : true } } ;
870
+ assert . throws ( ( ) => {
871
+ parseArgs ( { args, options } ) ;
872
+ } , / " o p t i o n s \. a l p h a \. d e f a u l t " p r o p e r t y m u s t b e o f t y p e s t r i n g /
873
+ ) ;
874
+ } ) ;
875
+
876
+ test ( 'default must be an array when option type is string and multiple is true' , ( ) => {
877
+ const args = [ ] ;
878
+ const options = { alpha : { type : 'string' , multiple : true , default : 'not an array' } } ;
879
+ assert . throws ( ( ) => {
880
+ parseArgs ( { args, options } ) ;
881
+ } , / " o p t i o n s \. a l p h a \. d e f a u l t " p r o p e r t y m u s t b e a n i n s t a n c e o f A r r a y /
882
+ ) ;
883
+ } ) ;
884
+
885
+ test ( 'default must be a string array when option type is string and multiple is true' , ( ) => {
886
+ const args = [ ] ;
887
+ const options = { alpha : { type : 'string' , multiple : true , default : [ 'str' , 42 ] } } ;
888
+ assert . throws ( ( ) => {
889
+ parseArgs ( { args, options } ) ;
890
+ } , / " o p t i o n s \. a l p h a \. d e f a u l t \[ 1 \] " p r o p e r t y m u s t b e o f t y p e s t r i n g /
891
+ ) ;
892
+ } ) ;
893
+
894
+ test ( 'default accepted input when multiple is true' , ( ) => {
895
+ const args = [ '--inputStringArr' , 'c' , '--inputStringArr' , 'd' , '--inputBoolArr' , '--inputBoolArr' ] ;
896
+ const options = {
897
+ inputStringArr : { type : 'string' , multiple : true , default : [ 'a' , 'b' ] } ,
898
+ emptyStringArr : { type : 'string' , multiple : true , default : [ ] } ,
899
+ fullStringArr : { type : 'string' , multiple : true , default : [ 'a' , 'b' ] } ,
900
+ inputBoolArr : { type : 'boolean' , multiple : true , default : [ false , true , false ] } ,
901
+ emptyBoolArr : { type : 'boolean' , multiple : true , default : [ ] } ,
902
+ fullBoolArr : { type : 'boolean' , multiple : true , default : [ false , true , false ] } ,
903
+ } ;
904
+ const expected = { values : { __proto__ : null ,
905
+ inputStringArr : [ 'c' , 'd' ] ,
906
+ inputBoolArr : [ true , true ] ,
907
+ emptyStringArr : [ ] ,
908
+ fullStringArr : [ 'a' , 'b' ] ,
909
+ emptyBoolArr : [ ] ,
910
+ fullBoolArr : [ false , true , false ] } ,
911
+ positionals : [ ] } ;
912
+ const result = parseArgs ( { args, options } ) ;
913
+ assert . deepStrictEqual ( result , expected ) ;
914
+ } ) ;
915
+
916
+ test ( 'when default is set, the option must be added as result' , ( ) => {
917
+ const args = [ ] ;
918
+ const options = {
919
+ a : { type : 'string' , default : 'HELLO' } ,
920
+ b : { type : 'boolean' , default : false } ,
921
+ c : { type : 'boolean' , default : true }
922
+ } ;
923
+ const expected = { values : { __proto__ : null , a : 'HELLO' , b : false , c : true } , positionals : [ ] } ;
924
+
925
+ const result = parseArgs ( { args, options } ) ;
926
+ assert . deepStrictEqual ( result , expected ) ;
927
+ } ) ;
928
+
929
+ test ( 'when default is set, the args value takes precedence' , ( ) => {
930
+ const args = [ '--a' , 'WORLD' , '--b' , '-c' ] ;
931
+ const options = {
932
+ a : { type : 'string' , default : 'HELLO' } ,
933
+ b : { type : 'boolean' , default : false } ,
934
+ c : { type : 'boolean' , default : true }
935
+ } ;
936
+ const expected = { values : { __proto__ : null , a : 'WORLD' , b : true , c : true } , positionals : [ ] } ;
937
+
938
+ const result = parseArgs ( { args, options } ) ;
939
+ assert . deepStrictEqual ( result , expected ) ;
940
+ } ) ;
941
+
942
+ test ( 'tokens should not include the default options' , ( ) => {
943
+ const args = [ ] ;
944
+ const options = {
945
+ a : { type : 'string' , default : 'HELLO' } ,
946
+ b : { type : 'boolean' , default : false } ,
947
+ c : { type : 'boolean' , default : true }
948
+ } ;
949
+
950
+ const expectedTokens = [ ] ;
951
+
952
+ const { tokens } = parseArgs ( { args, options, tokens : true } ) ;
953
+ assert . deepStrictEqual ( tokens , expectedTokens ) ;
954
+ } ) ;
955
+
956
+ test ( 'tokens:true should not include the default options after the args input' , ( ) => {
957
+ const args = [ '--z' , 'zero' , 'positional-item' ] ;
958
+ const options = {
959
+ z : { type : 'string' } ,
960
+ a : { type : 'string' , default : 'HELLO' } ,
961
+ b : { type : 'boolean' , default : false } ,
962
+ c : { type : 'boolean' , default : true }
963
+ } ;
964
+
965
+ const expectedTokens = [
966
+ { kind : 'option' , name : 'z' , rawName : '--z' , index : 0 , value : 'zero' , inlineValue : false } ,
967
+ { kind : 'positional' , index : 2 , value : 'positional-item' } ,
968
+ ] ;
969
+
970
+ const { tokens } = parseArgs ( { args, options, tokens : true , allowPositionals : true } ) ;
971
+ assert . deepStrictEqual ( tokens , expectedTokens ) ;
972
+ } ) ;
973
+
974
+ test ( 'proto as default value must be ignored' , ( ) => {
975
+ const args = [ ] ;
976
+ const options = Object . create ( null ) ;
977
+
978
+ // eslint-disable-next-line no-proto
979
+ options . __proto__ = { type : 'string' , default : 'HELLO' } ;
980
+
981
+ const result = parseArgs ( { args, options, allowPositionals : true } ) ;
982
+ const expected = { values : { __proto__ : null } , positionals : [ ] } ;
983
+ assert . deepStrictEqual ( result , expected ) ;
984
+ } ) ;
985
+
986
+
987
+ test ( 'multiple as false should expect a String' , ( ) => {
988
+ const args = [ ] ;
989
+ const options = { alpha : { type : 'string' , multiple : false , default : [ 'array' ] } } ;
990
+ assert . throws ( ( ) => {
991
+ parseArgs ( { args, options } ) ;
992
+ } , / " o p t i o n s \. a l p h a \. d e f a u l t " p r o p e r t y m u s t b e o f t y p e s t r i n g /
993
+ ) ;
994
+ } ) ;
0 commit comments