Skip to content

Commit

Permalink
Remember source string for null scalars (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 authored Sep 16, 2020
1 parent 3b807a1 commit cc4456c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/tags/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ export const nullObj = {
default: true,
tag: 'tag:yaml.org,2002:null',
test: /^(?:~|[Nn]ull|NULL)?$/,
resolve: () => null,
resolve: str => {
const node = new Scalar(null)
node.sourceStr = str
return node
},
options: nullOptions,
stringify: () => nullOptions.nullStr
stringify: ({ sourceStr }) => sourceStr ?? nullOptions.nullStr
}

export const boolObj = {
Expand Down
8 changes: 6 additions & 2 deletions src/tags/yaml-1.1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ export const yaml11 = failsafe.concat(
default: true,
tag: 'tag:yaml.org,2002:null',
test: /^(?:~|[Nn]ull|NULL)?$/,
resolve: () => null,
resolve: str => {
const node = new Scalar(null)
node.sourceStr = str
return node
},
options: nullOptions,
stringify: () => nullOptions.nullStr
stringify: ({ sourceStr }) => sourceStr ?? nullOptions.nullStr
},
{
identify: value => typeof value === 'boolean',
Expand Down
18 changes: 18 additions & 0 deletions tests/doc/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ describe('tags', () => {
})
})

describe('custom string on node', () => {
test('tiled null', () => {
YAML.scalarOptions.null.nullStr = '~'
const doc = YAML.parse('a: null')
const str = YAML.stringify(doc, {simpleKeys: true})
expect(str).toBe('a: ~\n')
expect(YAML.parse(str)).toEqual({a: null})
})

test('empty string null', () => {
YAML.scalarOptions.null.nullStr = ''
const doc = YAML.parse('a: null')
const str = YAML.stringify(doc, {simpleKeys: true})
expect(str).toBe('a: \n')
expect(YAML.parse(str)).toEqual({a: null})
})
})

describe('number types', () => {
describe('asBigInt: false', () => {
test('Version 1.1', () => {
Expand Down
11 changes: 9 additions & 2 deletions tests/doc/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,19 @@ test('eemeli/yaml#87', () => {
expect(String(doc)).toBe('test:\n a: test\n')
})

describe('scalar styles', () => {
test('null Scalar styles', () => {
const doc = YAML.parseDocument('[ null, Null, NULL, ~ ]')
expect(String(doc)).toBe('[ null, Null, NULL, ~ ]\n')
})
})

describe('simple keys', () => {
test('key with null value', () => {
const doc = YAML.parseDocument('~: ~')
expect(String(doc)).toBe('? null\n')
expect(String(doc)).toBe('? ~\n')
doc.options.simpleKeys = true
expect(String(doc)).toBe('null: null\n')
expect(String(doc)).toBe('~: ~\n')
})

test('key with block scalar value', () => {
Expand Down
15 changes: 9 additions & 6 deletions tests/doc/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ describe('json schema', () => {
})
expect(doc.errors).toHaveLength(2)
doc.errors = []
expect(String(doc)).toBe(
'"empty": null\n"canonical": null\n"english": null\n? null\n: "null key"\n'
expect(String(doc)).toBe(`"empty": null
"canonical": null
"english": null
? null
: "null key"\n`
)
})
})
Expand Down Expand Up @@ -178,9 +181,9 @@ english: null
'': 'null key'
})
expect(String(doc)).toBe(`empty: null
canonical: null
canonical: ~
english: null
null: null key\n`)
~: null key\n`)
})

describe('!!map', () => {
Expand Down Expand Up @@ -371,9 +374,9 @@ english: null
expect(String(doc)).toBe(`%YAML 1.1
---
empty: null
canonical: null
canonical: ~
english: null
null: null key\n`)
~: null key\n`)
})

describe('!!timestamp', () => {
Expand Down

0 comments on commit cc4456c

Please sign in to comment.