Merge pull request #8 from Conaclos/working
Tests - Update syntax and improve implementation
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
class AUTHOR
|
||||
|
||||
class
|
||||
AUTHOR
|
||||
|
||||
create
|
||||
make
|
||||
@@ -6,19 +8,26 @@ create
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make (a_name: STRING_32)
|
||||
-- Create an author with `a_name' as `name'.
|
||||
do
|
||||
set_name (a_name)
|
||||
ensure
|
||||
name_set: name = a_name
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
name: STRING_32
|
||||
-- Author name
|
||||
|
||||
feature -- Status setting
|
||||
feature -- Change
|
||||
|
||||
set_name (a_name: STRING_32)
|
||||
-- Set `name' with `a_name'.
|
||||
do
|
||||
name := a_name
|
||||
ensure
|
||||
name_set: name = a_name
|
||||
end
|
||||
|
||||
end -- class AUTHOR
|
||||
|
||||
@@ -1,40 +1,60 @@
|
||||
class BOOK
|
||||
|
||||
class
|
||||
BOOK
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make (a_title: STRING_32; an_author: AUTHOR; an_isbn: STRING_32)
|
||||
make (a_title: STRING_32; a_author: AUTHOR; a_isbn: STRING_32)
|
||||
-- Create a book with `a_title' as `title',
|
||||
-- `a_author' as `author', and `a_isbn' as `isbn',
|
||||
do
|
||||
set_title (a_title)
|
||||
set_author (an_author)
|
||||
set_isbn (an_isbn)
|
||||
set_author (a_author)
|
||||
set_isbn (a_isbn)
|
||||
ensure
|
||||
title_set: title = a_title
|
||||
author_set: author = a_author
|
||||
isbn_set: isbn = a_isbn
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
title: STRING_32
|
||||
-- Main title.
|
||||
|
||||
isbn: STRING_32
|
||||
-- ISBN.
|
||||
|
||||
author: AUTHOR
|
||||
-- Author.
|
||||
|
||||
feature -- Status setting
|
||||
feature -- Change
|
||||
|
||||
set_title (a_title: STRING_32)
|
||||
-- Set `title' with `a_title'.
|
||||
do
|
||||
title := a_title
|
||||
ensure
|
||||
title_set: title = a_title
|
||||
end
|
||||
|
||||
set_author (an_author: AUTHOR)
|
||||
set_author (a_author: AUTHOR)
|
||||
-- Set `author' with `a_author'.
|
||||
do
|
||||
author := an_author
|
||||
author := a_author
|
||||
ensure
|
||||
author_set: author = a_author
|
||||
end
|
||||
|
||||
set_isbn (an_isbn: STRING_32)
|
||||
set_isbn (a_isbn: STRING_32)
|
||||
-- Set `isbn' with `a_isbn'.
|
||||
do
|
||||
isbn := an_isbn
|
||||
isbn := a_isbn
|
||||
ensure
|
||||
isbn_set: isbn = a_isbn
|
||||
end
|
||||
|
||||
end -- class BOOK
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
class BOOK_COLLECTION
|
||||
class
|
||||
BOOK_COLLECTION
|
||||
|
||||
create
|
||||
make
|
||||
@@ -6,75 +7,72 @@ create
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make (a_name: STRING_32)
|
||||
-- Create a collection of book with `a_name' as `name'
|
||||
do
|
||||
set_name (a_name)
|
||||
create book_index.make (10)
|
||||
ensure
|
||||
name_set: name = a_name
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
name: STRING_32
|
||||
-- Name.
|
||||
|
||||
books: LIST [BOOK]
|
||||
-- collection of book.
|
||||
do
|
||||
from
|
||||
create {LINKED_LIST [BOOK]} Result.make
|
||||
book_index.start
|
||||
until
|
||||
book_index.after
|
||||
loop
|
||||
Result.append (book_index.item_for_iteration)
|
||||
book_index.forth
|
||||
create {LINKED_LIST [BOOK]} Result.make
|
||||
across book_index as it loop
|
||||
Result.append (it.item)
|
||||
end
|
||||
end
|
||||
|
||||
books_by_author (an_author: STRING_32): detachable LIST [BOOK]
|
||||
books_by_author (a_author: STRING_32): LIST [BOOK]
|
||||
-- Books wrote by `a_author' in this collection.
|
||||
do
|
||||
if book_index.has (an_author) then
|
||||
Result := book_index @ an_author
|
||||
if attached book_index [a_author] as l_result then
|
||||
Result := l_result
|
||||
else
|
||||
create {LINKED_LIST [BOOK]} Result.make
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Status setting
|
||||
feature -- Change
|
||||
|
||||
set_name (a_name: STRING_32)
|
||||
-- Set `name' with `a_name'.
|
||||
do
|
||||
name := a_name
|
||||
ensure
|
||||
name_set: name = a_name
|
||||
end
|
||||
|
||||
add_book (a_book: BOOK)
|
||||
-- Extend collection with `a_book'.
|
||||
local
|
||||
l: detachable LIST [BOOK]
|
||||
do
|
||||
if book_index.has (a_book.author.name) then
|
||||
l := book_index.at ( a_book.author.name )
|
||||
else
|
||||
l := book_index.at (a_book.author.name )
|
||||
if l = Void then
|
||||
create {LINKED_LIST [BOOK]} l.make
|
||||
book_index.put (l, a_book.author.name)
|
||||
end
|
||||
if attached l as la then
|
||||
la.force (a_book)
|
||||
end
|
||||
|
||||
l.force (a_book)
|
||||
end
|
||||
|
||||
add_books (book_list: like books)
|
||||
|
||||
-- Append collection with `book_list'.
|
||||
do
|
||||
from
|
||||
book_list.start
|
||||
until
|
||||
book_list.after
|
||||
loop
|
||||
add_book (book_list.item)
|
||||
book_list.forth
|
||||
across book_list as it loop
|
||||
add_book (it.item)
|
||||
end
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
book_index: HASH_TABLE [LIST [BOOK], STRING_32]
|
||||
-- Association of author name and its books.
|
||||
|
||||
end -- class BOOK_COLLECTION
|
||||
|
||||
@@ -4,7 +4,8 @@ note
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class JSON_AUTHOR_CONVERTER
|
||||
class
|
||||
JSON_AUTHOR_CONVERTER
|
||||
|
||||
inherit
|
||||
JSON_CONVERTER
|
||||
@@ -29,12 +30,10 @@ feature -- Access
|
||||
feature -- Conversion
|
||||
|
||||
from_json (j: like to_json): detachable like object
|
||||
local
|
||||
ucs: detachable STRING_32
|
||||
do
|
||||
ucs ?= json.object (j.item (name_key), Void)
|
||||
check ucs /= Void end
|
||||
create Result.make (ucs)
|
||||
if attached {STRING_32} json.object (j.item (name_key), Void) as l_name then
|
||||
create Result.make (l_name)
|
||||
end
|
||||
end
|
||||
|
||||
to_json (o: like object): JSON_OBJECT
|
||||
@@ -43,9 +42,10 @@ feature -- Conversion
|
||||
Result.put (json.value (o.name), name_key)
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
name_key: JSON_STRING
|
||||
-- Author's name label.
|
||||
once
|
||||
create Result.make_json ("name")
|
||||
end
|
||||
|
||||
@@ -4,7 +4,8 @@ note
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class JSON_BOOK_COLLECTION_CONVERTER
|
||||
class
|
||||
JSON_BOOK_COLLECTION_CONVERTER
|
||||
|
||||
inherit
|
||||
JSON_CONVERTER
|
||||
@@ -30,30 +31,32 @@ feature -- Conversion
|
||||
|
||||
from_json (j: like to_json): detachable like object
|
||||
local
|
||||
ucs: detachable STRING_32
|
||||
ll: LINKED_LIST [BOOK]
|
||||
b: detachable BOOK
|
||||
ja: detachable JSON_ARRAY
|
||||
i: INTEGER
|
||||
l_books: LINKED_LIST [BOOK]
|
||||
do
|
||||
ucs ?= json.object (j.item (name_key), Void)
|
||||
check ucs /= Void end
|
||||
create Result.make (ucs)
|
||||
ja ?= j.item (books_key)
|
||||
check ja /= Void end
|
||||
from
|
||||
i := 1
|
||||
create ll.make
|
||||
until
|
||||
i > ja.count
|
||||
loop
|
||||
b ?= json.object (ja [i], "BOOK")
|
||||
check b /= Void end
|
||||
ll.force (b)
|
||||
i := i + 1
|
||||
if
|
||||
attached {STRING_32} json.object (j.item (name_key), Void) as l_name and
|
||||
attached {JSON_ARRAY} j.item (books_key) as l_json_array
|
||||
then
|
||||
create Result.make (l_name)
|
||||
create l_books.make
|
||||
|
||||
across
|
||||
l_json_array as it
|
||||
until
|
||||
Result = Void
|
||||
loop
|
||||
if attached {BOOK} json.object (it.item, "BOOK") as l_book then
|
||||
l_books.extend (l_book)
|
||||
else
|
||||
Result := Void
|
||||
-- Failed
|
||||
end
|
||||
end
|
||||
|
||||
if Result /= Void then
|
||||
Result.add_books (l_books)
|
||||
end
|
||||
end
|
||||
check ll /= Void end
|
||||
Result.add_books (ll)
|
||||
end
|
||||
|
||||
to_json (o: like object): JSON_OBJECT
|
||||
@@ -66,11 +69,13 @@ feature -- Conversion
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
name_key: JSON_STRING
|
||||
-- Collection's name label.
|
||||
once
|
||||
create Result.make_json ("name")
|
||||
end
|
||||
|
||||
books_key: JSON_STRING
|
||||
-- Book list label.
|
||||
once
|
||||
create Result.make_json ("books")
|
||||
end
|
||||
|
||||
@@ -4,7 +4,8 @@ note
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class JSON_BOOK_CONVERTER
|
||||
class
|
||||
JSON_BOOK_CONVERTER
|
||||
|
||||
inherit
|
||||
JSON_CONVERTER
|
||||
@@ -31,17 +32,14 @@ feature -- Access
|
||||
feature -- Conversion
|
||||
|
||||
from_json (j: like to_json): detachable like object
|
||||
local
|
||||
ucs1, ucs2: detachable STRING_32
|
||||
a: detachable AUTHOR
|
||||
do
|
||||
ucs1 ?= json.object (j.item (title_key), Void)
|
||||
check ucs1 /= Void end
|
||||
ucs2 ?= json.object (j.item (isbn_key), Void)
|
||||
check ucs2 /= Void end
|
||||
a ?= json.object (j.item (author_key), "AUTHOR")
|
||||
check a /= Void end
|
||||
create Result.make (ucs1, a, ucs2)
|
||||
if
|
||||
attached {STRING_32} json.object (j.item (title_key), Void) as l_title and
|
||||
attached {STRING_32} json.object (j.item (isbn_key), Void) as l_isbn and
|
||||
attached {AUTHOR} json.object (j.item (author_key), "AUTHOR") as l_author
|
||||
then
|
||||
create Result.make (l_title, l_author, l_isbn)
|
||||
end
|
||||
end
|
||||
|
||||
to_json (o: like object): JSON_OBJECT
|
||||
@@ -52,19 +50,22 @@ feature -- Conversion
|
||||
Result.put (json.value (o.author), author_key)
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
title_key: JSON_STRING
|
||||
-- Book's title label.
|
||||
once
|
||||
create Result.make_json ("title")
|
||||
end
|
||||
|
||||
isbn_key: JSON_STRING
|
||||
-- Book ISBN label.
|
||||
once
|
||||
create Result.make_json ("isbn")
|
||||
end
|
||||
|
||||
author_key: JSON_STRING
|
||||
-- Author label.
|
||||
once
|
||||
create Result.make_json ("author")
|
||||
end
|
||||
|
||||
@@ -1,74 +1,76 @@
|
||||
class TEST_DS
|
||||
|
||||
note
|
||||
description: "Linked list and hash table converters test."
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
TEST_DS
|
||||
|
||||
inherit
|
||||
SHARED_EJSON
|
||||
rename default_create as shared_default_create end
|
||||
EQA_TEST_SET
|
||||
select default_create end
|
||||
undefine
|
||||
default_create
|
||||
end
|
||||
|
||||
EQA_TEST_SET
|
||||
|
||||
feature -- Test
|
||||
|
||||
test_linked_list_converter
|
||||
-- Convert a linked list to a json value and
|
||||
-- convert this one to a linked list.
|
||||
local
|
||||
jc: JSON_LINKED_LIST_CONVERTER
|
||||
l: LINKED_LIST [STRING]
|
||||
l2: detachable LINKED_LIST [detachable ANY]
|
||||
s: STRING
|
||||
jv: detachable JSON_VALUE
|
||||
do
|
||||
create jc.make
|
||||
json.add_converter (jc)
|
||||
create l.make
|
||||
s := "foo"
|
||||
l.force (s)
|
||||
s := "bar"
|
||||
l.force (s)
|
||||
jv := json.value (l)
|
||||
assert ("jv /= Void", jv /= Void)
|
||||
if attached jv as l_jv then
|
||||
s := jv.representation
|
||||
l2 ?= json.object (jv, "LINKED_LIST")
|
||||
assert ("l2 /= Void", l2 /= Void)
|
||||
l.force ("foo")
|
||||
l.force ("bar")
|
||||
|
||||
if attached json.value (l) as l_value then
|
||||
s := l_value.representation
|
||||
assert ("JSON array converted to LINKED_LIST", attached {LINKED_LIST [detachable ANY]} json.object (l_value, "LINKED_LIST"))
|
||||
else
|
||||
assert ("LINKED_LIST converted to a JSON value", False)
|
||||
end
|
||||
end
|
||||
|
||||
test_hash_table_converter
|
||||
-- Convert a hash table to a json value and
|
||||
-- convert this one to a hash table.
|
||||
local
|
||||
tc: JSON_HASH_TABLE_CONVERTER
|
||||
t: HASH_TABLE [STRING, STRING]
|
||||
t2: detachable HASH_TABLE [ANY, HASHABLE]
|
||||
s: STRING
|
||||
ucs_key, ucs_value: detachable STRING_32
|
||||
jv: detachable JSON_VALUE
|
||||
l_ucs_key: detachable STRING_32
|
||||
do
|
||||
create tc.make
|
||||
json.add_converter (tc)
|
||||
create t.make (2)
|
||||
t.put ("foo", "1")
|
||||
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 -- class TEST_DS
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
class TEST_JSON_CORE
|
||||
class
|
||||
TEST_JSON_CORE
|
||||
|
||||
inherit
|
||||
SHARED_EJSON
|
||||
rename default_create as shared_default_create end
|
||||
undefine
|
||||
default_create
|
||||
end
|
||||
|
||||
EQA_TEST_SET
|
||||
select default_create end
|
||||
|
||||
feature -- Test
|
||||
|
||||
@@ -21,7 +24,7 @@ feature -- Test
|
||||
assert ("jn.representation.same_string (%"42%")", jn.representation.same_string ("42"))
|
||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||
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
|
||||
assert ("json.value (i) is a JSON_NUMBER", False)
|
||||
end
|
||||
@@ -477,64 +480,60 @@ feature -- Test
|
||||
|
||||
test_json_null
|
||||
local
|
||||
a: detachable ANY
|
||||
dummy_object: STRING
|
||||
jn: detachable JSON_NULL
|
||||
jrep: STRING
|
||||
jn: JSON_NULL
|
||||
parser: JSON_PARSER
|
||||
do
|
||||
-- Eiffel value -> JSON value -> JSON representation
|
||||
create jn
|
||||
assert ("jn /= Void", jn /= Void)
|
||||
assert ("jn.representation.is_equal (%"%"null%"%")", jn.representation.is_equal ("null"))
|
||||
jrep := "null"
|
||||
|
||||
assert ("jn.representation.is_equal (%"%"null%"%")", jn.representation.is_equal (jrep))
|
||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||
jn ?= json.value (Void)
|
||||
assert ("jn /= Void", jn /= Void)
|
||||
if attached jn as l_jn then
|
||||
assert ("jn.representation.is_equal (%"null%")", l_jn.representation.is_equal ("null"))
|
||||
if attached {JSON_NULL} json.value (Void) as l_json_null then
|
||||
assert ("jn.representation.is_equal (%"null%")", l_json_null.representation.is_equal ("null"))
|
||||
else
|
||||
assert ("json.value (Void) /= Void", False)
|
||||
end
|
||||
|
||||
-- JSON representation -> JSON value -> Eiffel value
|
||||
jrep := "null"
|
||||
create parser.make_parser (jrep)
|
||||
jn := Void
|
||||
jn ?= parser.parse
|
||||
assert ("jn /= Void", jn /= Void)
|
||||
create dummy_object.make_empty
|
||||
a := dummy_object
|
||||
a ?= json.object (jn, Void)
|
||||
assert ("a = Void", a = Void)
|
||||
if attached parser.parse as l_json_null then
|
||||
assert ("a = Void", json.object (l_json_null, Void) = Void)
|
||||
else
|
||||
assert ("parser.parse /= Void", False)
|
||||
end
|
||||
end
|
||||
|
||||
test_json_string_and_character
|
||||
local
|
||||
c: CHARACTER
|
||||
js: detachable JSON_STRING
|
||||
jrep: STRING
|
||||
js: JSON_STRING
|
||||
parser: JSON_PARSER
|
||||
do
|
||||
c := 'a'
|
||||
-- Eiffel value -> JSON value -> JSON representation
|
||||
create js.make_json (c.out)
|
||||
assert ("js /= Void", js /= Void)
|
||||
|
||||
assert ("js.representation.is_equal (%"%"a%"%")", js.representation.is_equal ("%"a%""))
|
||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||
js ?= json.value (c)
|
||||
assert ("js /= Void", js /= Void)
|
||||
if attached js as l_js then
|
||||
assert ("js.representation.is_equal (%"%"a%"%")", l_js.representation.is_equal ("%"a%""))
|
||||
if attached {JSON_STRING} json.value (c) as l_json_str then
|
||||
assert ("js.representation.is_equal (%"%"a%"%")", l_json_str.representation.is_equal ("%"a%""))
|
||||
else
|
||||
assert ("json.value (c) /= Void", False)
|
||||
end
|
||||
|
||||
-- JSON representation -> JSON value -> Eiffel value
|
||||
jrep := "%"a%""
|
||||
create parser.make_parser (jrep)
|
||||
js := Void
|
||||
js ?= parser.parse
|
||||
assert ("js /= Void", js /= Void)
|
||||
if attached {STRING_32} json.object (js, Void) as ucs then
|
||||
assert ("ucs.string.is_equal (%"a%")", ucs.string.is_equal ("a"))
|
||||
end
|
||||
|
||||
if attached {JSON_STRING} parser.parse as l_json_str then
|
||||
if attached {STRING_32} json.object (l_json_str, Void) as ucs then
|
||||
assert ("ucs.string.is_equal (%"a%")", ucs.string.is_equal ("a"))
|
||||
end
|
||||
else
|
||||
assert ("parser.parse /= Void", False)
|
||||
end
|
||||
end
|
||||
|
||||
test_json_string_and_string
|
||||
@@ -545,94 +544,101 @@ feature -- Test
|
||||
parser: JSON_PARSER
|
||||
do
|
||||
s := "foobar"
|
||||
jrep := "%"foobar%""
|
||||
|
||||
-- Eiffel value -> JSON value -> JSON representation
|
||||
create js.make_json (s)
|
||||
assert ("js /= Void", js /= Void)
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal (jrep))
|
||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||
js ?= json.value (s)
|
||||
assert ("js /= Void", js /= Void)
|
||||
if attached js as l_js then
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
||||
if attached {JSON_STRING} json.value (s) as l_js then
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal (jrep))
|
||||
else
|
||||
assert ("json.value (s) /= Void", False)
|
||||
end
|
||||
|
||||
-- JSON representation -> JSON value -> Eiffel value
|
||||
jrep := "%"foobar%""
|
||||
create parser.make_parser (jrep)
|
||||
js := Void
|
||||
js ?= parser.parse
|
||||
assert ("js /= Void", js /= Void)
|
||||
if attached {STRING_32} json.object (js, Void) as l_ucs then
|
||||
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal ("foobar"))
|
||||
end
|
||||
if attached {JSON_STRING} parser.parse as l_js then
|
||||
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
|
||||
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal (s))
|
||||
end
|
||||
else
|
||||
assert ("parser.parse /= Void", False)
|
||||
end
|
||||
end
|
||||
|
||||
test_json_string_and_uc_string
|
||||
local
|
||||
js: detachable JSON_STRING
|
||||
ucs: detachable STRING_32
|
||||
jrep: STRING
|
||||
jrep, s: STRING
|
||||
parser: JSON_PARSER
|
||||
do
|
||||
create ucs.make_from_string ("foobar")
|
||||
s := "foobar"
|
||||
jrep := "%"foobar%""
|
||||
create ucs.make_from_string (s)
|
||||
|
||||
-- Eiffel value -> JSON value -> JSON representation
|
||||
create js.make_json (ucs)
|
||||
assert ("js /= Void", js /= Void)
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal (jrep))
|
||||
|
||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||
js ?= json.value (ucs)
|
||||
assert ("js /= Void", js /= Void)
|
||||
if attached js as l_js then
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", l_js.representation.is_equal ("%"foobar%""))
|
||||
if attached {JSON_STRING} json.value (ucs) as l_js then
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", l_js.representation.is_equal (jrep))
|
||||
else
|
||||
assert ("json.value (ucs) /= Void", False)
|
||||
end
|
||||
|
||||
-- JSON representation -> JSON value -> Eiffel value
|
||||
jrep := "%"foobar%""
|
||||
create parser.make_parser (jrep)
|
||||
js := Void
|
||||
js ?= parser.parse
|
||||
assert ("js /= Void", js /= Void)
|
||||
ucs := Void
|
||||
ucs ?= json.object (js, Void)
|
||||
if attached ucs as l_ucs then
|
||||
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal ("foobar"))
|
||||
end
|
||||
if attached {JSON_STRING} parser.parse as l_js then
|
||||
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
|
||||
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal (s))
|
||||
else
|
||||
assert ("json.object (js, Void) /= Void", False)
|
||||
end
|
||||
else
|
||||
assert ("parser.parse /= Void", False)
|
||||
end
|
||||
end
|
||||
|
||||
test_json_string_and_special_characters
|
||||
local
|
||||
js: detachable JSON_STRING
|
||||
s: detachable STRING_8
|
||||
ucs: detachable STRING_32
|
||||
jrep: STRING
|
||||
parser: JSON_PARSER
|
||||
do
|
||||
jrep := "%"foo\\bar%""
|
||||
create s.make_from_string ("foo\bar")
|
||||
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
|
||||
js ?= json.value (s)
|
||||
assert ("js /= Void", js /= Void)
|
||||
if js /= Void then
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.same_string ("%"foo\\bar%""))
|
||||
if attached {JSON_STRING} json.value (s) as l_js then
|
||||
assert ("js.representation.is_equal (%"%"foobar%"%")", l_js.representation.same_string (jrep))
|
||||
else
|
||||
assert ("json.value (s) /= Void", False)
|
||||
end
|
||||
|
||||
-- JSON representation -> JSON value -> Eiffel value
|
||||
jrep := "%"foo\\bar%""
|
||||
create parser.make_parser (jrep)
|
||||
js ?= parser.parse
|
||||
assert ("js /= Void", js /= Void)
|
||||
ucs ?= json.object (js, Void)
|
||||
if ucs /= Void then
|
||||
assert ("ucs.same_string (%"foo\bar%")", ucs.same_string ("foo\bar"))
|
||||
end
|
||||
if attached {JSON_STRING} parser.parse as l_js then
|
||||
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
|
||||
assert ("ucs.same_string (%"foo\bar%")", l_ucs.same_string ("foo\bar"))
|
||||
end
|
||||
else
|
||||
assert ("parser.parse /= Void", False)
|
||||
end
|
||||
|
||||
|
||||
jrep := "%"foo\\bar%""
|
||||
create parser.make_parser (jrep)
|
||||
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"))
|
||||
else
|
||||
assert ("parser.parse /= Void", False)
|
||||
end
|
||||
|
||||
create js.make_json_from_string_32 ({STRING_32}"%/20320/%/22909/")
|
||||
@@ -642,13 +648,14 @@ feature -- Test
|
||||
create parser.make_parser (jrep)
|
||||
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/"))
|
||||
else
|
||||
assert ("parser.parse /= Void", False)
|
||||
end
|
||||
end
|
||||
|
||||
test_json_array
|
||||
local
|
||||
ll: LINKED_LIST [INTEGER_8]
|
||||
ll2: detachable LINKED_LIST [detachable ANY]
|
||||
ja: detachable JSON_ARRAY
|
||||
jn: JSON_NUMBER
|
||||
jrep: STRING
|
||||
@@ -677,11 +684,10 @@ feature -- Test
|
||||
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]"))
|
||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||
ja := Void
|
||||
ja ?= json.value (ll)
|
||||
assert ("ja /= Void", ja /= Void)
|
||||
if attached ja as l_ja then
|
||||
if attached {JSON_ARRAY} json.value (ll) 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]"))
|
||||
else
|
||||
assert ("json.value (ll) /= Void", False)
|
||||
end
|
||||
|
||||
-- JSON representation -> JSON value -> Eiffel value
|
||||
@@ -691,22 +697,20 @@ feature -- Test
|
||||
-- values 0, 1, 1, 2, 3, 5
|
||||
jrep := "[0,1,1,2,3,5]"
|
||||
create parser.make_parser (jrep)
|
||||
ja := Void
|
||||
ja ?= parser.parse
|
||||
assert ("ja /= Void", ja /= Void)
|
||||
ll2 ?= json.object (ja, Void)
|
||||
assert ("ll2 /= Void", ll2 /= Void)
|
||||
--ll.compare_objects
|
||||
--ll2.compare_objects
|
||||
if attached ll2 as l_ll2 then
|
||||
assert ("ll2.is_equal (ll)", l_ll2.is_equal (ll))
|
||||
if attached {JSON_ARRAY} parser.parse as l_ja then
|
||||
if attached {LINKED_LIST [detachable ANY]} json.object (ja, Void) as l_ll2 then
|
||||
assert ("ll2.is_equal (ll)", l_ll2.is_equal (ll))
|
||||
else
|
||||
assert ("json.object (ja, Void) /= Void", False)
|
||||
end
|
||||
else
|
||||
assert ("parser.parse /= Void", False)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_json_object
|
||||
local
|
||||
t, t2: detachable HASH_TABLE [detachable ANY, STRING_GENERAL]
|
||||
t: detachable HASH_TABLE [detachable ANY, STRING_GENERAL]
|
||||
i: INTEGER
|
||||
ucs_key, ucs: STRING_32
|
||||
a: ARRAY [INTEGER]
|
||||
@@ -745,6 +749,7 @@ feature -- Test
|
||||
jo.put (ja, js_key)
|
||||
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]}"))
|
||||
|
||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
||||
create t.make (3)
|
||||
create ucs_key.make_from_string ("name")
|
||||
@@ -756,26 +761,28 @@ feature -- Test
|
||||
create ucs_key.make_from_string ("contents")
|
||||
a := <<0, 1, 1, 2, 3, 5>>
|
||||
t.put (a, ucs_key)
|
||||
jo := Void
|
||||
jo ?= json.value (t)
|
||||
assert ("jo /= Void", jo /= Void)
|
||||
if attached jo as l_jo then
|
||||
if attached {JSON_OBJECT} json.value (t) 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]}"))
|
||||
else
|
||||
assert ("json.value (t) /= Void", False)
|
||||
end
|
||||
|
||||
-- JSON representation -> JSON value -> Eiffel value -> JSON value -> JSON representation
|
||||
jrep := "{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}"
|
||||
create parser.make_parser (jrep)
|
||||
jo := Void
|
||||
jo ?= parser.parse
|
||||
assert ("jo /= Void", jo /= Void)
|
||||
t2 ?= json.object (jo, Void)
|
||||
assert ("t2 /= Void", t2 /= Void)
|
||||
jo ?= json.value (t2)
|
||||
assert ("jo /= Void", jo /= Void)
|
||||
if attached jo as l_jo then
|
||||
assert ("jrep.is_equal (jo.representation)", jrep.is_equal (jo.representation))
|
||||
end
|
||||
|
||||
if attached {JSON_OBJECT} parser.parse as l_jo then
|
||||
if attached {HASH_TABLE [detachable ANY, STRING_GENERAL]} json.object (l_jo, Void) as l_t2 then
|
||||
if attached json.value (l_t2) as l_jo_2 then
|
||||
assert ("jrep.is_equal (jo.representation)", jrep.is_equal (l_jo_2.representation))
|
||||
else
|
||||
assert ("json.value (t2) /= Void", False)
|
||||
end
|
||||
else
|
||||
assert ("json.object (jo, Void) /= Void", False)
|
||||
end
|
||||
else
|
||||
assert ("parser.parse /= Void", jo /= Void)
|
||||
end
|
||||
end
|
||||
|
||||
test_json_object_hash_code
|
||||
@@ -802,7 +809,6 @@ feature -- Test
|
||||
jv := json.value (gv)
|
||||
else
|
||||
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
|
||||
rescue
|
||||
exception := True
|
||||
@@ -813,17 +819,15 @@ feature -- Test
|
||||
-- Test converting from a JSON value to an Eiffel object based on a
|
||||
-- class for which no JSON converter has been registered.
|
||||
local
|
||||
gv : detachable OPERATING_ENVIRONMENT
|
||||
gv : detachable ANY
|
||||
jo: JSON_OBJECT
|
||||
exception: BOOLEAN
|
||||
do
|
||||
if not exception then
|
||||
create jo.make
|
||||
gv ?= json.object (jo, "OPERATING_ENVIRONMENT")
|
||||
gv := json.object (jo, "OPERATING_ENVIRONMENT")
|
||||
else
|
||||
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
|
||||
rescue
|
||||
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
|
||||
SHARED_EJSON
|
||||
rename default_create as shared_default_create end
|
||||
undefine
|
||||
default_create
|
||||
end
|
||||
|
||||
EQA_TEST_SET
|
||||
select default_create end
|
||||
|
||||
feature -- Test
|
||||
|
||||
test_custom_classes
|
||||
-- Parse JSON representation to JSON_OBJECT and test book collection converter.
|
||||
local
|
||||
bc: detachable BOOK_COLLECTION
|
||||
jbc: JSON_BOOK_CONVERTER
|
||||
jbcc: JSON_BOOK_COLLECTION_CONVERTER
|
||||
jac: JSON_AUTHOR_CONVERTER
|
||||
jo: detachable JSON_OBJECT
|
||||
parser: JSON_PARSER
|
||||
jrep: STRING
|
||||
do
|
||||
@@ -25,18 +34,20 @@ feature -- Test
|
||||
json.add_converter (jac)
|
||||
jrep := "{%"name%":%"Test collection%",%"books%":[{%"title%":%"eJSON: The Definitive Guide%",%"isbn%":%"123123-413243%",%"author%":{%"name%":%"Foo Bar%"}}]}"
|
||||
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 -- class TEST_JSON_CUSTOM_CLASS
|
||||
|
||||
Reference in New Issue
Block a user