@@ -958,6 +958,86 @@ describe('$substitute directive', () => {
958
958
await expect ( source . read ( [ environmentVariableSubstitution ( ) ] ) ) . rejects . toThrow ( ) ;
959
959
} ) ;
960
960
961
+ it ( 'parses ints' , async ( ) => {
962
+ process . env . FOO = '11' ;
963
+
964
+ const source = new LiteralSource ( {
965
+ $substitute : { $name : 'FOO' , $parseInt : true } ,
966
+ } ) ;
967
+
968
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( 11 ) ;
969
+ } ) ;
970
+
971
+ it ( 'fails when int is invalid' , async ( ) => {
972
+ process . env . FOO = 'not a number' ;
973
+
974
+ const source = new LiteralSource ( {
975
+ $substitute : { $name : 'FOO' , $parseInt : true } ,
976
+ } ) ;
977
+
978
+ await expect ( source . read ( [ environmentVariableSubstitution ( ) ] ) ) . rejects . toThrow ( ) ;
979
+ } ) ;
980
+
981
+ it ( 'parses float' , async ( ) => {
982
+ process . env . FOO = '11.2' ;
983
+
984
+ const source = new LiteralSource ( {
985
+ $substitute : { $name : 'FOO' , $parseFloat : true } ,
986
+ } ) ;
987
+
988
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( 11.2 ) ;
989
+ } ) ;
990
+
991
+ it ( 'fails when float is invalid' , async ( ) => {
992
+ process . env . FOO = 'not a number' ;
993
+
994
+ const source = new LiteralSource ( {
995
+ $substitute : { $name : 'FOO' , $parseFloat : true } ,
996
+ } ) ;
997
+
998
+ await expect ( source . read ( [ environmentVariableSubstitution ( ) ] ) ) . rejects . toThrow ( ) ;
999
+ } ) ;
1000
+
1001
+ it ( 'parses boolean = true' , async ( ) => {
1002
+ process . env . FOO = 'true' ;
1003
+
1004
+ const source = new LiteralSource ( {
1005
+ $substitute : { $name : 'FOO' , $parseBool : true } ,
1006
+ } ) ;
1007
+
1008
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( true ) ;
1009
+ } ) ;
1010
+
1011
+ it ( 'parses boolean = 1' , async ( ) => {
1012
+ process . env . FOO = '1' ;
1013
+
1014
+ const source = new LiteralSource ( {
1015
+ $substitute : { $name : 'FOO' , $parseBool : true } ,
1016
+ } ) ;
1017
+
1018
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( true ) ;
1019
+ } ) ;
1020
+
1021
+ it ( 'parses boolean = 0' , async ( ) => {
1022
+ process . env . FOO = '0' ;
1023
+
1024
+ const source = new LiteralSource ( {
1025
+ $substitute : { $name : 'FOO' , $parseBool : true } ,
1026
+ } ) ;
1027
+
1028
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( false ) ;
1029
+ } ) ;
1030
+
1031
+ it ( 'parses boolean = false' , async ( ) => {
1032
+ process . env . FOO = 'false' ;
1033
+
1034
+ const source = new LiteralSource ( {
1035
+ $substitute : { $name : 'FOO' , $parseBool : true } ,
1036
+ } ) ;
1037
+
1038
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( false ) ;
1039
+ } ) ;
1040
+
961
1041
it ( 'doesnt visit fallback if name is defined' , async ( ) => {
962
1042
const failDirective = forKey ( '$fail' , ( ) => ( ) => {
963
1043
throw new Error ( ) ;
0 commit comments