Skip to content

Commit ad72558

Browse files
committed
Everything works with full tests.
1 parent 2b276cb commit ad72558

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

histogrammar/util.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,18 @@ def function(datum):
247247
except AttributeError:
248248
v, = varname # otherwise, use the one and only variable
249249
if v is None: # as the object (only discover it once)
250-
try:
251-
v, = set(c.co_names) - set(context.keys())
252-
except ValueError:
250+
v = set(c.co_names) - set(context.keys())
251+
if len(v) > 1:
253252
raise NameError("more than one unrecognized variable names in single-argument function: {0}".format(set(c.co_names) - set(context.keys())))
253+
elif len(v) == 0:
254+
v = None
255+
else:
256+
v = list(v)[0]
257+
254258
varname[0] = v
255259

256-
context.update({v: datum})
260+
if v is not None:
261+
context.update({v: datum})
257262

258263
return eval(c, context)
259264

test/testbasic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from histogrammar import *
2323

24-
class TestOriginal(unittest.TestCase):
24+
class TestBasic(unittest.TestCase):
2525
simple = [3.4, 2.2, -1.8, 0.0, 7.3, -4.7, 1.6, 0.0, -3.0, -1.7]
2626

2727
class Struct(object):

test/testspec.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,24 @@ def compare(self, x, y, name):
3838
else:
3939
sys.stderr.write("{0:50s} | {1}\n".format(leftline, rightline))
4040
self.assertEqual(Factory.fromJson(x), Factory.fromJson(y))
41-
41+
4242
def runTest(self):
4343
testdata = json.load(open("../histogrammar-multilang/test-data.json"))
4444
for x in testdata:
4545
for k, v in x.items():
4646
if k != "strings" and v in ("nan", "inf", "-inf"):
4747
x[k] = float(v)
48-
48+
4949
testresults = json.load(open("../histogrammar-multilang/test-results.json"))
5050

51+
def stripNames(x):
52+
if hasattr(x, "quantity"):
53+
x.quantity.name = None
54+
elif hasattr(x, "quantityName"):
55+
x.quantityName = None
56+
for xi in x.children:
57+
stripNames(xi)
58+
5159
for testresult in testresults:
5260
sys.stderr.write(testresult["expr"] + "\n")
5361

@@ -57,15 +65,48 @@ def runTest(self):
5765

5866
h1 = eval(testresult["expr"])
5967
h2 = eval(testresult["expr"])
68+
6069
self.compare(h1.toJson(), zero, "NAMED ZERO")
70+
self.compare((h1 + h1).toJson(), zero, "NAMED ZERO + ZERO")
71+
self.compare(h1.zero().toJson(), zero, "NAMED ZERO.zero()")
6172

6273
for x in testdata:
6374
h1.fill(x)
6475
h2.fill(x)
6576
self.compare(h1.toJson(), one, "NAMED ONE")
77+
self.compare(h1.zero().toJson(), zero, "NAMED ONE.zero()")
78+
self.compare((h1 + h1.zero()).toJson(), one, "NAMED ONE + ZERO")
79+
self.compare((h1.zero() + h1).toJson(), one, "NAMED ZERO + ONE")
6680

6781
self.compare((h1 + h2).toJson(), two, "NAMED TWO VIA PLUS")
6882

6983
for x in testdata:
7084
h1.fill(x)
7185
self.compare(h1.toJson(), two, "NAMED TWO VIA FILL")
86+
87+
zero = testresult["zero-anonymous"]
88+
one = testresult["one-anonymous"]
89+
two = testresult["two-anonymous"]
90+
91+
h1 = eval(testresult["expr"])
92+
stripNames(h1)
93+
h2 = eval(testresult["expr"])
94+
stripNames(h2)
95+
96+
self.compare(h1.toJson(), zero, "ANONYMOUS ZERO")
97+
self.compare((h1 + h1).toJson(), zero, "ANONYMOUS ZERO + ZERO")
98+
self.compare(h1.zero().toJson(), zero, "ANONYMOUS ZERO.zero()")
99+
100+
for x in testdata:
101+
h1.fill(x)
102+
h2.fill(x)
103+
self.compare(h1.toJson(), one, "ANONYMOUS ONE")
104+
self.compare(h1.zero().toJson(), zero, "ANONYMOUS ONE.zero()")
105+
self.compare((h1 + h1.zero()).toJson(), one, "ANONYMOUS ONE + ZERO")
106+
self.compare((h1.zero() + h1).toJson(), one, "ANONYMOUS ZERO + ONE")
107+
108+
self.compare((h1 + h2).toJson(), two, "ANONYMOUS TWO VIA PLUS")
109+
110+
for x in testdata:
111+
h1.fill(x)
112+
self.compare(h1.toJson(), two, "ANONYMOUS TWO VIA FILL")

0 commit comments

Comments
 (0)