Skip to content

Commit

Permalink
Added missing copyright notices and removed superfluous whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Narea committed Feb 18, 2015
1 parent ec0e7f2 commit 5e9de40
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 52 deletions.
28 changes: 21 additions & 7 deletions tests/test_generic_utils.py
@@ -1,3 +1,17 @@
# Copyright 2013-2015, Gustavo Narea.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from nose.tools import assert_false
from nose.tools import eq_
from nose.tools import ok_
Expand All @@ -7,29 +21,29 @@


class TestDuplicatedIterableItems(object):

def test_no_items(self):
original_iterable = []
duplicated_items = get_duplicated_iterable_items(original_iterable)
eq_(0, len(duplicated_items))

def test_unique_items(self):
original_iterable = [1, 2, 3]
duplicated_items = get_duplicated_iterable_items(original_iterable)
eq_(0, len(duplicated_items))

def test_one_duplicate(self):
original_iterable = [1, 2, 2]
duplicated_items = get_duplicated_iterable_items(original_iterable)
eq_(1, len(duplicated_items))
ok_(2 in duplicated_items)

def test_two_duplicates_of_same_item(self):
original_iterable = [1, 2, 2, 2]
duplicated_items = get_duplicated_iterable_items(original_iterable)
eq_(1, len(duplicated_items))
ok_(2 in duplicated_items)

def test_duplicates_of_various_items(self):
original_iterable = [1, 1, 2, 2]
duplicated_items = get_duplicated_iterable_items(original_iterable)
Expand All @@ -39,7 +53,7 @@ def test_duplicates_of_various_items(self):


class TestPythonIdentifierCheck(object):

def test_valid_identifiers(self):
valid_identifiers = (
"_",
Expand All @@ -52,7 +66,7 @@ def test_valid_identifiers(self):
)
for identifier in valid_identifiers:
ok_(is_valid_python_identifier(identifier), identifier)

def test_invalid_identifiers(self):
invalid_identifiers = (
"",
Expand Down
90 changes: 52 additions & 38 deletions tests/test_record.py
@@ -1,3 +1,17 @@
# Copyright 2013-2015, Gustavo Narea.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pickle import dumps as pickle_serialize
from pickle import loads as pickle_deserialize

Expand All @@ -16,41 +30,41 @@


class TestInitialization(object):

def test_initialization(self):
my_point = Point(1, 3)
ok_(isinstance(my_point, Point))
eq_(my_point.coordinate_x, 1)
eq_(my_point.coordinate_y, 3)

def test_initialization_by_name(self):
my_point = Point(coordinate_x=1, coordinate_y=3)
eq_(my_point.coordinate_x, 1)
eq_(my_point.coordinate_y, 3)

def test_subtype(self):
my_point_3d = Point3D(1, 3, 5)
eq_(my_point_3d.coordinate_x, 1)
eq_(my_point_3d.coordinate_y, 3)
eq_(my_point_3d.coordinate_z, 5)

def test_overriding_default_field_value(self):
Point = Record.create_type("Point", "coordinate_x", coordinate_x=2)
my_point = Point(1)
eq_(my_point.coordinate_x, 1)

def test_skipping_field_with_default_value(self):
Point = Record.create_type("Point", "coordinate_x", coordinate_x=2)
my_point = Point()
eq_(my_point.coordinate_x, 2)

def test_skipping_field_without_default_value(self):
assert_raises_regexp(
RecordInstanceError,
'^Field "coordinate_x" is undefined$',
Point,
)

def test_setting_unknown_field(self):
# By position
assert_raises_regexp(
Expand All @@ -60,7 +74,7 @@ def test_setting_unknown_field(self):
1,
3,
0,
-1,
- 1,
)
# By name
assert_raises_regexp(
Expand All @@ -71,7 +85,7 @@ def test_setting_unknown_field(self):
3,
coordinate_z=0,
)

def test_field_value_set_multiple_times(self):
assert_raises_regexp(
RecordInstanceError,
Expand All @@ -81,24 +95,24 @@ def test_field_value_set_multiple_times(self):
3,
coordinate_x=2,
)

def test_copy(self):
original_point = Point(1, 3)

derived_point = original_point.copy()
derived_point.coordinate_y = 5

eq_(original_point.coordinate_x, derived_point.coordinate_x)
eq_(3, original_point.coordinate_y)
eq_(5, derived_point.coordinate_y)

def test_generalization(self):
my_point_3d = Point3D(1, 3, 5)
my_point = Point.init_from_specialization(my_point_3d)
ok_(isinstance(my_point, Point))
eq_(my_point.coordinate_x, my_point_3d.coordinate_x)
eq_(my_point.coordinate_y, my_point_3d.coordinate_y)

def test_invalid_generalization(self):
my_point = Point(1, 3)
assert_raises_regexp(
Expand All @@ -107,15 +121,15 @@ def test_invalid_generalization(self):
Point3D.init_from_specialization,
my_point,
)

def test_specialization(self):
my_point = Point(1, 3)
my_point_3d = Point3D.init_from_generalization(my_point, coordinate_z=5)
ok_(isinstance(my_point_3d, Point3D))
eq_(my_point_3d.coordinate_x, my_point.coordinate_x)
eq_(my_point_3d.coordinate_y, my_point.coordinate_y)
eq_(my_point_3d.coordinate_z, 5)

def test_invalid_specialization(self):
my_point_3d = Point3D(1, 3, 5)
assert_raises_regexp(
Expand All @@ -124,7 +138,7 @@ def test_invalid_specialization(self):
Point.init_from_generalization,
my_point_3d,
)

def test_incomplete_specialization(self):
my_point = Point(1, 3)
assert_raises_regexp(
Expand All @@ -133,7 +147,7 @@ def test_incomplete_specialization(self):
Point3D.init_from_generalization,
my_point,
)

def test_specialization_overriding_field_values(self):
my_point = Point(1, 3)
assert_raises_regexp(
Expand All @@ -147,66 +161,66 @@ def test_specialization_overriding_field_values(self):


class TestComparison(object):

def test_same_type_and_same_field_values(self):
point1 = Point(1, 3)
point2 = Point(1, 3)
self.assert_equals(point1, point2)

def test_same_type_and_different_field_values(self):
point1 = Point(2, 4)
point2 = Point(6, 8)
self.assert_not_equals(point1, point2)

def test_different_type_and_same_field_values(self):
point = Point(2, 4)

AlternativePoint = Record.create_type(
"AlternativePoint",
"coordinate_x",
"coordinate_y",
)
alternative_point = AlternativePoint(2, 4)

self.assert_not_equals(point, alternative_point)

def test_specialization(self):
point = Point(2, 4)
point_3d = Point3D(2, 4, 8)
self.assert_not_equals(point, point_3d)

def test_non_record(self):
point = Point(1, 3)
self.assert_not_equals(point, object())

#{ Check equality in all possible ways

# In Python, "a == b = True" DOESN'T necessarily mean that "a != b = False"
# nor "b == a = True".

@staticmethod
def assert_equals(item1, item2):
ok_(item1 == item2)
ok_(item2 == item1)
assert_false(item1 != item2)
assert_false(item2 != item1)

@staticmethod
def assert_not_equals(item1, item2):
ok_(item1 != item2)
ok_(item2 != item1)
assert_false(item1 == item2)
assert_false(item2 == item1)

#}


class TestFieldAccess(object):

def test_getting_valid_field(self):
point = Point(1, 3)
eq_(1, point.coordinate_x)

def test_getting_invalid_field(self):
point = Point(1, 3)
assert_raises_regexp(
Expand All @@ -216,28 +230,28 @@ def test_getting_invalid_field(self):
point,
"coordinate_z",
)

def test_getting_all_field_values(self):
point = Point(1, 3)
expected_values = {'coordinate_x': 1, 'coordinate_y': 3}
actual_values = point.get_field_values()
eq_(expected_values, actual_values)

actual_values['coordinate_x'] = 7
eq_(1, point.coordinate_x, "The field values must've been copied")

def test_setting_valid_field(self):
point = Point(1, 3)
point.coordinate_x = 5

# Ensure it wasn't just set on __dict__
field_values = point.get_field_values()
eq_(5, field_values['coordinate_x'])

def test_setting_invalid_field(self):
point = Point(1, 3)
point.coordinate_z = 5

# Ensure it wasn't set on __dict__
field_values = point.get_field_values()
assert_not_in("coordinate_z", field_values)
Expand Down

0 comments on commit 5e9de40

Please sign in to comment.