@@ -1053,6 +1053,162 @@ describe('$substitute directive', () => {
1053
1053
1054
1054
expect ( parsed . toJSON ( ) ) . toEqual ( { foo : 'foo' } ) ;
1055
1055
} ) ;
1056
+
1057
+ it ( 'reads object with name' , async ( ) => {
1058
+ process . env . FOO = 'foo' ;
1059
+
1060
+ const source = new LiteralSource ( {
1061
+ foo : { $substitute : { name : 'FOO' } } ,
1062
+ } ) ;
1063
+
1064
+ const parsed = await source . read ( [ environmentVariableSubstitution ( ) ] ) ;
1065
+
1066
+ expect ( parsed . toJSON ( ) ) . toEqual ( { foo : 'foo' } ) ;
1067
+ } ) ;
1068
+
1069
+ it ( 'fails with name when not defined' , async ( ) => {
1070
+ const source = new LiteralSource ( {
1071
+ foo : { $substitute : { name : 'FOO' } } ,
1072
+ } ) ;
1073
+
1074
+ await expect ( source . read ( [ environmentVariableSubstitution ( ) ] ) ) . rejects . toThrow ( ) ;
1075
+ } ) ;
1076
+
1077
+ it ( 'uses name when fallback is defined' , async ( ) => {
1078
+ process . env . FOO = 'foo' ;
1079
+
1080
+ const source = new LiteralSource ( {
1081
+ foo : { $substitute : { name : 'FOO' , fallback : 'bar' } } ,
1082
+ } ) ;
1083
+
1084
+ const parsed = await source . read ( [ environmentVariableSubstitution ( ) ] ) ;
1085
+
1086
+ expect ( parsed . toJSON ( ) ) . toEqual ( { foo : 'foo' } ) ;
1087
+ } ) ;
1088
+
1089
+ it ( 'uses fallback when name was not found' , async ( ) => {
1090
+ const source = new LiteralSource ( {
1091
+ foo : { $substitute : { name : 'FOO' , fallback : 'bar' } } ,
1092
+ } ) ;
1093
+
1094
+ const parsed = await source . read ( [ environmentVariableSubstitution ( ) ] ) ;
1095
+
1096
+ expect ( parsed . toJSON ( ) ) . toEqual ( { foo : 'bar' } ) ;
1097
+ } ) ;
1098
+
1099
+ it ( 'allows null value when allowNull' , async ( ) => {
1100
+ const source = new LiteralSource ( {
1101
+ foo : { $substitute : { name : 'FOO' , fallback : null , allowNull : true } } ,
1102
+ } ) ;
1103
+
1104
+ const parsed = await source . read ( [ environmentVariableSubstitution ( ) ] ) ;
1105
+
1106
+ expect ( parsed . toJSON ( ) ) . toEqual ( { foo : null } ) ;
1107
+ } ) ;
1108
+
1109
+ it ( 'does not allow number even when allowNull' , async ( ) => {
1110
+ const source = new LiteralSource ( {
1111
+ foo : { $substitute : { name : 'FOO' , fallback : 42 , allowNull : true } } ,
1112
+ } ) ;
1113
+
1114
+ await expect ( source . read ( [ environmentVariableSubstitution ( ) ] ) ) . rejects . toThrow ( ) ;
1115
+ } ) ;
1116
+
1117
+ it ( 'parses ints' , async ( ) => {
1118
+ process . env . FOO = '11' ;
1119
+
1120
+ const source = new LiteralSource ( {
1121
+ $substitute : { name : 'FOO' , parseInt : true } ,
1122
+ } ) ;
1123
+
1124
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( 11 ) ;
1125
+ } ) ;
1126
+
1127
+ it ( 'fails when int is invalid' , async ( ) => {
1128
+ process . env . FOO = 'not a number' ;
1129
+
1130
+ const source = new LiteralSource ( {
1131
+ $substitute : { name : 'FOO' , parseInt : true } ,
1132
+ } ) ;
1133
+
1134
+ await expect ( source . read ( [ environmentVariableSubstitution ( ) ] ) ) . rejects . toThrow ( ) ;
1135
+ } ) ;
1136
+
1137
+ it ( 'parses float' , async ( ) => {
1138
+ process . env . FOO = '11.2' ;
1139
+
1140
+ const source = new LiteralSource ( {
1141
+ $substitute : { name : 'FOO' , parseFloat : true } ,
1142
+ } ) ;
1143
+
1144
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( 11.2 ) ;
1145
+ } ) ;
1146
+
1147
+ it ( 'fails when float is invalid' , async ( ) => {
1148
+ process . env . FOO = 'not a number' ;
1149
+
1150
+ const source = new LiteralSource ( {
1151
+ $substitute : { name : 'FOO' , parseFloat : true } ,
1152
+ } ) ;
1153
+
1154
+ await expect ( source . read ( [ environmentVariableSubstitution ( ) ] ) ) . rejects . toThrow ( ) ;
1155
+ } ) ;
1156
+
1157
+ it ( 'parses boolean = true' , async ( ) => {
1158
+ process . env . FOO = 'true' ;
1159
+
1160
+ const source = new LiteralSource ( {
1161
+ $substitute : { name : 'FOO' , parseBool : true } ,
1162
+ } ) ;
1163
+
1164
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( true ) ;
1165
+ } ) ;
1166
+
1167
+ it ( 'parses boolean = 1' , async ( ) => {
1168
+ process . env . FOO = '1' ;
1169
+
1170
+ const source = new LiteralSource ( {
1171
+ $substitute : { name : 'FOO' , parseBool : true } ,
1172
+ } ) ;
1173
+
1174
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( true ) ;
1175
+ } ) ;
1176
+
1177
+ it ( 'parses boolean = 0' , async ( ) => {
1178
+ process . env . FOO = '0' ;
1179
+
1180
+ const source = new LiteralSource ( {
1181
+ $substitute : { name : 'FOO' , parseBool : true } ,
1182
+ } ) ;
1183
+
1184
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( false ) ;
1185
+ } ) ;
1186
+
1187
+ it ( 'parses boolean = false' , async ( ) => {
1188
+ process . env . FOO = 'false' ;
1189
+
1190
+ const source = new LiteralSource ( {
1191
+ $substitute : { name : 'FOO' , parseBool : true } ,
1192
+ } ) ;
1193
+
1194
+ expect ( await source . readToJSON ( [ environmentVariableSubstitution ( ) ] ) ) . toEqual ( false ) ;
1195
+ } ) ;
1196
+
1197
+ it ( 'doesnt visit fallback if name is defined' , async ( ) => {
1198
+ const failDirective = forKey ( '$fail' , ( ) => ( ) => {
1199
+ throw new Error ( ) ;
1200
+ } ) ;
1201
+
1202
+ process . env . FOO = 'foo' ;
1203
+
1204
+ const source = new LiteralSource ( {
1205
+ foo : { $substitute : { name : 'FOO' , fallback : { $fail : true } } } ,
1206
+ } ) ;
1207
+
1208
+ const parsed = await source . read ( [ environmentVariableSubstitution ( ) , failDirective ] ) ;
1209
+
1210
+ expect ( parsed . toJSON ( ) ) . toEqual ( { foo : 'foo' } ) ;
1211
+ } ) ;
1056
1212
} ) ;
1057
1213
1058
1214
describe ( '$timestamp directive' , ( ) => {
0 commit comments