|
1 | 1 | import * as YAML from 'yaml'
|
2 | 2 | import { Scalar, YAMLSeq } from 'yaml'
|
3 |
| -import { stringifyString } from 'yaml/util' |
| 3 | +import { seqTag, stringTag, stringifyString } from 'yaml/util' |
4 | 4 | import { source } from '../_utils'
|
5 | 5 |
|
6 | 6 | describe('json schema', () => {
|
@@ -774,6 +774,69 @@ describe('custom tags', () => {
|
774 | 774 | const str = YAML.stringify(obj, { customTags: [[regexp, sharedSymbol]] })
|
775 | 775 | expect(str).toBe('re: !re /re/g\nsymbol: !symbol/shared foo\n')
|
776 | 776 | })
|
| 777 | + |
| 778 | + describe('completely custom schema', () => { |
| 779 | + test('customTags is required', () => { |
| 780 | + expect(() => |
| 781 | + YAML.parseDocument('foo', { schema: 'custom-test' }) |
| 782 | + ).toThrow(/Unknown schema "custom-test"/) |
| 783 | + }) |
| 784 | + |
| 785 | + test('parse success', () => { |
| 786 | + const src = '- foo\n- !re /bar/\n' |
| 787 | + const doc = YAML.parseDocument(src, { |
| 788 | + customTags: [seqTag, stringTag, regexp], |
| 789 | + schema: 'custom-test' |
| 790 | + }) |
| 791 | + expect(doc.errors).toEqual([]) |
| 792 | + expect(doc.warnings).toEqual([]) |
| 793 | + expect(doc.schema.name).toBe('custom-test') |
| 794 | + expect(doc.toJS()).toEqual(['foo', /bar/]) |
| 795 | + }) |
| 796 | + |
| 797 | + test('parse fail', () => { |
| 798 | + // map, seq & string are always parsed, even if not included |
| 799 | + const src = '- foo\n- !re /bar/\n' |
| 800 | + const doc = YAML.parseDocument(src, { |
| 801 | + customTags: [], |
| 802 | + schema: 'custom-test' |
| 803 | + }) |
| 804 | + expect(doc.errors).toEqual([]) |
| 805 | + expect(doc.warnings).toHaveLength(1) |
| 806 | + expect(doc.warnings[0].message).toMatch(/Unresolved tag: !re/) |
| 807 | + }) |
| 808 | + |
| 809 | + test('stringify success', () => { |
| 810 | + let src = '- foo\n' |
| 811 | + let doc = YAML.parseDocument(src, { |
| 812 | + customTags: [seqTag, stringTag], |
| 813 | + schema: 'custom-test' |
| 814 | + }) |
| 815 | + expect(doc.toString()).toBe(src) |
| 816 | + |
| 817 | + src = '- foo\n- !re /bar/\n' |
| 818 | + doc = YAML.parseDocument(src, { |
| 819 | + customTags: [seqTag, stringTag, regexp], |
| 820 | + schema: 'custom-test' |
| 821 | + }) |
| 822 | + expect(doc.toString()).toBe(src) |
| 823 | + }) |
| 824 | + |
| 825 | + test('stringify fail', () => { |
| 826 | + const src = '- foo\n' |
| 827 | + let doc = YAML.parseDocument(src, { |
| 828 | + customTags: [], |
| 829 | + schema: 'custom-test' |
| 830 | + }) |
| 831 | + expect(() => String(doc)).toThrow(/Tag not resolved for YAMLSeq value/) |
| 832 | + |
| 833 | + doc = YAML.parseDocument(src, { |
| 834 | + customTags: [seqTag], |
| 835 | + schema: 'custom-test' |
| 836 | + }) |
| 837 | + expect(() => String(doc)).toThrow(/Tag not resolved for String value/) |
| 838 | + }) |
| 839 | + }) |
777 | 840 | })
|
778 | 841 |
|
779 | 842 | describe('schema changes', () => {
|
|
0 commit comments