Files
python/exercises/react/react_test.py

203 lines
6.7 KiB
Python
Raw Normal View History

import unittest
react: Update tests to v2.0.0 (#1346) * react: re-implement according to canonical data * react: update tests to v1.2.0 * react: correct test method name * react: fix example solution py2-compatibility * `copy()` method to `[:]` * `clear()` method to reassigning an empty list * react: refactor callbacks into pure functions * Merge upstream/master into react-1257 pig-latin: improve documentation (#1352) add "Running the tests" section to README template (#1271) * add "Running the tests" section to README template * regenerate README files with new template * rewrite Running the tests section for clarity * -switch to a plaintext-readable format -add link explaining py.test vs pytest queen-attack: re-implement according to canonical data 2.1.0 (#1351) * queen-attack: re-implement according to canonical data 2.1.0 * queen-attack: rewrite to pass tests v2.1.0 * queen-attack: remove redundant tests Rm `test_invalid_position_can_attack` since `Queen()` grains: update tests to v1.1.0 (#1357) Fix typo and minor style issues in all READMEs (#1356) * Fix typo and style in README template * Fix `shold` to `should` * Fix minor style issues in `### Common pytest options` * Add blank line after all headers * Regenerate all READMEs * Remove redundant periods in READMEs add `awaiting review` exempt label to stale.yml (#1364) go-counting: adapt tests to canonical data v1.0.0 (#1360) * set correct canonical data version * adapt tests and example solution to canonical data 1.0.0 * use assertSetEqual() consistently sgf-parsing: implement exercise (#1359) * implement sgf-parsing * fix import statement * create entry in config.json * fix __eq__ for Python2 lens-person: forego exercise (#1299) * lens-person: forego exercise `lens-person` is specific to languages with immutable data (i.e. Haskell). This concept does not exist in Python, so the exercise should be foregone. * remove bad comma Implement exercise bank-account (#1260) * Implement exercise bank-account * bank-account: generate README using configlet * bank-account: fix typo and comments pascals-triangle: update tests to canonical-date v1.2.0 (#1164) house: return singleton list for single verse (#1354) * house: return singleton list for single verse RE #1347 ([comment](https://github.com/exercism/python/pull/1347#discussion_r170626365)) * house: fix example solution to pass changed tests meetup: remove fail-safe for undefined MeetupDayException (#1345) * meetup: remove fail-safe for undefined MeetupDayException Fixes #1344 * meetup: define MeetupDayException in example solution * meetup: fix example solution to raise correct exception * meetup: fix flake8 violations * meetup: add exception message Curriculum (#1355) * select core exercises and set exercise ordering * add missing obvious topics * make list-ops a core exercise * rational-numbers: increase difficulty * unlocked_by core exercises only (exercism/configlet#61) Ref: https://github.com/exercism/v2-feedback/issues/61#issuecomment-324130763 alphametics: mark computationally intensive test as extra-credit (#1358) * alphametics: mark computationally intensive test as extra-credit While this test is canonical, it does not technically add additional coverage. This test serves as a test for efficiency (https://github.com/exercism/problem-specifications/pull/1024#issuecomment-347316485) of a solution, not completeness. Furthermore, here are the run-times for this exercise from the [latest Travis build]() (at the time of this writing): | Python Version | Run-time (seconds) | | --- | --- | | 2.7 | 3.155 | | 3.3 | 2.461 | | 3.4 | 3.567 | | 3.5 | 7.270 | | 3.6 | 0.774 | Notice that the optimized example solution is only "fast" in 3.6. * alphametics: add to list of exercises allowed to skip tests in CI bank-account: update README using configlet (#1366) go-counting: update README to latest description (#1367) bracket-push: update tests to v1.3.0 (#1369) isbn-verifier: update tests to v2.4.0 (#1373) * Replace test case - "invalid character in isbn" * Add test case with only 9 digits Python "bowling" test issue. (#1372) Fixes /exercism/python/#1371. yacht: implement exercise (#1368) * yacht: implement exercise * yacht: use enumeration of score categories * Use enumeration instead of plain strings to represent categories * Improve func `ns` in example solution crypto-square: Clarify rectangular output requirement in README (#1375) scale-generator: clarify docs. (#1374) * Removed most mentions of terms that were irrelevant ("diminished interval") or undefined ("accidentals"). * Removed irrelevant table * Some light reformatting * react: update tests to v2.0.0
2018-04-20 22:35:07 +08:00
from functools import partial
from react import InputCell, ComputeCell
# Tests adapted from `problem-specifications//canonical-data.json` @ v2.0.0
class ReactTests(unittest.TestCase):
def test_input_cells_have_a_value(self):
input_ = InputCell(10)
self.assertEqual(input_.value, 10)
def test_can_set_input_cell_value(self):
input_ = InputCell(4)
input_.value = 20
self.assertEqual(input_.value, 20)
def test_compute_cells_calculate_initial_value(self):
input_ = InputCell(1)
output = ComputeCell([input_], lambda inputs: inputs[0] + 1)
self.assertEqual(output.value, 2)
def test_compute_cells_take_inputs_in_right_order(self):
one = InputCell(1)
two = InputCell(2)
output = ComputeCell(
[one, two],
lambda inputs: inputs[0] + inputs[1]*10
)
self.assertEqual(output.value, 21)
def test_compute_cells_update_value_when_dependencies_are_changed(self):
input_ = InputCell(1)
output = ComputeCell([input_], lambda inputs: inputs[0] + 1)
input_.value = 3
self.assertEqual(output.value, 4)
def test_compute_cells_can_depend_on_other_compute_cells(self):
input_ = InputCell(1)
times_two = ComputeCell([input_], lambda inputs: inputs[0] * 2)
times_thirty = ComputeCell([input_], lambda inputs: inputs[0] * 30)
output = ComputeCell(
[times_two, times_thirty],
lambda inputs: inputs[0] + inputs[1]
)
self.assertEqual(output.value, 32)
input_.value = 3
self.assertEqual(output.value, 96)
def test_compute_cells_fire_callbacks(self):
input_ = InputCell(1)
output = ComputeCell([input_], lambda inputs: inputs[0] + 1)
observer = []
callback1 = self.callback_factory(observer)
output.add_callback(callback1)
input_.value = 3
self.assertEqual(observer[-1], 4)
def test_callbacks_only_fire_on_change(self):
input_ = InputCell(1)
output = ComputeCell(
[input_],
lambda inputs: 111 if inputs[0] < 3 else 222
)
observer = []
callback1 = self.callback_factory(observer)
output.add_callback(callback1)
input_.value = 2
self.assertEqual(observer, [])
input_.value = 4
self.assertEqual(observer[-1], 222)
def test_callbacks_do_not_report_already_reported_values(self):
input_ = InputCell(1)
output = ComputeCell([input_], lambda inputs: inputs[0] + 1)
observer = []
callback1 = self.callback_factory(observer)
output.add_callback(callback1)
input_.value = 2
self.assertEqual(observer[-1], 3)
input_.value = 3
self.assertEqual(observer[-1], 4)
def test_callbacks_can_fire_from_multiple_cells(self):
input_ = InputCell(1)
plus_one = ComputeCell([input_], lambda inputs: inputs[0] + 1)
minus_one = ComputeCell([input_], lambda inputs: inputs[0] - 1)
cb1_observer, cb2_observer = [], []
callback1 = self.callback_factory(cb1_observer)
callback2 = self.callback_factory(cb2_observer)
plus_one.add_callback(callback1)
minus_one.add_callback(callback2)
input_.value = 10
self.assertEqual(cb1_observer[-1], 11)
self.assertEqual(cb2_observer[-1], 9)
def test_callbacks_can_be_added_and_removed(self):
input_ = InputCell(11)
output = ComputeCell([input_], lambda inputs: inputs[0] + 1)
cb1_observer, cb2_observer, cb3_observer = [], [], []
callback1 = self.callback_factory(cb1_observer)
callback2 = self.callback_factory(cb2_observer)
callback3 = self.callback_factory(cb3_observer)
output.add_callback(callback1)
output.add_callback(callback2)
input_.value = 31
self.assertEqual(cb1_observer[-1], 32)
self.assertEqual(cb2_observer[-1], 32)
output.remove_callback(callback1)
output.add_callback(callback3)
input_.value = 41
self.assertEqual(cb2_observer[-1], 42)
self.assertEqual(cb3_observer[-1], 42)
# Expect callback1 not to be called.
self.assertEqual(len(cb1_observer), 1)
def test_removing_a_callback_multiple_times(self):
"""Guard against incorrect implementations which store their
callbacks in an array."""
input_ = InputCell(1)
output = ComputeCell([input_], lambda inputs: inputs[0] + 1)
cb1_observer, cb2_observer = [], []
callback1 = self.callback_factory(cb1_observer)
callback2 = self.callback_factory(cb2_observer)
output.add_callback(callback1)
output.add_callback(callback2)
output.remove_callback(callback1)
output.remove_callback(callback1)
output.remove_callback(callback1)
input_.value = 2
self.assertEqual(cb1_observer, [])
self.assertEqual(cb2_observer[-1], 3)
def test_callbacks_should_only_be_called_once(self):
"""Guard against incorrect implementations which call a callback
function multiple times when multiple dependencies change."""
input_ = InputCell(1)
plus_one = ComputeCell([input_], lambda inputs: inputs[0] + 1)
minus_one1 = ComputeCell([input_], lambda inputs: inputs[0] - 1)
minus_one2 = ComputeCell([minus_one1], lambda inputs: inputs[0] - 1)
output = ComputeCell(
[plus_one, minus_one2],
lambda inputs: inputs[0] * inputs[1]
)
observer = []
callback1 = self.callback_factory(observer)
output.add_callback(callback1)
input_.value = 4
self.assertEqual(observer[-1], 10)
def test_callbacks_not_called_so_long_as_output_not_changed(self):
"""Guard against incorrect implementations which call callbacks
if dependencies change but output value doesn't change."""
input_ = InputCell(1)
plus_one = ComputeCell([input_], lambda inputs: inputs[0] + 1)
minus_one = ComputeCell([input_], lambda inputs: inputs[0] - 1)
always_two = ComputeCell(
[plus_one, minus_one],
lambda inputs: inputs[0] - inputs[1]
)
observer = []
callback1 = self.callback_factory(observer)
always_two.add_callback(callback1)
input_.value = 2
input_.value = 3
input_.value = 4
input_.value = 5
self.assertEqual(observer, [])
# Utility functions.
def callback_factory(self, observer):
def callback(observer, value):
observer.append(value)
return partial(callback, observer)
if __name__ == '__main__':
unittest.main()