|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { extractValueOfKeyByPattern } from './pattern-extractor' |
| 4 | + |
| 5 | +describe('json key-value pattern extractor', () => { |
| 6 | + it('should extract values correctly', async () => { |
| 7 | + const testString = JSON.stringify({ |
| 8 | + a: 'this is a test string with a "quote" and a newline\ncharacter', |
| 9 | + b: 123, |
| 10 | + c: true, |
| 11 | + d: null, |
| 12 | + e: false, |
| 13 | + }) |
| 14 | + const a = extractValueOfKeyByPattern(testString, 'a') |
| 15 | + expect(a).toBe('this is a test string with a "quote" and a newline\ncharacter') |
| 16 | + const b = extractValueOfKeyByPattern(testString, 'b') |
| 17 | + expect(b).toBe(123) |
| 18 | + const c = extractValueOfKeyByPattern(testString, 'c') |
| 19 | + expect(c).toBe(true) |
| 20 | + const d = extractValueOfKeyByPattern(testString, 'd') |
| 21 | + expect(d).toBe(null) |
| 22 | + const e = extractValueOfKeyByPattern(testString, 'e') |
| 23 | + expect(e).toBe(false) |
| 24 | + const undefinedKey = extractValueOfKeyByPattern(testString, 'undefinedKey') |
| 25 | + expect(undefinedKey).toBe(undefined) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should extract values correctly for pretty-printed JSON', async () => { |
| 29 | + const testString = JSON.stringify({ |
| 30 | + a: 'this is a test string with a "quote" and a newline\ncharacter', |
| 31 | + b: 123, |
| 32 | + c: true, |
| 33 | + d: null, |
| 34 | + e: false, |
| 35 | + f: { |
| 36 | + nested: 'value', |
| 37 | + }, |
| 38 | + }, null, 2) |
| 39 | + const a = extractValueOfKeyByPattern(testString, 'a') |
| 40 | + expect(a).toBe('this is a test string with a "quote" and a newline\ncharacter') |
| 41 | + const b = extractValueOfKeyByPattern(testString, 'b') |
| 42 | + expect(b).toBe(123) |
| 43 | + const c = extractValueOfKeyByPattern(testString, 'c') |
| 44 | + expect(c).toBe(true) |
| 45 | + const d = extractValueOfKeyByPattern(testString, 'd') |
| 46 | + expect(d).toBe(null) |
| 47 | + const e = extractValueOfKeyByPattern(testString, 'e') |
| 48 | + expect(e).toBe(false) |
| 49 | + const f = extractValueOfKeyByPattern(testString, 'f') |
| 50 | + expect(f).toEqual(undefined) |
| 51 | + const nested = extractValueOfKeyByPattern(testString, 'nested') |
| 52 | + expect(nested).toBe('value') |
| 53 | + }) |
| 54 | +}) |
0 commit comments