Syntax update.
Replace assigment attempt with object test.
This commit is contained in:
@@ -1,74 +1,76 @@
|
|||||||
class TEST_DS
|
|
||||||
|
note
|
||||||
|
description: "Linked list and hash table converters test."
|
||||||
|
date: "$Date$"
|
||||||
|
revision: "$Revision$"
|
||||||
|
|
||||||
|
class
|
||||||
|
TEST_DS
|
||||||
|
|
||||||
inherit
|
inherit
|
||||||
SHARED_EJSON
|
SHARED_EJSON
|
||||||
rename default_create as shared_default_create end
|
undefine
|
||||||
EQA_TEST_SET
|
default_create
|
||||||
select default_create end
|
end
|
||||||
|
|
||||||
|
EQA_TEST_SET
|
||||||
|
|
||||||
feature -- Test
|
feature -- Test
|
||||||
|
|
||||||
test_linked_list_converter
|
test_linked_list_converter
|
||||||
|
-- Convert a linked list to a json value and
|
||||||
|
-- convert this one to a linked list.
|
||||||
local
|
local
|
||||||
jc: JSON_LINKED_LIST_CONVERTER
|
|
||||||
l: LINKED_LIST [STRING]
|
l: LINKED_LIST [STRING]
|
||||||
l2: detachable LINKED_LIST [detachable ANY]
|
|
||||||
s: STRING
|
s: STRING
|
||||||
jv: detachable JSON_VALUE
|
|
||||||
do
|
do
|
||||||
create jc.make
|
|
||||||
json.add_converter (jc)
|
|
||||||
create l.make
|
create l.make
|
||||||
s := "foo"
|
l.force ("foo")
|
||||||
l.force (s)
|
l.force ("bar")
|
||||||
s := "bar"
|
|
||||||
l.force (s)
|
if attached json.value (l) as l_value then
|
||||||
jv := json.value (l)
|
s := l_value.representation
|
||||||
assert ("jv /= Void", jv /= Void)
|
assert ("JSON array converted to LINKED_LIST", attached {LINKED_LIST [detachable ANY]} json.object (l_value, "LINKED_LIST"))
|
||||||
if attached jv as l_jv then
|
else
|
||||||
s := jv.representation
|
assert ("LINKED_LIST converted to a JSON value", False)
|
||||||
l2 ?= json.object (jv, "LINKED_LIST")
|
|
||||||
assert ("l2 /= Void", l2 /= Void)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test_hash_table_converter
|
test_hash_table_converter
|
||||||
|
-- Convert a hash table to a json value and
|
||||||
|
-- convert this one to a hash table.
|
||||||
local
|
local
|
||||||
tc: JSON_HASH_TABLE_CONVERTER
|
|
||||||
t: HASH_TABLE [STRING, STRING]
|
t: HASH_TABLE [STRING, STRING]
|
||||||
t2: detachable HASH_TABLE [ANY, HASHABLE]
|
|
||||||
s: STRING
|
s: STRING
|
||||||
ucs_key, ucs_value: detachable STRING_32
|
l_ucs_key: detachable STRING_32
|
||||||
jv: detachable JSON_VALUE
|
|
||||||
do
|
do
|
||||||
create tc.make
|
|
||||||
json.add_converter (tc)
|
|
||||||
create t.make (2)
|
create t.make (2)
|
||||||
t.put ("foo", "1")
|
t.put ("foo", "1")
|
||||||
t.put ("bar", "2")
|
t.put ("bar", "2")
|
||||||
jv := json.value (t)
|
|
||||||
assert ("jv /= Void", jv /= Void)
|
|
||||||
if attached jv as l_jv then
|
|
||||||
s := l_jv.representation
|
|
||||||
t2 ?= json.object (l_jv, "HASH_TABLE")
|
|
||||||
assert ("t2 /= Void", t2 /= Void)
|
|
||||||
end
|
|
||||||
create ucs_key.make_from_string ("1")
|
|
||||||
if attached t2 as l_t2 then
|
|
||||||
ucs_value ?= t2 @ ucs_key
|
|
||||||
assert ("ucs_value /= Void", ucs_value /= Void)
|
|
||||||
if attached ucs_value as l_ucs_value then
|
|
||||||
assert ("ucs_value.string.is_equal (%"foo%")", l_ucs_value.string.is_equal ("foo"))
|
|
||||||
end
|
|
||||||
create ucs_key.make_from_string ("2")
|
|
||||||
ucs_value ?= t2 @ ucs_key
|
|
||||||
assert ("ucs_value /= Void", ucs_value /= Void)
|
|
||||||
if attached ucs_value as l_ucs_value then
|
|
||||||
assert ("ucs_value.string.is_equal (%"bar%")", l_ucs_value.string.is_equal ("bar"))
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
if attached json.value (t) as l_value then
|
||||||
|
s := l_value.representation
|
||||||
|
if attached {HASH_TABLE [ANY, HASHABLE]} json.object (l_value, "HASH_TABLE") as t2 then
|
||||||
|
|
||||||
|
create l_ucs_key.make_from_string ("1")
|
||||||
|
if attached {STRING_32} t2 [l_ucs_key] as l_ucs_value then
|
||||||
|
assert ("ucs_value.string.is_equal (%"foo%")", l_ucs_value.string.is_equal ("foo"))
|
||||||
|
else
|
||||||
|
assert ("ucs_value /= Void", False)
|
||||||
|
end
|
||||||
|
|
||||||
|
create l_ucs_key.make_from_string ("2")
|
||||||
|
if attached {STRING_32} t2 [l_ucs_key] as l_ucs_value then
|
||||||
|
assert ("ucs_value.string.is_equal (%"bar%")", l_ucs_value.string.is_equal ("bar"))
|
||||||
|
else
|
||||||
|
assert ("ucs_value /= Void", False)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
assert ("JSON object converted to HASH_TABLE", False);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
assert ("HASH_TABLE converted to a JSON value", False)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end -- class TEST_DS
|
end -- class TEST_DS
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
class TEST_JSON_CORE
|
class
|
||||||
|
TEST_JSON_CORE
|
||||||
|
|
||||||
inherit
|
inherit
|
||||||
SHARED_EJSON
|
SHARED_EJSON
|
||||||
rename default_create as shared_default_create end
|
undefine
|
||||||
|
default_create
|
||||||
|
end
|
||||||
|
|
||||||
EQA_TEST_SET
|
EQA_TEST_SET
|
||||||
select default_create end
|
|
||||||
|
|
||||||
feature -- Test
|
feature -- Test
|
||||||
|
|
||||||
@@ -21,7 +24,7 @@ feature -- Test
|
|||||||
assert ("jn.representation.same_string (%"42%")", jn.representation.same_string ("42"))
|
assert ("jn.representation.same_string (%"42%")", jn.representation.same_string ("42"))
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||||
if attached {JSON_NUMBER} json.value (i) as l_jn then
|
if attached {JSON_NUMBER} json.value (i) as l_jn then
|
||||||
assert ("l_jn.representation.same_string (%"42%")", jn.representation.same_string ("42"))
|
assert ("l_jn.representation.same_string (%"42%")", jn.representation.same_string ("42"))
|
||||||
else
|
else
|
||||||
assert ("json.value (i) is a JSON_NUMBER", False)
|
assert ("json.value (i) is a JSON_NUMBER", False)
|
||||||
end
|
end
|
||||||
@@ -477,64 +480,60 @@ feature -- Test
|
|||||||
|
|
||||||
test_json_null
|
test_json_null
|
||||||
local
|
local
|
||||||
a: detachable ANY
|
|
||||||
dummy_object: STRING
|
|
||||||
jn: detachable JSON_NULL
|
|
||||||
jrep: STRING
|
jrep: STRING
|
||||||
|
jn: JSON_NULL
|
||||||
parser: JSON_PARSER
|
parser: JSON_PARSER
|
||||||
do
|
do
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
-- Eiffel value -> JSON value -> JSON representation
|
||||||
create jn
|
create jn
|
||||||
assert ("jn /= Void", jn /= Void)
|
jrep := "null"
|
||||||
assert ("jn.representation.is_equal (%"%"null%"%")", jn.representation.is_equal ("null"))
|
|
||||||
|
assert ("jn.representation.is_equal (%"%"null%"%")", jn.representation.is_equal (jrep))
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||||
jn ?= json.value (Void)
|
if attached {JSON_NULL} json.value (Void) as l_json_null then
|
||||||
assert ("jn /= Void", jn /= Void)
|
assert ("jn.representation.is_equal (%"null%")", l_json_null.representation.is_equal ("null"))
|
||||||
if attached jn as l_jn then
|
else
|
||||||
assert ("jn.representation.is_equal (%"null%")", l_jn.representation.is_equal ("null"))
|
assert ("json.value (Void) /= Void", False)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
-- JSON representation -> JSON value -> Eiffel value
|
||||||
jrep := "null"
|
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
jn := Void
|
if attached parser.parse as l_json_null then
|
||||||
jn ?= parser.parse
|
assert ("a = Void", json.object (l_json_null, Void) = Void)
|
||||||
assert ("jn /= Void", jn /= Void)
|
else
|
||||||
create dummy_object.make_empty
|
assert ("parser.parse /= Void", False)
|
||||||
a := dummy_object
|
end
|
||||||
a ?= json.object (jn, Void)
|
|
||||||
assert ("a = Void", a = Void)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test_json_string_and_character
|
test_json_string_and_character
|
||||||
local
|
local
|
||||||
c: CHARACTER
|
c: CHARACTER
|
||||||
js: detachable JSON_STRING
|
|
||||||
jrep: STRING
|
jrep: STRING
|
||||||
|
js: JSON_STRING
|
||||||
parser: JSON_PARSER
|
parser: JSON_PARSER
|
||||||
do
|
do
|
||||||
c := 'a'
|
c := 'a'
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
-- Eiffel value -> JSON value -> JSON representation
|
||||||
create js.make_json (c.out)
|
create js.make_json (c.out)
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
assert ("js.representation.is_equal (%"%"a%"%")", js.representation.is_equal ("%"a%""))
|
assert ("js.representation.is_equal (%"%"a%"%")", js.representation.is_equal ("%"a%""))
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||||
js ?= json.value (c)
|
if attached {JSON_STRING} json.value (c) as l_json_str then
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("js.representation.is_equal (%"%"a%"%")", l_json_str.representation.is_equal ("%"a%""))
|
||||||
if attached js as l_js then
|
else
|
||||||
assert ("js.representation.is_equal (%"%"a%"%")", l_js.representation.is_equal ("%"a%""))
|
assert ("json.value (c) /= Void", False)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
-- JSON representation -> JSON value -> Eiffel value
|
||||||
jrep := "%"a%""
|
jrep := "%"a%""
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
js := Void
|
if attached {JSON_STRING} parser.parse as l_json_str then
|
||||||
js ?= parser.parse
|
if attached {STRING_32} json.object (l_json_str, Void) as ucs then
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("ucs.string.is_equal (%"a%")", ucs.string.is_equal ("a"))
|
||||||
if attached {STRING_32} json.object (js, Void) as ucs then
|
end
|
||||||
assert ("ucs.string.is_equal (%"a%")", ucs.string.is_equal ("a"))
|
else
|
||||||
end
|
assert ("parser.parse /= Void", False)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test_json_string_and_string
|
test_json_string_and_string
|
||||||
@@ -545,94 +544,101 @@ feature -- Test
|
|||||||
parser: JSON_PARSER
|
parser: JSON_PARSER
|
||||||
do
|
do
|
||||||
s := "foobar"
|
s := "foobar"
|
||||||
|
jrep := "%"foobar%""
|
||||||
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
-- Eiffel value -> JSON value -> JSON representation
|
||||||
create js.make_json (s)
|
create js.make_json (s)
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal (jrep))
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||||
js ?= json.value (s)
|
if attached {JSON_STRING} json.value (s) as l_js then
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal (jrep))
|
||||||
if attached js as l_js then
|
else
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
assert ("json.value (s) /= Void", False)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
-- JSON representation -> JSON value -> Eiffel value
|
||||||
jrep := "%"foobar%""
|
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
js := Void
|
if attached {JSON_STRING} parser.parse as l_js then
|
||||||
js ?= parser.parse
|
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal (s))
|
||||||
if attached {STRING_32} json.object (js, Void) as l_ucs then
|
end
|
||||||
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal ("foobar"))
|
else
|
||||||
end
|
assert ("parser.parse /= Void", False)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test_json_string_and_uc_string
|
test_json_string_and_uc_string
|
||||||
local
|
local
|
||||||
js: detachable JSON_STRING
|
js: detachable JSON_STRING
|
||||||
ucs: detachable STRING_32
|
ucs: detachable STRING_32
|
||||||
jrep: STRING
|
jrep, s: STRING
|
||||||
parser: JSON_PARSER
|
parser: JSON_PARSER
|
||||||
do
|
do
|
||||||
create ucs.make_from_string ("foobar")
|
s := "foobar"
|
||||||
|
jrep := "%"foobar%""
|
||||||
|
create ucs.make_from_string (s)
|
||||||
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
-- Eiffel value -> JSON value -> JSON representation
|
||||||
create js.make_json (ucs)
|
create js.make_json (ucs)
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal (jrep))
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||||
js ?= json.value (ucs)
|
if attached {JSON_STRING} json.value (ucs) as l_js then
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("js.representation.is_equal (%"%"foobar%"%")", l_js.representation.is_equal (jrep))
|
||||||
if attached js as l_js then
|
else
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", l_js.representation.is_equal ("%"foobar%""))
|
assert ("json.value (ucs) /= Void", False)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
-- JSON representation -> JSON value -> Eiffel value
|
||||||
jrep := "%"foobar%""
|
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
js := Void
|
if attached {JSON_STRING} parser.parse as l_js then
|
||||||
js ?= parser.parse
|
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal (s))
|
||||||
ucs := Void
|
else
|
||||||
ucs ?= json.object (js, Void)
|
assert ("json.object (js, Void) /= Void", False)
|
||||||
if attached ucs as l_ucs then
|
end
|
||||||
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal ("foobar"))
|
else
|
||||||
end
|
assert ("parser.parse /= Void", False)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test_json_string_and_special_characters
|
test_json_string_and_special_characters
|
||||||
local
|
local
|
||||||
js: detachable JSON_STRING
|
js: detachable JSON_STRING
|
||||||
s: detachable STRING_8
|
s: detachable STRING_8
|
||||||
ucs: detachable STRING_32
|
|
||||||
jrep: STRING
|
jrep: STRING
|
||||||
parser: JSON_PARSER
|
parser: JSON_PARSER
|
||||||
do
|
do
|
||||||
|
jrep := "%"foo\\bar%""
|
||||||
create s.make_from_string ("foo\bar")
|
create s.make_from_string ("foo\bar")
|
||||||
create js.make_json (s)
|
create js.make_json (s)
|
||||||
|
|
||||||
assert ("js.representation.same_string (%"%"foo\\bar%"%")", js.representation.same_string ("%"foo\\bar%""))
|
assert ("js.representation.same_string (%"%"foo\\bar%"%")", js.representation.same_string (jrep))
|
||||||
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||||
js ?= json.value (s)
|
if attached {JSON_STRING} json.value (s) as l_js then
|
||||||
assert ("js /= Void", js /= Void)
|
assert ("js.representation.is_equal (%"%"foobar%"%")", l_js.representation.same_string (jrep))
|
||||||
if js /= Void then
|
else
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.same_string ("%"foo\\bar%""))
|
assert ("json.value (s) /= Void", False)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
-- JSON representation -> JSON value -> Eiffel value
|
||||||
jrep := "%"foo\\bar%""
|
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
js ?= parser.parse
|
if attached {JSON_STRING} parser.parse as l_js then
|
||||||
assert ("js /= Void", js /= Void)
|
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
|
||||||
ucs ?= json.object (js, Void)
|
assert ("ucs.same_string (%"foo\bar%")", l_ucs.same_string ("foo\bar"))
|
||||||
if ucs /= Void then
|
end
|
||||||
assert ("ucs.same_string (%"foo\bar%")", ucs.same_string ("foo\bar"))
|
else
|
||||||
end
|
assert ("parser.parse /= Void", False)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
jrep := "%"foo\\bar%""
|
jrep := "%"foo\\bar%""
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
if attached {JSON_STRING} parser.parse as jstring then
|
if attached {JSON_STRING} parser.parse as jstring then
|
||||||
assert ("unescaped string %"foo\\bar%" to %"foo\bar%"", jstring.unescaped_string_8.same_string ("foo\bar"))
|
assert ("unescaped string %"foo\\bar%" to %"foo\bar%"", jstring.unescaped_string_8.same_string ("foo\bar"))
|
||||||
|
else
|
||||||
|
assert ("parser.parse /= Void", False)
|
||||||
end
|
end
|
||||||
|
|
||||||
create js.make_json_from_string_32 ({STRING_32}"%/20320/%/22909/")
|
create js.make_json_from_string_32 ({STRING_32}"%/20320/%/22909/")
|
||||||
@@ -642,13 +648,14 @@ feature -- Test
|
|||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
if attached {JSON_STRING} parser.parse as jstring then
|
if attached {JSON_STRING} parser.parse as jstring then
|
||||||
assert ("same unicode string32 %"%%/20320/%%/22909/%"", jstring.unescaped_string_32.same_string ({STRING_32}"%/20320/%/22909/"))
|
assert ("same unicode string32 %"%%/20320/%%/22909/%"", jstring.unescaped_string_32.same_string ({STRING_32}"%/20320/%/22909/"))
|
||||||
|
else
|
||||||
|
assert ("parser.parse /= Void", False)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test_json_array
|
test_json_array
|
||||||
local
|
local
|
||||||
ll: LINKED_LIST [INTEGER_8]
|
ll: LINKED_LIST [INTEGER_8]
|
||||||
ll2: detachable LINKED_LIST [detachable ANY]
|
|
||||||
ja: detachable JSON_ARRAY
|
ja: detachable JSON_ARRAY
|
||||||
jn: JSON_NUMBER
|
jn: JSON_NUMBER
|
||||||
jrep: STRING
|
jrep: STRING
|
||||||
@@ -677,11 +684,10 @@ feature -- Test
|
|||||||
assert ("ja /= Void", ja /= Void)
|
assert ("ja /= Void", ja /= Void)
|
||||||
assert ("ja.representation.is_equal (%"[0,1,1,2,3,5]%")", ja.representation.is_equal ("[0,1,1,2,3,5]"))
|
assert ("ja.representation.is_equal (%"[0,1,1,2,3,5]%")", ja.representation.is_equal ("[0,1,1,2,3,5]"))
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||||
ja := Void
|
if attached {JSON_ARRAY} json.value (ll) as l_ja then
|
||||||
ja ?= json.value (ll)
|
|
||||||
assert ("ja /= Void", ja /= Void)
|
|
||||||
if attached ja as l_ja then
|
|
||||||
assert ("ja.representation.is_equal (%"[0,1,1,2,3,5]%")", l_ja.representation.is_equal ("[0,1,1,2,3,5]"))
|
assert ("ja.representation.is_equal (%"[0,1,1,2,3,5]%")", l_ja.representation.is_equal ("[0,1,1,2,3,5]"))
|
||||||
|
else
|
||||||
|
assert ("json.value (ll) /= Void", False)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
-- JSON representation -> JSON value -> Eiffel value
|
||||||
@@ -691,22 +697,20 @@ feature -- Test
|
|||||||
-- values 0, 1, 1, 2, 3, 5
|
-- values 0, 1, 1, 2, 3, 5
|
||||||
jrep := "[0,1,1,2,3,5]"
|
jrep := "[0,1,1,2,3,5]"
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
ja := Void
|
if attached {JSON_ARRAY} parser.parse as l_ja then
|
||||||
ja ?= parser.parse
|
if attached {LINKED_LIST [detachable ANY]} json.object (ja, Void) as l_ll2 then
|
||||||
assert ("ja /= Void", ja /= Void)
|
assert ("ll2.is_equal (ll)", l_ll2.is_equal (ll))
|
||||||
ll2 ?= json.object (ja, Void)
|
else
|
||||||
assert ("ll2 /= Void", ll2 /= Void)
|
assert ("json.object (ja, Void) /= Void", False)
|
||||||
--ll.compare_objects
|
end
|
||||||
--ll2.compare_objects
|
else
|
||||||
if attached ll2 as l_ll2 then
|
assert ("parser.parse /= Void", False)
|
||||||
assert ("ll2.is_equal (ll)", l_ll2.is_equal (ll))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test_json_object
|
test_json_object
|
||||||
local
|
local
|
||||||
t, t2: detachable HASH_TABLE [detachable ANY, STRING_GENERAL]
|
t: detachable HASH_TABLE [detachable ANY, STRING_GENERAL]
|
||||||
i: INTEGER
|
i: INTEGER
|
||||||
ucs_key, ucs: STRING_32
|
ucs_key, ucs: STRING_32
|
||||||
a: ARRAY [INTEGER]
|
a: ARRAY [INTEGER]
|
||||||
@@ -745,6 +749,7 @@ feature -- Test
|
|||||||
jo.put (ja, js_key)
|
jo.put (ja, js_key)
|
||||||
assert ("jo /= Void", jo /= Void)
|
assert ("jo /= Void", jo /= Void)
|
||||||
assert ("jo.representation.is_equal (%"{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}%")", jo.representation.is_equal ("{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}"))
|
assert ("jo.representation.is_equal (%"{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}%")", jo.representation.is_equal ("{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}"))
|
||||||
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||||
create t.make (3)
|
create t.make (3)
|
||||||
create ucs_key.make_from_string ("name")
|
create ucs_key.make_from_string ("name")
|
||||||
@@ -756,26 +761,28 @@ feature -- Test
|
|||||||
create ucs_key.make_from_string ("contents")
|
create ucs_key.make_from_string ("contents")
|
||||||
a := <<0, 1, 1, 2, 3, 5>>
|
a := <<0, 1, 1, 2, 3, 5>>
|
||||||
t.put (a, ucs_key)
|
t.put (a, ucs_key)
|
||||||
jo := Void
|
if attached {JSON_OBJECT} json.value (t) as l_jo then
|
||||||
jo ?= json.value (t)
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
if attached jo as l_jo then
|
|
||||||
assert ("jo.representation.is_equal (%"{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}%")", l_jo.representation.is_equal ("{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}"))
|
assert ("jo.representation.is_equal (%"{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}%")", l_jo.representation.is_equal ("{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}"))
|
||||||
|
else
|
||||||
|
assert ("json.value (t) /= Void", False)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value -> JSON value -> JSON representation
|
-- JSON representation -> JSON value -> Eiffel value -> JSON value -> JSON representation
|
||||||
jrep := "{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}"
|
jrep := "{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}"
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
jo := Void
|
if attached {JSON_OBJECT} parser.parse as l_jo then
|
||||||
jo ?= parser.parse
|
if attached {HASH_TABLE [detachable ANY, STRING_GENERAL]} json.object (l_jo, Void) as l_t2 then
|
||||||
assert ("jo /= Void", jo /= Void)
|
if attached json.value (l_t2) as l_jo_2 then
|
||||||
t2 ?= json.object (jo, Void)
|
assert ("jrep.is_equal (jo.representation)", jrep.is_equal (l_jo_2.representation))
|
||||||
assert ("t2 /= Void", t2 /= Void)
|
else
|
||||||
jo ?= json.value (t2)
|
assert ("json.value (t2) /= Void", False)
|
||||||
assert ("jo /= Void", jo /= Void)
|
end
|
||||||
if attached jo as l_jo then
|
else
|
||||||
assert ("jrep.is_equal (jo.representation)", jrep.is_equal (jo.representation))
|
assert ("json.object (jo, Void) /= Void", False)
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
assert ("parser.parse /= Void", jo /= Void)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test_json_object_hash_code
|
test_json_object_hash_code
|
||||||
@@ -802,7 +809,6 @@ feature -- Test
|
|||||||
jv := json.value (gv)
|
jv := json.value (gv)
|
||||||
else
|
else
|
||||||
assert ("exceptions.is_developer_exception", json.is_developer_exception)
|
assert ("exceptions.is_developer_exception", json.is_developer_exception)
|
||||||
-- assert ("exceptions.is_developer_exception_of_name", json.is_developer_exception_of_name ("eJSON exception: Failed to convert Eiffel object to a JSON_VALUE: OPERATING_ENVIRONMENT"))
|
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
exception := True
|
exception := True
|
||||||
@@ -813,17 +819,15 @@ feature -- Test
|
|||||||
-- Test converting from a JSON value to an Eiffel object based on a
|
-- Test converting from a JSON value to an Eiffel object based on a
|
||||||
-- class for which no JSON converter has been registered.
|
-- class for which no JSON converter has been registered.
|
||||||
local
|
local
|
||||||
gv : detachable OPERATING_ENVIRONMENT
|
gv : detachable ANY
|
||||||
jo: JSON_OBJECT
|
jo: JSON_OBJECT
|
||||||
exception: BOOLEAN
|
exception: BOOLEAN
|
||||||
do
|
do
|
||||||
if not exception then
|
if not exception then
|
||||||
create jo.make
|
create jo.make
|
||||||
gv ?= json.object (jo, "OPERATING_ENVIRONMENT")
|
gv := json.object (jo, "OPERATING_ENVIRONMENT")
|
||||||
else
|
else
|
||||||
assert ("exceptions.is_developer_exception", json.is_developer_exception)
|
assert ("exceptions.is_developer_exception", json.is_developer_exception)
|
||||||
-- assert ("exceptions.is_developer_exception_of_name", json.is_developer_exception_of_name ("eJSON exception: Failed to convert JSON_VALUE to an Eiffel object: JSON_OBJECT -> OPERATING_ENVIRONMENT"))
|
|
||||||
|
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
exception := True
|
exception := True
|
||||||
|
|||||||
@@ -1,19 +1,28 @@
|
|||||||
class TEST_JSON_CUSTOM_CLASSES
|
|
||||||
|
note
|
||||||
|
description: "Parsing and converter of book collection test."
|
||||||
|
date: "$Date$"
|
||||||
|
revision: "$Revision$"
|
||||||
|
|
||||||
|
class
|
||||||
|
TEST_JSON_CUSTOM_CLASSES
|
||||||
|
|
||||||
inherit
|
inherit
|
||||||
SHARED_EJSON
|
SHARED_EJSON
|
||||||
rename default_create as shared_default_create end
|
undefine
|
||||||
|
default_create
|
||||||
|
end
|
||||||
|
|
||||||
EQA_TEST_SET
|
EQA_TEST_SET
|
||||||
select default_create end
|
|
||||||
feature -- Test
|
feature -- Test
|
||||||
|
|
||||||
test_custom_classes
|
test_custom_classes
|
||||||
|
-- Parse JSON representation to JSON_OBJECT and test book collection converter.
|
||||||
local
|
local
|
||||||
bc: detachable BOOK_COLLECTION
|
|
||||||
jbc: JSON_BOOK_CONVERTER
|
jbc: JSON_BOOK_CONVERTER
|
||||||
jbcc: JSON_BOOK_COLLECTION_CONVERTER
|
jbcc: JSON_BOOK_COLLECTION_CONVERTER
|
||||||
jac: JSON_AUTHOR_CONVERTER
|
jac: JSON_AUTHOR_CONVERTER
|
||||||
jo: detachable JSON_OBJECT
|
|
||||||
parser: JSON_PARSER
|
parser: JSON_PARSER
|
||||||
jrep: STRING
|
jrep: STRING
|
||||||
do
|
do
|
||||||
@@ -25,18 +34,20 @@ feature -- Test
|
|||||||
json.add_converter (jac)
|
json.add_converter (jac)
|
||||||
jrep := "{%"name%":%"Test collection%",%"books%":[{%"title%":%"eJSON: The Definitive Guide%",%"isbn%":%"123123-413243%",%"author%":{%"name%":%"Foo Bar%"}}]}"
|
jrep := "{%"name%":%"Test collection%",%"books%":[{%"title%":%"eJSON: The Definitive Guide%",%"isbn%":%"123123-413243%",%"author%":{%"name%":%"Foo Bar%"}}]}"
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
jo := Void
|
|
||||||
jo ?= parser.parse
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
bc := Void
|
|
||||||
bc ?= json.object (jo, "BOOK_COLLECTION")
|
|
||||||
assert ("bc /= Void", bc /= Void)
|
|
||||||
jo ?= json.value (bc)
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
if attached jo as l_jo then
|
|
||||||
assert ("JSON representation is correct", l_jo.representation.same_string ("{%"name%":%"Test collection%",%"books%":[{%"title%":%"eJSON: The Definitive Guide%",%"isbn%":%"123123-413243%",%"author%":{%"name%":%"Foo Bar%"}}]}"))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
if attached {JSON_OBJECT} parser.parse as l_json_object then
|
||||||
|
if attached {BOOK_COLLECTION} json.object (l_json_object, "BOOK_COLLECTION") as l_collection then
|
||||||
|
if attached {JSON_OBJECT} json.value (l_collection) as l_json_object_2 then
|
||||||
|
assert ("JSON representation is correct", l_json_object_2.representation.same_string (jrep))
|
||||||
|
else
|
||||||
|
assert ("BOOK_COLLECTION converted to JSON_OBJECT", False)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
assert ("JSON_OBJECT converted to BOOK_COLLECTION", False)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
assert ("JSON object representation to JSON_OBJECT", False)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end -- class TEST_JSON_CUSTOM_CLASS
|
end -- class TEST_JSON_CUSTOM_CLASS
|
||||||
|
|||||||
Reference in New Issue
Block a user