forked from mockturtl/dotenv
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathparser_test.dart
170 lines (154 loc) · 6.48 KB
/
parser_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import 'dart:math';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
const ceil = 100000;
void main() {
late Random rand;
const psr = Parser();
group('[Parser]', () {
setUp(() => rand = Random());
test('it swallows leading "export"', () {
var out = psr.trimExportKeyword(' export foo = bar ');
expect(out, equals('foo = bar'));
out = psr.trimExportKeyword(' foo = bar export');
expect(out, equals('foo = bar export'));
});
test('it strips trailing comments', () {
var out = psr.strip(
'needs="explanation" # It was the year when they finally immanentized the Eschaton.');
expect(out, equals('needs="explanation"'));
out = psr.strip(
'needs="explanation # It was the year when they finally immanentized the Eschaton." ');
expect(
out,
equals(
'needs="explanation # It was the year when they finally immanentized the Eschaton."'));
out = psr.strip(
'needs=explanation # It was the year when they finally immanentized the Eschaton."',
includeQuotes: true);
expect(out, equals('needs=explanation'));
out = psr.strip(' # It was the best of times, it was a waste of time.');
expect(out, isEmpty);
});
test('it knows quoted # is not a comment', () {
var doub = psr.parseOne('foo = "ab#c"');
var single = psr.parseOne("foo = 'ab#c'");
expect(doub['foo'], equals('ab#c'));
expect(single['foo'], equals('ab#c'));
});
test('it handles quotes in a comment', () {
// note terminal whitespace
var sing = psr.parseOne("fruit = 'banana' # comments can be 'sneaky!' ");
var doub =
psr.parseOne('fruit = " banana" # comments can be "sneaky!" ');
var none =
psr.parseOne('fruit = banana # comments can be "sneaky!" ');
expect(sing['fruit'], equals('banana'));
expect(doub['fruit'], equals(' banana'));
expect(none['fruit'], equals('banana'));
});
test('treats all # in unquoted as comments', () {
var fail =
psr.parseOne('fruit = banana # I\'m a comment with a final "quote"');
expect(fail['fruit'], equals('banana'));
});
test('it handles unquoted values', () {
var out = psr.removeSurroundingQuotes(' str ');
expect(out, equals('str'));
});
test('it handles double quoted values', () {
var out = psr.removeSurroundingQuotes('"val "');
expect(out, equals('val '));
});
test('it handles single quoted values', () {
var out = psr.removeSurroundingQuotes("' val'");
expect(out, equals(' val'));
});
test('retain trailing single quote', () {
var out = psr.removeSurroundingQuotes("retained'");
expect(out, equals("retained'"));
});
// test('it handles escaped quotes within values', () { // Does not
// var out = _psr.unquote('''\'val_with_\\"escaped\\"_\\'quote\\'s \'''');
// expect(out, equals('''val_with_"escaped"_'quote's '''));
// out = _psr.unquote(" val_with_\"escaped\"_\'quote\'s ");
// expect(out, equals('''val_with_"escaped"_'quote's'''));
// });
test('it skips empty lines', () {
var out = psr.parse([
'# Define environment variables.',
' # comments will be stripped',
'foo=bar # trailing junk',
' baz = qux',
'# another comment'
]);
expect(out, equals({'foo': 'bar', 'baz': 'qux'}));
});
test('it ignores duplicate keys', () {
var out = psr.parse(['foo=bar', 'foo=baz']);
expect(out, equals({'foo': 'bar'}));
});
test('it substitutes known variables into other values', () {
var out = psr.parse(['foo=bar', r'baz=super$foo${foo}']);
expect(out, equals({'foo': 'bar', 'baz': 'superbarbar'}));
});
test('it discards surrounding quotes', () {
var out = psr.parse([r"foo = 'bar'", r'export baz="qux"']);
expect(out, equals({'foo': 'bar', 'baz': 'qux'}));
});
test('it detects unquoted values', () {
var out = psr.getSurroundingQuoteCharacter('no quotes here!');
expect(out, isEmpty);
});
test('it detects double-quoted values', () {
var out = psr.getSurroundingQuoteCharacter('"double quoted"');
expect(out, equals('"'));
});
test('it detects single-quoted values', () {
var out = psr.getSurroundingQuoteCharacter("'single quoted'");
expect(out, equals("'"));
});
test('it performs variable substitution', () {
var out = psr.interpolate(r'a$foo$baz', {'foo': 'bar', 'baz': 'qux'});
expect(out, equals('abarqux'));
});
test('it skips undefined variables', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.interpolate('a\$jinx_$r', {});
expect(out, equals('a'));
});
test('it handles explicitly null values in env', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.interpolate('a\$foo_$r\$baz_$r', {'foo_$r': null});
expect(out, equals('a'));
});
test('it handles \${surrounding braces} on vars', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.interpolate('optional_\${foo_$r}', {'foo_$r': 'curlies'});
expect(out, equals('optional_curlies'));
});
test('it handles equal signs in values', () {
var none = psr.parseOne('foo=bar=qux');
var sing = psr.parseOne("foo='bar=qux'");
var doub = psr.parseOne('foo="bar=qux"');
expect(none['foo'], equals('bar=qux'));
expect(sing['foo'], equals('bar=qux'));
expect(doub['foo'], equals('bar=qux'));
});
test('it skips var substitution in single quotes', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.parseOne("some_var='my\$key_$r'", envMap: {'key_$r': 'val'});
expect(out['some_var'], equals('my\$key_$r'));
});
test('it performs var subs in double quotes', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.parseOne('some_var="my\$key_$r"', envMap: {'key_$r': 'val'});
expect(out['some_var'], equals('myval'));
});
test('it performs var subs without quotes', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.parseOne("some_var=my\$key_$r", envMap: {'key_$r': 'val'});
expect(out['some_var'], equals('myval'));
});
});
}