aboutsummaryrefslogtreecommitdiff
path: root/src/matcher/options.test.ts
blob: 39c4b3e3815f230c6004624962e4181d90f8ab3e (plain) (blame)
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
import * as mocha from 'mocha';
import chai = require('chai');
var should = chai.should();

import * as options from './options';
import { default as types } from './types';

describe('Object Matching:', () => {
  describe('Rough Matching', () => {
    it('accepts basic things', () => {
      let template = {
        a: types.string
      };
      let matchee = {
        a: "yes"
      };
      options.match(template, matchee).should.be.true;
    });
    it('accepts objects with superfluous properties', () => {
      let template = {
        a: types.number
      };
      let matchee = {
        a: 40,
        b: 20
      };
      options.match(template, matchee).should.be.true;
    });
    it('rejects objects missing some properties', () => {
      let template = {
        a: types.boolean,
        b: types.number,
        c: types.number
      };
      let matchee = {
        b: 5
      };
      options.match(template, matchee).should.be.false;
    });
    it('accepts objects that omit not_present properties', () => {
      let template = {
        a: types.function,
        b: types.not_present,
        c: types.not_present
      };
      let matchee = {
        a: new Function()
      };
      options.match(template, matchee).should.be.true;
    });
    it('rejects empty objects for non-empty templates', () => {
      let template = {
        a: types.boolean
      };
      let matchee = {
      };
      options.match(template, matchee).should.be.false;
    });
    it('accepts empty objects for empty templates', () => {
      let template = {
      };
      let matchee = {
      };
      options.match(template, matchee).should.be.true;
    });
    it('accepts nested objects that match', () => {
      let template = {
        a: types.number,
        b: {
          ba: types.string,
          bb: types.function
        },
        c: types.boolean
      };
      let matchee = {
        a: 432,
        b: {
          ba: "baracus",
          bb: new Function(),
        },
        c: true,
        x: false
      };
      options.match(template, matchee).should.be.true;
    });
    it('accepts literal matches', () => {
      let template = {
        a: "literal"
      };
      let matchee = {
        a: "literal"
      };
      options.match(template, matchee).should.be.true;
    });
    it('rejects incorrect literal matches', () => {
      let template = {
        g: 99
      };
      let matchee = {
        g: 80
      };
      options.match(template, matchee).should.be.false;
    });
  });
});