Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 26b63d3

Browse files
authored
PHPLIB-1354 Add tests on String Expression Operators (#42)
1 parent 33dfd41 commit 26b63d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2533
-344
lines changed

generator/config/expression/concat.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ arguments:
1212
type:
1313
- resolvesToString
1414
variadic: array
15+
tests:
16+
-
17+
name: 'Example'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/concat/#examples'
19+
pipeline:
20+
-
21+
$project:
22+
itemDescription:
23+
$concat:
24+
- '$item'
25+
- ' - '
26+
- '$description'

generator/config/expression/indexOfBytes.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ arguments:
3737
description: |
3838
An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number. If you specify a <end> index value, you should also specify a <start> index value; otherwise, $indexOfArray uses the <end> value as the <start> index value instead of the <end> value.
3939
If unspecified, the ending index position for the search is the end of the string.
40+
tests:
41+
-
42+
name: 'Example'
43+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexOfBytes/#examples'
44+
pipeline:
45+
-
46+
$project:
47+
byteLocation:
48+
$indexOfBytes:
49+
- '$item'
50+
- 'foo'

generator/config/expression/indexOfCP.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ arguments:
3737
description: |
3838
An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number. If you specify a <end> index value, you should also specify a <start> index value; otherwise, $indexOfArray uses the <end> value as the <start> index value instead of the <end> value.
3939
If unspecified, the ending index position for the search is the end of the string.
40+
tests:
41+
-
42+
name: 'Examples'
43+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexOfCP/#examples'
44+
pipeline:
45+
-
46+
$project:
47+
cpLocation:
48+
$indexOfCP:
49+
- '$item'
50+
- 'foo'

generator/config/expression/ltrim.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,14 @@ arguments:
2323
The character(s) to trim from the beginning of the input.
2424
The argument can be any valid expression that resolves to a string. The $ltrim operator breaks down the string into individual UTF code point to trim from input.
2525
If unspecified, $ltrim removes whitespace characters, including the null character.
26+
tests:
27+
-
28+
name: 'Example'
29+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ltrim/#example'
30+
pipeline:
31+
-
32+
$project:
33+
item: 1
34+
description:
35+
$ltrim:
36+
input: '$description'

generator/config/expression/regexFind.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,41 @@ arguments:
2626
type:
2727
- string
2828
optional: true
29+
tests:
30+
-
31+
name: '$regexFind and Its Options'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexFind/#-regexfind-and-its-options'
33+
pipeline:
34+
-
35+
$addFields:
36+
returnObject:
37+
$regexFind:
38+
input: '$description'
39+
regex: !regex 'line'
40+
-
41+
name: 'i Option'
42+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexFind/#i-option'
43+
pipeline:
44+
-
45+
$addFields:
46+
returnObject:
47+
# Specify i as part of the Regex type
48+
$regexFind:
49+
input: '$description'
50+
regex: !regex ['line', 'i']
51+
-
52+
$addFields:
53+
returnObject:
54+
# Specify i in the options field
55+
$regexFind:
56+
input: '$description'
57+
regex: 'line'
58+
options: 'i'
59+
-
60+
$addFields:
61+
returnObject:
62+
# Mix Regex type with options field
63+
$regexFind:
64+
input: '$description'
65+
regex: !regex 'line'
66+
options: 'i'

generator/config/expression/regexFindAll.yaml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,74 @@ arguments:
2626
type:
2727
- string
2828
optional: true
29+
tests:
30+
-
31+
name: '$regexFindAll and Its Options'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexFindAll/#-regexfindall-and-its-options'
33+
pipeline:
34+
-
35+
$addFields:
36+
returnObject:
37+
$regexFindAll:
38+
input: '$description'
39+
regex: !regex 'line'
40+
-
41+
name: 'i Option'
42+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexFindAll/#i-option'
43+
pipeline:
44+
-
45+
$addFields:
46+
returnObject:
47+
# Specify i as part of the regex type
48+
$regexFindAll:
49+
input: '$description'
50+
regex: !regex ['line', 'i']
51+
-
52+
$addFields:
53+
returnObject:
54+
# Specify i in the options field
55+
$regexFindAll:
56+
input: '$description'
57+
regex: 'line'
58+
options: 'i'
59+
-
60+
$addFields:
61+
returnObject:
62+
# Mix Regex type with options field
63+
$regexFindAll:
64+
input: '$description'
65+
regex: !regex 'line'
66+
options: 'i'
67+
-
68+
name: 'Use $regexFindAll to Parse Email from String'
69+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexFindAll/#use--regexfindall-to-parse-email-from-string'
70+
pipeline:
71+
-
72+
$addFields:
73+
email:
74+
$regexFindAll:
75+
input: '$comment'
76+
regex: !regex ['[a-z0-9_.+-]+@[a-z0-9_.+-]+\.[a-z0-9_.+-]+', 'i']
77+
-
78+
$set:
79+
email: '$email.match'
80+
-
81+
name: 'Use Captured Groupings to Parse User Name'
82+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexFindAll/#use-captured-groupings-to-parse-user-name'
83+
pipeline:
84+
-
85+
$addFields:
86+
names:
87+
$regexFindAll:
88+
input: '$comment'
89+
regex: !regex ['([a-z0-9_.+-]+)@[a-z0-9_.+-]+\.[a-z0-9_.+-]+', 'i']
90+
-
91+
$set:
92+
names:
93+
$reduce:
94+
input: '$names.captures'
95+
initialValue: []
96+
in:
97+
$concatArrays:
98+
- '$$value'
99+
- '$$this'

generator/config/expression/regexMatch.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,55 @@ arguments:
2626
type:
2727
- string
2828
optional: true
29+
tests:
30+
-
31+
name: '$regexMatch and Its Options'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexMatch/#-regexmatch-and-its-options'
33+
pipeline:
34+
-
35+
$addFields:
36+
result:
37+
$regexMatch:
38+
input: '$description'
39+
regex: !regex 'line'
40+
-
41+
name: 'i Option'
42+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexMatch/#i-option'
43+
pipeline:
44+
-
45+
$addFields:
46+
result:
47+
# Specify i as part of the Regex type
48+
$regexMatch:
49+
input: '$description'
50+
regex: !regex ['line', 'i']
51+
-
52+
$addFields:
53+
result:
54+
# Specify i in the options field
55+
$regexMatch:
56+
input: '$description'
57+
regex: 'line'
58+
options: 'i'
59+
-
60+
$addFields:
61+
result:
62+
# Mix Regex type with options field
63+
$regexMatch:
64+
input: '$description'
65+
regex: !regex 'line'
66+
options: 'i'
67+
-
68+
name: 'Use $regexMatch to Check Email Address'
69+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexMatch/#use--regexmatch-to-check-email-address'
70+
pipeline:
71+
-
72+
$addFields:
73+
category:
74+
$cond:
75+
if:
76+
$regexMatch:
77+
input: '$comment'
78+
regex: !regex ['[a-z0-9_.+-][email protected]', 'i']
79+
then: 'Employee'
80+
else: 'External'

generator/config/expression/replaceAll.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ arguments:
3030
- resolvesToNull
3131
description: |
3232
The string to use to replace all matched instances of find in input. Can be any valid expression that resolves to a string or a null.
33+
tests:
34+
-
35+
name: 'Example'
36+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceAll/#example'
37+
pipeline:
38+
-
39+
$project:
40+
item:
41+
$replaceAll:
42+
input: '$item'
43+
find: 'blue paint'
44+
replacement: 'red paint'

generator/config/expression/replaceOne.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ arguments:
2929
- resolvesToNull
3030
description: |
3131
The string to use to replace all matched instances of find in input. Can be any valid expression that resolves to a string or a null.
32+
tests:
33+
-
34+
name: 'Example'
35+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceOne/#example'
36+
pipeline:
37+
-
38+
$project:
39+
item:
40+
$replaceOne:
41+
input: '$item'
42+
find: 'blue paint'
43+
replacement: 'red paint'

generator/config/expression/rtrim.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,14 @@ arguments:
2222
The character(s) to trim from the beginning of the input.
2323
The argument can be any valid expression that resolves to a string. The $ltrim operator breaks down the string into individual UTF code point to trim from input.
2424
If unspecified, $ltrim removes whitespace characters, including the null character.
25+
tests:
26+
-
27+
name: 'Example'
28+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rtrim/#example'
29+
pipeline:
30+
-
31+
$project:
32+
item: 1
33+
description:
34+
$rtrim:
35+
input: '$description'

generator/config/expression/split.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,30 @@ arguments:
1919
- resolvesToString
2020
description: |
2121
The delimiter to use when splitting the string expression. delimiter can be any valid expression as long as it resolves to a string.
22+
tests:
23+
-
24+
name: 'Example'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/split/#example'
26+
pipeline:
27+
-
28+
$project:
29+
city_state:
30+
$split:
31+
- '$city'
32+
- ', '
33+
qty: 1
34+
-
35+
$unwind:
36+
path: '$city_state'
37+
-
38+
$match:
39+
city_state: !regex '[A-Z]{2}'
40+
-
41+
$group:
42+
_id:
43+
state: '$city_state'
44+
total_qty:
45+
$sum: '$qty'
46+
-
47+
$sort:
48+
total_qty: -1

generator/config/expression/strLenBytes.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ arguments:
1111
name: expression
1212
type:
1313
- resolvesToString
14+
tests:
15+
-
16+
name: 'Single-Byte and Multibyte Character Set'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenBytes/#single-byte-and-multibyte-character-set'
18+
pipeline:
19+
-
20+
$project:
21+
name: 1
22+
length:
23+
$strLenBytes: '$name'

generator/config/expression/strLenCP.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ arguments:
1111
name: expression
1212
type:
1313
- resolvesToString
14+
tests:
15+
-
16+
name: 'Single-Byte and Multibyte Character Set'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenBytes/#single-byte-and-multibyte-character-set'
18+
pipeline:
19+
-
20+
$project:
21+
name: 1
22+
length:
23+
$strLenCP: '$name'

generator/config/expression/strcasecmp.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ arguments:
1515
name: expression2
1616
type:
1717
- resolvesToString
18+
tests:
19+
-
20+
name: 'Example'
21+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/strcasecmp/#example'
22+
pipeline:
23+
-
24+
$project:
25+
item: 1
26+
comparisonResult:
27+
$strcasecmp:
28+
- '$quarter'
29+
- '13q4'

generator/config/expression/substr.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@ arguments:
2323
- resolvesToInt
2424
description: |
2525
If length is a negative number, $substr returns a substring that starts at the specified index and includes the rest of the string.
26+
tests:
27+
-
28+
name: 'Example'
29+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/substr/#example'
30+
pipeline:
31+
-
32+
$project:
33+
item: 1
34+
yearSubstring:
35+
$substr:
36+
- '$quarter'
37+
- 0
38+
- 2
39+
quarterSubtring:
40+
$substr:
41+
- '$quarter'
42+
- 2
43+
- -1

0 commit comments

Comments
 (0)