Skip to content

Commit

Permalink
fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
juunini committed Oct 12, 2023
1 parent 46870d0 commit 0bcf5c3
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 219 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ When receive JSON-LD,
{
"@context": "https://www.w3.org/ns/activitystreams",
"name": "juunini",
"type": "Person",
"id": "juunini"
}
```
Expand All @@ -30,6 +31,7 @@ is equals
{
"@context": "https://www.w3.org/ns/activitystreams",
"as:name": "juunini",
"type": "Person",
"@id": "juunini"
}
```
Expand All @@ -44,7 +46,12 @@ and it also equals
"@value": "juunini"
}
],
"@id": "juunini"
"@id": "juunini",
"https://www.w3.org/ns/activitystreams#type": [
{
"@value": "Person"
}
]
}
]
```
Expand Down Expand Up @@ -79,45 +86,39 @@ const jsonld = await JsonLDReader.parse({
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers"
}
],
"as:id": "https://mastodon.social/users/juunini",
"@id": "https://mastodon.social/users/juunini",
"as:type": "Person",
"as:url": "https://mastodon.social/@juunini",
"url": "https://mastodon.social/@juunini",
"as:image": {
"as:type": "Image",
"@type": "Image",
"as:mediaType": "image/png",
"url": "https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png"
},
"as:manuallyApprovesFollowers": "true"
"manuallyApprovesFollowers": "true"
})

const imageURL = jsonld
.setNamespace({ as: 'https://www.w3.org/ns/activitystreams' })
.read('as', 'image')
.read('as', 'url')
.stringOrThrow()
// https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png
const imageType = jsonld
.read('image')
.read('mediaType')
.stringOrElse('')
// image/png

const imageURL = jsonld
.read('image')
.read('url')
.stringOrThrow()
// https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png

const id = jsonld.read('@id').get()
// https://mastodon.social/users/juunini

const id = jsonld.read('id').get()
const id = jsonld.read('@id').get()
// https://mastodon.social/users/juunini

const type = jsonld.read('@type').get()
// Person

const type = jsonld.read('type').get()
const type = jsonld.read('@type').get()
// Person

const manuallyApprovesFollowers = jsonld
.setNamespace({ as: 'https://www.w3.org/ns/activitystreams' })
.read('as', 'manuallyApprovesFollowers')
.read('manuallyApprovesFollowers')
.booleanOrElse(false)
// true
```
Expand All @@ -130,8 +131,8 @@ JsonLDReader.parse (value: object | object[], options?: Options.Expand): Promise
.value [readonly]: unknown
.length [readonly]: number

.read (key: string | number): JsonLDReader
.read (namespace: string, key: string): JsonLDReader
.read (key: string): JsonLDReader
.read (index: number): JsonLDReader

.get (): unknown
.getOrThrow (error?: Error): unknown
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cloudmatelabs/jsonld-helper",
"version": "1.2.3",
"version": "1.3.0",
"description": "JSON-LD Helper",
"homepage": "/~https://github.com/cloudmatelabs/jsonld-helper-ts",
"repository": "cloudmatelabs/jsonld-helper-ts",
Expand Down
245 changes: 245 additions & 0 deletions src/reader/JsonLDReader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
import { describe, it, expect } from 'bun:test'

import { JsonLDReader } from './JsonLDReader'

describe('get', () => {
describe('when given key can not find', () => {
const givenKey = 'notFoundKey'

it('should return null', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read(givenKey).get()).toBeNull()
expect(jsonld.read(0).read(givenKey).get()).toBeNull()
})
})

describe('when given index, result is object', () => {
it('should return null', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read(0).read(0).get()).toBeNull()
})
})

describe('when given key can find', () => {
const givenKey = 'name'

it('should return value', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read(givenKey).get()).toBe('지상 최강의 개발자 쥬니니')
expect(jsonld.read(0).read(givenKey).get()).toBe('지상 최강의 개발자 쥬니니')
})
})

describe('when given key is preDefined key', () => {
it('should return value', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read('id').get()).toBe('acct:juunini@snippet.cloudmt.co.kr')
expect(jsonld.read('@id').get()).toBe('acct:juunini@snippet.cloudmt.co.kr')
expect(jsonld.read('type').get()).toBe('Person')
expect(jsonld.read('@type').get()).toBe('Person')
expect(jsonld.read(0).read('id').get()).toBe('acct:juunini@snippet.cloudmt.co.kr')
expect(jsonld.read(0).read('@id').get()).toBe('acct:juunini@snippet.cloudmt.co.kr')
expect(jsonld.read(0).read('type').get()).toBe('Person')
expect(jsonld.read(0).read('@type').get()).toBe('Person')
})
})

describe('when given index and key can find', () => {
it('should return value', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read('attachment').read(0).read('value').get()).toBe('juunini')
})
})
})

describe('getOrElse', () => {
describe('when given key can not find', () => {
const givenKey = 'notFoundKey'
const givenDefaultValue = 'defaultValue'

it('should return default value', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read(givenKey).getOrElse(givenDefaultValue)).toBe(givenDefaultValue)
expect(jsonld.read(0).read(givenKey).getOrElse(givenDefaultValue)).toBe(givenDefaultValue)
})
})

describe('when given key can find', () => {
const givenKey = 'name'
const givenDefaultValue = 'defaultValue'

it('should return value', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read(givenKey).getOrElse(givenDefaultValue)).toBe('지상 최강의 개발자 쥬니니')
expect(jsonld.read(0).read(givenKey).getOrElse(givenDefaultValue)).toBe('지상 최강의 개발자 쥬니니')
})
})

describe('when given index can not find', () => {
const givenIndex = 1
const givenDefaultValue = 'defaultValue'

it('should return default value', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read(givenIndex).getOrElse(givenDefaultValue)).toBe(givenDefaultValue)
expect(jsonld.read(0).read(0).getOrElse(givenDefaultValue)).toBe(givenDefaultValue)
})
})
})

describe('getOrThrow', () => {
describe('when given key can not find', () => {
const givenKey = 'notFoundKey'

it('should throw error', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(() => jsonld.read(givenKey).getOrThrow()).toThrow('Not found key: notFoundKey')
expect(() => jsonld.read(0).read(givenKey).getOrThrow()).toThrow('Not found key: notFoundKey')
})
})

describe('when given key can find', () => {
const givenKey = 'name'

it('should return value', async () => {
const jsonld = await JsonLDReader.parse(givenJsonLD, options)

expect(jsonld.read(givenKey).getOrThrow()).toBe('지상 최강의 개발자 쥬니니')
expect(jsonld.read(0).read(givenKey).getOrThrow()).toBe('지상 최강의 개발자 쥬니니')
})
})
})

describe('readme JSON-LD test', () => {
it('should return value', async () => {
const jsonld = await JsonLDReader.parse(readmeJsonLD, options)

expect(jsonld.read('id').get()).toBe('https://mastodon.social/users/juunini')
expect(jsonld.read('@id').get()).toBe('https://mastodon.social/users/juunini')
expect(jsonld.read('type').get()).toBe('Person')
expect(jsonld.read('@type').get()).toBe('Person')
expect(jsonld.read('url').get()).toBe('https://mastodon.social/@juunini')
expect(jsonld.read('image').read('type').get()).toBe('Image')
expect(jsonld.read('image').read('@type').get()).toBe('Image')
expect(jsonld.read('image').read('mediaType').get()).toBe('image/png')
expect(jsonld.read('image').read('url').get()).toBe('https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png')
expect(jsonld.read('manuallyApprovesFollowers').get()).toBe('true')
})
})

const options = {
documentLoader: async (url: string) => {
if (url === 'https://www.w3.org/ns/activitystreams') {
return {
document: activitystream,
documentUrl: url
}
}

if (url === 'http://schema.org') {
return {
document: schema,
documentUrl: url
}
}

return {
documentUrl: url,
document: {}
}
}
}

const givenJsonLD = {
'@context': [
'https://www.w3.org/ns/activitystreams',
{
schema: 'http://schema.org#',
PropertyValue: 'schema:PropertyValue',
value: 'schema:value'
}
],
'as:type': 'Person',
'@id': 'acct:juunini@snippet.cloudmt.co.kr',
name: '지상 최강의 개발자 쥬니니',
attachment: [
{
type: 'PropertyValue',
name: 'GitHub',
value: 'juunini'
}
]
}

const readmeJsonLD = {
'@context': [
'https://www.w3.org/ns/activitystreams',
{
manuallyApprovesFollowers: 'as:manuallyApprovesFollowers'
}
],
'@id': 'https://mastodon.social/users/juunini',
'as:type': 'Person',
url: 'https://mastodon.social/@juunini',
'as:image': {
'@type': 'Image',
'as:mediaType': 'image/png',
url: 'https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png'
},
manuallyApprovesFollowers: 'true'
}

const activitystream = {
'@context': {
'@vocab': '_:',
xsd: 'http://www.w3.org/2001/XMLSchema#',
as: 'https://www.w3.org/ns/activitystreams#',
ldp: 'http://www.w3.org/ns/ldp#',
vcard: 'http://www.w3.org/2006/vcard/ns#',
id: '@id',
type: '@type',
attachment: {
'@id': 'as:attachment',
'@type': '@id'
},
url: {
'@id': 'as:url',
'@type': '@id'
},
mediaType: 'as:mediaType',
name: 'as:name',
Image: 'as:Image',
Person: 'as:Person'
}
}

const schema = {
'@context': {
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
schema: 'https://schema.org/'
},
'@graph': [
{
'@id': 'schema:value',
'@type': 'rdf:Property'
},
{
'@id': 'schema:name',
'@type': 'rdf:Property'
},
{
'@id': 'schema:PropertyValue',
'@type': 'rdfs:Class'
}
]
}
12 changes: 7 additions & 5 deletions src/reader/JsonLDReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ export class JsonLDReader {
return JsonLDReader.of(data)
}

public strict (): JsonLDReader {
return JsonLDReader.of(this.value)
}

/**
* @param keyOrIndex
* @param key key of parsed JSON-LD value
* @returns JsonLDReader instance. if key is not found, returns `Nothing` instance.
*/
public read (key: string): JsonLDReader
/**
* @param index index of parsed JSON-LD value
* @returns JsonLDReader instance. if key is not found, returns `Nothing` instance.
*/
public read (index: number): JsonLDReader
public read (keyOrIndex: string | number): JsonLDReader {
return read({
jsonld: this.value,
Expand Down
Loading

0 comments on commit 0bcf5c3

Please sign in to comment.