1# -*- coding: utf-8 -*- 2 3""" 4Tests the Angle string formatting capabilities. SkyCoord formatting is in 5test_sky_coord 6""" 7 8 9from astropy.coordinates.angles import Angle 10from astropy import units as u 11 12 13def test_to_string_precision(): 14 # There are already some tests in test_api.py, but this is a regression 15 # test for the bug in issue #1319 which caused incorrect formatting of the 16 # seconds for precision=0 17 18 angle = Angle(-1.23456789, unit=u.degree) 19 20 assert angle.to_string(precision=3) == '-1d14m04.444s' 21 assert angle.to_string(precision=1) == '-1d14m04.4s' 22 assert angle.to_string(precision=0) == '-1d14m04s' 23 24 angle2 = Angle(-1.23456789, unit=u.hourangle) 25 26 assert angle2.to_string(precision=3, unit=u.hour) == '-1h14m04.444s' 27 assert angle2.to_string(precision=1, unit=u.hour) == '-1h14m04.4s' 28 assert angle2.to_string(precision=0, unit=u.hour) == '-1h14m04s' 29 30 # Regression test for #7141 31 angle3 = Angle(-0.5, unit=u.degree) 32 assert angle3.to_string(precision=0, fields=3) == '-0d30m00s' 33 assert angle3.to_string(precision=0, fields=2) == '-0d30m' 34 assert angle3.to_string(precision=0, fields=1) == '-1d' 35 36 37def test_to_string_decimal(): 38 39 # There are already some tests in test_api.py, but this is a regression 40 # test for the bug in issue #1323 which caused decimal formatting to not 41 # work 42 43 angle1 = Angle(2., unit=u.degree) 44 45 assert angle1.to_string(decimal=True, precision=3) == '2.000' 46 assert angle1.to_string(decimal=True, precision=1) == '2.0' 47 assert angle1.to_string(decimal=True, precision=0) == '2' 48 49 angle2 = Angle(3., unit=u.hourangle) 50 51 assert angle2.to_string(decimal=True, precision=3) == '3.000' 52 assert angle2.to_string(decimal=True, precision=1) == '3.0' 53 assert angle2.to_string(decimal=True, precision=0) == '3' 54 55 angle3 = Angle(4., unit=u.radian) 56 57 assert angle3.to_string(decimal=True, precision=3) == '4.000' 58 assert angle3.to_string(decimal=True, precision=1) == '4.0' 59 assert angle3.to_string(decimal=True, precision=0) == '4' 60 61 62def test_to_string_formats(): 63 a = Angle(1.113355, unit=u.deg) 64 assert a.to_string(format='latex') == r'$1^\circ06{}^\prime48.078{}^{\prime\prime}$' 65 assert a.to_string(format='unicode') == '1°06′48.078″' 66 67 a = Angle(1.113355, unit=u.hour) 68 assert a.to_string(format='latex') == r'$1^{\mathrm{h}}06^{\mathrm{m}}48.078^{\mathrm{s}}$' 69 assert a.to_string(format='unicode') == '1ʰ06ᵐ48.078ˢ' 70 71 a = Angle(1.113355, unit=u.radian) 72 assert a.to_string(format='latex') == r'$1.11336\mathrm{rad}$' 73 assert a.to_string(format='unicode') == '1.11336rad' 74 75 76def test_to_string_fields(): 77 a = Angle(1.113355, unit=u.deg) 78 assert a.to_string(fields=1) == r'1d' 79 assert a.to_string(fields=2) == r'1d07m' 80 assert a.to_string(fields=3) == r'1d06m48.078s' 81 82 83def test_to_string_padding(): 84 a = Angle(0.5653, unit=u.deg) 85 assert a.to_string(unit='deg', sep=':', pad=True) == r'00:33:55.08' 86 87 # Test to make sure negative angles are padded correctly 88 a = Angle(-0.5653, unit=u.deg) 89 assert a.to_string(unit='deg', sep=':', pad=True) == r'-00:33:55.08' 90 91 92def test_sexagesimal_rounding_up(): 93 a = Angle(359.999999999999, unit=u.deg) 94 95 assert a.to_string(precision=None) == '360d00m00s' 96 assert a.to_string(precision=4) == '360d00m00.0000s' 97 assert a.to_string(precision=5) == '360d00m00.00000s' 98 assert a.to_string(precision=6) == '360d00m00.000000s' 99 assert a.to_string(precision=7) == '360d00m00.0000000s' 100 assert a.to_string(precision=8) == '360d00m00.00000000s' 101 assert a.to_string(precision=9) == '359d59m59.999999996s' 102 103 a = Angle(3.999999, unit=u.deg) 104 assert a.to_string(fields=2, precision=None) == '4d00m' 105 assert a.to_string(fields=2, precision=1) == '4d00m' 106 assert a.to_string(fields=2, precision=5) == '4d00m' 107 assert a.to_string(fields=1, precision=1) == '4d' 108 assert a.to_string(fields=1, precision=5) == '4d' 109 110 111def test_to_string_scalar(): 112 a = Angle(1.113355, unit=u.deg) 113 assert isinstance(a.to_string(), str) 114 115 116def test_to_string_radian_with_precision(): 117 """ 118 Regression test for a bug that caused ``to_string`` to crash for angles in 119 radians when specifying the precision. 120 """ 121 122 # Check that specifying the precision works 123 a = Angle(3., unit=u.rad) 124 assert a.to_string(precision=3, sep='fromunit') == '3.000rad' 125 126 127def test_sexagesimal_round_down(): 128 a1 = Angle(1, u.deg).to(u.hourangle) 129 a2 = Angle(2, u.deg) 130 assert a1.to_string() == '0h04m00s' 131 assert a2.to_string() == '2d00m00s' 132 133 134def test_to_string_fields_colon(): 135 a = Angle(1.113355, unit=u.deg) 136 assert a.to_string(fields=2, sep=':') == '1:07' 137 assert a.to_string(fields=3, sep=':') == '1:06:48.078' 138 assert a.to_string(fields=1, sep=':') == '1' 139