@@ -1283,6 +1283,54 @@ func TestEncoder_CustomMarshaler(t *testing.T) {
12831283 })
12841284}
12851285
1286+ func TestEncoder_AutoInt (t * testing.T ) {
1287+ for _ , test := range []struct {
1288+ desc string
1289+ input any
1290+ expected string
1291+ }{
1292+ {
1293+ desc : "int-convertible float64" ,
1294+ input : map [string ]float64 {
1295+ "key" : 1.0 ,
1296+ },
1297+ expected : "key: 1\n " ,
1298+ },
1299+ {
1300+ desc : "non int-convertible float64" ,
1301+ input : map [string ]float64 {
1302+ "key" : 1.1 ,
1303+ },
1304+ expected : "key: 1.1\n " ,
1305+ },
1306+ {
1307+ desc : "int-convertible float32" ,
1308+ input : map [string ]float32 {
1309+ "key" : 1.0 ,
1310+ },
1311+ expected : "key: 1\n " ,
1312+ },
1313+ {
1314+ desc : "non int-convertible float32" ,
1315+ input : map [string ]float32 {
1316+ "key" : 1.1 ,
1317+ },
1318+ expected : "key: 1.1\n " ,
1319+ },
1320+ } {
1321+ t .Run (test .desc , func (t * testing.T ) {
1322+ var buf bytes.Buffer
1323+ enc := yaml .NewEncoder (& buf , yaml .AutoInt ())
1324+ if err := enc .Encode (test .input ); err != nil {
1325+ t .Fatalf ("failed to encode: %s" , err )
1326+ }
1327+ if actual := buf .String (); actual != test .expected {
1328+ t .Errorf ("expect:\n %s\n actual\n %s\n " , test .expected , actual )
1329+ }
1330+ })
1331+ }
1332+ }
1333+
12861334func TestEncoder_MultipleDocuments (t * testing.T ) {
12871335 var buf bytes.Buffer
12881336 enc := yaml .NewEncoder (& buf )
0 commit comments