public PokemonBag Load_PokemonBag(string filePath) { //TODO:: check if file end of .xml DONE if (!filePath.EndsWith(".xml")) { filePath = filePath + ".xml"; } if (!File.Exists(filePath)) { throw new Exception(string.Format("{0} does not exist", filePath)); } PokemonBag pokeBag = null; using (var file = new StreamReader(filePath)) { try { pokeBag = pokeBagSerializer.Deserialize(file) as PokemonBag; } catch (Exception e) { throw new Exception(string.Format("Unable to deserialize the {0} due to following: {1}", filePath, e.Message)); } } return(pokeBag); }
public void PokemonReader_Load_PokemonBag_HappyPath() { PokemonBag expect = new PokemonBag(); PokemonBag actrual; // set up expect expect.Pokemons.Add(151); expect.Pokemons.Add(149); // Create a simple test xml for Pokedex string fileName = "testUse_pokemonBag.xml"; path += fileName; using (FileStream fs = new FileStream(path, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(pokemonBag_sourceExample_XML); } } actrual = testReader.Load_PokemonBag(path); Assert.AreEqual(expect, actrual); }
public override bool Equals(object obj) { bool returnBool; if (obj == null || GetType() != obj.GetType()) { returnBool = false; } PokemonBag other = (PokemonBag)obj; if (Pokemons.Count == other.Pokemons.Count) { for (int i = 0; i < Pokemons.Count; i++) { if (!(Pokemons[i] == other.Pokemons[i])) { returnBool = false; } } returnBool = true; } else { returnBool = false; } return(returnBool); }
public void PokemonBag_AddPokemon_CaseUnknowName(string name) { PokemonBag expect = new PokemonBag(); bag.AddPokemon(dex, "Blasise"); Assert.AreEqual(bag, expect); }
public void SavePokemonBag(string filepath, PokemonBag input) { using (FileStream fs = File.Open(filepath, File.Exists(filepath) ? FileMode.Append : FileMode.Create)) { new XmlSerializer(typeof(PokemonBag)).Serialize(fs, input); } }
public void PokemonBag_AddPokemon_HappyPath() { PokemonBag expect = new PokemonBag(); expect.Pokemons.Add(9); bag.AddPokemon(dex, "Blastoise"); Assert.AreEqual(bag, expect); }
public void PokemonSaver_SavePokemonBag_FileAlreadyExist(string fileName) { PokemonBag myBag = new PokemonBag(); path += fileName; saver.Save_PokeBag(myBag, path); Assert.That(() => saver.Save_PokeBag(myBag, path), Throws.TypeOf <Exception>()); }
public void PokemonReader_Load_PokemonBag_Case_FileNotExist() { string fileName = "testUse_pokedex.xml"; path += fileName; // Delete the file in case its really exist File.Delete(path); PokemonBag theBag = new PokemonBag(); Assert.That(() => testReader.Load_Pokedex(path), Throws.TypeOf <Exception>()); }
public void AddPokemonsToProcess(ref PokemonBag pokemonBag, ref Pokedex pokedex) { for (int i = 0; i < pokemonBag.Pokemons.Count; ++i) { for (int j = 0; j < pokedex.Pokemons.Count; ++j) { if (pokemonBag.Pokemons[i] == pokedex.Pokemons[j].Index) { pokemonsToProcess.Add(pokedex.Pokemons[j]); break; } } } }
public void SetUp() { bag = new PokemonBag(); dex = new Pokedex(); dex.Pokemons.Add(new Pokemon { Index = 1, Name = "Bulbasaur", Type1 = "Grass", Type2 = "Poison", HP = 128, Attack = 118, Defense = 111, MaxCP = 1115 }); dex.Pokemons.Add(new Pokemon { Index = 2, Name = "Ivysaur", Type1 = "Grass", Type2 = "Poison", HP = 155, Attack = 151, Defense = 143, MaxCP = 1699 }); dex.Pokemons.Add(new Pokemon { Index = 6, Name = "Charizard", Type1 = "Fire", Type2 = "Flying", HP = 186, Attack = 223, Defense = 173, MaxCP = 2889 }); dex.Pokemons.Add(new Pokemon { Index = 9, Name = "Blastoise", Type1 = "Water", Type2 = "", HP = 188, Attack = 171, Defense = 207, MaxCP = 2466 }); }
public void PokemonSaver_SavePokemonBag_HappyPass(string fileName) { PokemonBag expect = new PokemonBag(); PokemonBag actrul; expect.Pokemons.Add(1); // Add a Bulbasaur //set up unique fileName path += fileName; saver.Save_PokeBag(expect, path); // Load up the actrul actrul = reader.Load_PokemonBag(path); Assert.AreEqual(expect, actrul); }
public void Save_PokeBag(PokemonBag pokeBag, string fileName) { //TODO:: check if file end of .xml DONE if (!fileName.EndsWith(".xml")) { fileName = fileName + ".xml"; } if (File.Exists(fileName)) { throw new Exception(string.Format("File {0} already exist, " + "overwrite a existing pokemonBag is not allowed, " + "the bag onwer going to be upset", fileName)); } using (FileStream fs = new FileStream(fileName, FileMode.Create)) { pokeBagSerializer.Serialize(fs, pokeBag); } }
public PokemonBag LoadPokemonBag(string filepath) { if (!File.Exists(filepath)) { throw new Exception($"{filepath} does not exist"); } PokemonBag dex = null; using (var file = new StreamReader(filepath)) { try { dex = new XmlSerializer(typeof(PokemonBag)).Deserialize(file) as PokemonBag; } catch (Exception ex) { throw new Exception($"Unable to deserialize the {filepath} due to following: {ex.Message}"); } } return(dex); }