From 6ad76fc34885747ebe34b0e5edc5fb99b11e2cdd Mon Sep 17 00:00:00 2001 From: "Joel Z. Leibo" Date: Tue, 5 Dec 2023 09:59:54 -0800 Subject: [PATCH] test of a component to set the pattern for future tests PiperOrigin-RevId: 588107004 Change-Id: I495412edf5b319a4763da69fbd919a521c55d238 --- .../components/game_master/player_status.py | 2 +- .../game_master/player_status_test.py | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 concordia/components/game_master/player_status_test.py diff --git a/concordia/components/game_master/player_status.py b/concordia/components/game_master/player_status.py index 193097b0..9c365022 100644 --- a/concordia/components/game_master/player_status.py +++ b/concordia/components/game_master/player_status.py @@ -67,7 +67,7 @@ def partial_state( return self._partial_states[player_name] def update(self) -> None: - self._state = '\n' + self._state = '' self._partial_states = {name: '' for name in self._player_names} per_player_prompt = {} for player_name in self._player_names: diff --git a/concordia/components/game_master/player_status_test.py b/concordia/components/game_master/player_status_test.py new file mode 100644 index 00000000..266e8936 --- /dev/null +++ b/concordia/components/game_master/player_status_test.py @@ -0,0 +1,56 @@ +# Copyright 2023 DeepMind Technologies Limited. +# +# 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 +# +# https://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. + +"""Tests for player_status.py.""" + +import datetime +from unittest import mock + +from absl.testing import absltest +from absl.testing import parameterized +from concordia.associative_memory import associative_memory +from concordia.components.game_master import player_status +from concordia.language_model import language_model + + +def _clock_now() -> datetime.datetime: + return datetime.datetime(2023, 1, 1, 0, 0, 0) + + +class PlayerStatusTest(parameterized.TestCase): + + @parameterized.named_parameters( + dict(testcase_name="library", location="at the library"), + dict(testcase_name="memory", location="at the pub"), + ) + def test_output_in_right_format(self, location): + model = mock.create_autospec( + language_model.LanguageModel, instance=True, spec_set=True) + model.sample_text.return_value = location + memory = mock.create_autospec(associative_memory.AssociativeMemory, + instance=True) + memory.retrieve_associative.return_value = "gibberish" + player_names = ["Alice", "Bob"] + component = player_status.PlayerStatus( + clock_now=_clock_now, + model=model, + memory=memory, + player_names=player_names) + component.update() + expected = "\n".join([f" {name} is {location}" for name in player_names]) + self.assertEqual(component.state(), expected + "\n") + + +if __name__ == "__main__": + absltest.main()