Skip to content

Commit e9bd6c3

Browse files
committed
Added tests for validations of values against specs.
1 parent e07b44b commit e9bd6c3

14 files changed

+704
-14
lines changed

src/OpenSpec/Spec/Spec.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import ParseSpecException from "../ParseSpecException";
2-
import SpecLibrary from "../SpecLibrary";
3-
import Entity from "../Entity";
42

53

64
class Spec
@@ -14,7 +12,7 @@ class Spec
1412
return this._library;
1513
}
1614

17-
validate(value,throwExceptionOnInvalid = false)
15+
validate(value, throwExceptionOnInvalid = false)
1816
{
1917
let errors = [];
2018

@@ -24,7 +22,7 @@ class Spec
2422

2523
} catch (ex) {
2624

27-
if (!(err instanceof ParseSpecException)) {
25+
if (!(ex instanceof ParseSpecException)) {
2826
throw new Error('Unexpected exception.');
2927
}
3028

src/OpenSpec/Spec/Type/NullSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class NullSpec extends TypeSpec
2525

2626
parse(value)
2727
{
28-
errors = [];
28+
let errors = [];
2929

3030
if (value !== null) {
3131
errors.push([ParseSpecException.CODE_NULL_EXPECTED, "Expected null value for 'null' type spec, but " + (typeof value) + " given."]);

src/OpenSpec/Spec/Type/RefSpec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ class RefSpec extends TypeSpec
4141
}
4242

4343
parse(value)
44-
{/*
45-
$errors = [];
44+
{
45+
let errors = [];
4646

47-
if (!$this->_library->hasSpec($this->_specName)) {
48-
$errors[] = [ParseSpecException::CODE_UNDEFINED_NAMED_SPEC, "Undefined named spec '" . $this->_specName . "'."];
49-
throw new ParseSpecException('Could not parse the value', ParseSpecException::CODE_MULTIPLE_PARSER_ERROR, $errors);
47+
if (!this._library.hasSpec(this._specName)) {
48+
errors.push([ParseSpecException.CODE_UNDEFINED_NAMED_SPEC, "Undefined named spec '" + this._specName + "'."]);
49+
throw new ParseSpecException('Could not parse the value', ParseSpecException.CODE_MULTIPLE_PARSER_ERROR, errors);
5050
}
5151

52-
return $this->_library->getSpec($this->_specName)->parse($value);*/
52+
return this._library.getSpec(this._specName).parse(value);
5353
}
5454
}
5555

src/OpenSpec/Spec/Type/TypeSpec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use OpenSpec\Spec\Spec;*/
33
import SpecBuilder from "../../SpecBuilder";
44
import SpecLibrary from "../../SpecLibrary";
55
import ParseSpecException from "../../ParseSpecException";
6+
import Spec from "../Spec";
67
// import ObjectSpec from "./ObjectSpec";
78

89

@@ -13,10 +14,12 @@ const array_diff = function (array1, array2) {
1314
});
1415
};
1516

16-
class TypeSpec //extends Spec
17+
class TypeSpec extends Spec
1718
{
1819
constructor(specData, library)
1920
{
21+
super();
22+
2023
this._initValues();
2124

2225
this._anySpec = null;

src/OpenSpec/SpecLibrary.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SpecLibrary
5555
{
5656
this._specs = [];
5757
}
58-
/*
58+
5959
getSpec(name)
6060
{
6161
if (!this.hasSpec(name)) {
@@ -64,7 +64,7 @@ class SpecLibrary
6464

6565
return this._specs[name];
6666
}
67-
67+
/*
6868
validateValue(specName, value)
6969
{
7070
errors = this.validateValueGetErrors(specName, value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import assert from 'assert';
2+
3+
import SpecBuilder from "../../../../src/OpenSpec/SpecBuilder";
4+
import SpecLibrary from "../../../../src/OpenSpec/SpecLibrary";
5+
6+
7+
function getSpecInstance()
8+
{
9+
let specData = { type: 'array', items: { type: 'string' } };
10+
let spec = SpecBuilder.getInstance().build(specData, new SpecLibrary());
11+
12+
return spec;
13+
}
14+
15+
function getValidValueInstance()
16+
{
17+
return ['one', 'two', 'three'];
18+
}
19+
20+
function getInvalidValueInstance()
21+
{
22+
return [1, 2, 3];
23+
}
24+
25+
describe('ArrayValidationTest', function() {
26+
27+
it('testValidValue', function() {
28+
let spec = getSpecInstance();
29+
let value = getValidValueInstance();
30+
31+
let result = spec.validate(value);
32+
33+
assert(result, "Given value not recognized by the spec, even when it should.");
34+
});
35+
36+
it('testInvalidValue', function() {
37+
let spec = getSpecInstance();
38+
let value = getInvalidValueInstance();
39+
40+
let result = spec.validate(value);
41+
42+
assert(!result, "Given value recognized by the spec, even when it should not.");
43+
});
44+
45+
it('testSeveralValidations', function() {
46+
47+
let specData, value, spec, result;
48+
49+
// 1 ------------------------------------------------
50+
specData = {
51+
'type': 'array',
52+
'items': {
53+
'type': 'array',
54+
'items': {
55+
'type': 'mixed',
56+
'options': [
57+
{'type':'string'},
58+
{'type':'boolean'}
59+
]
60+
}
61+
}
62+
};
63+
64+
value = [
65+
[true, false, true],
66+
['string', 'value', false],
67+
[],
68+
['other', 'another one']
69+
];
70+
71+
spec = SpecBuilder.getInstance().build(specData, new SpecLibrary());
72+
result = spec.validate(value);
73+
74+
assert(result, "Not validated array of arrays of string|boolean.");
75+
// End: 1 ------------------------------------------------
76+
77+
// 2 ------------------------------------------------
78+
specData = {
79+
'type': 'object',
80+
'extensible': true,
81+
'fields': {
82+
'name': {'type': 'string'},
83+
'happy': {'type': 'boolean'},
84+
'age': {'type': 'string'},
85+
'hobbies': {
86+
'type': 'array',
87+
'items': {
88+
'type': 'string'
89+
}
90+
},
91+
'address': {
92+
'type': 'object',
93+
'fields': {
94+
'country': {'type': 'string'},
95+
'city': {'type': 'string'},
96+
'phones': {'type': 'array', 'items': {'type': 'string'}},
97+
}
98+
},
99+
}
100+
};
101+
102+
value = {
103+
'name': 'Daniel',
104+
'happy': true,
105+
'age': '37',
106+
'hobbies': ['numismatics', 'rubik cubes'],
107+
'address': {
108+
'country': 'Argentina',
109+
'city': 'Tandil',
110+
'phones': []
111+
},
112+
'comments': ''
113+
};
114+
115+
spec = SpecBuilder.getInstance().build(specData, new SpecLibrary());
116+
result = spec.validate(value);
117+
118+
assert(result, "Not validated person info.");
119+
// End: 2 ------------------------------------------------
120+
121+
// 3 ------------------------------------------------
122+
specData = {'type' : 'array'};
123+
let values = [
124+
[], // Empty array
125+
[false, true, true], // Boolean array
126+
["hello", "bye"], // String array
127+
[null, null, null], // Null array
128+
[{'name' : 'Daniel', 'alias' : 'Dani'}, {'a': 1, 'b': [2, 3]}], // Object array
129+
[[], {'field' : 'value'}, [null, false, "hi!"]], // Array of arrays (bidimensional array / matrix)
130+
];
131+
132+
spec = SpecBuilder.getInstance().build(specData, new SpecLibrary());
133+
for (let i = 0; i < values.length; i++) {
134+
let value = values[i];
135+
136+
result = spec.validate(value);
137+
assert(result, "Not validated array.");
138+
}
139+
140+
let complexValue = values;
141+
result = spec.validate(complexValue);
142+
assert(result, "Not validated array of any type of items.");
143+
// End: 3 ------------------------------------------------
144+
});
145+
146+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import assert from 'assert';
2+
3+
import SpecBuilder from "../../../../src/OpenSpec/SpecBuilder";
4+
import BooleanSpec from "../../../../src/OpenSpec/Spec/Type/BooleanSpec";
5+
import TypeSpec from "../../../../src/OpenSpec/Spec/Type/TypeSpec";
6+
import ParseSpecException from "../../../../src/OpenSpec/ParseSpecException";
7+
import SpecLibrary from "../../../../src/OpenSpec/SpecLibrary";
8+
9+
10+
function getSpecInstance()
11+
{
12+
let specData = { type: 'boolean' };
13+
let spec = SpecBuilder.getInstance().build(specData, new SpecLibrary());
14+
15+
return spec;
16+
}
17+
function getValidValueInstance()
18+
{
19+
return false;
20+
}
21+
22+
function getInvalidValueInstance()
23+
{
24+
return 'a string, invalid boolean value';
25+
}
26+
27+
28+
describe('BooleanValidationTest', function() {
29+
30+
it('testValidValue', function() {
31+
let spec = getSpecInstance();
32+
let value = getValidValueInstance();
33+
34+
let result = spec.validate(value);
35+
36+
assert(result, "Given value not recognized by the spec, even when it should.");
37+
});
38+
39+
it('testInvalidValue', function() {
40+
let spec = getSpecInstance();
41+
let value = getInvalidValueInstance();
42+
43+
let result = spec.validate(value);
44+
45+
assert(!result, "Given value recognized by the spec, even when it should not.");
46+
});
47+
48+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import assert from 'assert';
2+
3+
import SpecBuilder from "../../../../src/OpenSpec/SpecBuilder";
4+
import ParseSpecException from "../../../../src/OpenSpec/ParseSpecException";
5+
import SpecLibrary from "../../../../src/OpenSpec/SpecLibrary";
6+
7+
8+
function getSpecInstance()
9+
{
10+
let specData = { type: 'float' };
11+
let spec = SpecBuilder.getInstance().build(specData, new SpecLibrary());
12+
13+
return spec;
14+
}
15+
16+
function getValidValueInstance()
17+
{
18+
return 3.14159265359;
19+
}
20+
21+
function getInvalidValueInstance()
22+
{
23+
return 3;
24+
}
25+
26+
27+
describe('FloatValidationTest', function() {
28+
29+
// @todo modify similar tests to use validateGetErrors instead of validate
30+
it('testValidValue', function() {
31+
let spec = getSpecInstance();
32+
let value = getValidValueInstance();
33+
34+
let errors = [];
35+
try {
36+
spec.parse(value);
37+
} catch (ex) {
38+
39+
if (!(ex instanceof ParseSpecException)) {
40+
throw new Error('Unexpected exception.');
41+
}
42+
43+
errors = ex._errors; // @todo ->getErrors();
44+
}
45+
46+
errors = errors.map(elem => { return elem[1]; });
47+
let msg = '- ' + errors.join("\n- ");
48+
assert(errors.length === 0, "Given value not recognized by the spec, even when it should.\n" + msg);
49+
});
50+
51+
it('testInvalidValue', function() {
52+
let spec = getSpecInstance();
53+
let value = getInvalidValueInstance();
54+
55+
let result = spec.validate(value);
56+
57+
assert(!result, "Given value recognized by the spec, even when it should not.");
58+
});
59+
});

0 commit comments

Comments
 (0)