mcollina / bloomrun

A js pattern matcher
MIT License
120 stars 10 forks source link

Tests: `.list()` seems sensible to key order despite `depth` #39

Closed gabssnake closed 8 years ago

gabssnake commented 8 years ago

Hello, thanks for this great module. I have encountered the following situation:

Passing test:

test('List does match partial pattern, first by depth then by insertion order', function (t) {
  t.plan(1)
  var instance = bloomrun({ indexing: 'depth' })

  instance.add({ c: 'CCC' }, 1)
  instance.add({ a: 'AAA', b: 'BBB', c: 'CCC' }, 2)
  instance.add({ a: 'AAA' }, 3)

  t.deepEqual(instance.list({ a: 'AAA', b: 'BBB', c: 'CCC', d: 'DDD' }), [2, 1, 3])
})

Failing => expected: [ 3, 1, 2, 4 ] actual: [ 3, 1, 4 ]

test('List matches partially, regardless of key order (1)', function (t) {
  t.plan(1)
  var instance = bloomrun({ indexing: 'depth' })

  instance.add({ c: 'CCC' }, 1)
  instance.add({ b: 'BBB' }, 2)
  instance.add({ a: 'AAA', b: 'BBB', c: 'CCC' }, 3)
  instance.add({ a: 'AAA' }, 4)

  t.deepEqual(instance.list({ a: 'AAA', b: 'BBB', c: 'CCC', d: 'DDD' }), [3, 1, 2, 4])
})

Failing => expected: [ 1 ] actual: [ 3 ]

test('List matches partially, regardless of key order (2)', function (t) {
  t.plan(1)
  var instance = bloomrun({ indexing: 'depth' })

  instance.add({ c: 'CCC', d: 'DDD' }, 1)
  instance.add({ c: 'CCC' }, 2)
  instance.add({ a: 'AAA' }, 3)

  t.deepEqual(instance.list({ a: 'AAA', b: 'BBB', c: 'CCC', d: 'DDD' }), [1])
})

Am I using this wrong?

gabssnake commented 8 years ago

That is great, thank you.

If indexing by depth, what order should one expect for same-weight but different partial patterns? Is it insertion order?

test('bloom depth, partial match order', function (t) {
  t.plan(1)
  var instance = bloomrun({ indexing: 'depth' })

  instance.add({ c: 'CCC' }, 1)
  instance.add({ b: 'BBB' }, 2)
  instance.add({ a: 'AAA' }, 3)

  t.deepEqual(instance.list({ a: 'AAA', b: 'BBB', c: 'CCC', d: 'DDD' }), [ 1, 2, 3 ])
})
mcollina commented 8 years ago

@gabssnake insertion by bucket order. Meaning, if your pattern overlaps with another, than it will be checked before the others. The truth is, sending messages that overlap multiple patterns is not a good idea and a code smell. Whatever the rule is, it is either a) highly inefficient or b) very hard to understand.

gabssnake commented 8 years ago

It’s Ok, not an actual use case, just poking around to understand. Thanks again for your time,

mcollina commented 8 years ago

@gabssnake your questions/edge cases where really helpful!