|
5 | 5 |
|
6 | 6 | class TestReprMethods(unittest.TestCase): |
7 | 7 | """Test __repr__ methods for Demand, Supply, and PPF classes.""" |
8 | | - |
| 8 | + |
9 | 9 | def test_demand_repr_simple(self): |
10 | 10 | """Test basic demand curve representations.""" |
11 | 11 | # Standard demand: P = 10 - Q |
12 | 12 | d1 = Demand(10, -1) |
13 | 13 | self.assertEqual(repr(d1), "Demand: P = 10-Q") |
14 | | - |
| 14 | + |
15 | 15 | # Demand with different slope: P = 20 - 2Q |
16 | 16 | d2 = Demand(20, -2) |
17 | 17 | self.assertEqual(repr(d2), "Demand: P = 20-2Q") |
18 | | - |
| 18 | + |
19 | 19 | # Demand with fractional slope: P = 15 - 0.5Q |
20 | 20 | d3 = Demand(15, -0.5) |
21 | 21 | self.assertEqual(repr(d3), "Demand: P = 15-0.5Q") |
22 | | - |
| 22 | + |
23 | 23 | # Demand with small intercept: P = 0.5 - Q |
24 | 24 | d4 = Demand(0.5, -1) |
25 | 25 | self.assertEqual(repr(d4), "Demand: P = 0.5-Q") |
26 | | - |
| 26 | + |
27 | 27 | def test_demand_repr_special_cases(self): |
28 | 28 | """Test special cases for demand representations.""" |
29 | 29 | # Perfectly elastic demand (horizontal) |
30 | 30 | d1 = Demand(5, 0) |
31 | 31 | self.assertEqual(repr(d1), "Demand: P = 5") |
32 | | - |
| 32 | + |
33 | 33 | # Piecewise demand |
34 | 34 | d_piece1 = Demand(20, -1) |
35 | 35 | d_piece2 = Demand(10, -0.5) |
36 | 36 | d_piecewise = d_piece1 + d_piece2 |
37 | 37 | self.assertEqual(repr(d_piecewise), "Demand: 2-piece piecewise function") |
38 | | - |
| 38 | + |
39 | 39 | def test_supply_repr_simple(self): |
40 | 40 | """Test basic supply curve representations.""" |
41 | 41 | # Standard supply: P = 2 + Q |
42 | 42 | s1 = Supply(2, 1) |
43 | 43 | self.assertEqual(repr(s1), "Supply: P = 2+Q") |
44 | | - |
| 44 | + |
45 | 45 | # Supply with different slope: P = 5 + 2Q |
46 | 46 | s2 = Supply(5, 2) |
47 | 47 | self.assertEqual(repr(s2), "Supply: P = 5+2Q") |
48 | | - |
| 48 | + |
49 | 49 | # Supply with fractional slope: P = 1 + 0.5Q |
50 | 50 | s3 = Supply(1, 0.5) |
51 | 51 | self.assertEqual(repr(s3), "Supply: P = 1+0.5Q") |
52 | | - |
| 52 | + |
53 | 53 | # Supply through origin: P = Q |
54 | 54 | s4 = Supply(0, 1) |
55 | 55 | self.assertEqual(repr(s4), "Supply: P = Q") |
56 | | - |
| 56 | + |
57 | 57 | # Supply with negative intercept: P = -2 + 3Q |
58 | 58 | s5 = Supply(-2, 3) |
59 | 59 | self.assertEqual(repr(s5), "Supply: P = -2+3Q") |
60 | | - |
| 60 | + |
61 | 61 | def test_supply_repr_special_cases(self): |
62 | 62 | """Test special cases for supply representations.""" |
63 | 63 | # Perfectly elastic supply (horizontal) |
64 | 64 | s1 = Supply(3, 0) |
65 | 65 | self.assertEqual(repr(s1), "Supply: P = 3") |
66 | | - |
| 66 | + |
67 | 67 | # Piecewise supply |
68 | 68 | s_piece1 = Supply(0, 1) |
69 | 69 | s_piece2 = Supply(10, 2) |
70 | 70 | s_piecewise = s_piece1 + s_piece2 |
71 | 71 | self.assertEqual(repr(s_piecewise), "Supply: 2-piece piecewise function") |
72 | | - |
| 72 | + |
73 | 73 | def test_ppf_repr_simple(self): |
74 | 74 | """Test basic PPF representations.""" |
75 | 75 | # Standard PPF: y = 10 - x |
76 | 76 | ppf1 = PPF(10, -1) |
77 | 77 | self.assertEqual(repr(ppf1), "PPF: y = 10-x") |
78 | | - |
| 78 | + |
79 | 79 | # PPF with different slope: y = 20 - 2x |
80 | 80 | ppf2 = PPF(20, -2) |
81 | 81 | self.assertEqual(repr(ppf2), "PPF: y = 20-2x") |
82 | | - |
| 82 | + |
83 | 83 | # PPF through origin: y = -x |
84 | 84 | ppf3 = PPF(0, -1) |
85 | 85 | self.assertEqual(repr(ppf3), "PPF: y = -x") |
86 | | - |
| 86 | + |
87 | 87 | # PPF with fractional slope: y = 15 - 0.5x |
88 | 88 | ppf4 = PPF(15, -0.5) |
89 | 89 | self.assertEqual(repr(ppf4), "PPF: y = 15-0.5x") |
90 | | - |
| 90 | + |
91 | 91 | def test_ppf_repr_piecewise(self): |
92 | 92 | """Test piecewise PPF representation.""" |
93 | 93 | # Create two PPF segments |
94 | 94 | ppf1 = PPF(10, -2) |
95 | 95 | ppf2 = PPF(20, -1) |
96 | 96 | ppf_combined = ppf1 + ppf2 |
97 | 97 | self.assertEqual(repr(ppf_combined), "PPF: 2-piece frontier") |
98 | | - |
| 98 | + |
99 | 99 | def test_number_formatting(self): |
100 | 100 | """Test that numbers are formatted cleanly without unnecessary decimals.""" |
101 | 101 | # Integer values should not show .0 |
102 | 102 | d1 = Demand(10.0, -1.0) |
103 | 103 | self.assertEqual(repr(d1), "Demand: P = 10-Q") |
104 | | - |
| 104 | + |
105 | 105 | s1 = Supply(5.0, 2.0) |
106 | 106 | self.assertEqual(repr(s1), "Supply: P = 5+2Q") |
107 | | - |
| 107 | + |
108 | 108 | # But fractional values should be preserved |
109 | 109 | d2 = Demand(10.5, -1.5) |
110 | 110 | self.assertEqual(repr(d2), "Demand: P = 10.5-1.5Q") |
111 | | - |
| 111 | + |
112 | 112 | s2 = Supply(2.5, 0.75) |
113 | 113 | self.assertEqual(repr(s2), "Supply: P = 2.5+0.75Q") |
114 | | - |
| 114 | + |
115 | 115 | def test_sign_handling(self): |
116 | 116 | """Test that signs are handled correctly in all cases.""" |
117 | 117 | # Very small numbers should use g formatting |
118 | 118 | d1 = Demand(10, -0.0001) |
119 | 119 | self.assertEqual(repr(d1), "Demand: P = 10-0.0001Q") |
120 | | - |
| 120 | + |
121 | 121 | # Very large numbers |
122 | 122 | s1 = Supply(1000000, 1000) |
123 | 123 | self.assertEqual(repr(s1), "Supply: P = 1e+06+1000Q") |
|
0 commit comments