# Path: replayenhancer/Track.py
# class Track:
#     """Represents a Project CARS track.
# 
#     Notes
#     -----
#     Track detection requires the existence of `lib/track_data.json` that
#     contains the track data.
# 
#     Parameters
#     ----------
#     track_length : float
#         Length of the track, as found in the telemetry data.
#     """
#     def __init__(self, track_length):
#         try:
#             with open(os.path.join(
#                     os.path.dirname(__file__),
#                     'lib/track_data.json')) as json_file:
#                 json_data = load(json_file)
# 
#             matching_tracks = sorted(
#                 json_data.values(),
#                 key=lambda x: abs(x['length'] - float(track_length))
#             )
#             track = matching_tracks[0]
# 
#             try:
#                 # If all the pit keys are present, we populate the pit
#                 # information.
#                 self._pit_entry = (
#                     float(track['pit_entry'][0]),
#                     float(track['pit_entry'][1])
#                 )
#                 self._pit_exit = (
#                     float(track['pit_exit'][0]),
#                     float(track['pit_exit'][1])
#                 )
#                 self._pit_radius = float(track['pit_radius'])
#             except KeyError:
#                 self._pit = False
#             else:
#                 self._pit = True
#         except FileNotFoundError:
#             self._pit = False
#         except ValueError as json_error:
#             raise json_error
# 
#     def at_pit_entry(self, coordinates):
#         """Determines if a car is at the pit entry.
# 
#         Parameters
#         ----------
#         coordinates : [float, float, float]
#             Car coordinates.
# 
#         Returns
#         -------
#         bool
#             True if car is at the pit entry
#         """
#         return self._pit \
#             and abs(self._pit_entry[0]-coordinates[0]) < self._pit_radius \
#             and abs(self._pit_entry[1]-coordinates[2]) < self._pit_radius
# 
#     def at_pit_exit(self, coordinates):
#         """Determines if a car is at the pit exit.
# 
#         Parameters
#         ----------
#         coordinates : [float, float, float]
#             Car coordinates.
# 
#         Returns
#         -------
#         bool
#             True if car is at the pit exit
#         """
#         return self._pit \
#             and abs(self._pit_exit[0]-coordinates[0]) < self._pit_radius \
#             and abs(self._pit_exit[1]-coordinates[2]) < self._pit_radius


import sys
import unittest
from unittest.mock import mock_open, patch
from replayenhancer.Track import Track


    @unittest.skipIf(sys.version_info < (3, 5), "Not supported.")
    def test_init_bad_json(self):
        with patch('replayenhancer.Track.open', mock_open()), \
                patch('replayenhancer.Track.load') as mock_json_load:
            mock_json_load.side_effect = ValueError
            with self.assertRaises(ValueError):
                _ = Track(42.0)

    @unittest.skipIf(sys.version_info >= (3, 5), "Not supported.")
    def test_init_bad_json_pre35(self):
        with patch('builtins.open', mock_open()), \
                patch('replayenhancer.Track.load') as mock_json_load:
            mock_json_load.side_effect = ValueError
            with self.assertRaises(ValueError):
                _ = Track(42.0)

    @unittest.skipIf(sys.version_info < (3, 5), "Not supported.")
    def test_at_pit_entry_exact_true(self):
        with patch('replayenhancer.Track.open', mock_open()), \
                patch('replayenhancer.Track.load') as mock_json_load:
            mock_json_load.return_value = self.track_data
            instance = Track(42.0)
            self.assertTrue(instance.at_pit_entry([0, -99, 0]))

    @unittest.skipIf(sys.version_info >= (3, 5), "Not supported.")
    def test_at_pit_entry_exact_true_pre35(self):
        with patch('builtins.open', mock_open()), \
                patch('replayenhancer.Track.load') as mock_json_load:
            mock_json_load.return_value = self.track_data

# Path: replayenhancer/Track.py
# class Track:
#     """Represents a Project CARS track.
# 
#     Notes
#     -----
#     Track detection requires the existence of `lib/track_data.json` that
#     contains the track data.
# 
#     Parameters
#     ----------
#     track_length : float
#         Length of the track, as found in the telemetry data.
#     """
#     def __init__(self, track_length):
#         try:
#             with open(os.path.join(
#                     os.path.dirname(__file__),
#                     'lib/track_data.json')) as json_file:
#                 json_data = load(json_file)
# 
#             matching_tracks = sorted(
#                 json_data.values(),
#                 key=lambda x: abs(x['length'] - float(track_length))
#             )
#             track = matching_tracks[0]
# 
#             try:
#                 # If all the pit keys are present, we populate the pit
#                 # information.
#                 self._pit_entry = (
#                     float(track['pit_entry'][0]),
#                     float(track['pit_entry'][1])
#                 )
#                 self._pit_exit = (
#                     float(track['pit_exit'][0]),
#                     float(track['pit_exit'][1])
#                 )
#                 self._pit_radius = float(track['pit_radius'])
#             except KeyError:
#                 self._pit = False
#             else:
#                 self._pit = True
#         except FileNotFoundError:
#             self._pit = False
#         except ValueError as json_error:
#             raise json_error
# 
#     def at_pit_entry(self, coordinates):
#         """Determines if a car is at the pit entry.
# 
#         Parameters
#         ----------
#         coordinates : [float, float, float]
#             Car coordinates.
# 
#         Returns
#         -------
#         bool
#             True if car is at the pit entry
#         """
#         return self._pit \
#             and abs(self._pit_entry[0]-coordinates[0]) < self._pit_radius \
#             and abs(self._pit_entry[1]-coordinates[2]) < self._pit_radius
# 
#     def at_pit_exit(self, coordinates):
#         """Determines if a car is at the pit exit.
# 
#         Parameters
#         ----------
#         coordinates : [float, float, float]
#             Car coordinates.
# 
#         Returns
#         -------
#         bool
#             True if car is at the pit exit
#         """
#         return self._pit \
#             and abs(self._pit_exit[0]-coordinates[0]) < self._pit_radius \
#             and abs(self._pit_exit[1]-coordinates[2]) < self._pit_radius


# Path: test/test_Track.py
import sys
import unittest
from unittest.mock import mock_open, patch
from replayenhancer.Track import Track


    @unittest.skipIf(sys.version_info < (3, 5), "Not supported.")
    def test_init_bad_json(self):
        with patch('replayenhancer.Track.open', mock_open()), \
                patch('replayenhancer.Track.load') as mock_json_load:
            mock_json_load.side_effect = ValueError
            with self.assertRaises(ValueError):
                _ = Track(42.0)

    @unittest.skipIf(sys.version_info >= (3, 5), "Not supported.")
    def test_init_bad_json_pre35(self):
        with patch('builtins.open', mock_open()), \
                patch('replayenhancer.Track.load') as mock_json_load:
            mock_json_load.side_effect = ValueError
            with self.assertRaises(ValueError):
                _ = Track(42.0)

    @unittest.skipIf(sys.version_info < (3, 5), "Not supported.")
    def test_at_pit_entry_exact_true(self):
        with patch('replayenhancer.Track.open', mock_open()), \
                patch('replayenhancer.Track.load') as mock_json_load:
            mock_json_load.return_value = self.track_data
            instance = Track(42.0)
            self.assertTrue(instance.at_pit_entry([0, -99, 0]))

    @unittest.skipIf(sys.version_info >= (3, 5), "Not supported.")
    def test_at_pit_entry_exact_true_pre35(self):
        with patch('builtins.open', mock_open()), \
                patch('replayenhancer.Track.load') as mock_json_load:
            mock_json_load.return_value = self.track_data

            instance = Track(42.0)
