Skip to content

Commit

Permalink
fix(path): fix nested group match is not work (#2818)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Feb 13, 2022
1 parent babc5c2 commit cad37da
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 151 deletions.
45 changes: 43 additions & 2 deletions packages/path/src/__tests__/match.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import expect from 'expect'
import { Path } from '../'
import { Matcher } from '../matcher'

const match = (obj) => {
for (let name in obj) {
Expand Down Expand Up @@ -31,6 +32,23 @@ const unmatch = (obj) => {
}
}

test('basic match', () => {
expect(Path.parse('xxx').match('')).toBeFalsy()
expect(Path.parse('xxx').match('aaa')).toBeFalsy()
expect(Path.parse('xxx.eee').match('xxx')).toBeFalsy()
expect(Path.parse('*(xxx.eee~)').match('xxx')).toBeFalsy()
expect(Path.parse('xxx.eee~').match('xxx.eee')).toBeTruthy()
expect(Path.parse('*(!xxx.eee,yyy)').match('xxx')).toBeFalsy()
expect(Path.parse('*(!xxx.eee,yyy)').match('xxx.ooo.ppp')).toBeTruthy()
expect(Path.parse('*(!xxx.eee,yyy)').match('xxx.eee')).toBeFalsy()
expect(Path.parse('*(!xxx.eee~,yyy)').match('xxx.eee')).toBeFalsy()
expect(Path.parse('~.aa').match('xxx.aa')).toBeTruthy()
})

test('not expect match not', () => {
expect(new Matcher({}).match(['']).matched).toBeFalsy()
})

test('test matchGroup', () => {
const pattern = new Path('*(aa,bb,cc)')
expect(pattern.matchAliasGroup('aa', 'bb')).toEqual(true)
Expand Down Expand Up @@ -115,8 +133,8 @@ test('test optional wild match', () => {
expect(Path.parse('*(aa.**,bb.**)').match(['bb'])).toEqual(true)
expect(Path.parse('*(aa.**,bb.**)').match(['bb', 'cc', 'dd'])).toEqual(true)
expect(Path.parse('*(aa.**,bb.**)').match(['cc'])).toEqual(false)
expect(Path.parse('*(aa.**,bb.**).bb').match(['aa', 'oo'])).toEqual(false)
expect(Path.parse('*(aa.**,bb.**).bb').match(['bb', 'oo'])).toEqual(false)
expect(Path.parse('*(aa.**,bb.**).bb').match(['aa', 'oo'])).toEqual(true)
expect(Path.parse('*(aa.**,bb.**).bb').match(['bb', 'oo'])).toEqual(true)
expect(Path.parse('*(aa.**,bb.**).bb').match(['aa', 'oo', 'bb'])).toEqual(
true
)
Expand All @@ -125,9 +143,15 @@ test('test optional wild match', () => {
)
expect(
Path.parse('*(aa.**,bb.**).bb').match(['aa', 'oo', 'kk', 'dd', 'bb'])
).toEqual(true)
expect(
Path.parse('*(aa.**,bb.**).bb').match(['cc', 'oo', 'kk', 'dd', 'bb'])
).toEqual(false)
expect(
Path.parse('*(aa.**,bb.**).bb').match(['bb', 'oo', 'kk', 'dd', 'bb'])
).toEqual(true)
expect(
Path.parse('*(aa.**,bb.**).bb').match(['kk', 'oo', 'kk', 'dd', 'bb'])
).toEqual(false)
})

Expand All @@ -151,6 +175,12 @@ test('test segments', () => {
expect(node.match(['a', 0, 'b'])).toEqual(true)
})

test('nested group match', () => {
expect(
Path.parse('aa.*.*(bb,cc).dd.*(kk,oo).ee').match('aa.0.cc.dd.kk.ee')
).toEqual(true)
})

test('group match with destructor', () => {
expect(Path.parse('*([startDate,endDate],date,weak)').match('date')).toEqual(
true
Expand Down Expand Up @@ -224,6 +254,16 @@ match({
['a', 10, 's'],
['a', 50, 's'],
],
'a.*[10:].*(!a,b)': [
['a', 49, 's'],
['a', 10, 's'],
['a', 50, 's'],
],
'a.*[].*(!a,b)': [
['a', 49, 's'],
['a', 10, 's'],
['a', 50, 's'],
],
'a.*[:50].*(!a,b)': [
['a', 49, 's'],
['a', 10, 's'],
Expand All @@ -247,6 +287,7 @@ match({
['a', 'b'],
['a', 'b', 'c'],
],
'aa.*.*(bb,cc).dd': [['aa', '0', 'cc', 'dd']],
'aaa.products.0.*': [['aaa', 'products', '0', 'aaa']],
'aa~.ccc': [
['aa', 'ccc'],
Expand Down
Loading

0 comments on commit cad37da

Please sign in to comment.