Browse Source

SaveAsXML returns bool

Piotr Czajkowski 3 years ago
parent
commit
f8b9b76a36
2 changed files with 17 additions and 6 deletions
  1. 13 1
      PSDText/PSDText.cs
  2. 4 5
      PSDTextTests/PSDTextTests.cs

+ 13 - 1
PSDText/PSDText.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
+using System.Linq;
 using System.Text;
 using System.Xml;
 using System.Xml.Serialization;
@@ -86,13 +87,24 @@ namespace PSDText
             TextData = GetTextData();
         }
 
-        public void SaveAsXML(string path)
+        /// <summary>
+        /// Serializes text layers as XML.
+        /// </summary>
+        /// <param name="path">Output XML path.</param>
+        /// <returns>True on successful serialization,
+        /// false if there's nothing to write.</returns>
+        public bool SaveAsXML(string path)
         {
+            if (!TextData.Any())
+                return false;
+
             var serializer = new XmlSerializer(typeof(List<TextData>));
             using (var sr = new StreamWriter(path))
             {
                 serializer.Serialize(sr, TextData);
             }
+
+            return true;
         }
 
         public void SaveAsJSON(string path)

+ 4 - 5
PSDTextTests/PSDTextTests.cs

@@ -22,7 +22,7 @@ namespace PSDTextTests
             Assert.NotEmpty(test.TextData);
 
             var destination = "./test.xml";
-            test.SaveAsXML(destination);
+            Assert.True(test.SaveAsXML(destination));
             Assert.True(File.Exists(destination));
             File.Delete(destination);
         }
@@ -33,10 +33,9 @@ namespace PSDTextTests
             var test = new PSDText.PSDText(NoTextPSD);
             Assert.Empty(test.TextData);
 
-            var destination = "./test.xml";
-            test.SaveAsXML(destination);
-            Assert.True(File.Exists(destination));
-            File.Delete(destination);
+            var destination = "./testNoText.xml";
+            Assert.False(test.SaveAsXML(destination));
+            Assert.False(File.Exists(destination));
         }
 
         [Fact]