-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from mizunashi-mana/fix-two-examples
Fix two examples
- Loading branch information
Showing
10 changed files
with
377 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: 'example-sonparser' | ||
version: '0.0.0' | ||
description: 'An example of sonparser' | ||
keywords: [ | ||
'example' | ||
'more' | ||
'complexible' | ||
] | ||
scripts: | ||
test: 'echo "not implements!" && exit 1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: 'example-sonparser' | ||
version: '0.0.0' | ||
description: 'An example of sonparser' | ||
keywords: [ | ||
'example' | ||
'more' | ||
'complexible' | ||
] | ||
scripts: | ||
test: 'echo "not implements!" && exit 1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "example-sonparser", | ||
"version": "0.0.0", | ||
"description": "An example of sonparser", | ||
"keywords": [ | ||
"example", | ||
"more", | ||
"complexible" | ||
], | ||
"scripts": { | ||
"test": "echo \"not implements!\" && exit 1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: 'example-sonparser' | ||
version: 0 | ||
description: 'An example of sonparser' | ||
keywords: [ | ||
'example' | ||
'more' | ||
'complexible' | ||
true | ||
] | ||
scripts: | ||
test: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: 'example-sonparser' | ||
version: '0.0.0' | ||
description: 'An example of sonparser', 'invalid!' | ||
keywords: [ | ||
'example' | ||
'more' | ||
'complexible' | ||
] | ||
scripts: | ||
test: 'echo "not implements!" && exit 1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This is normal text file. | ||
This is not son file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
const sparse = require("sonparser"); | ||
const assert = require("assert"); | ||
const path = require("path"); | ||
|
||
function resolvePath(pathStr) { | ||
return path.isAbsolute(pathStr) | ||
? pathStr | ||
: path.join(__dirname, pathStr) | ||
; | ||
} | ||
|
||
const myConfParser = sparse.hasProperties([ | ||
["private", sparse.boolean.option(false)], | ||
["name", sparse.string], | ||
["version", sparse.string], | ||
["description", sparse.string], | ||
["keywords", sparse.array(sparse.string).option([])], | ||
["scripts", sparse.object], | ||
["repository", sparse.hasProperties([ | ||
["type", sparse.string], | ||
["url", sparse.string], | ||
]).option({ | ||
"type": "git", | ||
"url": "", | ||
})], | ||
]).desc("failed to parse my config", "my config"); | ||
|
||
// pattern throw Error on failure | ||
assert.deepEqual( | ||
sparse.parseFile(resolvePath("datas/config.json"), myConfParser), | ||
{ | ||
"private": false, | ||
"name": "example-sonparser", | ||
"version": "0.0.0", | ||
"description": "An example of sonparser", | ||
"keywords": [ | ||
"example", | ||
"more", "complexible", | ||
], | ||
"scripts": { | ||
"test": "echo \"not implements!\" && exit 1", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "", | ||
}, | ||
} | ||
); // success | ||
|
||
|
||
assert.deepEqual( | ||
sparse.parseFile(resolvePath("datas/config.cson"), myConfParser), | ||
{ | ||
"private": false, | ||
"name": "example-sonparser", | ||
"version": "0.0.0", | ||
"description": "An example of sonparser", | ||
"keywords": [ | ||
"example", | ||
"more", "complexible", | ||
], | ||
"scripts": { | ||
"test": "echo \"not implements!\" && exit 1", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "", | ||
}, | ||
} | ||
); // success | ||
|
||
/** | ||
* Unusual extension is also ok. | ||
* It will be tryed by all son format. | ||
*/ | ||
assert.deepEqual( | ||
sparse.parseFile(resolvePath("datas/config.conf"), myConfParser), | ||
{ | ||
"private": false, | ||
"name": "example-sonparser", | ||
"version": "0.0.0", | ||
"description": "An example of sonparser", | ||
"keywords": [ | ||
"example", | ||
"more", "complexible", | ||
], | ||
"scripts": { | ||
"test": "echo \"not implements!\" && exit 1", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "", | ||
}, | ||
} | ||
); // success | ||
|
||
/** | ||
* Type check error or convert missing error will be thrown. | ||
* You can receive and output error message. | ||
*/ | ||
try { | ||
sparse.parseFile(resolvePath("datas/illconf.cson"), myConfParser); | ||
assert(false, "Don't reach here!"); | ||
} catch (e) { | ||
assert(e instanceof sparse.ConfigParseError); | ||
console.log(`Got Error: ${e.message}`); | ||
} | ||
|
||
/** | ||
* errno error will be recept | ||
*/ | ||
assert.throws( | ||
() => sparse.parseFile("not exists file!", myConfParser), | ||
Error | ||
); // failure | ||
|
||
/** | ||
* parse format error will be recept. | ||
*/ | ||
assert.throws( | ||
() => sparse.parseFile(resolvePath("datas/invalid.cson"), myConfParser), | ||
Error | ||
); // failure | ||
|
||
/** | ||
* not son file will also receive parse format error. | ||
*/ | ||
assert.throws( | ||
() => sparse.parseFile(resolvePath("datas/normal.txt"), myConfParser), | ||
Error | ||
); // failure | ||
|
||
// you can use with status method. | ||
const resultConfig = sparse.parseFileWithStatus(resolvePath("datas/config.conf"), myConfParser); | ||
assert.strictEqual(resultConfig.status, true); | ||
assert.deepEqual( | ||
resultConfig.value, | ||
{ | ||
"private": false, | ||
"name": "example-sonparser", | ||
"version": "0.0.0", | ||
"description": "An example of sonparser", | ||
"keywords": [ | ||
"example", | ||
"more", "complexible", | ||
], | ||
"scripts": { | ||
"test": "echo \"not implements!\" && exit 1", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "", | ||
}, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
const sparse = require("sonparser"); | ||
const assert = require("assert"); | ||
|
||
const nestConsoleReporter = sparse.Reporters.nestReporter(console.log); | ||
|
||
/** | ||
* check boolean | ||
*/ | ||
const booleanParser = sparse.boolean; | ||
|
||
const resultCheckBoolean = booleanParser.parse(true); | ||
assert.strictEqual( | ||
resultCheckBoolean, | ||
true | ||
); | ||
|
||
assert.throws( | ||
() => booleanParser.parse("not boolean!"), | ||
sparse.ConfigParseError | ||
); | ||
|
||
// if you don't want to use try-catch | ||
const resultCheckBooleanSafetySuccess = booleanParser.parseWithStatus(false); | ||
assert.strictEqual( | ||
resultCheckBooleanSafetySuccess.status, | ||
true | ||
); | ||
assert.strictEqual( | ||
resultCheckBooleanSafetySuccess.value, | ||
false | ||
); | ||
|
||
const resultCheckBooleanSafetyFailure = booleanParser.parseWithStatus("not boolean!"); | ||
assert.strictEqual( | ||
resultCheckBooleanSafetyFailure.status, | ||
false | ||
); | ||
|
||
// you can see more human readable message by using reporters | ||
/** | ||
* Output: | ||
* this : "not boolean!" is not 'boolean' | ||
*/ | ||
try { | ||
booleanParser.parseWithReporter("not boolean!", nestConsoleReporter); | ||
} catch (e) { | ||
// catch Error | ||
} | ||
|
||
/** | ||
* check and convert my object | ||
*/ | ||
const myObjectParser = sparse.hasProperties([ | ||
["private", sparse.boolean.option(false)], | ||
["name", sparse.string], | ||
["version", sparse.string], | ||
["description", sparse.string], | ||
["keywords", sparse.array(sparse.string).option([])], | ||
["scripts", sparse.object], | ||
["repository", sparse.hasProperties([ | ||
["type", sparse.string], | ||
["url", sparse.string], | ||
]).option({ | ||
"type": "git", | ||
"url": "", | ||
})], | ||
]); | ||
|
||
const resultCheckMyObject = myObjectParser.parse({ | ||
"name": "example-sonparser", | ||
"version": "0.0.0", | ||
"description": "An example of sonparser", | ||
"keywords": [ | ||
"example", | ||
"more", "complexible", | ||
], | ||
"scripts": { | ||
"test": "echo \"not implements!\" && exit 1", | ||
}, | ||
}); | ||
assert.deepEqual( | ||
resultCheckMyObject, | ||
{ | ||
"private": false, | ||
"name": "example-sonparser", | ||
"version": "0.0.0", | ||
"description": "An example of sonparser", | ||
"keywords": [ | ||
"example", | ||
"more", "complexible", | ||
], | ||
"scripts": { | ||
"test": "echo \"not implements!\" && exit 1", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "", | ||
}, | ||
} | ||
); | ||
|
||
assert.throws( | ||
() => myObjectParser.parse("not my object!"), | ||
sparse.ConfigParseError | ||
); | ||
|
||
// you can see more human readable message by using reporters | ||
/** | ||
* Output: | ||
* this : failed to parse elem of 'object' | ||
* ├── .private : "not boolean!" is not 'boolean' | ||
* ├── .version : 0 is not 'string' | ||
* ├─┬ .keywords : failed to parse elem of 'array' | ||
* │ └── [1] : true is not 'string' | ||
* ├── .scripts : undefined is not 'object' | ||
* └─┬ .repository : failed to parse elem of 'object' | ||
* └── .type : 0 is not 'string' | ||
*/ | ||
try { | ||
myObjectParser.parseWithReporter({ | ||
"private": "not boolean!", | ||
"name": "example-sonparser", | ||
"version": 0, | ||
"description": "An example of sonparser", | ||
"keywords": [ | ||
"example", | ||
true, | ||
], | ||
"repository": { | ||
"type": 0, | ||
"url": "", | ||
}, | ||
}, nestConsoleReporter); | ||
} catch (e) { | ||
// catch Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.