Coverage for tests/unit/generation/test_coord_str_tuple.py: 100%

73 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-24 00:33 -0600

1import numpy as np 

2import pytest 

3 

4from maze_dataset.token_utils import ( 

5 _coord_to_strings_indexed, 

6 _coord_to_strings_UT, 

7 coord_str_to_coord_np, 

8 coord_str_to_tuple, 

9 coord_str_to_tuple_noneable, 

10 coords_to_strings, 

11 str_is_coord, 

12) 

13 

14 

15def test_coord_to_strings(): 

16 assert _coord_to_strings_UT((1, 2)) == ["(1,2)"] 

17 # assert _coord_to_strings_UT((-1, 0)) == ["(-1,0)"] 

18 

19 assert _coord_to_strings_indexed((1, 2)) == ["(", "1", ",", "2", ")"] 

20 assert _coord_to_strings_indexed((-1, 0)) == ["(", "-1", ",", "0", ")"] 

21 

22 

23# TODO: test for negative coords 

24 

25 

26def test_str_is_coord(): 

27 assert str_is_coord("(1,2)") 

28 # assert str_is_coord("(-1,0)") 

29 assert str_is_coord("(1,2,3)") 

30 assert not str_is_coord("1,2") 

31 assert str_is_coord("(1, 2)") 

32 assert str_is_coord("( 1 , 2 )") 

33 assert not str_is_coord("(1, 2)", allow_whitespace=False) 

34 

35 

36def test_coord_str_to_tuple(): 

37 assert coord_str_to_tuple("(1,2)") == (1, 2) 

38 # assert coord_str_to_tuple("(-1,0)") == (-1, 0) 

39 assert coord_str_to_tuple("(1,2,3)") == (1, 2, 3) 

40 assert coord_str_to_tuple("(1, 2)") == (1, 2) 

41 assert coord_str_to_tuple("( 1 , 2 )") == (1, 2) 

42 assert coord_str_to_tuple("(1, 2)", allow_whitespace=False) == (1, 2) 

43 

44 

45def test_coord_str_to_coord_np(): 

46 assert (coord_str_to_coord_np("(1,2)") == np.array([1, 2])).all() 

47 # assert (coord_str_to_coord_np("(-1,0)") == np.array([-1, 0])).all() 

48 assert (coord_str_to_coord_np("(1,2,3)") == np.array([1, 2, 3])).all() 

49 assert (coord_str_to_coord_np("(1, 2)") == np.array([1, 2])).all() 

50 assert (coord_str_to_coord_np("( 1 , 2 )") == np.array([1, 2])).all() 

51 assert ( 

52 coord_str_to_coord_np("(1, 2)", allow_whitespace=False) == np.array([1, 2]) 

53 ).all() 

54 

55 

56def test_coord_str_to_tuple_noneable(): 

57 assert coord_str_to_tuple_noneable("(1,2)") == (1, 2) 

58 # assert coord_str_to_tuple_noneable("(-1,0)") == (-1, 0) 

59 assert coord_str_to_tuple_noneable("(1,2,3)") == (1, 2, 3) 

60 assert coord_str_to_tuple_noneable("(1, 2)") == (1, 2) 

61 assert coord_str_to_tuple_noneable("( 1 , 2 )") == (1, 2) 

62 assert coord_str_to_tuple_noneable("1,2") is None 

63 

64 

65def test_coords_to_strings(): 

66 # TODO: resolve testing duplication in test_token_utils.py 

67 assert coords_to_strings( 

68 [(1, 2), "<ADJLIST_START>", (5, 6)], 

69 _coord_to_strings_UT, 

70 ) == ["(1,2)", "(5,6)"] 

71 assert coords_to_strings( 

72 [(1, 2), "<ADJLIST_START>", (5, 6)], 

73 _coord_to_strings_UT, 

74 when_noncoord="skip", 

75 ) == ["(1,2)", "(5,6)"] 

76 assert coords_to_strings( 

77 [(1, 2), "<ADJLIST_START>", (5, 6)], 

78 _coord_to_strings_UT, 

79 when_noncoord="include", 

80 ) == ["(1,2)", "<ADJLIST_START>", "(5,6)"] 

81 with pytest.raises(ValueError): # noqa: PT011 

82 # this is meant to raise an error, so type ignore 

83 coords_to_strings( # type: ignore[call-overload] 

84 [(1, 2), "<ADJLIST_START>", (5, 6)], 

85 _coord_to_strings_UT, 

86 when_noncoord="error", 

87 ) 

88 

89 assert coords_to_strings( 

90 [(1, 2), "<ADJLIST_START>", (5, 6)], 

91 _coord_to_strings_indexed, 

92 ) == ["(", "1", ",", "2", ")", "(", "5", ",", "6", ")"] 

93 assert coords_to_strings( 

94 [(1, 2), "<ADJLIST_START>", (5, 6)], 

95 _coord_to_strings_indexed, 

96 when_noncoord="skip", 

97 ) == ["(", "1", ",", "2", ")", "(", "5", ",", "6", ")"] 

98 assert coords_to_strings( 

99 [(1, 2), "<ADJLIST_START>", (5, 6)], 

100 _coord_to_strings_indexed, 

101 when_noncoord="include", 

102 ) == ["(", "1", ",", "2", ")", "<ADJLIST_START>", "(", "5", ",", "6", ")"] 

103 

104 

105def test_str_is_coord_2(): 

106 assert str_is_coord("(1,2)") 

107 assert str_is_coord("( 1 , 2 )") 

108 assert not str_is_coord("1,2") 

109 assert not str_is_coord("(1,2") 

110 assert not str_is_coord("1,2)") 

111 assert not str_is_coord("(1, a)") 

112 assert not str_is_coord("()") 

113 

114 

115def test_coord_str_to_tuple_excepts(): 

116 assert coord_str_to_tuple("(1,2)") == (1, 2) 

117 with pytest.raises(ValueError): # noqa: PT011 

118 coord_str_to_tuple("(1, a)") 

119 with pytest.raises(ValueError): # noqa: PT011 

120 coord_str_to_tuple("()") 

121 

122 

123def test_coord_str_to_tuple_noneable_2(): 

124 assert coord_str_to_tuple_noneable("(1,2)") == (1, 2) 

125 assert coord_str_to_tuple_noneable("1,2") is None 

126 assert coord_str_to_tuple_noneable("(1,2") is None 

127 assert coord_str_to_tuple_noneable("1,2)") is None 

128 assert coord_str_to_tuple_noneable("(1, a)") is None 

129 assert coord_str_to_tuple_noneable("()") is None 

130 

131 

132def test_coord_to_str(): 

133 assert _coord_to_strings_UT((1, 2)) == ["(1,2)"] 

134 assert _coord_to_strings_UT((10, 20)) == ["(10,20)"] 

135 assert _coord_to_strings_UT((0, 0)) == ["(0,0)"] 

136 with pytest.raises(TypeError): 

137 # this is meant to raise an error, so type ignore 

138 _coord_to_strings_UT(1) # type: ignore[arg-type] 

139 

140 assert _coord_to_strings_indexed((1, 2)) == ["(", "1", ",", "2", ")"] 

141 assert _coord_to_strings_indexed((10, 20)) == ["(", "10", ",", "20", ")"] 

142 assert _coord_to_strings_indexed((0, 0)) == ["(", "0", ",", "0", ")"] 

143 with pytest.raises(TypeError): 

144 _coord_to_strings_indexed(1) # type: ignore[arg-type]