Skip to content

Commit 0db02a8

Browse files
committed
Merge pull request #8 from Conaclos/working
Tests - Update syntax and improve implementation
2 parents a74cda2 + 3617731 commit 0db02a8

File tree

9 files changed

+306
-256
lines changed

9 files changed

+306
-256
lines changed

test/autotest/test_suite/author.e

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1-
class AUTHOR
1+
2+
class
3+
AUTHOR
24

35
create
46
make
57

68
feature {NONE} -- Initialization
79

810
make (a_name: STRING_32)
11+
-- Create an author with `a_name' as `name'.
912
do
1013
set_name (a_name)
14+
ensure
15+
name_set: name = a_name
1116
end
1217

1318
feature -- Access
1419

1520
name: STRING_32
21+
-- Author name
1622

17-
feature -- Status setting
23+
feature -- Change
1824

1925
set_name (a_name: STRING_32)
26+
-- Set `name' with `a_name'.
2027
do
2128
name := a_name
29+
ensure
30+
name_set: name = a_name
2231
end
2332

2433
end -- class AUTHOR

test/autotest/test_suite/book.e

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,60 @@
1-
class BOOK
1+
2+
class
3+
BOOK
24

35
create
46
make
57

68
feature {NONE} -- Initialization
79

8-
make (a_title: STRING_32; an_author: AUTHOR; an_isbn: STRING_32)
10+
make (a_title: STRING_32; a_author: AUTHOR; a_isbn: STRING_32)
11+
-- Create a book with `a_title' as `title',
12+
-- `a_author' as `author', and `a_isbn' as `isbn',
913
do
1014
set_title (a_title)
11-
set_author (an_author)
12-
set_isbn (an_isbn)
15+
set_author (a_author)
16+
set_isbn (a_isbn)
17+
ensure
18+
title_set: title = a_title
19+
author_set: author = a_author
20+
isbn_set: isbn = a_isbn
1321
end
1422

1523
feature -- Access
1624

1725
title: STRING_32
26+
-- Main title.
1827

1928
isbn: STRING_32
29+
-- ISBN.
2030

2131
author: AUTHOR
32+
-- Author.
2233

23-
feature -- Status setting
34+
feature -- Change
2435

2536
set_title (a_title: STRING_32)
37+
-- Set `title' with `a_title'.
2638
do
2739
title := a_title
40+
ensure
41+
title_set: title = a_title
2842
end
2943

30-
set_author (an_author: AUTHOR)
44+
set_author (a_author: AUTHOR)
45+
-- Set `author' with `a_author'.
3146
do
32-
author := an_author
47+
author := a_author
48+
ensure
49+
author_set: author = a_author
3350
end
3451

35-
set_isbn (an_isbn: STRING_32)
52+
set_isbn (a_isbn: STRING_32)
53+
-- Set `isbn' with `a_isbn'.
3654
do
37-
isbn := an_isbn
55+
isbn := a_isbn
56+
ensure
57+
isbn_set: isbn = a_isbn
3858
end
3959

4060
end -- class BOOK
Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,78 @@
1-
class BOOK_COLLECTION
1+
class
2+
BOOK_COLLECTION
23

34
create
45
make
56

67
feature {NONE} -- Initialization
78

89
make (a_name: STRING_32)
10+
-- Create a collection of book with `a_name' as `name'
911
do
1012
set_name (a_name)
1113
create book_index.make (10)
14+
ensure
15+
name_set: name = a_name
1216
end
1317

1418
feature -- Access
1519

1620
name: STRING_32
21+
-- Name.
1722

1823
books: LIST [BOOK]
24+
-- collection of book.
1925
do
20-
from
21-
create {LINKED_LIST [BOOK]} Result.make
22-
book_index.start
23-
until
24-
book_index.after
25-
loop
26-
Result.append (book_index.item_for_iteration)
27-
book_index.forth
26+
create {LINKED_LIST [BOOK]} Result.make
27+
across book_index as it loop
28+
Result.append (it.item)
2829
end
2930
end
3031

31-
books_by_author (an_author: STRING_32): detachable LIST [BOOK]
32+
books_by_author (a_author: STRING_32): LIST [BOOK]
33+
-- Books wrote by `a_author' in this collection.
3234
do
33-
if book_index.has (an_author) then
34-
Result := book_index @ an_author
35+
if attached book_index [a_author] as l_result then
36+
Result := l_result
3537
else
3638
create {LINKED_LIST [BOOK]} Result.make
3739
end
3840
end
3941

40-
feature -- Status setting
42+
feature -- Change
4143

4244
set_name (a_name: STRING_32)
45+
-- Set `name' with `a_name'.
4346
do
4447
name := a_name
48+
ensure
49+
name_set: name = a_name
4550
end
4651

4752
add_book (a_book: BOOK)
53+
-- Extend collection with `a_book'.
4854
local
4955
l: detachable LIST [BOOK]
5056
do
51-
if book_index.has (a_book.author.name) then
52-
l := book_index.at ( a_book.author.name )
53-
else
57+
l := book_index.at (a_book.author.name )
58+
if l = Void then
5459
create {LINKED_LIST [BOOK]} l.make
5560
book_index.put (l, a_book.author.name)
5661
end
57-
if attached l as la then
58-
la.force (a_book)
59-
end
60-
62+
l.force (a_book)
6163
end
6264

6365
add_books (book_list: like books)
64-
66+
-- Append collection with `book_list'.
6567
do
66-
from
67-
book_list.start
68-
until
69-
book_list.after
70-
loop
71-
add_book (book_list.item)
72-
book_list.forth
68+
across book_list as it loop
69+
add_book (it.item)
7370
end
7471
end
7572

7673
feature {NONE} -- Implementation
7774

7875
book_index: HASH_TABLE [LIST [BOOK], STRING_32]
76+
-- Association of author name and its books.
7977

8078
end -- class BOOK_COLLECTION

test/autotest/test_suite/json_author_converter.e

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ note
44
date: "$Date$"
55
revision: "$Revision$"
66

7-
class JSON_AUTHOR_CONVERTER
7+
class
8+
JSON_AUTHOR_CONVERTER
89

910
inherit
1011
JSON_CONVERTER
@@ -29,12 +30,10 @@ feature -- Access
2930
feature -- Conversion
3031

3132
from_json (j: like to_json): detachable like object
32-
local
33-
ucs: detachable STRING_32
3433
do
35-
ucs ?= json.object (j.item (name_key), Void)
36-
check ucs /= Void end
37-
create Result.make (ucs)
34+
if attached {STRING_32} json.object (j.item (name_key), Void) as l_name then
35+
create Result.make (l_name)
36+
end
3837
end
3938

4039
to_json (o: like object): JSON_OBJECT
@@ -43,9 +42,10 @@ feature -- Conversion
4342
Result.put (json.value (o.name), name_key)
4443
end
4544

46-
feature {NONE} -- Implementation
45+
feature {NONE} -- Implementation
4746

4847
name_key: JSON_STRING
48+
-- Author's name label.
4949
once
5050
create Result.make_json ("name")
5151
end

test/autotest/test_suite/json_book_collection_converter.e

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ note
44
date: "$Date$"
55
revision: "$Revision$"
66

7-
class JSON_BOOK_COLLECTION_CONVERTER
7+
class
8+
JSON_BOOK_COLLECTION_CONVERTER
89

910
inherit
1011
JSON_CONVERTER
@@ -30,30 +31,32 @@ feature -- Conversion
3031

3132
from_json (j: like to_json): detachable like object
3233
local
33-
ucs: detachable STRING_32
34-
ll: LINKED_LIST [BOOK]
35-
b: detachable BOOK
36-
ja: detachable JSON_ARRAY
37-
i: INTEGER
34+
l_books: LINKED_LIST [BOOK]
3835
do
39-
ucs ?= json.object (j.item (name_key), Void)
40-
check ucs /= Void end
41-
create Result.make (ucs)
42-
ja ?= j.item (books_key)
43-
check ja /= Void end
44-
from
45-
i := 1
46-
create ll.make
47-
until
48-
i > ja.count
49-
loop
50-
b ?= json.object (ja [i], "BOOK")
51-
check b /= Void end
52-
ll.force (b)
53-
i := i + 1
36+
if
37+
attached {STRING_32} json.object (j.item (name_key), Void) as l_name and
38+
attached {JSON_ARRAY} j.item (books_key) as l_json_array
39+
then
40+
create Result.make (l_name)
41+
create l_books.make
42+
43+
across
44+
l_json_array as it
45+
until
46+
Result = Void
47+
loop
48+
if attached {BOOK} json.object (it.item, "BOOK") as l_book then
49+
l_books.extend (l_book)
50+
else
51+
Result := Void
52+
-- Failed
53+
end
54+
end
55+
56+
if Result /= Void then
57+
Result.add_books (l_books)
58+
end
5459
end
55-
check ll /= Void end
56-
Result.add_books (ll)
5760
end
5861

5962
to_json (o: like object): JSON_OBJECT
@@ -66,11 +69,13 @@ feature -- Conversion
6669
feature {NONE} -- Implementation
6770

6871
name_key: JSON_STRING
72+
-- Collection's name label.
6973
once
7074
create Result.make_json ("name")
7175
end
7276

7377
books_key: JSON_STRING
78+
-- Book list label.
7479
once
7580
create Result.make_json ("books")
7681
end

test/autotest/test_suite/json_book_converter.e

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ note
44
date: "$Date$"
55
revision: "$Revision$"
66

7-
class JSON_BOOK_CONVERTER
7+
class
8+
JSON_BOOK_CONVERTER
89

910
inherit
1011
JSON_CONVERTER
@@ -31,17 +32,14 @@ feature -- Access
3132
feature -- Conversion
3233

3334
from_json (j: like to_json): detachable like object
34-
local
35-
ucs1, ucs2: detachable STRING_32
36-
a: detachable AUTHOR
3735
do
38-
ucs1 ?= json.object (j.item (title_key), Void)
39-
check ucs1 /= Void end
40-
ucs2 ?= json.object (j.item (isbn_key), Void)
41-
check ucs2 /= Void end
42-
a ?= json.object (j.item (author_key), "AUTHOR")
43-
check a /= Void end
44-
create Result.make (ucs1, a, ucs2)
36+
if
37+
attached {STRING_32} json.object (j.item (title_key), Void) as l_title and
38+
attached {STRING_32} json.object (j.item (isbn_key), Void) as l_isbn and
39+
attached {AUTHOR} json.object (j.item (author_key), "AUTHOR") as l_author
40+
then
41+
create Result.make (l_title, l_author, l_isbn)
42+
end
4543
end
4644

4745
to_json (o: like object): JSON_OBJECT
@@ -52,19 +50,22 @@ feature -- Conversion
5250
Result.put (json.value (o.author), author_key)
5351
end
5452

55-
feature {NONE} -- Implementation
53+
feature {NONE} -- Implementation
5654

5755
title_key: JSON_STRING
56+
-- Book's title label.
5857
once
5958
create Result.make_json ("title")
6059
end
6160

6261
isbn_key: JSON_STRING
62+
-- Book ISBN label.
6363
once
6464
create Result.make_json ("isbn")
6565
end
6666

6767
author_key: JSON_STRING
68+
-- Author label.
6869
once
6970
create Result.make_json ("author")
7071
end

0 commit comments

Comments
 (0)