1
- const Cjson = require ( 'coded-json' ) . Cjson ;
2
1
const assert = require ( 'assert' ) ;
3
2
const fs = require ( 'fs' ) ;
4
3
const path = require ( 'path' ) ;
4
+ const Cjson = require ( "../../out/cjson" ) . Cjson ;
5
+ const jasmine = require ( "jasmine" ) ;
5
6
7
+ const basePath = path . join ( __dirname , ".." , ".." , ".." , "tests" , "test-files" ) ;
6
8
/** Path to target.cjson */
7
- const cjsonfilePath = path . join ( __dirname , ".." , ".." , ".." , "\\test-files\\ target.cjson") ;
9
+ const cjsonfilePath = path . join ( basePath , "target.cjson" ) ;
8
10
/** Path to source.json */
9
- const jsonfilePath = path . join ( __dirname , ".." , ".." , ".." , "\\test-files\\ source.json") ;
11
+ const jsonfilePath = path . join ( basePath , "source.json" ) ;
10
12
/** Path to pure.json */
11
- const pureJsonfilePath = path . join ( __dirname , ".." , ".." , ".." , "\\test-files\\ pure.json") ;
13
+ const pureJsonfilePath = path . join ( basePath , "pure.json" ) ;
12
14
/** Path to relativeTargetCjson.json */
13
- const relativeTargetCjson = path . join ( __dirname , ".." , ".." , ".." , "\\test-files\\ targetRelativeCalls.cjson") ;
15
+ const relativeTargetCjson = path . join ( basePath , "targetRelativeCalls.cjson" ) ;
14
16
/** Path to relativeTargetCjson.json */
15
- const VariableInjection = path . join ( __dirname , ".." , ".." , ".." , "\\test-files\\ VariableInjection.cjson") ;
17
+ const VariableInjection = path . join ( basePath , "VariableInjection.cjson" ) ;
16
18
17
19
/**
18
20
* Tests related to CJSON files
19
21
*/
20
22
describe ( "CJSON Test 1" , ( ) => {
21
-
22
23
it ( "I should be able to import pure JSON files" , ( ) => {
23
24
var cjson = new Cjson ( pureJsonfilePath ) ;
24
25
var pureJSONContent = cjson . deserialize ( ) ;
@@ -48,10 +49,7 @@ describe("CJSON Test 1", () => {
48
49
49
50
it ( "I should be able to deserialize relative path to local variable" , ( ) => {
50
51
var cjson = new Cjson ( relativeTargetCjson ) ;
51
-
52
52
var decodedJSON = cjson . deserialize ( ) ;
53
-
54
- console . log ( decodedJSON ) ;
55
53
56
54
assert . equal ( decodedJSON . target . digitCheck , cjson . json . parse ( "target.digitCheck" ) ) ;
57
55
assert . equal ( decodedJSON . target . digitImport , cjson . json . parse ( "target.digitImport" ) ) ;
@@ -70,9 +68,8 @@ describe("CJSON Test 1", () => {
70
68
injectedData : "jsonInjectionValue"
71
69
}
72
70
} ;
73
- var deserializedVal = cjson . inject ( injectObj ) ;
71
+ var deserializedVal = cjson . inject ( injectObj ) . deserialize ( ) ;
74
72
75
- console . log ( deserializedVal ) ;
76
73
assert . equal ( deserializedVal . target . fruit , injectObj . fruit ) ;
77
74
assert . equal ( JSON . stringify ( deserializedVal . jsonInjection ) , JSON . stringify ( injectObj . jsonTypeData ) )
78
75
} ) ;
@@ -115,44 +112,32 @@ describe("CJSON Test 1", () => {
115
112
assert . equal ( cjson . deserialize ( ) . quiz . sport . q1 , undefined ) ;
116
113
} ) ;
117
114
118
- it ( "I should be able to stringify JSON object using toString function " , ( ) => {
115
+ it ( "I should be able to stringify JSON object using toString() " , ( ) => {
119
116
var fileContent = fs . readFileSync ( pureJsonfilePath ) . toJSON ( ) ;
120
117
var stringContent = Cjson . toString ( fileContent ) ;
121
118
assert . equal ( JSON . stringify ( fileContent ) , stringContent ) ;
122
119
} ) ;
123
- } ) ;
124
-
125
- /**
126
- * Tests related to native JSON files
127
- */
128
- describe ( "JSON Test 2" , ( ) => {
129
-
130
- it ( "I should be able to use isContentJson()" , ( ) => {
131
- var cjson = new Cjson ( pureJsonfilePath ) ;
132
- assert . equal ( cjson . isContentJson ( ) , true ) ;
133
- } ) ;
134
120
135
- it ( "I should be able to parse jpath using `obj< Cjson >.json.parse(\"Valid.JPATH\")` " , ( ) => {
121
+ it ( "I should be able to replace a value in JSON context using replace() " , ( ) => {
136
122
var cjson = new Cjson ( cjsonfilePath ) ;
137
- cjson . deserialize ( ) ;
138
-
139
- var value = cjson . json . parse ( "$.source.pure.quiz.sport.q1.question" ) ;
140
- assert . equal ( value , "Which one is correct team name in NBA?" ) ;
141
- } ) ;
123
+ cjson = cjson . replace ( "$.source.pure.quiz.sport.q1.answer" , "Los Angeles Kings" ) ;
142
124
143
- it ( "I should be able to parse full json using `obj< Cjson >.json.parse()`" , ( ) => {
144
- var cjson = new Cjson ( cjsonfilePath ) ;
145
- cjson . deserialize ( ) ;
125
+ assert . equal ( cjson . json . parse ( "$.source.pure.quiz.sport.q1.answer" ) , "Los Angeles Kings" ) ;
126
+ } ) ;
146
127
147
- var value = JSON . stringify ( cjson . json . parse ( ) ) ;
148
- assert . equal ( value , JSON . stringify ( cjson . deserialize ( ) ) ) ;
128
+ it ( "I should be able to inject null value during runtime" , ( ) => {
129
+ var cjson = new Cjson ( VariableInjection ) ;
130
+ cjson = cjson . inject ( {
131
+ "fruit" : null
132
+ } ) ;
133
+ assert . equal ( cjson . deserialize ( ) . target . fruit , null , "Null value injection is successfull" ) ;
149
134
} ) ;
150
135
151
- it ( "I should be able to replace a value using replace() " , ( ) => {
152
- var cjson = new Cjson ( cjsonfilePath ) ;
153
- cjson . deserialize ( ) ;
154
- var jPath = "$.source.pure.quiz.sport.q1.question"
155
- cjson = cjson . replace ( jPath , "New question" ) ;
156
- assert . equal ( cjson . json . parse ( jPath ) , "New question ") ;
136
+ it ( "I should be able to inject array " , ( ) => {
137
+ var cjson = new Cjson ( VariableInjection ) ;
138
+ cjson = cjson . inject ( {
139
+ "fruit" : [ "Orange" , "Apple" , "Banana" ]
140
+ } ) ;
141
+ assert . equal ( cjson . deserialize ( ) . target . fruit . length , 3 , "Null value injection is successfull ") ;
157
142
} ) ;
158
143
} ) ;
0 commit comments