|
| 1 | +'use strict'; |
| 2 | + |
| 3 | + |
| 4 | +const assert = require('assert'); |
| 5 | +const yaml = require('../../'); |
| 6 | + |
| 7 | + |
| 8 | +describe('Multi tag', function () { |
| 9 | + it('should process multi tags', function () { |
| 10 | + let tags = [ 'scalar', 'mapping', 'sequence' ].map(kind => |
| 11 | + new yaml.Type('!', { |
| 12 | + kind, |
| 13 | + multi: true, |
| 14 | + resolve: function () { |
| 15 | + return true; |
| 16 | + }, |
| 17 | + construct: function (value, tag) { |
| 18 | + return { kind, tag, value }; |
| 19 | + } |
| 20 | + }) |
| 21 | + ); |
| 22 | + |
| 23 | + let schema = yaml.DEFAULT_SCHEMA.extend(tags); |
| 24 | + |
| 25 | + let expected = [ |
| 26 | + { |
| 27 | + kind: 'scalar', |
| 28 | + tag: '!t1', |
| 29 | + value: '123' |
| 30 | + }, |
| 31 | + { |
| 32 | + kind: 'sequence', |
| 33 | + tag: '!t2', |
| 34 | + value: [ 1, 2, 3 ] |
| 35 | + }, |
| 36 | + { |
| 37 | + kind: 'mapping', |
| 38 | + tag: '!t3', |
| 39 | + value: { a: 1, b: 2 } |
| 40 | + } |
| 41 | + ]; |
| 42 | + |
| 43 | + assert.deepStrictEqual(yaml.load(` |
| 44 | +- !t1 123 |
| 45 | +- !t2 [ 1, 2, 3 ] |
| 46 | +- !t3 { a: 1, b: 2 } |
| 47 | +`, { |
| 48 | + schema: schema |
| 49 | + }), expected); |
| 50 | + }); |
| 51 | + |
| 52 | + |
| 53 | + it('should process tags depending on prefix', function () { |
| 54 | + let tags = [ '!foo', '!bar', '!' ].map(prefix => |
| 55 | + new yaml.Type(prefix, { |
| 56 | + kind: 'scalar', |
| 57 | + multi: true, |
| 58 | + resolve: function () { |
| 59 | + return true; |
| 60 | + }, |
| 61 | + construct: function (value, tag) { |
| 62 | + return { prefix, tag, value }; |
| 63 | + } |
| 64 | + }) |
| 65 | + ); |
| 66 | + |
| 67 | + tags.push( |
| 68 | + new yaml.Type('!bar', { |
| 69 | + kind: 'scalar', |
| 70 | + resolve: function () { |
| 71 | + return true; |
| 72 | + }, |
| 73 | + construct: function (value) { |
| 74 | + return { single: true, value }; |
| 75 | + } |
| 76 | + }) |
| 77 | + ); |
| 78 | + |
| 79 | + let schema = yaml.DEFAULT_SCHEMA.extend(tags); |
| 80 | + |
| 81 | + let expected = [ |
| 82 | + { prefix: '!foo', tag: '!foo', value: '1' }, |
| 83 | + { prefix: '!foo', tag: '!foo2', value: '2' }, |
| 84 | + { single: true, value: '3' }, |
| 85 | + { prefix: '!bar', tag: '!bar2', value: '4' }, |
| 86 | + { prefix: '!', tag: '!baz', value: '5' } |
| 87 | + ]; |
| 88 | + |
| 89 | + assert.deepStrictEqual(yaml.load(` |
| 90 | +- !foo 1 |
| 91 | +- !foo2 2 |
| 92 | +- !bar 3 |
| 93 | +- !bar2 4 |
| 94 | +- !baz 5 |
| 95 | +`, { |
| 96 | + schema: schema |
| 97 | + }), expected); |
| 98 | + }); |
| 99 | +}); |
0 commit comments