Skip to content

Commit df19f9b

Browse files
committed
more dict tests
1 parent 50fc426 commit df19f9b

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/datatype/DictTests.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -54,36 +54,78 @@ public void del() {
5454
assertPrints("{3: 4}\n", source);
5555
}
5656

57+
@Test
58+
public void dict() {
59+
String source = "print(type({}))\n" +
60+
"print(type(iter({1: 'a', 2: 'b'})))\n" +
61+
"print({})\n" +
62+
"print({1: 'a'})\n" +
63+
"print({1: 'a', 2: 'b'})\n";
64+
65+
assertPrints("<class 'dict'>\n" +
66+
"<class 'dict_keyiterator'>\n" +
67+
"{}\n" +
68+
"{1: 'a'}\n" +
69+
"{1: 'a', 2: 'b'}\n", source);
70+
71+
source = "d = {1: 1}\n" +
72+
"d.__setitem__(2, d)\n" +
73+
"print(d)\n";
74+
assertPrints("{1: 1, 2: {...}}\n", source);
75+
}
76+
5777
@Test
5878
public void dictViewKeys() {
5979
String source = "print(type({}.keys()))\n" +
6080
"print(type(iter({1: 'a', 2: 'b'}.keys())))\n" +
81+
"print({}.keys())\n" +
82+
"print({1: 'a'}.keys())\n" +
6183
"print({1: 'a', 2: 'b'}.keys())\n";
6284

6385
assertPrints("<class 'dict_keys'>\n" +
64-
"<class 'dict_keysiterator'>\n" +
86+
"<class 'dict_keyiterator'>\n" +
87+
"dict_keys([])\n" +
88+
"dict_keys([1])\n" +
6589
"dict_keys([1, 2])\n", source);
6690
}
6791

6892
@Test
6993
public void dictViewValues() {
7094
String source = "print(type({}.values()))\n" +
7195
"print(type(iter({1: 'a', 2: 'b'}.values())))\n" +
96+
"print({}.values())\n" +
97+
"print({1: 'a'}.values())\n" +
7298
"print({1: 'a', 2: 'b'}.values())\n";
7399

74100
assertPrints("<class 'dict_values'>\n" +
75-
"<class 'dict_valuesiterator'>\n" +
101+
"<class 'dict_valueiterator'>\n" +
102+
"dict_values([])\n" +
103+
"dict_values(['a'])\n" +
76104
"dict_values(['a', 'b'])\n", source);
105+
106+
source = "d = {1: 1}\n" +
107+
"d.__setitem__(2, d)\n" +
108+
"print(d.values())\n";
109+
assertPrints("dict_values([1, {1: 1, 2: {...}}])\n", source);
77110
}
78111

79112
@Test
80113
public void dictViewItems() {
81114
String source = "print(type({}.items()))\n" +
82115
"print(type(iter({1: 'a', 2: 'b'}.items())))\n" +
116+
"print({}.items())\n" +
117+
"print({1: 'a'}.items())\n" +
83118
"print({1: 'a', 2: 'b'}.items())\n";
84119

85120
assertPrints("<class 'dict_items'>\n" +
86-
"<class 'dict_itemsiterator'>\n" +
121+
"<class 'dict_itemiterator'>\n" +
122+
"dict_items([])\n" +
123+
"dict_items([(1, 'a')])\n" +
87124
"dict_items([(1, 'a'), (2, 'b')])\n", source);
125+
126+
source = "d = {1: 1}\n" +
127+
"d.__setitem__(2, d)\n" +
128+
"print(d.items())\n";
129+
assertPrints("dict_items([(1, 1), (2, {1: 1, 2: {...}})])\n", source);
88130
}
89131
}

graalpython/com.oracle.graal.python.test/src/tests/test_dict.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ def test_fromkeys():
103103
assert set(d.keys()) == {'a', 'b', 'c'}
104104
assert set(d.values()) == {o}
105105

106+
class preset(dict):
107+
def __init__(self):
108+
self['a'] = 1
109+
assert preset.fromkeys(['b']) == {'a':1, 'b':None}
110+
111+
class morethanoneinitargraiseserror(dict):
112+
def __init__(self, anotherArg):
113+
self.__init__()
114+
assert_raises(TypeError, morethanoneinitargraiseserror.fromkeys, [1])
115+
116+
class nosetitem:
117+
pass
118+
119+
class nosetitem2(dict):
120+
def __new__(cls):
121+
return nosetitem()
122+
assert_raises(TypeError, nosetitem2.fromkeys, [1])
123+
106124

107125
def test_init():
108126
d = dict(a=1, b=2, c=3)

0 commit comments

Comments
 (0)