Skip to content

Commit 13bc15a

Browse files
authored
Merge pull request #16 from Software-Engineering-DHBW/TestsWithRefactoring
some more test and also try catch in SaveSystem
2 parents e4ec3d2 + 4b27a71 commit 13bc15a

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

LearningForDummies/Assets/Editor/Tests/Test.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using NUnit.Framework;
44
using UnityEngine;
55
using UnityEngine.TestTools;
6+
using SaveData;
67

78
public class Test
89
{
@@ -18,6 +19,7 @@ public void loadRawReturnsNull(string catalogueName)
1819
SaveSystem saveSystem = new SaveSystem();
1920
var answer = saveSystem.loadTextRawFromJson(catalogueName);
2021
Assert.AreEqual(answer, null);
22+
//should be null for cataloguenames that are not in the app
2123
}
2224
[Test]
2325
[TestCase("Test")]
@@ -30,13 +32,47 @@ public void loadPlayerProfileReturnsNull(string profileName)
3032
SaveSystem saveSystem = new SaveSystem();
3133
var answer = saveSystem.loadPlayerProfileFromJson(profileName);
3234
Assert.AreEqual(answer, null);
35+
//should be null when the app is shipped because there is no profile
3336
}
37+
3438
[Test]
3539
public void loadCatalougesReturnsNull()
3640
{
3741
SaveSystem saveSystem = new SaveSystem();
3842
var answer = saveSystem.loadQuestionCataloguesFromJson();
43+
//Assert.IsInstanceOf<List<QuestionCatalogue>>(answer);
3944
Assert.AreEqual(answer, null);
45+
//should be null if there are no catalogues
46+
//should be of type List<QuestionCatalogues> if something is found in the foulder
47+
}
48+
49+
[Test]
50+
[TestCase("Name", "Data")]
51+
public void saveRawReturnsOK(string name, string data)
52+
{
53+
SaveSystem saveSystem = new SaveSystem();
54+
SaveSystem.StatusCodes answer = saveSystem.saveRawJsonTextToJson(name, data);
55+
Assert.AreEqual(answer, SaveSystem.StatusCodes.OK);
56+
//should be ok
57+
}
58+
59+
[Test]
60+
public void saveCatalogueReturnsOK()
61+
{
62+
QuestionCatalogue catalogue = new QuestionCatalogue();
63+
SaveSystem saveSystem = new SaveSystem();
64+
SaveSystem.StatusCodes answer = saveSystem.saveQuestionCatalogueToJson(catalogue);
65+
Assert.AreEqual(answer, SaveSystem.StatusCodes.OK);
66+
//should be ok
67+
}
68+
69+
[Test]
70+
public void saveProfileReturnsOK()
71+
{
72+
PlayerProfile player = new PlayerProfile();
73+
SaveSystem saveSystem = new SaveSystem();
74+
SaveSystem.StatusCodes answer = saveSystem.savePlayerProfileToJson(player);
75+
Assert.AreEqual(answer, SaveSystem.StatusCodes.OK);
76+
//should be ok
4077
}
41-
4278
}

LearningForDummies/Assets/Scripts/SaveSystem.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class SaveSystem : MonoBehaviour
1111

1212
string filePath;
1313
string playerProfilePath;
14+
public enum StatusCodes{OK,Error};
1415

1516

1617
private void Awake()
@@ -35,32 +36,51 @@ private void Awake()
3536

3637
}
3738

38-
public void saveRawJsonTextToJson(string catalogueName, string data)
39+
public StatusCodes saveRawJsonTextToJson(string catalogueName, string data)
3940
{
41+
try{
4042
string filename = "/" + catalogueName + ".qcat";
4143
string content = data;
4244
System.IO.File.WriteAllText(Application.persistentDataPath + filename, content);
4345
Debug.Log("Speicherung erfolgt als... " + filename);
46+
return StatusCodes.OK;
47+
}
48+
catch{
49+
return StatusCodes.Error;
50+
}
4451
}
4552

46-
public void saveQuestionCatalogueToJson(QuestionCatalogue _questionCatalogue)
53+
public StatusCodes saveQuestionCatalogueToJson(QuestionCatalogue _questionCatalogue)
4754
{
55+
try{
4856
string fileName = "/" + _questionCatalogue.fileName + ".qcat";
4957
string content = JsonUtility.ToJson(_questionCatalogue);
5058
System.IO.File.WriteAllText(Application.persistentDataPath + fileName, content);
5159
Debug.Log("QuestionCatalogue has been successfully saved to the FileSystem");
60+
return StatusCodes.OK;
61+
}
62+
catch{
63+
return StatusCodes.Error;
64+
}
5265
}
5366

54-
public void savePlayerProfileToJson(PlayerProfile _playerProfile)
67+
public StatusCodes savePlayerProfileToJson(PlayerProfile _playerProfile)
5568
{
69+
try{
5670
string fileName = "/" + _playerProfile.fileName + ".pp";
5771
string content = JsonUtility.ToJson(_playerProfile);
5872
System.IO.File.WriteAllText(Application.persistentDataPath + fileName, content);
5973
Debug.Log("PlayerProfile has been successfully saved to the FileSystem");
74+
return StatusCodes.OK;
75+
}
76+
catch{
77+
return StatusCodes.Error;
78+
}
6079
}
6180

6281
public string loadTextRawFromJson(string catalogueName)
6382
{
83+
try{
6484
string extension = "*.qcat"; // The "*" ist really important. It is a placeholder for the rest of the File
6585
string[] Files = Directory.GetFiles(Application.persistentDataPath, extension);
6686
foreach (string file in Files)
@@ -80,6 +100,10 @@ public string loadTextRawFromJson(string catalogueName)
80100
}
81101
Debug.Log("----------\nOPERATION FAILED\n----------");
82102
return null;
103+
}
104+
catch{
105+
return StatusCodes.Error.ToString();
106+
}
83107
}
84108

85109
public List<QuestionCatalogue> loadQuestionCataloguesFromJson()
@@ -91,21 +115,31 @@ public List<QuestionCatalogue> loadQuestionCataloguesFromJson()
91115
{
92116
Debug.Log("The following file has been found: " + Path.GetFileName(file));
93117
string DataFromJson = File.ReadAllText(file);
118+
try{
94119
QuestionCatalogue questionCatalogue = JsonUtility.FromJson<QuestionCatalogue>(DataFromJson);
95120
allCatalogues.Add(questionCatalogue);
96121
Debug.Log("A catalogue has been added to the List.");
122+
}
123+
catch{
124+
Debug.Log("The file format was invalid.");
125+
}
97126
}
98127
if (Files.Length == 0)
99128
{
100129
Debug.Log("No files with the extension " + extension + " have been found.");
101130
return null;
102131
}
132+
else if(allCatalogues.Count == 0){
133+
Debug.Log("A file was found but not compatible.");
134+
return null;
135+
}
103136
Debug.Log("The List of found QuestionCatalogues contains " + allCatalogues.Count + " QuestionCatalogues.");
104137
return allCatalogues;
105138
}
106139

107140
public PlayerProfile loadPlayerProfileFromJson(string fileName)
108141
{
142+
try{
109143
string extension = "*.pp"; // The "*" ist really important. It is a placeholder for the rest of the File
110144
string[] Files = Directory.GetFiles(Application.persistentDataPath, extension);
111145
foreach (string file in Files)
@@ -127,6 +161,10 @@ public PlayerProfile loadPlayerProfileFromJson(string fileName)
127161
}
128162
Debug.Log("----------\nOPERATION FAILED\n----------");
129163
return null;
164+
}
165+
catch{
166+
return null;
167+
}
130168
}
131169

132170

0 commit comments

Comments
 (0)